001    package org.LiveGraph.settings;
002    
003    import java.io.FileInputStream;
004    import java.io.FileOutputStream;
005    import java.io.IOException;
006    import java.util.Properties;
007    
008    import org.LiveGraph.LiveGraph;
009    
010    /**
011     * Encapsulates the settings concerned with reading the data file, the update frequency
012     * and the caching method. 
013     * 
014     * <p style="font-size:smaller;">This product includes software developed by the
015     *    <strong>LiveGraph</strong> project and its contributors.<br />
016     *    (<a href="http://www.live-graph.org" target="_blank">http://www.live-graph.org</a>)<br />
017     *    Copyright (c) 2007 G. Paperin.<br />
018     *    All rights reserved.
019     * </p>
020     * <p style="font-size:smaller;">File: DataFileSettings.java</p> 
021     * <p style="font-size:smaller;">Redistribution and use in source and binary forms, with or
022     *    without modification, are permitted provided that the following terms and conditions are met:
023     * </p>
024     * <p style="font-size:smaller;">1. Redistributions of source code must retain the above
025     *    acknowledgement of the LiveGraph project and its web-site, the above copyright notice,
026     *    this list of conditions and the following disclaimer.<br />
027     *    2. Redistributions in binary form must reproduce the above acknowledgement of the
028     *    LiveGraph project and its web-site, the above copyright notice, this list of conditions
029     *    and the following disclaimer in the documentation and/or other materials provided with
030     *    the distribution.<br />
031     *    3. All advertising materials mentioning features or use of this software or any derived
032     *    software must display the following acknowledgement:<br />
033     *    <em>This product includes software developed by the LiveGraph project and its
034     *    contributors.<br />(http://www.live-graph.org)</em><br />
035     *    4. All advertising materials distributed in form of HTML pages or any other technology
036     *    permitting active hyper-links that mention features or use of this software or any
037     *    derived software must display the acknowledgment specified in condition 3 of this
038     *    agreement, and in addition, include a visible and working hyper-link to the LiveGraph
039     *    homepage (http://www.live-graph.org).
040     * </p>
041     * <p style="font-size:smaller;">THIS SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY
042     *    OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
043     *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT SHALL
044     *    THE AUTHORS, CONTRIBUTORS OR COPYRIGHT  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
045     *    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING  FROM, OUT OF OR
046     *    IN CONNECTION WITH THE SOFTWARE OR THE USE OR  OTHER DEALINGS IN THE SOFTWARE.
047     * </p>
048     * 
049     * @author Greg Paperin (<a href="http://www.paperin.org" target="_blank">http://www.paperin.org</a>)
050     * @version {@value org.LiveGraph.LiveGraph#version}
051     */
052    public class DataFileSettings extends ObservableSettings {
053    
054    /**
055     * Standard file extension.
056     */
057    public static final String preferredFileExtension = ".lgdfs";
058    
059    /**
060     * Default settings.
061     */
062    private static final Properties defaultValues = new Properties();
063    static {
064            defaultValues.setProperty("DataFile", "");
065            defaultValues.setProperty("ShowOnlyTailData", "0");
066            defaultValues.setProperty("DoNotCacheData", "0");
067            defaultValues.setProperty("UpdateFrequency", "-1");
068    }
069    
070    /**
071     * Holds the current settings.
072     */
073    private Properties values = null;
074    
075    /**
076     * Creates a new data file settings object with the default settings values.
077     */
078    public DataFileSettings() {
079            super();
080            values = new Properties();
081            values.putAll(defaultValues);
082    }
083    
084    /**
085     * Creates a new data file settings object and loads the settings values from
086     * the specified file.
087     *  
088     * @param fileName File to load the settings from.
089     */
090    public DataFileSettings(String fileName) {
091            this();
092            load(fileName);
093    }
094    
095    /**
096     * Loads the settings from a specified file.
097     * 
098     * @param fileName The file to load the settings from.
099     * @return {@code true} if the settings were loaded, {@code false} if an exception occured. 
100     */
101    public boolean load(String fileName) {
102            try {
103                    FileInputStream in = new FileInputStream(fileName);
104                    try { values.loadFromXML(in); notifyObservers("load"); }
105                    finally { in.close(); }
106                    return true;
107            } catch(IOException e) {
108                    e.printStackTrace();
109                    return false;
110            }
111    }
112    
113    /**
114     * Saves the settings to a specified file.
115     * 
116     * @param fileName The file to save the settings to.
117     * @return {@code true} if the settings were saved, {@code false} if an exception occured. 
118     */
119    public boolean save(String fileName) {
120            try {
121                    FileOutputStream out = new FileOutputStream(fileName);
122                    try { values.storeToXML(out, "LiveGraph version " + LiveGraph.version + ". DataFileSettings."); }
123                    finally { out.close(); }
124                    return true;
125            } catch(IOException e) {
126                    e.printStackTrace();
127                    return false;
128            }
129    }
130    
131    /**
132     * Gets the data file.
133     * 
134     * @return Name of the data stream to plot.
135     */
136    public String getDataFile() { return values.getProperty("DataFile"); }
137    
138    /**
139     * Sets the data file.
140     * 
141     * @param fn Name of the data stream to plot.
142     */
143    public void setDataFile(String fn) {
144            values.setProperty("DataFile", fn);
145            notifyObservers("DataFile");
146    }
147    
148    
149    /**
150     * Gets whether to plot only tail data.
151     *  
152     * @return {@code true} if only the datasets at the end of the data setream should be plotted,
153     * {@code false} if data sets should be sampled from the complete data stream.
154     */
155    public boolean getShowOnlyTailData() { return "1".equalsIgnoreCase(values.getProperty("ShowOnlyTailData")); }
156    
157    /**
158     * Setts whether to plot only tail data.
159     *  
160     * @param v {@code true} if only the datasets at the end of the data setream should be plotted,
161     * {@code false} if data sets should be sampled from the complete data stream.
162     */
163    public void setShowOnlyTailData(boolean v) {
164            values.setProperty("ShowOnlyTailData", v ? "1" : "0");
165            notifyObservers("ShowOnlyTailData");
166    }
167    
168    /**
169     * Gets whether the data stream should not be cached in memory.
170     *  
171     * @return {@code true} if the data stream should be read from the start each time the graph is updated,
172     * {@code false} if data should be cached in memory and only the new data sets should be read on each update. 
173     */
174    public boolean getDoNotCacheData() { return "1".equalsIgnoreCase(values.getProperty("DoNotCacheData")); }
175    
176    /**
177     * Sets whether the data stream should not be cached in memory.
178     *  
179     * @param v {@code true} if the data stream should be read from the start each time the graph is updated,
180     * {@code false} if data should be cached in memory and only the new data sets should be read on each update. 
181     */
182    public void setDoNotCacheData(boolean v) {
183            values.setProperty("DoNotCacheData", v ? "1" : "0");
184            notifyObservers("DoNotCacheData");
185    }
186    
187    /**
188     * Gets the interval between automatic graph updates.
189     * 
190     * @return The number of milliseconds to wait between trying to read from the data stream next time.
191     * If this values is {@code < 1} no automatic update should be initiated.
192     */
193    public long getUpdateFrequency() { return Long.parseLong(values.getProperty("UpdateFrequency")); }
194    
195    /**
196     * Sets the interval between automatic graph updates.
197     * 
198     * @param f The number of milliseconds to wait between trying to read from the data stream next time.
199     * Pass a value {@code f < 1} to indicate that no automatic update should be initiated.
200     */
201    public void setUpdateFrequency(long f) {
202            values.setProperty("UpdateFrequency", ""+f);
203            notifyObservers("UpdateFrequency");
204    }
205    
206    } // public class DataFileSettings