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