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 java.util.HashMap;
23 import java.util.Map;
24
25 import com.cosylab.gui.displayers.CommonDisplayer;
26 import com.cosylab.gui.displayers.DoubleConsumer;
27
28
29 /**
30 * <code>MulitplierConverter</code> acts as data source and data consumer in
31 * the same time. This converter multiplies all data flow with user defined
32 * multiplication factor. As implementation base
33 * <code>SimpleConverterSupport</code> is used.
34 *
35 * <p>
36 * <b>Note!</b> This implementation works only with data sources and consumers,
37 * whcih support <code>double</code> as value type.
38 * </p>
39 *
40 * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
41 * @version $Id: MultiplierConverter.java,v 1.18 2008-04-22 12:31:02 jbobnar Exp $
42 *
43 * @see com.cosylab.gui.adapters.Converter
44 * @see com.cosylab.gui.adapters.SimpleConverterSupport
45 * @since Feb 14, 2004.
46 */
47 public class MultiplierConverter extends SimpleConverterSupport
48 implements DoubleConsumer
49 {
50 private static final long serialVersionUID = 2621276961866711269L;
51
52 public static final String SHORT_NAME = "Multiplier";
53
54 // the multiplication factor
55 private double multiplicationFactor = 1.0;
56
57 /**
58 * Creates new multiplyer with default factor 1.0.
59 */
60 public MultiplierConverter()
61 {
62 this(1.0);
63 }
64
65 /**
66 * Creates new multiplyer with defined factor.
67 *
68 * @param factor new multiplicationfactor
69 */
70 public MultiplierConverter(double factor)
71 {
72 // default value
73 setMultiplicationFactor(factor);
74
75 // the name of this converter when it acts as a consumer
76 name = "MulitplierConverter";
77 }
78
79 /**
80 * This is called by peer data source. Call is delegated to contained
81 * consumers. In the process it is modified with multiplication factor
82 *
83 * @see com.cosylab.gui.displayers.DataConsumer#setCharacteristics(java.util.Map)
84 */
85 public void setCharacteristics(Map characteristics)
86 {
87 // We store unmodified characteristic for possible later reinitialization of
88 // consumers, eg. if multiplication factor changes
89 cacheLastCharacteristics(characteristics);
90
91 HashMap map = new HashMap(characteristics);
92
93 double min;
94 double max;
95
96 // Modifying the graph_min and graph_max characteristics.
97 if (map.containsKey(CommonDisplayer.C_GRAPH_MIN)
98 && map.containsKey(CommonDisplayer.C_GRAPH_MAX)) {
99 min = ((Number)map.get(CommonDisplayer.C_GRAPH_MIN)).doubleValue();
100 max = ((Number)map.get(CommonDisplayer.C_GRAPH_MAX)).doubleValue();
101
102 if (multiplicationFactor < 0) {
103 double a = max;
104 max = min;
105 min = a;
106 }
107
108 map.put(CommonDisplayer.C_GRAPH_MIN,
109 new Double(min * multiplicationFactor));
110 map.put(CommonDisplayer.C_GRAPH_MAX,
111 new Double(max * multiplicationFactor));
112 }
113
114 // Modifying the minimum and maximum characteristics.
115 if (map.containsKey(CommonDisplayer.C_MINIMUM)
116 && map.containsKey(CommonDisplayer.C_MAXIMUM)) {
117 min = ((Number)map.get(CommonDisplayer.C_MINIMUM)).doubleValue();
118 max = ((Number)map.get(CommonDisplayer.C_MAXIMUM)).doubleValue();
119
120 if (multiplicationFactor < 0) {
121 double a = max;
122 max = min;
123 min = a;
124 }
125
126 map.put(CommonDisplayer.C_MINIMUM,
127 new Double(min * multiplicationFactor));
128 map.put(CommonDisplayer.C_MAXIMUM,
129 new Double(max * multiplicationFactor));
130 }
131
132 // Modifying the units characteristics
133 map.put(CommonDisplayer.C_UNITS,
134 "1/" + multiplicationFactor + "*" + map.get(CommonDisplayer.C_UNITS));
135
136 super.setCharacteristics(map);
137 }
138
139 /**
140 * Returns factor by which all data from input will be multiplied
141 *
142 * @return multiplication factor
143 */
144 public double getMultiplicationFactor()
145 {
146 return multiplicationFactor;
147 }
148
149 /**
150 * Sets factor by which all data from input will be multiplied
151 *
152 * @param d new multiplication factor
153 *
154 * @throws IllegalArgumentException if set value is 0.0
155 */
156 public void setMultiplicationFactor(double d)
157 {
158 if (d == 0.0) {
159 throw new IllegalArgumentException(
160 "It's a bad idea to set multiplication factor to 0.0.");
161 }
162
163 multiplicationFactor = d;
164
165 if (getLastCharacteristics() != null) {
166 // reinitialize the peer consumers with changed multiplication factor
167 setCharacteristics(getLastCharacteristics());
168 }
169 }
170
171 /**
172 * Here we implement our transformation function.
173 *
174 * @see com.cosylab.gui.adapters.SimpleConverterSupport#transform(double)
175 */
176 protected double transform(double value)
177 {
178 return value * multiplicationFactor;
179 }
180
181 /**
182 * Here we implement invers transformation of our transformation function.
183 *
184 * @see com.cosylab.gui.adapters.SimpleConverterSupport#inverseTransform(double)
185 */
186 protected double inverseTransform(double value)
187 {
188 return value / multiplicationFactor;
189 }
190
191 /*
192 * (non-Javadoc)
193 * @see com.cosylab.gui.adapters.DataConsumerDispatcher#getName()
194 */
195 public String getName(){
196 return SHORT_NAME;
197 }
198
199 /*
200 * (non-Javadoc)
201 * @see java.lang.Object#toString()
202 */
203 public String toString(){
204 return SHORT_NAME + ": " + getMultiplicationFactor();
205 }
206
207 /*
208 * (non-Javadoc)
209 * @see java.lang.Object#equals(java.lang.Object)
210 */
211 @Override
212 public boolean equals(Object obj) {
213 if (!(obj instanceof MultiplierConverter)) return false;
214 MultiplierConverter c = (MultiplierConverter)obj;
215 return c.getMultiplicationFactor() == multiplicationFactor;
216 }
217 }
218
219 /* __oOo__ */