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.BorderLayout;
23  import java.awt.Dimension;
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 javax.swing.ButtonGroup;
31  import javax.swing.JLabel;
32  import javax.swing.JPanel;
33  import javax.swing.JRadioButton;
34  import javax.swing.event.CaretEvent;
35  import javax.swing.event.CaretListener;
36  
37  import com.cosylab.gui.components.customizer.AbstractCustomizerPanel;
38  
39  /**
40   * <code>DialKnobCustomizer</code> is a customizer for <code>DialKnob</code>
41   * which allows customization of its most common properties.
42   * 
43   * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
44   * @version $Id: DialKnobCustomizer.java,v 1.18 2008-04-22 12:28:40 jbobnar Exp $
45   * @see DialKnob
46   */
47  public class DialKnobCustomizer extends AbstractCustomizerPanel {
48  	private static final long serialVersionUID = 1L;
49  	
50  	/** Title of the basic visual tab. */
51  	public static final String VISUAL_BASIC = "Visual";
52  
53  	/** Title of the value display tab. */
54  	public static final String VALUE_DISPLAY = "Value Display";
55  	
56  	/** Title of the auto synchronization tab. */
57  	public static final String AUTO_SYNCHRONIZATION = "Auto synchronization";
58  
59  	/** Title of the value policy tab. */
60  	public static final String VALUE_POLICY = "Value Display/Value Range Policy";
61  
62  	/** Title of the advance value display tab. */
63  	public static final String VALUE_DISPLAY_ADVANCED = "Value Display/Advanced";
64  
65  	/** Supported aspects  */
66  	public static String[] ASPECTS = {
67  		VISUAL_BASIC, VALUE_DISPLAY, VALUE_DISPLAY_ADVANCED, VALUE_POLICY
68  	};
69  
70  	/** Tag for titleVisible property */
71  	public static final String TITLE_VISIBLE = "titleVisible";
72  
73  	/** Tag for title property */
74  	public static final String TITLE = "title";
75  
76  	/** Tag for units property */
77  	public static final String UNITS = "units";
78  
79  	/** Tag for unitsVisible property */
80  	public static final String UNITS_VISIBLE = "unitsVisible";
81  
82  	/** Tag for format property */
83  	public static final String FORMAT = "format";
84  
85  	/** Tag for graphMax property */
86  	public static final String GRAPH_MAX = "graphMax";
87  
88  	/** Tag for graphMin property */
89  	public static final String GRAPH_MIN = "graphMin";
90  
91  	/** Tag for maximum property */
92  	public static final String MAXIMUM = "maximum";
93  
94  	/** Tag for minimum property */
95  	public static final String MINIMUM = "minimum";
96  	
97  	/** Tag for editable property */
98  	public static final String EDITABLE = "editable";
99  
100 	/** Tag for resizable property */
101 	public static final String RESIZABLE = "resizable";
102 
103 	/** Tag for enhanced property */
104 	public static final String ENHANCED = "enhanced";
105 
106 	/** Tag for tiltingEnabled property */
107 	public static final String TILTING_ENABLED = "tiltingEnabled";
108 
109 	/** Names of the value display properties */
110 	public static String[] VALUE_DISPLAY_PROPERTIES = {
111 		MINIMUM, MAXIMUM, UNITS, UNITS_VISIBLE, FORMAT
112 	};
113 
114 	/** Names of basic visual properties */
115 	public static String[] VISUAL_BASIC_PROPERTIES = {
116 		TITLE, TITLE_VISIBLE, RESIZABLE, ENHANCED, TILTING_ENABLED
117 	};
118 
119 	/** Names of advanced properties */
120 	public static String[] ADVANCED_PROPERTIES = { EDITABLE };	
121 	
122 	/**
123 	 * 
124 	 * <code>SyncPanel</code> enables configuring auto synchronization of the DialKnob.
125 	 *
126 	 * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
127 	 * @version $Id: DialKnobCustomizer.java,v 1.18 2008-04-22 12:28:40 jbobnar Exp $
128 	 * 
129 	 * @since VERSION
130 	 */
131 	private class SyncPanel extends JPanel implements Customizer {
132 		private static final long serialVersionUID = 1L;
133 		private DialKnob displayer;
134 		private boolean initialized = false;
135 		private JPanel displayPanel;
136 		private NumberField autoSyncDelay;
137 		private JLabel millis;
138 		private JLabel syncDelay;
139 		private JRadioButton syncManual;
140 		private JRadioButton syncAuto;
141 		
142 		public SyncPanel(){
143 			if (displayer != null){
144 				initialize();
145 			}
146 		}
147 		
148 		private void initialize()
149 		{
150 			if (initialized) {
151 				return;
152 			}
153 
154 			setLayout(new BorderLayout());
155 
156 			add(getDisplayPanel(), BorderLayout.CENTER);
157 			updateEditor();
158 			initialized = true;
159 
160 			setSize(300, 450);
161 		}
162 		
163 		private void updateEditor() {
164 			syncAuto.setSelected(displayer.isAutoSynchronize());
165 			syncManual.setSelected(!displayer.isAutoSynchronize());
166 			autoSyncDelay.setLongValue(displayer.getAutoSynchronizeDelay());
167 			autoSyncDelay.setEnabled(displayer.isAutoSynchronize());
168 		}
169 		
170 		private JPanel getDisplayPanel() {
171 			if (displayPanel == null) {
172 				displayPanel = new JPanel();
173 			}
174 			displayPanel.setBorder(new GroupBoxBorder("Synchronization configuration :"));
175 			displayPanel.setLayout(new GridBagLayout());
176 						
177 			syncManual = new JRadioButton("Manual synchronization");
178 			syncManual.addActionListener(new ActionListener() {
179 
180 				public void actionPerformed(ActionEvent e) {
181 					autoSyncDelay.setEnabled(false);
182 					applySettings();
183 				}
184 				
185 				
186 			});
187 			syncAuto = new JRadioButton("Automatic synchronization");
188 			syncAuto.addActionListener(new ActionListener() {
189 
190 				public void actionPerformed(ActionEvent e) {
191 					autoSyncDelay.setEnabled(true);	
192 					applySettings();
193 				}
194 				
195 				
196 			});
197 			ButtonGroup syncModeButtonGroup = new ButtonGroup();
198 			syncModeButtonGroup.add(syncManual);
199 			syncModeButtonGroup.add(syncAuto);
200 						
201 			displayPanel.add(syncManual, new GridBagConstraints(0,0,1,1,1,1,
202 					GridBagConstraints.WEST, GridBagConstraints.NONE,
203 					new Insets(0,0,0,0), 1,1));
204 			displayPanel.add(syncAuto, new GridBagConstraints(0,1,1,1,1,1,
205 					GridBagConstraints.WEST, GridBagConstraints.NONE,
206 					new Insets(0,0,0,0), 1,1));
207 			
208 			syncDelay = new JLabel("Automatic synchronization delay:");
209 			displayPanel.add(syncDelay, new GridBagConstraints(0,2,1,1,1,1,
210 					GridBagConstraints.WEST, GridBagConstraints.NONE,
211 					new Insets(0,21,0,0), 1,1));
212 			
213 			JPanel fieldPanel = new JPanel();
214 			fieldPanel.setLayout(new GridBagLayout());
215 			autoSyncDelay = new NumberField();
216 			autoSyncDelay.setNumberType(long.class);
217 			autoSyncDelay.addCaretListener(new CaretListener(){
218 
219 				public void caretUpdate(CaretEvent e) {
220 					applySettings();
221 				}
222 				
223 			});
224 			autoSyncDelay.setPreferredSize(new Dimension(70, 20));
225 			fieldPanel.add(autoSyncDelay, new GridBagConstraints(0,0,1,1,1,1,
226 					GridBagConstraints.WEST, GridBagConstraints.NONE,
227 					new Insets(0,0,0,0), 1,1));
228 			
229 			millis = new JLabel("milliseconds");
230 			fieldPanel.add(millis, new GridBagConstraints(1,0,1,1,1,1,
231 					GridBagConstraints.WEST, GridBagConstraints.NONE,
232 					new Insets(0,10,0,0), 1,1));
233 			
234 			displayPanel.add(fieldPanel, new GridBagConstraints(0,3,1,1,1,1,
235 					GridBagConstraints.WEST, GridBagConstraints.NONE,
236 					new Insets(5,21,0,0), 1,1));
237 			return displayPanel;
238 		}
239 
240 		public void applySettings() {
241 			boolean auto = syncAuto.isSelected();
242 			displayer.setAutoSynchronize(auto);
243 			if (syncAuto.isSelected()) {
244 				displayer.setAutoSynchronizeDelay(autoSyncDelay.getLongValue());
245 				firePropertyChange("autoSynchronizeDelay", -1, autoSyncDelay.getLongValue());
246 			}
247 			firePropertyChange("autoSynchronize", !auto, auto);
248 			
249 		}
250 
251 		public void setObject(Object arg0) {
252 			displayer = (DialKnob) arg0;
253 			initialize();
254 		}
255 		
256 	}
257 	
258 	public DialKnobCustomizer() {
259 		addCustomizerTable(VISUAL_BASIC, VISUAL_BASIC_PROPERTIES);
260 		addCustomizerTable(VALUE_DISPLAY, VALUE_DISPLAY_PROPERTIES);
261 		addCustomizerTable(VALUE_DISPLAY_ADVANCED, ADVANCED_PROPERTIES);
262 		addCustomizer(VALUE_POLICY, new ValuePolicyCustomizer());
263 		addCustomizer(AUTO_SYNCHRONIZATION, new SyncPanel());
264 		setSize(423, 184);
265 	}
266 
267 }