View Javadoc

1   /* OpenLogViewer
2    *
3    * Copyright 2011
4    *
5    * This file is part of the OpenLogViewer project.
6    *
7    * OpenLogViewer software is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * OpenLogViewer software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with any OpenLogViewer software.  If not, see http://www.gnu.org/licenses/
19   *
20   * I ask that if you make any changes to this file you fork the code on github.com!
21   *
22   */
23  package org.diyefi.openlogviewer.genericlog;
24  
25  import java.beans.PropertyChangeEvent;
26  import java.beans.PropertyChangeListener;
27  import java.beans.PropertyChangeSupport;
28  import java.util.HashMap;
29  import org.diyefi.openlogviewer.OpenLogViewerApp;
30  
31  
32  
33  /**
34   *
35   * @author Bryan
36   */
37  @SuppressWarnings("serial")
38  public class GenericLog extends HashMap<String, GenericDataElement> {
39  
40  	// Stuff for showing a line count in all files loaded, if we ever do exports, either don't include this, and/or check for it before doing this
41  	private final String lineKey = "OLV Line Count";
42  	private String firstKey;
43  	private int lineCount = 0;
44  	private GenericDataElement lineCountElement;
45  
46  	final public static int LOG_NOT_LOADED = -1;
47  	final public static int LOG_LOADING = 0;
48  	final public static int LOG_LOADED = 1;
49  
50  	private String metaData;
51  	protected final PropertyChangeSupport PCS;
52  	private int logStatus;
53  
54  	private final PropertyChangeListener autoLoad = new PropertyChangeListener() {
55  		public void propertyChange(final PropertyChangeEvent propertyChangeEvent) {
56              if ((Integer) propertyChangeEvent.getNewValue() == 0) {
57              	GenericLog genLog = (GenericLog) propertyChangeEvent.getSource();
58              	genLog.setLogStatus(GenericLog.LOG_LOADING);
59                  OpenLogViewerApp.getInstance().setLog(genLog);
60              } else if ((Integer) propertyChangeEvent.getNewValue() == 1) {
61              	GenericLog genLog = (GenericLog) propertyChangeEvent.getSource();
62              	genLog.setLogStatus(GenericLog.LOG_LOADED);
63              	OpenLogViewerApp.getInstance().setLog(genLog);
64                  OpenLogViewerApp.getInstance().getOptionFrame().updateFromLog(genLog);
65              }
66          }
67      };
68  
69      public GenericLog() {
70          super();
71          logStatus = LOG_NOT_LOADED;
72          PCS = new PropertyChangeSupport(this);
73          addPropertyChangeListener("LogLoaded", autoLoad);
74          metaData = "";
75  
76      }
77  
78      /**
79       * provide a <code>String</code> array of headers<br>
80       * each header will be used as a HashMap key, the data related to each header will be added to an <code>ArrayList</code>.
81       * @param headers - of the data to be converted
82       */
83      public GenericLog(String[] headers) {
84          super();
85  
86          logStatus = LOG_NOT_LOADED;
87          PCS = new PropertyChangeSupport(this);
88          addPropertyChangeListener("LogLoaded", autoLoad);
89  
90          metaData = "";
91  
92          // A bit dirty, but not too bad.
93          String[] internalHeaders = new String[headers.length + 1];
94          for(int i=0;i<headers.length;i++){
95          	internalHeaders[i] = headers[i];
96          }
97          internalHeaders[headers.length] = lineKey;
98          this.setHeaders(internalHeaders);
99          lineCountElement = this.get(lineKey);
100         firstKey = headers[0];
101     }
102 
103     /**
104      * Add a piece of data to the <code>ArrayList</code> associated with the <code>key</code>
105      * @param key - header
106      * @param value - data to be added
107      * @return true or false if it was successfully added
108      */
109     public boolean addValue(String key, double value) {
110         GenericDataElement logElement = this.get(key);
111         if(key.equals(firstKey)){
112         	lineCountElement.add((double)lineCount);
113         	lineCount++;
114         }
115         return logElement.add(value);
116     }
117 
118     /**
119      * Set the state of the log
120      * @param newLogStatus GenericLog.LOG_NOT_LOADED / GenericLog.LOG_LOADING / GenericLog.LOG_LOADED
121      */
122     public void setLogStatus(int newLogStatus) {
123         int oldLogStatus = this.logStatus;
124         this.logStatus = newLogStatus;
125         PCS.firePropertyChange("LogLoaded", oldLogStatus, newLogStatus);
126     }
127 
128     /**
129      *
130      * @return -1 if log not loaded 0 if loading or 1 if log is loaded
131      */
132     public int getLogStatus() {
133         return this.logStatus;
134     }
135     /**
136      * sets the names of the headers for the Comparable interface of GenericDataElement
137      * @param headers
138      */
139     public void setHeaders(String[] headers) {
140         for (int x = 0; x < headers.length; x++) {
141             GenericDataElement GDE = new GenericDataElement();
142             GDE.setName(headers[x]);
143             this.put(headers[x], GDE);
144         }
145     }
146 
147     /**
148      * Add metadata This is information about the log being converted such as the location it was from or the date<br>
149      * This method does not add to its self so in order to add more info you must VAR.addMetaData(VAR.getMetaData() + NEWINFO)
150      * @param md meta data to be added
151      */
152     public void setMetaData(String md) {
153         metaData = md;
154     }
155 
156     /**
157      *
158      * @return String containing the current meta data
159      */
160     public String getMetadata() {
161         return metaData;
162     }
163     /**
164      * Add a property change listener to the generic log, REQUIRED!!
165      * GenericLog.LOG_STATUS is the name of the status property
166      * @param name
167      * @param listener
168      * <code>new OBJECT.addPropertyChangeListener("LogLoaded", new PropertyChangeListener() {<br>
169      *       public void propertyChange( final PropertyChangeEvent propertyChangeEvent) {<br>
170      *           OpenLogViewerApp.getInstance().setLog((GenericLog) propertyChangeEvent.getSource());
171      *           ...Insert code here...<br>
172      *
173      *       }<br>
174      *   });</code>
175      */
176     public void addPropertyChangeListener(final String name, final PropertyChangeListener listener) {
177         PCS.addPropertyChangeListener(name, listener);
178     }
179 
180     /**
181      * Remove a PropertyChangeListener
182      * @param propertyName name of listener
183      * @param listener listener
184      */
185     public void removePropertyChangeListener(final String propertyName, final PropertyChangeListener listener) {
186         PCS.removePropertyChangeListener(propertyName, listener);
187     }
188 }