1 /*
2 * Copyright (c) 2003-2008 by Cosylab d. d.
3 *
4 * This file is part of CosyBeans.
5 *
6 * CosyBeans is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CosyBeans is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CosyBeans. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package com.cosylab.gui.adapters;
21
22 import com.cosylab.gui.displayers.DataConsumer;
23 import com.cosylab.gui.displayers.DisplayerUtilities;
24 import com.cosylab.gui.displayers.DoubleConsumer;
25 import com.cosylab.gui.displayers.DoubleSeqConsumer;
26 import com.cosylab.util.CommonException;
27
28
29 /**
30 * <code>SimpleConverterSupport</code> is implementation of
31 * <code>Converter</code> interface intended for simple implementation, which
32 * use transfromation function on double data flow.
33 *
34 * <p>
35 * <b>Note!</b> This implementation works only with data sources and consumers,
36 * whcih support <code>double</code> as value type.
37 * </p>
38 *
39 * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
40 * @version $Id: SimpleConverterSupport.java,v 1.12 2008-04-22 12:31:02 jbobnar Exp $
41 *
42 * @see com.cosylab.gui.adapters.Converter
43 * @since Feb 14, 2004.
44 */
45 public abstract class SimpleConverterSupport extends AbstractConverter
46 implements Converter, DoubleConsumer, DoubleSeqConsumer
47 {
48 public static final String SHORT_NAME = "conv";
49 protected int arrayIndex=0;
50
51
52 /**
53 * Returns the arrayIndex.
54 * This index is used to extract value if this converter receives double array update.
55 * @return Returns the arrayIndex.
56 */
57 public int getArrayIndex() {
58 return arrayIndex;
59 }
60
61
62 /**
63 * Sets new arrayIndex.
64 * This index is used to extract value if this converter receives double array update.
65 * @param arrayIndex The arrayIndex to set.
66 */
67 public void setArrayIndex(int arrayIndex) {
68 this.arrayIndex = arrayIndex;
69 }
70
71 /**
72 * Creates new converter.
73 */
74 public SimpleConverterSupport()
75 {
76 // we dalagate to super suppport class only the consumer type/types we want to suppport
77 super(new Class[]{ DoubleConsumer.class });
78
79 // the name of this converter when it acts as a consumer
80 name = "SimpleConverter";
81 }
82
83 /**
84 * This is called by peer data source. Call is delegated to contained
85 * consumers.
86 *
87 * @see com.cosylab.gui.displayers.DoubleConsumer#updateValue(long, double)
88 */
89 public void updateValue(long timestamp, double value)
90 throws CommonException
91 {
92
93 DataConsumer[] delegates = getConsumers();
94
95 // transform value
96 final double newValue = transform(value);
97
98 for (int i = 0; i < delegates.length; i++) {
99 try {
100 if (delegates[i] instanceof DoubleSeqConsumer) ((DoubleSeqConsumer)delegates[i]).updateValue(timestamp, new double[]{newValue});
101 else if (delegates[i] instanceof DoubleConsumer)((DoubleConsumer)delegates[i]).updateValue(timestamp, newValue);
102 } catch (Exception e) {
103 e.printStackTrace();
104 }
105 }
106 }
107
108 protected NonblockingNumberDispatcher getNonblDisplatecher()
109 {
110 if (nonblDispatcher == null) {
111 nonblDispatcher = new NonblockingNumberDispatcher() {
112 private static final long serialVersionUID = -2136890381501631542L;
113
114 public void updateNonblocking(Number value)
115 {
116 // we delegate with inverse to multiplication
117 super.updateNonblocking(new Double(inverseTransform(
118 value.doubleValue())));
119 }
120 };
121 }
122
123 return nonblDispatcher;
124 }
125
126 /**
127 * User must implement this method and from prvided parameter calculate
128 * with own transfromatin function new value and return it.
129 *
130 * @param value the value to be transfirmed
131 *
132 * @return the transformed value
133 */
134 abstract protected double transform(double value);
135
136 /**
137 * User must implement this method to make inverse transformation as in
138 * <code>transfirm(double)</code> function. Following must be true
139 * <code>some_value == inverseTransform(transfirm(some_value))</code>.
140 *
141 * @param value the value to be transfirmed with inverse function
142 *
143 * @return the inversed value
144 *
145 * @see #transform(double)
146 */
147 abstract protected double inverseTransform(double value);
148
149 /* (non-Javadoc)
150 * @see com.cosylab.gui.displayers.DoubleSeqConsumer#updateValue(long, double[])
151 */
152 public void updateValue(long timestamp, double[] value) throws CommonException {
153 DataConsumer[] delegates = getConsumers();
154
155 for (int i = 0; i < delegates.length; i++) {
156 try {
157 if (delegates[i] instanceof DoubleSeqConsumer) ((DoubleSeqConsumer)delegates[i]).updateValue(timestamp, transform(value));
158 else if (delegates[i] instanceof DoubleConsumer)((DoubleConsumer)delegates[i]).updateValue(timestamp, transform(DisplayerUtilities.extract(arrayIndex,value)));
159 } catch (Exception e) {
160 e.printStackTrace();
161 }
162 }
163 }
164
165
166 private double[] transform(double[] value) {
167 double[] d = new double[value.length];
168 for (int i = 0; i < value.length; i++) d[i] = transform(value[i]);
169 return d;
170 }
171 }
172
173 /* __oOo__ */