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