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.JCheckBox;
032    import javax.swing.JComponent;
033    import javax.swing.JTextField;
034    
035    import edu.wisc.ssec.mcidasv.startupmanager.Platform;
036    import edu.wisc.ssec.mcidasv.startupmanager.StartupManager;
037    import edu.wisc.ssec.mcidasv.startupmanager.options.OptionMaster.OptionPlatform;
038    import edu.wisc.ssec.mcidasv.startupmanager.options.OptionMaster.Type;
039    import edu.wisc.ssec.mcidasv.startupmanager.options.OptionMaster.Visibility;
040    
041    public abstract class AbstractOption implements Option {
042    
043        /**
044         * A unique identifier for an option. Should be the same as the 
045         * startup variable name found in the startup preference file.
046         */
047        private final String optionId;
048    
049        /** 
050         * Brief description of the option. It will appear as the option's 
051         * label in the GUI.
052         */
053        private final String label;
054    
055        /** @see Type */
056        private final Type optionType;
057    
058        /** @see OptionPlatform */
059        private final OptionPlatform optionPlatform;
060    
061        /** @see Visibility */
062        private final Visibility optionVisibility;
063    
064        /**
065         * Creates an option that can hold a specified sort of data and that
066         * applies to a given platform.
067         * 
068         * @param id ID used to refer to this option.
069         * @param label Text that'll be used as the GUI label for this option
070         * @param optionType Type of data this option will represent.
071         * @param optionPlatform Platform(s) where this option is applicable.
072         * @param optionVisibility Visibility behavior of this option.
073         */
074        public AbstractOption(final String id, final String label, 
075            final Type optionType, final OptionPlatform optionPlatform, 
076            final Visibility optionVisibility) 
077        {
078            this.optionId = id;
079            this.label = label;
080            this.optionType = optionType;
081            this.optionPlatform = optionPlatform;
082            this.optionVisibility = optionVisibility;
083        }
084    
085        /**
086         * Determines if the option applies to the current platform.
087         * 
088         * @return {@code true} if this option is applicable, {@code false} 
089         * otherwise.
090         */
091        protected boolean onValidPlatform() {
092            OptionPlatform platform = getOptionPlatform();
093            if (platform == OptionPlatform.ALL) {
094                return true;
095            }
096            if (platform == OptionMaster.getInstance().convertToOptionPlatform()) {
097                return true;
098            }
099            return false;
100        }
101    
102        /**
103         * Tests the specified string to see if it's valid for the current 
104         * platform. Currently strings that contain {@literal "SET "} 
105         * <b>[ note the space ]</b> are considered to be Windows-only, while 
106         * strings lacking {@literal "SET "} are considered Unix-like.
107         * 
108         * @param text The string to test.
109         * 
110         * @return Whether or not the string is valid.
111         */
112        private boolean isValidPrefFormat(final String text) {
113            assert text != null;
114            boolean hasSet = text.contains("SET ");
115            boolean isWin = (StartupManager.getInstance().getPlatform() == Platform.WINDOWS);
116            return (isWin == hasSet);
117        }
118    
119        /**
120         * Returns this option's type.
121         * 
122         * @return The option's type.
123         * 
124         * @see Type
125         */
126        public Type getOptionType() {
127            return optionType;
128        }
129    
130        /**
131         * Returns the platform(s) to which this option applies.
132         * 
133         * @return The option's platform.
134         * 
135         * @see OptionPlatform
136         */
137        public OptionPlatform getOptionPlatform() {
138            return optionPlatform;
139        }
140    
141        /**
142         * Returns whether or not this option represents a visible UI element.
143         * 
144         * @return The option's visibility.
145         * 
146         * @see Visibility
147         */
148        public Visibility getOptionVisibility() {
149            return optionVisibility;
150        }
151    
152        /**
153         * Returns the ID used when referring to this option.
154         * 
155         * @return The option's ID.
156         */
157        public String getOptionId() {
158            return optionId;
159        }
160    
161        /**
162         * Returns a brief description of this option. Mostly useful for 
163         * providing a GUI label.
164         * 
165         * @return The option's label.
166         */
167        public String getLabel() {
168            return label;
169        }
170    
171        /**
172         * Initializes the current option using a relevant variable from the 
173         * startup script.
174         * 
175         * @param text Line from the startup script that represents the current
176         * option.
177         * 
178         * @throws IllegalArgumentException if {@code text} is not in the proper
179         * format for the current platform.
180         */
181        public void fromPrefsFormat(final String text) {
182            if (!isValidPrefFormat(text)) {
183                throw new IllegalArgumentException("Incorrect syntax for this platform: " + text);
184            }
185            String copy = new String(text);
186            if (StartupManager.getInstance().getPlatform() == Platform.WINDOWS) {
187                copy = copy.replace("SET ", "");
188            }
189            String[] chunks = copy.split("=");
190            if (chunks.length == 2 && chunks[0].equals(optionId)) {
191                setValue(chunks[1]);
192            } else {
193                setValue("");
194            }
195        }
196    
197        /**
198         * Returns a string representation of the current option that is suitable 
199         * for use in the startup script.
200         * 
201         * @return Current value of this option as a startup script variable. The 
202         * formatting changes slightly between {@literal "Unix-like"} platforms 
203         * and Windows.
204         * 
205         * @see #isValidPrefFormat(String)
206         */
207        public String toPrefsFormat() {
208            StringBuilder str = new StringBuilder(optionId);
209            String value = getValue();
210            if (StartupManager.getInstance().getPlatform() == Platform.WINDOWS) {
211                str.insert(0, "SET ");
212            }
213            return str.append("=").append(value).toString();
214        }
215    
216        /**
217         * Returns the GUI component that represents the option. 
218         * {@link BooleanOption}s are represented by a {@link JCheckBox}, while
219         * {@link TextOption}s appear as a {@link JTextField}.
220         * 
221         * @return The GUI representation of this option.
222         */
223        public abstract JComponent getComponent();
224    
225        /**
226         * Returns the value of the option. Note that {@link BooleanOption}s
227         * return either "0" or "1".
228         * 
229         * @return The current value of the option.
230         */
231        public abstract String getValue();
232    
233        /**
234         * Forces the value of the option to the data specified. Note that 
235         * {@link BooleanOption}s accept either "0", or "1".
236         * 
237         * @param value New value to use.
238         */
239        public abstract void setValue(final String value);
240    
241        /**
242         * Friendly string representation of the option.
243         * 
244         * @return String containing relevant info about the option.
245         * 
246         * @see TextOption#toString()
247         * @see BooleanOption#toString()
248         */
249        public abstract String toString();
250    }