View Javadoc

1   /* Open Log Viewer
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.awt.Color;
26  import java.awt.datatransfer.DataFlavor;
27  import java.awt.datatransfer.Transferable;
28  import java.awt.datatransfer.UnsupportedFlavorException;
29  import java.beans.PropertyChangeListener;
30  import java.beans.PropertyChangeSupport;
31  import java.io.IOException;
32  import java.io.Serializable;
33  import java.util.ArrayList;
34  
35  import org.diyefi.openlogviewer.coloring.InitialLineColoring;
36  
37  /**
38   * GenericDataElement is Comparable Serializable and Transferable and supports property change events
39   * it was built this way in order to be copy/pasteable later in the future
40   * when constructed this is the meat and potatoes of the program, the graphs and data
41   * displayed are pulled from these objects.
42   * @author Bryan Harris
43   */
44  public class GenericDataElement extends ArrayList<Double> implements Comparable, Serializable, Transferable {
45      /**
46       * default max value determined while loading up data
47       */
48      private Double maxValue;
49      /**
50       * newMaxValue is the value returned at any time getMaxValue() is called.
51       * it can be modified to change the graphing display
52       */
53      private Double newMaxValue;
54      /**
55       * default min value determined while loading up data
56       */
57      private Double minValue;
58      /**
59       * newMinValue is the value returned at any time getMinValue() is called.
60       * it can be modified to change the graphing display
61       */
62      private Double newMinValue;
63      /**
64       * default color created when building the GDE
65       */
66      private Color color;
67      /**
68       * Color that can be modified
69       */
70      private Color newColor;
71      /**
72       * GDE Header name
73       */
74      private String name;
75      /**
76       * Division on the Graphing layer
77       */
78      private int splitNumber;
79      private PropertyChangeSupport PCS;
80      private DataFlavor[] dataFlavor;
81  
82      /**
83       * Constructor brings the GDE up to speed, defaulting with an available 50,000 datapoints
84       * in order to reduce the number of times the Array list has to copy its contents
85       * in order to increase size.
86       */
87  
88      public GenericDataElement() {
89          super(50000); // TODO remove MAGIC
90          PCS = new PropertyChangeSupport(this);
91          maxValue = Double.MIN_VALUE;
92          newMaxValue = maxValue;
93          minValue = Double.MAX_VALUE;
94          newMinValue = minValue;
95          color = InitialLineColoring.INSTANCE.getBestAvailableColor();
96          newColor = color;
97          splitNumber = 1;
98          addFlavors();
99      }
100 
101     /**
102      * Data type support for Transferable
103      */
104     private void addFlavors() {
105         dataFlavor = new DataFlavor[3];
106         //try {
107         dataFlavor[0] = new DataFlavor(DataFlavor.javaSerializedObjectMimeType+
108                 ";class=\"" + GenericDataElement.class.getName() + "\"",
109                 "OLV GenericDataElement");
110         dataFlavor[1] = DataFlavor.stringFlavor;
111         dataFlavor[2] = DataFlavor.getTextPlainUnicodeFlavor();
112     }
113     /**
114      * override add(<T> t) of ArrayList to find min and max values before adding to the List
115      * @param d Double - Double.parseDouble(String) value to add to the array
116      * @return true on success, false if unable
117      */
118     @Override
119     public boolean add(Double d) {
120         if (newMaxValue < d) {
121             maxValue = d;
122             newMaxValue = d;
123         }
124         if (newMinValue > d) {
125             minValue = d;
126             newMinValue = d;
127         }
128 
129         return super.add(d);
130     }
131     /**
132      * set header name, called during GenericLog construction
133      * @param name
134      */
135     public void setName(String name) {
136         this.name = name;
137     }
138     /**
139      * getter
140      * @return name
141      */
142     public String getName() {
143         return name;
144     }
145     /**
146      * getter
147      * @return newMaxValue
148      */
149     public Double getMaxValue() {
150         return newMaxValue;
151     }
152 
153     /**
154      * setter
155      * @param highValue
156      */
157     public void setMaxValue(Double highValue) {
158         this.newMaxValue = highValue;
159     }
160     /**
161      * getter
162      * @return newMinValue
163      */
164     public Double getMinValue() {
165         return newMinValue;
166     }
167     /**
168      * setter
169      * @param lowValue
170      */
171     public void setMinValue(Double lowValue) {
172         this.newMinValue = lowValue;
173     }
174 
175     /**
176      * getter
177      * @return newColor
178      */
179     public Color getColor() {
180         return newColor;
181     }
182     /**
183      * setter
184      * @param c
185      */
186     public void setColor(Color c) {
187         newColor = c;
188     }
189     /**
190      * getter
191      * @return splitNumber
192      */
193     public int getSplitNumber() {
194         return splitNumber;
195     }
196     /**
197      * sets the splitNumber or division of the graph in the graphing screen
198      * if its the same a property change event is fired called "Split"
199      * @param splitNumber
200      */
201     public void setSplitNumber(int splitNumber) {
202         if(splitNumber < 1 ) {
203             splitNumber = 1;
204         }
205         int old = this.splitNumber;
206         this.splitNumber = splitNumber;
207         PCS.firePropertyChange("Split", old, this.splitNumber);
208 
209     }
210     /**
211      * this will set the min and max to the default min/max values
212      */
213     public void reset() {
214         newMinValue = minValue;
215         newMaxValue = maxValue;
216         //newColor = color;
217     }
218 
219     public void addPropertyChangeListener(String property, PropertyChangeListener PCL) {
220         PCS.addPropertyChangeListener(property, PCL);
221     }
222 
223     public void removePropertyChangeListener(String property, PropertyChangeListener PCL) {
224         PCS.removePropertyChangeListener(property, PCL);
225     }
226     ///Object
227     @Override
228     public String toString() {
229         return this.name;
230     }
231     //Comparable
232     @Override
233     public int compareTo(Object o) {
234         if (o instanceof GenericDataElement) {
235             GenericDataElement GDE = (GenericDataElement) o;
236             return this.getName().compareToIgnoreCase(GDE.getName());
237         } else {
238             return -1;
239         }
240     }
241     //Transferable
242     @Override
243     public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
244         if(flavor.equals(dataFlavor[0])) {
245             return this;
246         }else if(flavor.equals(dataFlavor[1])){
247             return "Unsupported";
248         }else if(flavor.equals(dataFlavor[2])){
249             return "Unsupported";
250         }
251         else {
252             throw new UnsupportedFlavorException(flavor);
253         }
254     }
255 
256     @Override
257     public DataFlavor[] getTransferDataFlavors() {
258         return dataFlavor;
259     }
260 
261     @Override
262     public boolean isDataFlavorSupported(DataFlavor flavor) {
263         for(int i = 0; i<dataFlavor.length;i++){
264             if(flavor.equals(dataFlavor[i])){
265                 return true;
266             }
267         }
268         return false;
269     }
270 
271     //Serializable
272 
273 }