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.GridBagConstraints;
24  import java.awt.GridBagLayout;
25  import java.awt.Insets;
26  import java.awt.event.ActionEvent;
27  import java.awt.event.ActionListener;
28  import java.beans.Customizer;
29  import java.beans.PropertyChangeEvent;
30  import java.beans.PropertyChangeListener;
31  
32  import javax.swing.ButtonGroup;
33  import javax.swing.JCheckBox;
34  import javax.swing.JComponent;
35  import javax.swing.JPanel;
36  import javax.swing.JRadioButton;
37  
38  import com.cosylab.gui.components.range2.RangedValuePolicy;
39  import com.cosylab.gui.components.range2.RescalingValuePolicy;
40  import com.cosylab.gui.components.range2.ShiftValuePolicy;
41  import com.cosylab.gui.components.range2.TrimValuePolicy;
42  
43  /**
44   * <code>LabelledGaugerCustomizer</code> is a customizer for
45   * <code>LabelledGauger</code> which allows customization of its most
46   * common properties.
47   * 
48   * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
49   * @version $Id: LabelledGaugerCustomizer.java,v 1.13 2008-04-22 12:28:40 jbobnar Exp $
50   * @see LabelledGauger
51   */
52  public class LabelledGaugerCustomizer extends AbstractDisplayerPanelCustomizer {
53  	private static final long serialVersionUID = 1L;
54  	
55  	/** Value display tab name. */
56  	public static final String VALUE_DISPLAY = "Value Display";
57  	/** Tag for minimum property. */
58  	public static final String GRAPH_MIN = "minimum";
59  	/** Tag for maximum property. */
60  	public static final String GRAPH_MAX = "maximum";
61  	/** Tag for unitsVisibl property. */
62  	public static final String UNITS_VISIBLE = "unitsVisible";
63  	/** Tag for format property. */
64  	public static final String FORMAT = "format";
65  	/** Tag for units property. */
66  	public static final String UNITS = "units";
67  	/** Scale tab name */
68  	public static final String VISUAL_SPECIFIC = "Value Display/Scale";
69  	/** Names of the basic visual properties. */
70  	public static final String[] VISUAL_BASIC_PROPERTIES = { TITLE, TITLE_VISIBLE, RESIZABLE };
71  	/** Names of the value display properties. */
72  	public static String[] VALUE_DISPLAY_PROPERTIES = {
73  		GRAPH_MIN, GRAPH_MAX, UNITS, UNITS_VISIBLE, FORMAT
74  	};
75  	
76  	private class WPanel extends JPanel implements Customizer
77  	{
78  		private static final long serialVersionUID = 1L;
79  		private ButtonGroup scaleGroup;
80  		private LabelledGauger disp;
81  
82  		// GUI elements
83  		private JCheckBox logarithmic;
84  		private JRadioButton fixedButton;
85  		private JRadioButton slideButton;
86  		private JRadioButton stretchButton;
87  		private boolean initialized = false;
88  		private ActionListener al = null;
89  		public WPanel() {
90  			al = new ActionListener(){
91  
92  				public void actionPerformed(ActionEvent e) {
93  					applySettings();
94  				}
95  				
96  			};
97  			initialize();
98  		}
99  
100 		public void applySettings()
101 		{
102 			applyDefaultProperties();
103 			update();
104 		}
105 
106 		private JRadioButton getFixedButton()
107 		{
108 			if (fixedButton == null) {
109 				fixedButton = new JRadioButton("Fixed");
110 				getScaleGroup().add(fixedButton);
111 				fixedButton.addActionListener(al);
112 			}
113 
114 			return fixedButton;
115 		}
116 
117 		private JCheckBox getLogarithmic()
118 		{
119 			if (logarithmic == null) {
120 				logarithmic = new JCheckBox("Logarithmic");
121 				logarithmic.setPreferredSize(new Dimension(150, 21));
122 				logarithmic.setMinimumSize(new Dimension(75, 21));
123 				logarithmic.setToolTipText("Use logarithmic scale.");
124 				logarithmic.addActionListener(al);
125 			}
126 
127 			return logarithmic;
128 		}
129 
130 		private ButtonGroup getScaleGroup()
131 		{
132 			if (scaleGroup == null) {
133 				scaleGroup = new ButtonGroup();
134 			}
135 
136 			return scaleGroup;
137 		}
138 
139 		private JRadioButton getSlideButton()
140 		{
141 			if (slideButton == null) {
142 				slideButton = new JRadioButton("Slide");
143 				getScaleGroup().add(slideButton);
144 				slideButton.addActionListener(al);
145 			}
146 
147 			return slideButton;
148 		}
149 
150 		private JRadioButton getStretchButton()
151 		{
152 			if (stretchButton == null) {
153 				stretchButton = new JRadioButton("Stretch");
154 				getScaleGroup().add(stretchButton);
155 				stretchButton.addActionListener(al);
156 			}
157 
158 			return stretchButton;
159 		}
160 
161 		/*
162 		 * Sets editor values to displayer.
163 		 */
164 		private void applyDefaultProperties()
165 		{
166 			if (disp == null) {
167 				return;
168 			}
169 			boolean log = getLogarithmic().isSelected();
170 			disp.setLogarithmicScale(log);
171 
172 			RangedValuePolicy policy = new TrimValuePolicy();
173 
174 			if (getFixedButton().isSelected()) {
175 				policy = new TrimValuePolicy();
176 			}
177 
178 			if (getSlideButton().isSelected()) {
179 				policy = new ShiftValuePolicy();
180 			}
181 
182 			if (getStretchButton().isSelected()) {
183 				policy = new RescalingValuePolicy();
184 			}
185 
186 			disp.setValuePolicy(policy);
187 			LabelledGaugerCustomizer.this.firePropertyChange("logarithmicScale", !log, log);
188 			LabelledGaugerCustomizer.this.firePropertyChange("scaleMode", null, policy);
189 		}
190 
191 		private void initialize()
192 		{
193 			if (initialized) {
194 				return;
195 			}
196 
197 			initialized = true;
198 
199 			setLayout(new GridBagLayout());
200 			setSize(300, 450);
201 			setBorder(new GroupBoxBorder("Scale preferences"));
202 
203 			Insets insets = new Insets(2, 2, 2, 2);
204 
205 			add(getLogarithmic(),
206 			    new GridBagConstraints(0, 1, 3, 1, 1.0, 1.0,
207 			        GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
208 			        insets, 0, 0));
209 
210 			add(getFixedButton(),
211 			    new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
212 			        GridBagConstraints.WEST, GridBagConstraints.NONE, insets,
213 			        0, 0));
214 
215 			add(getStretchButton(),
216 			    new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0,
217 			        GridBagConstraints.WEST, GridBagConstraints.NONE, insets,
218 			        0, 0));
219 
220 			add(getSlideButton(),
221 			    new GridBagConstraints(2, 0, 1, 1, 1.0, 1.0,
222 			        GridBagConstraints.WEST, GridBagConstraints.NONE, insets,
223 			        0, 0));
224 		}
225 
226 		/*
227 		 * Updates editor with values from diplayer.
228 		 */
229 		private void update()
230 		{
231 			if (disp != null) {
232 				getLogarithmic().setSelected(disp.isLogarithmicScale());
233 
234 				RangedValuePolicy policy = disp.getValuePolicy();
235 				if (policy instanceof TrimValuePolicy) {
236 					getFixedButton().setSelected(true);
237 				} else if (policy instanceof ShiftValuePolicy) {
238 					getSlideButton().setSelected(true);
239 				} else if (policy instanceof RescalingValuePolicy) {
240 					getStretchButton().setSelected(true);
241 				}
242 			}
243 		}
244 
245 		public void setObject(Object bean) {
246 			disp = (LabelledGauger) bean;
247 			disp.addPropertyChangeListener("scaleMode", new PropertyChangeListener(){
248 				public void propertyChange(PropertyChangeEvent evt) {
249 					update();
250 				}
251 			});
252 			disp.addPropertyChangeListener("logarithmicScale", new PropertyChangeListener(){
253 				public void propertyChange(PropertyChangeEvent evt) {
254 					update();
255 				}
256 			});
257 			update();
258 			
259 		}
260 	}
261 	
262 	
263 	public LabelledGaugerCustomizer() {
264 		addCustomizer(VISUAL_SPECIFIC, new WPanel());
265 		addCustomizerTable(VALUE_DISPLAY, VALUE_DISPLAY_PROPERTIES);
266 		addCustomizerTable(VISUAL_BASIC, VISUAL_BASIC_PROPERTIES);
267 		setSize(357, 352);
268 	}
269 }