001 /*
002 * $Id: SliderOption.java,v 1.7 2012/02/19 17:35:49 davep Exp $
003 *
004 * This file is part of McIDAS-V
005 *
006 * Copyright 2007-2012
007 * Space Science and Engineering Center (SSEC)
008 * University of Wisconsin - Madison
009 * 1225 W. Dayton Street, Madison, WI 53706, USA
010 * https://www.ssec.wisc.edu/mcidas
011 *
012 * All Rights Reserved
013 *
014 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
015 * some McIDAS-V source code is based on IDV and VisAD source code.
016 *
017 * McIDAS-V is free software; you can redistribute it and/or modify
018 * it under the terms of the GNU Lesser Public License as published by
019 * the Free Software Foundation; either version 3 of the License, or
020 * (at your option) any later version.
021 *
022 * McIDAS-V is distributed in the hope that it will be useful,
023 * but WITHOUT ANY WARRANTY; without even the implied warranty of
024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
025 * GNU Lesser Public License for more details.
026 *
027 * You should have received a copy of the GNU Lesser Public License
028 * along with this program. If not, see http://www.gnu.org/licenses.
029 */
030
031 package edu.wisc.ssec.mcidasv.startupmanager.options;
032
033 import javax.swing.JComponent;
034 import javax.swing.JLabel;
035 import javax.swing.JPanel;
036 import javax.swing.JSlider;
037 import javax.swing.event.ChangeEvent;
038 import javax.swing.event.ChangeListener;
039
040 import ucar.unidata.util.GuiUtils;
041
042 import edu.wisc.ssec.mcidasv.startupmanager.StartupManager;
043 import edu.wisc.ssec.mcidasv.startupmanager.options.OptionMaster.OptionPlatform;
044 import edu.wisc.ssec.mcidasv.startupmanager.options.OptionMaster.Type;
045 import edu.wisc.ssec.mcidasv.startupmanager.options.OptionMaster.Visibility;
046
047 public class SliderOption extends AbstractOption {
048 private final int minValue = 10;
049 private final int maxValue = 80;
050 private final int total;
051
052 private int sliderValue = minValue;
053
054 public SliderOption(final String id, final String label,
055 final String defaultValue, final OptionPlatform optionPlatform,
056 Visibility optionVisibility)
057 {
058 super(id, label, Type.SLIDER, optionPlatform, optionVisibility);
059 total = StartupManager.INSTANCE.getPlatform().getAvailableMemory();
060 setValue(defaultValue);
061 }
062
063 private ChangeListener makeChangeListener(final JLabel sliderLabel) {
064 return new ChangeListener() {
065 public void stateChanged(ChangeEvent evt) {
066 JSlider src = (JSlider)evt.getSource();
067 int value = src.getValue();
068 sliderLabel.setText("Use "+value+"% ");
069 setValue(Integer.toString(value));
070 }
071 };
072 }
073
074 public JComponent getComponent() {
075 JLabel sliderLabel = new JLabel("Use "+sliderValue+"% ");
076 JLabel postLabel = new JLabel(" of available memory ("+total+"mb)");
077
078 ChangeListener listener = makeChangeListener(sliderLabel);
079
080 JComponent[] sliderComps = GuiUtils.makeSliderPopup(minValue, maxValue+1, sliderValue, listener);
081 JSlider slider = (JSlider)sliderComps[1];
082 slider.setMinorTickSpacing(5);
083 slider.setMajorTickSpacing(10);
084 slider.setSnapToTicks(true);
085 slider.setExtent(1);
086 slider.setPaintTicks(true);
087 slider.setPaintLabels(true);
088 sliderComps[0].setToolTipText("Set maximum memory by percent");
089
090 JPanel sliderPanel = GuiUtils.hbox(sliderLabel, sliderComps[0], postLabel);
091 return sliderPanel;
092 }
093
094 public String getValue() {
095 return Integer.toString(sliderValue)+"P";
096 }
097
098 public void setValue(final String newValue) {
099 int endIndex = newValue.length();
100 if (newValue.endsWith("P"))
101 endIndex = endIndex - 1;
102 int test = Integer.parseInt(newValue.substring(0, endIndex));
103 if (test >= 0)
104 sliderValue = test;
105 }
106
107 public String toString() {
108 return String.format("[SliderOption@%x: optionId=%s, value=%s]", hashCode(), getOptionId(), getValue());
109 }
110
111 }