1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.Arrays;
34
35 import org.diyefi.openlogviewer.coloring.InitialLineColoring;
36
37
38
39
40
41
42
43
44 public final class GenericDataElement implements Comparable<GenericDataElement>, Serializable, Transferable {
45 private static final long serialVersionUID = 1L;
46
47 private static int currentRecord;
48
49
50
51
52 private double[] values;
53
54
55 private double minValue;
56 private double maxValue;
57 private boolean realMinAndMaxFound = false;
58
59
60 private double displayMinValue;
61 private double displayMaxValue;
62 private Color displayColor;
63 private boolean displayMinAndMaxSet = false;
64
65
66
67
68 private String name;
69
70
71
72
73 private int splitNumber;
74 private PropertyChangeSupport PCS;
75 private DataFlavor[] dataFlavor;
76
77
78
79
80
81
82 protected GenericDataElement(final int initialLength) {
83 values = new double[initialLength];
84
85 PCS = new PropertyChangeSupport(this);
86
87 maxValue = -Double.MAX_VALUE;
88 minValue = Double.MAX_VALUE;
89
90
91 displayColor = InitialLineColoring.INSTANCE.getBestAvailableColor();
92 splitNumber = 1;
93 addFlavors();
94 }
95
96 protected void increaseCapacity(final int ourLoadFactor) {
97 values = Arrays.copyOf(values, (values.length * ourLoadFactor));
98 }
99
100 protected static void incrementPosition() {
101 currentRecord++;
102 }
103 protected static void resetPosition() {
104 currentRecord = -1;
105 }
106
107
108
109
110 private void addFlavors() {
111 dataFlavor = new DataFlavor[3];
112 final String supportedFlavour = DataFlavor.javaSerializedObjectMimeType + ";class=\"" + GenericDataElement.class.getName() + "\"";
113 dataFlavor[0] = new DataFlavor(supportedFlavour, "OLV GenericDataElement");
114 dataFlavor[1] = DataFlavor.stringFlavor;
115 dataFlavor[2] = DataFlavor.getTextPlainUnicodeFlavor();
116 }
117
118
119
120
121
122
123 public void add(final double value) {
124 values[currentRecord] = value;
125 }
126
127
128 public double get(final int index) {
129 return values[index];
130 }
131 public int size() {
132 return currentRecord;
133 }
134
135
136
137
138
139
140
141 public void setSplitNumber(final int splitNumber) {
142 int newSplitNumber = 1;
143 if (splitNumber > 1) {
144 newSplitNumber = splitNumber;
145 }
146
147 final int old = this.splitNumber;
148 this.splitNumber = newSplitNumber;
149 PCS.firePropertyChange("Split", old, this.splitNumber);
150 }
151
152
153
154
155 public void reset() {
156 displayMinValue = getMinValue();
157 displayMaxValue = getMaxValue();
158 }
159
160 public void addPropertyChangeListener(final String property, final PropertyChangeListener PCL) {
161 PCS.addPropertyChangeListener(property, PCL);
162 }
163
164 public void removePropertyChangeListener(final String property, final PropertyChangeListener PCL) {
165 PCS.removePropertyChangeListener(property, PCL);
166 }
167
168 @Override
169 public String toString() {
170 return this.name;
171 }
172
173 @Override
174 public int compareTo(final GenericDataElement otherGDE) {
175 return this.getName().compareToIgnoreCase(otherGDE.getName());
176 }
177
178 @Override
179 public Object getTransferData(final DataFlavor flavor) throws UnsupportedFlavorException, IOException {
180 if (flavor.equals(dataFlavor[0])) {
181 return this;
182 } else if (flavor.equals(dataFlavor[1])) {
183 return "Unsupported";
184 } else if (flavor.equals(dataFlavor[2])) {
185 return "Unsupported";
186 } else {
187 throw new UnsupportedFlavorException(flavor);
188 }
189 }
190
191 @Override
192 public DataFlavor[] getTransferDataFlavors() {
193 return dataFlavor;
194 }
195
196 @Override
197 public boolean isDataFlavorSupported(final DataFlavor flavor) {
198 for (int i = 0; i < dataFlavor.length; i++) {
199 if (flavor.equals(dataFlavor[i])) {
200 return true;
201 }
202 }
203 return false;
204 }
205
206
207
208
209
210 public void setName(final String name) {
211 this.name = name;
212 }
213 public String getName() {
214 return name;
215 }
216
217 public double getMaxValue() {
218 findMinAndMaxValues();
219 return maxValue;
220 }
221 public double getMinValue() {
222 findMinAndMaxValues();
223 return minValue;
224 }
225
226 private void findMinAndMaxValues() {
227 if (!realMinAndMaxFound) {
228 for (int i = 0; i < currentRecord; i++) {
229 final double value = values[i];
230 if (maxValue < value) {
231 maxValue = value;
232 }
233 if (minValue > value) {
234 minValue = value;
235 }
236 }
237
238
239 realMinAndMaxFound = true;
240 }
241 }
242
243 public double getDisplayMinValue() {
244 setDisplayMinAndMaxDefaultsIfRequired();
245 return displayMinValue;
246 }
247 public double getDisplayMaxValue() {
248 setDisplayMinAndMaxDefaultsIfRequired();
249 return displayMaxValue;
250 }
251 private void setDisplayMinAndMaxDefaultsIfRequired() {
252 if (!displayMinAndMaxSet) {
253 displayMinValue = getMinValue();
254 displayMaxValue = getMaxValue();
255 displayMinAndMaxSet = true;
256 }
257 }
258
259 public void setDisplayMaxValue(final double highValue) {
260 if (!displayMinAndMaxSet) {
261 displayMinValue = getMinValue();
262 displayMinAndMaxSet = true;
263 }
264 this.displayMaxValue = highValue;
265 }
266 public void setDisplayMinValue(final double lowValue) {
267 if (!displayMinAndMaxSet) {
268 displayMaxValue = getMaxValue();
269 displayMinAndMaxSet = true;
270 }
271 this.displayMinValue = lowValue;
272 }
273
274 public Color getDisplayColor() {
275 return displayColor;
276 }
277 public void setDisplayColor(final Color c) {
278 displayColor = c;
279 }
280 public int getSplitNumber() {
281 return splitNumber;
282 }
283
284 public void clearOut() {
285 values = null;
286 }
287 }