001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2024
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 https://www.gnu.org/licenses/.
027 */
028
029package edu.wisc.ssec.mcidasv.startupmanager.options;
030
031import javax.swing.JComponent;
032import javax.swing.JLabel;
033import javax.swing.JPanel;
034import javax.swing.JSlider;
035import javax.swing.event.ChangeListener;
036
037import ucar.unidata.util.GuiUtils;
038
039import edu.wisc.ssec.mcidasv.startupmanager.StartupManager;
040import edu.wisc.ssec.mcidasv.startupmanager.options.OptionMaster.OptionPlatform;
041import edu.wisc.ssec.mcidasv.startupmanager.options.OptionMaster.Type;
042import edu.wisc.ssec.mcidasv.startupmanager.options.OptionMaster.Visibility;
043import edu.wisc.ssec.mcidasv.util.MakeToString;
044
045public 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 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    public JPanel getComponent() {
074        JLabel sliderLabel = new JLabel("Use "+sliderValue+"% ");
075        JLabel postLabel = new JLabel(" of available memory ("+total+"mb)");
076        
077        ChangeListener listener = makeChangeListener(sliderLabel);
078        
079        JComponent[] sliderComps = GuiUtils.makeSliderPopup(minValue, maxValue+1, sliderValue, listener);
080        JSlider slider = (JSlider)sliderComps[1];
081        slider.setMinorTickSpacing(5);
082        slider.setMajorTickSpacing(10);
083        slider.setSnapToTicks(true);
084        slider.setExtent(1);
085        slider.setPaintTicks(true);
086        slider.setPaintLabels(true);
087        sliderComps[0].setToolTipText("Set maximum memory by percent");
088        
089        JPanel sliderPanel = GuiUtils.hbox(sliderLabel, sliderComps[0], postLabel);
090        return sliderPanel;
091    }
092    
093    public String getValue() {
094        return Integer.toString(sliderValue)+"P";
095    }
096    
097    public void setValue(final String newValue) {
098        int endIndex = newValue.length();
099        if (newValue.endsWith("P")) {
100            endIndex = endIndex - 1;
101        }
102        int test = Integer.parseInt(newValue.substring(0, endIndex));
103        if (test >= 0) {
104            sliderValue = test;
105        }
106    }
107    
108    public String toString() {
109        return MakeToString.fromInstance(this)
110                           .add("optionId", getOptionId())
111                           .add("value", getValue()).toString();
112    }
113}