View Javadoc

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.displayers;
21  
22  import java.io.IOException;
23  import java.io.ObjectStreamException;
24  import java.io.Serializable;
25  import java.util.Arrays;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  /**
31   * DisplayerParameters holds various properties of some displayed value. These
32   * properties can be anything that specify a displayable value (Color, name etc)
33   * and should also hold information about remote connection that displayed value
34   * is referred to. DisplayerParameters is a serializable object.
35   * 
36   * @author Jaka Bobnar, Cosylab
37   * 
38   */
39  public class DisplayerParameters implements Serializable {
40  
41  	private static final long serialVersionUID = 794831250607895558L;
42  	protected String name;
43  	transient protected String[] names;
44  	transient protected Map<String, Object> values;
45  
46  	/**
47  	 * Creates new instance of DisplayerParameters.
48  	 */
49  	public DisplayerParameters(String name) {
50  		this(name, 10);
51  	}
52  
53  	/**
54  	 * Creates new instance of DisplayerParameters.
55  	 */
56  	public DisplayerParameters(String name, int size) {
57  		this.name = name;
58  		values = new HashMap<String, Object>(size);
59  	}
60  
61  	/**
62  	 * Creates new instance of DisplayerParameters.
63  	 */
64  	protected DisplayerParameters(String name, List<String> names, List values) {
65  		this.name = name;
66  		this.values = new HashMap<String, Object>(names.size());
67  		for (int i = 0; i < names.size(); i++) {
68  			this.values.put(names.get(i), values.get(i));
69  		}
70  	}
71  
72  	/**
73  	 * Creates new instance of DisplayerParameters.
74  	 */
75  	protected DisplayerParameters(String name, String[] names, Object[] values) {
76  		this.name = name;
77  		this.values = new HashMap<String, Object>(names.length);
78  		for (int i = 0; i < names.length; i++) {
79  			this.values.put(names[i], values[i]);
80  		}
81  	}
82  
83  	protected DisplayerParameters(String name, int size, double min,
84  			double max, String units, String format) {
85  		this(name, size);
86  		values.put(Displayer.C_MINIMUM, min);
87  		values.put(Displayer.C_MAXIMUM, max);
88  		values.put(Displayer.C_UNITS, units);
89  		values.put(Displayer.C_FORMAT,format);
90  	}
91  
92  	protected DisplayerParameters(String name, int size, DoubleDisplayer disp) {
93  		this(name, size, disp.getMinimum(), disp.getMaximum(), disp.getUnits(),
94  				disp.getFormat());
95  	}
96  	
97  	/**
98  	 * Constructs new DisplayerParameters with the given name. The rest
99  	 * of the parameters will be extracted from the displayer.
100 	 * 
101 	 * @param name the name paramete
102 	 * @param disp source of parameters
103 	 */
104 	public DisplayerParameters(String name, DoubleDisplayer disp) {
105 		this(name, 4, disp.getMinimum(), disp.getMaximum(), disp.getUnits(),
106 				disp.getFormat());
107 	}
108 
109 	/**
110 	 * Constructs new DisplayerParameters with the given values.
111 	 * 
112 	 * @param name
113 	 * @param min
114 	 * @param max
115 	 * @param units
116 	 * @param format
117 	 */
118 	public DisplayerParameters(String name, double min, double max,
119 			String units, String format) {
120 		this(name, 4, min, max, units, format);
121 	}
122 
123 	/**
124 	 * Constructs new DisplayerParameters with the given name and values
125 	 * listed in the Map.
126 	 * 
127 	 * @param name the name parameter
128 	 * @param values optional parameters
129 	 */
130 	public DisplayerParameters(String name, Map<String, Object> values) {
131 		this(name);
132 		this.values.putAll(values);
133 	}
134 
135 	/**
136 	 * Adds a parameter with the given name.
137 	 * 
138 	 * @param name the name of the parameter
139 	 * @param value the parameter
140 	 * @return previous value under the given name
141 	 */
142 	public Object addParameter(String name, Object value) {
143 		names=null;
144 		return values.put(name, value);
145 	}
146 
147 	/**
148 	 * Puts a parameter under the given name. This method does not trigger
149 	 * refresh of the parameters names.
150 	 * 
151 	 * @param name the name of the parameter
152 	 * @param value the replacement parameter
153 	 * @return the previous parameter under the given name
154 	 */
155 	public Object putParameter(String name, Object value) {
156 		return values.put(name, value);
157 	}
158 
159 	/**
160 	 * Returns the parameter under the given name.
161 	 * 
162 	 * @param name the name of the parameter
163 	 * @return the parameter
164 	 */
165 	public Object getParameter(String name) {
166 		return values.get(name);
167 	}
168 
169 	/**
170 	 * Returns the name.
171 	 * 
172 	 * @return Returns the name.
173 	 */
174 	public String getName() {
175 		return name;
176 	}
177 
178 	/*
179 	 * (non-Javadoc)
180 	 * 
181 	 * @see java.lang.Object#toString()
182 	 */
183 	@Override
184 	public String toString() {
185 		String[] n= getNames();
186 		
187 		StringBuilder s = new StringBuilder(32 * n.length);
188 		s.append(name);
189 		s.append("::{ ");
190 		if (0 < n.length) {
191 			s.append(n[0]);
192 			s.append("=");
193 			s.append(values.get(n[0]));
194 		}
195 		for (int i = 1; i < n.length; i++) {
196 			s.append(", ");
197 			s.append(n[i]);
198 			s.append("=");
199 			s.append(values.get(n[i]));
200 		}
201 		s.append('}');
202 		return s.toString();
203 	}
204 
205 	/**
206 	 * @return the format
207 	 */
208 	public String getFormat() {
209 		return (String)values.get(Displayer.C_FORMAT);
210 	}
211 
212 	/**
213 	 * @return the maximum
214 	 */
215 	public double getMaximum() {
216 		return getDouble(Displayer.C_MAXIMUM);
217 	}
218 
219 	/**
220 	 * @return the minimum
221 	 */
222 	public double getMinimum() {
223 		return getDouble(Displayer.C_MINIMUM);
224 	}
225 
226 	protected double getDouble(String name) {
227 		Object o = values.get(name);
228 		if (o instanceof Number) {
229 			return ((Number)o).doubleValue();
230 		}
231 		return Double.NaN;
232 	}
233 
234 	protected int getInt(String name) {
235 		Object o = values.get(name);
236 		if (o instanceof Number) {
237 			return ((Number)o).intValue();
238 		}
239 		return 0;
240 	}
241 	
242 	protected boolean getBool(String name) {
243 		Object o= values.get(name);
244 		return (o instanceof Boolean) && ((Boolean)o).booleanValue();
245 	}
246 
247 	/**
248 	 * @return the units
249 	 */
250 	public String getUnits() {
251 		return (String)values.get(Displayer.C_UNITS);
252 	}
253 
254 	/**
255 	 * Returns the names of all parameters.
256 	 * 
257 	 * @return
258 	 */
259 	public String[] getNames() {
260 		if (names!=null) {
261 			return names;
262 		}
263 		names= values.keySet().toArray(new String[values.size()]);
264 		Arrays.sort(names);
265 		return names;
266 	}
267 
268 	/**
269 	 * Returns the values of all parameters
270 	 * @return
271 	 */
272 	public Object[] getValues() {
273 		String[] n= getNames();
274 		Object[] o= new Object[values.size()];
275 		for (int i = 0; i < o.length; i++) {
276 			o[i]=values.get(n[i]);
277 		}
278 		return o;
279 	}
280 
281 	/**
282 	 * Returns the number of all parameters.
283 	 * 
284 	 * @return
285 	 */
286 	public int count() {
287 		return values.size();
288 	}
289 
290 	/*
291 	 * (non-Javadoc)
292 	 * 
293 	 * @see java.lang.Object#equals(java.lang.Object)
294 	 */
295 	@Override
296 	public boolean equals(Object o) {
297 		if (!(o instanceof DisplayerParameters))
298 			return false;
299 		DisplayerParameters p = (DisplayerParameters)o;
300 		Object[] val = p.getValues();
301 		String[] names = p.getNames();
302 		return (Arrays.equals(val, getValues()) && Arrays.equals(names,
303 				getNames()));
304 	}
305 	
306 	/*
307 	 * (non-Javadoc)
308 	 * @see java.lang.Object#hashCode()
309 	 */
310 	@Override
311 	public int hashCode() {
312 		return toString().hashCode();
313 	}
314 	
315 	private void writeObject(java.io.ObjectOutputStream out) throws IOException {
316 		out.defaultWriteObject();
317 		String[] n= getNames();
318 		Object[] o= getValues();
319 		out.writeObject(n);
320 		out.writeObject(o);
321 	}
322 	private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
323 		in.defaultReadObject();
324 		String[] n= (String[])in.readObject();
325 		Object[] o= (Object[])in.readObject();
326 		 
327 		values= new HashMap<String, Object>(n.length);
328 		for (int i = 0; i < o.length; i++) {
329 			values.put(n[i], o[i]);
330 		}
331 	}
332 	@SuppressWarnings("unused")
333 	private void readObjectNoData() throws ObjectStreamException {
334 		values= new HashMap<String, Object>(0);
335 	}
336 }
337 
338 /* __oOo__ */