View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common 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-Common 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-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components;
21  
22  import java.awt.Dimension;
23  import java.awt.Font;
24  import java.awt.GridBagConstraints;
25  import java.awt.GridBagLayout;
26  import java.awt.Insets;
27  import java.awt.event.ActionEvent;
28  import java.awt.event.ActionListener;
29  import java.beans.Customizer;
30  import java.beans.PropertyChangeEvent;
31  import java.beans.PropertyChangeListener;
32  
33  import javax.swing.ButtonGroup;
34  import javax.swing.JCheckBox;
35  import javax.swing.JLabel;
36  import javax.swing.JPanel;
37  import javax.swing.JRadioButton;
38  import javax.swing.JTextField;
39  
40  import com.cosylab.gui.components.range2.RangedValuePolicy;
41  import com.cosylab.gui.components.range2.RescalingValuePolicy;
42  import com.cosylab.gui.components.range2.ShiftValuePolicy;
43  import com.cosylab.gui.components.range2.TrimValuePolicy;
44  
45  public class GaugerCustomizer extends AbstractDisplayerPanelCustomizer {
46  	private static final long serialVersionUID = 1L;
47  	/** Component name */
48  	public static final String GAUGER = "Gauger";
49  	/** ASPECTS */
50  	public static final String[] ASPECTS = { GAUGER };
51  	
52  	/** Tags for properties */
53  	public static final String FORMAT = "format", UNITS = "units", MINIMUM = "minimum", MAXIMUM = "maximum", FONT_SIZE = "fontSize";
54  	
55  	/** Value display properties */
56  	public static final String[] GAUGER_PROPERTIES = {FORMAT, UNITS, MINIMUM, MAXIMUM, FONT_SIZE};
57  		
58  	private class WPanel extends JPanel implements Customizer
59  	{
60  		private static final long serialVersionUID = 1L;
61  		private boolean initialized = false;
62  		private Gauger disp;
63  
64  		// GUI elements
65  		private NumberField minField;
66  		private NumberField maxField;
67  		private JTextField formatField;
68  		private JTextField unitsField;
69  		private JCheckBox logarithmic;
70  		private JPanel behaviourPanel;
71  		private JPanel displayPanel;
72  		private JRadioButton fixedButton;
73  		private JRadioButton stretchButton;
74  		private JRadioButton slideButton;
75  		private ButtonGroup scaleGroup;
76  		private JTextField fontSize;
77  
78  		private void initialize()
79  		{
80  			if (initialized) {
81  				return;
82  			}
83  
84  			initialized = true;
85  
86  			setLayout(new GridBagLayout());
87  			setSize(300, 450);
88  
89  			add(getDisplayPanel(),
90  			    new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
91  			        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
92  			        new Insets(11, 11, 4, 11), 0, 0));
93  			add(getBehaviourPanel(),
94  			    new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
95  			        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
96  			        new Insets(11, 11, 4, 11), 0, 0));
97  		}
98  
99  		private JPanel getDisplayPanel()
100 		{
101 			if (displayPanel == null) {
102 				displayPanel = new JPanel();
103 				displayPanel.setBorder(new GroupBoxBorder("Value Display"));
104 				displayPanel.setLayout(new GridBagLayout());
105 
106 				displayPanel.add(new JLabel("Format"),
107 				    new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
108 				        GridBagConstraints.WEST, GridBagConstraints.NONE,
109 				        new Insets(4, 11, 4, 4), 0, 0));
110 				displayPanel.add(getFormatField(),
111 				    new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0,
112 				        GridBagConstraints.WEST, GridBagConstraints.NONE,
113 				        new Insets(4, 4, 4, 4), 0, 0));
114 
115 				displayPanel.add(new JLabel("Units"),
116 				    new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
117 				        GridBagConstraints.WEST, GridBagConstraints.NONE,
118 				        new Insets(4, 11, 4, 4), 0, 0));
119 				displayPanel.add(getUnitsField(),
120 				    new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0,
121 				        GridBagConstraints.WEST, GridBagConstraints.NONE,
122 				        new Insets(4, 4, 4, 4), 0, 0));
123 
124 				displayPanel.add(new JLabel("Minimum"),
125 				    new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
126 				        GridBagConstraints.WEST, GridBagConstraints.NONE,
127 				        new Insets(4, 11, 4, 4), 0, 0));
128 				displayPanel.add(getMinField(),
129 				    new GridBagConstraints(1, 2, 1, 1, 1.0, 1.0,
130 				        GridBagConstraints.WEST, GridBagConstraints.NONE,
131 				        new Insets(4, 4, 4, 4), 0, 0));
132 
133 				displayPanel.add(new JLabel("Maximum"),
134 				    new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
135 				        GridBagConstraints.WEST, GridBagConstraints.NONE,
136 				        new Insets(4, 11, 4, 4), 0, 0));
137 				displayPanel.add(getMaxField(),
138 				    new GridBagConstraints(1, 3, 1, 1, 1.0, 1.0,
139 				        GridBagConstraints.WEST, GridBagConstraints.NONE,
140 				        new Insets(4, 4, 4, 4), 0, 0));
141 
142 				displayPanel.add(new JLabel("Value label size"),
143 				    new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0,
144 				        GridBagConstraints.WEST, GridBagConstraints.NONE,
145 				        new Insets(4, 11, 4, 4), 0, 0));
146 				displayPanel.add(getFontSize(),
147 				    new GridBagConstraints(1, 4, 1, 1, 1.0, 1.0,
148 				        GridBagConstraints.WEST, GridBagConstraints.NONE,
149 				        new Insets(4, 4, 4, 4), 0, 0));
150 			}
151 
152 			return displayPanel;
153 		}
154 
155 		private JPanel getBehaviourPanel()
156 		{
157 			if (behaviourPanel == null) {
158 				Insets insets = new Insets(2, 2, 2, 2);
159 				behaviourPanel = new JPanel();
160 				behaviourPanel.setBorder(new GroupBoxBorder("Scale preferences"));
161 				behaviourPanel.setLayout(new GridBagLayout());
162 				behaviourPanel.add(getLogarithmic(),
163 				    new GridBagConstraints(0, 1, 3, 1, 1.0, 1.0,
164 				        GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
165 				        insets, 0, 0));
166 
167 				behaviourPanel.add(getFixedButton(),
168 				    new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
169 				        GridBagConstraints.WEST, GridBagConstraints.NONE,
170 				        insets, 0, 0));
171 
172 				behaviourPanel.add(getStretchButton(),
173 				    new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0,
174 				        GridBagConstraints.WEST, GridBagConstraints.NONE,
175 				        insets, 0, 0));
176 
177 				behaviourPanel.add(getSlideButton(),
178 				    new GridBagConstraints(2, 0, 1, 1, 1.0, 1.0,
179 				        GridBagConstraints.WEST, GridBagConstraints.NONE,
180 				        insets, 0, 0));
181 			}
182 
183 			return behaviourPanel;
184 		}
185 
186 		private JTextField getFontSize()
187 		{
188 			if (fontSize == null) {
189 				fontSize = new JTextField();
190 				fontSize.setPreferredSize(new Dimension(75, 21));
191 				fontSize.setMinimumSize(new Dimension(75, 21));
192 				fontSize.setToolTipText("Font size of value label");
193 				fontSize.addActionListener(applySettingsListener);
194 			}
195 
196 			return fontSize;
197 		}
198 
199 		private JCheckBox getLogarithmic()
200 		{
201 			if (logarithmic == null) {
202 				logarithmic = new JCheckBox("Logarithmic");
203 				logarithmic.setPreferredSize(new Dimension(150, 21));
204 				logarithmic.setMinimumSize(new Dimension(75, 21));
205 				logarithmic.setToolTipText("Use logarithmic scale.");
206 				logarithmic.addActionListener(applySettingsListener);
207 			}
208 
209 			return logarithmic;
210 		}
211 
212 		/**
213 		 * Returns editor for units.
214 		 *
215 		 * @return Instance of JTextField.
216 		 */
217 		private JTextField getUnitsField()
218 		{
219 			if (unitsField == null) {
220 				unitsField = new JTextField();
221 				unitsField.setPreferredSize(new Dimension(75, 21));
222 				unitsField.setMinimumSize(new Dimension(75, 21));
223 				unitsField.setToolTipText("Physical units of displayed value");
224 				unitsField.addActionListener(applySettingsListener);
225 			}
226 
227 			return unitsField;
228 		}
229 
230 		/**
231 		 * Returns editor for format.
232 		 *
233 		 * @return Instance of JTextField.
234 		 */
235 		private JTextField getFormatField()
236 		{
237 			if (formatField == null) {
238 				formatField = new JTextField();
239 				formatField.setPreferredSize(new Dimension(75, 21));
240 				formatField.setMinimumSize(new Dimension(75, 21));
241 				formatField.setToolTipText("C Style Format");
242 				formatField.addActionListener(applySettingsListener);
243 			}
244 
245 			return formatField;
246 		}
247 
248 		/**
249 		 * Returns editor for max value.
250 		 *
251 		 * @return Instance of NumberField.
252 		 */
253 		private NumberField getMaxField()
254 		{
255 			if (maxField == null) {
256 				maxField = new NumberField();
257 				maxField.setNumberType(Double.class);
258 				maxField.setPreferredSize(new Dimension(100, 21));
259 				maxField.setToolTipText("Maximal allowed value");
260 				maxField.addActionListener(applySettingsListener);
261 			}
262 
263 			return maxField;
264 		}
265 
266 		private NumberField getMinField()
267 		{
268 			if (minField == null) {
269 				minField = new NumberField();
270 				minField.setNumberType(Double.class);
271 				minField.setPreferredSize(new Dimension(100, 21));
272 				minField.setToolTipText("Minimal allowed value");
273 				minField.addActionListener(applySettingsListener);
274 			}
275 
276 			return minField;
277 		}
278 
279 		private ButtonGroup getScaleGroup()
280 		{
281 			if (scaleGroup == null) {
282 				scaleGroup = new ButtonGroup();
283 			}
284 
285 			return scaleGroup;
286 		}
287 
288 		private JRadioButton getFixedButton()
289 		{
290 			if (fixedButton == null) {
291 				fixedButton = new JRadioButton("Fixed");
292 				getScaleGroup().add(fixedButton);
293 				fixedButton.addActionListener(applySettingsListener);
294 			}
295 
296 			return fixedButton;
297 		}
298 
299 		private JRadioButton getStretchButton()
300 		{
301 			if (stretchButton == null) {
302 				stretchButton = new JRadioButton("Stretch");
303 				getScaleGroup().add(stretchButton);
304 				stretchButton.addActionListener(applySettingsListener);
305 			}
306 
307 			return stretchButton;
308 		}
309 
310 		private JRadioButton getSlideButton()
311 		{
312 			if (slideButton == null) {
313 				slideButton = new JRadioButton("Slide");
314 				getScaleGroup().add(slideButton);
315 				slideButton.addActionListener(applySettingsListener);
316 			}
317 
318 			return slideButton;
319 		}
320 
321 		/*
322 		 * Updates editor with values from diplayer.
323 		 */
324 		private void update()
325 		{
326 			if (disp == null) {
327 				getMinField().setDoubleValue(0.0);
328 				getMaxField().setDoubleValue(0.0);
329 				getFormatField().setText("N/A");
330 				getUnitsField().setText("N/A");
331 			} else {
332 				getMinField().setDoubleValue(disp.getMinimum());
333 				getMaxField().setDoubleValue(disp.getMaximum());
334 				getFormatField().setText(disp.getFormat());
335 				getUnitsField().setText(disp.getUnits());
336 				getLogarithmic().setSelected(disp.isLogarithmicScale());
337 
338 				getFontSize().setText(String.valueOf(disp
339 				        .getValueLabelFont().getSize()));
340 
341 				RangedValuePolicy policy = disp.getValuePolicy();
342 				if (policy instanceof TrimValuePolicy) {
343 					getFixedButton().setSelected(true);
344 				} else if (policy instanceof ShiftValuePolicy) {
345 					getSlideButton().setSelected(true);
346 				} else if (policy instanceof RescalingValuePolicy) {
347 					getStretchButton().setSelected(true);
348 				}
349 			}
350 		}
351 
352 		/*
353 		 * Sets editor values to displayer.
354 		 */
355 		private void applyDefaultProperties()
356 		{
357 			if (disp == null) {
358 				return;
359 			}
360 			
361 			disp.setValue(getMinField().getDoubleValue(),
362 			    getMaxField().getDoubleValue(), disp.getValue());
363 			disp.setFormat(getFormatField().getText());
364 			disp.setUnits(getUnitsField().getText());
365 			
366 			
367 			if (getLogarithmic().isSelected()) {
368 				disp.setLogarithmicScale();
369 			} else {
370 				disp.setLinearScale();
371 			}
372 
373 			RangedValuePolicy policy = new TrimValuePolicy();
374 
375 			if (getFixedButton().isSelected()) {
376 				policy = new TrimValuePolicy();
377 			}
378 
379 			if (getSlideButton().isSelected()) {
380 				policy = new ShiftValuePolicy();
381 			}
382 
383 			if (getStretchButton().isSelected()) {
384 				policy = new RescalingValuePolicy();
385 			}
386 
387 			disp.setValuePolicy(policy);
388 
389 			setFontSize(getFontSize().getText());
390 
391 			disp.invalidate();
392 		}
393 
394 		public void setFontSize(String s){
395 			Font f = disp.getValueLabelFont();
396 			try {
397 				disp.setValueLabelFont(f.deriveFont(
398 				        (float)Integer.parseInt(s)));
399 			} catch (NumberFormatException e) {
400 			}
401 		}
402 
403 		/* (non-Javadoc)
404 		 * @see com.cosylab.gui.components.customizer.Editor#applySetting()
405 		 */
406 		public void applySettings()
407 		{
408 			applyDefaultProperties();
409 			update();
410 		}
411 
412 
413 		/* (non-Javadoc)
414 		 * @see com.cosylab.gui.components.customizer.Editor#stopEditing()
415 		 */
416 		public void stopEditing()
417 		{
418 			disp = null;
419 			update();
420 		}
421 
422 		public void setObject(Object arg0) {
423 			disp = (Gauger)arg0;
424 			disp.addPropertyChangeListener("Gauger", new PropertyChangeListener(){
425 
426 				public void propertyChange(PropertyChangeEvent evt) {
427 					if (!"Gauger".equals(evt.getPropertyName())) return;
428 					update();
429 				}
430 				
431 			});
432 			update();
433 		}
434 		
435 		ActionListener applySettingsListener= new ActionListener() {
436 			public void actionPerformed(ActionEvent e) {
437 				applySettings();
438 			}
439 		};
440 		public WPanel() {
441 			initialize();
442 			update();
443 		}
444 	}
445 	
446 	public GaugerCustomizer() {
447 		addCustomizer(GAUGER, new WPanel());
448 		setSize(349, 347);
449 	}
450 	
451 }