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.slider;
21  
22  import java.awt.Color;
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  
30  import javax.swing.JButton;
31  import javax.swing.JCheckBox;
32  import javax.swing.JLabel;
33  import javax.swing.JPanel;
34  import javax.swing.SwingConstants;
35  
36  import com.cosylab.gui.components.ResizableTextLabel;
37  import com.cosylab.gui.components.Slider;
38  import com.cosylab.gui.components.util.ColorHelper;
39  
40  /**
41   * Part of slider UI, displays title and any other general information. This
42   * class is intended to be extended.
43   * 
44   * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
45   * @version $id$
46   */
47  public class InfoBar extends JPanel {
48  	
49  	private static final long serialVersionUID = 1L;
50  
51  	private ResizableTextLabel deviceName;
52  
53  	private JButton sync;
54  	private JButton set;
55  	private JButton store;
56  	private JButton restore;
57  
58  	private Slider slider;
59  
60  	private JLabel stored;
61  	private JCheckBox continuousMode;
62  
63  	private boolean storedValueLabelVisible = true;
64  
65  	/**
66  	 * Creates a new InfoBar object.
67  	 * 
68  	 * @param slider
69  	 */
70  	public InfoBar(Slider slider) {
71  		super();
72  		this.slider = slider;
73  		initialize();
74  	}
75  
76  	private void initialize() {
77  		setLayout(new GridBagLayout());
78  		setOpaque(false);
79  
80  		add(getTitleComponent(), new GridBagConstraints(0, 0, 1, 1, 1, 0,
81  				GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
82  				new Insets(0, 4, 0, 0), 0, 0));
83  
84  		add(getSetButton(), new GridBagConstraints(1, 0, 1, 1, 0, 0,
85  				GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
86  						0, 1, 0, 0), 0, 0));
87  		getSetButton().setVisible(false);
88  
89  		add(getContinuousMode(), new GridBagConstraints(2, 0, 1, 1, 1, 0,
90  				GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0,
91  						4, 0, 4), 0, 0));
92  		add(getStoreButton(), new GridBagConstraints(3, 0, 1, 1, 0, 0,
93  				GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
94  						0, 1, 0, 0), 0, 0));
95  		add(getStoredValueLabel(), new GridBagConstraints(4, 0, 1, 1, 0, 0,
96  				GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
97  						0, 4, 0, 3), 0, 0));
98  		add(getRestoreButton(), new GridBagConstraints(5, 0, 1, 1, 0, 0,
99  				GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
100 						0, 1, 0, 0), 0, 0));
101 
102 		add(getSyncButton(), new GridBagConstraints(6, 0, 1, 1, 0, 0,
103 				GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,
104 						1, 0, 4), 0, 0));
105 	}
106 
107 	private ResizableTextLabel getTitleComponent() {
108 		if (deviceName == null) {
109 			deviceName = new ResizableTextLabel("");
110 			deviceName.setOpaque(false);
111 			deviceName.setResizable(false);
112 			deviceName.setHorizontalAlignment(SwingConstants.LEFT);
113 		}
114 
115 		return deviceName;
116 	}
117 
118 	private JButton getSyncButton() {
119 		if (sync == null) {
120 			sync = new JButton("Sync");
121 			sync.setMargin(new Insets(0, 1, 0, 1));
122 			sync.setFont(new Font(sync.getFont().getName(), Font.PLAIN, 9));
123 			sync.setOpaque(true);
124 			sync.setBackground(ColorHelper.getCosyControl());
125 			sync.addActionListener(new ActionListener() {
126 				public void actionPerformed(ActionEvent e) {
127 					slider.synchronize();
128 				}
129 			});
130 			sync.setToolTipText("Synchronize Value with Readback");
131 		}
132 
133 		return sync;
134 	}
135 
136 	private JButton getStoreButton() {
137 		if (store == null) {
138 			store = new JButton("Store");
139 			store.setMargin(new Insets(0, 1, 0, 1));
140 			store.setFont(new Font(store.getFont().getName(), Font.PLAIN, 9));
141 			store.setOpaque(true);
142 			store.setBackground(ColorHelper.getCosyControl());
143 			store.addActionListener(new ActionListener() {
144 				public void actionPerformed(ActionEvent e) {
145 					slider.storeValue();
146 				}
147 			});
148 			store.setToolTipText("Store Current Readback Value");
149 
150 		}
151 
152 		return store;
153 	}
154 
155 	private JButton getRestoreButton() {
156 		if (restore == null) {
157 			restore = new JButton("Restore");
158 			restore.setMargin(new Insets(0, 1, 0, 1));
159 			restore.setFont(new Font(restore.getFont().getName(),Font.PLAIN,9));
160 			restore.setOpaque(true);
161 			restore.setBackground(ColorHelper.getCosyControl());
162 			restore.addActionListener(new ActionListener() {
163 				public void actionPerformed(ActionEvent e) {
164 					slider.restoreValue();
165 				}
166 			});
167 			restore.setToolTipText("Set value to: <no value>");
168 
169 		}
170 
171 		return restore;
172 	}
173 
174 	private JLabel getStoredValueLabel() {
175 		if (stored == null) {
176 			stored = new JLabel("<no value>");
177 			stored.setFont(new Font(stored.getFont().getName(), Font.PLAIN,12));
178 			stored.setOpaque(true);
179 			// stored.setBackground(ColorHelper.getCosyControl());
180 			stored.setToolTipText("<no value>");
181 		}
182 
183 		return stored;
184 	}
185 
186 	private JButton getSetButton() {
187 		if (set == null) {
188 			set = new JButton("Set");
189 			set.setMargin(new Insets(0, 1, 0, 1));
190 			set.setFont(new Font(set.getFont().getName(), Font.PLAIN, 9));
191 			set.setOpaque(true);
192 			set.setBackground(ColorHelper.getCosyControl());
193 			set.addActionListener(new ActionListener() {
194 				public void actionPerformed(ActionEvent e) {
195 					slider.setManual();
196 				}
197 			});
198 			set.setToolTipText("Set Value");
199 		}
200 
201 		return set;
202 	}
203 
204 	/**
205 	 * Sets the visibility of the set button.
206 	 * 
207 	 * @param visible
208 	 */
209 	public void setSetButtonVisible(boolean visible) {
210 		getSetButton().setVisible(visible);
211 	}
212 
213 	/*
214 	 * (non-Javadoc)
215 	 * 
216 	 * @see javax.swing.JComponent#setBackground(java.awt.Color)
217 	 */
218 	@Override
219 	public void setBackground(Color bg) {
220 		super.setBackground(bg);
221 		getSyncButton().setBackground(bg);
222 		getStoreButton().setBackground(bg);
223 		getRestoreButton().setBackground(bg);
224 		getSetButton().setBackground(bg);
225 		getTitleComponent().setBackground(bg);
226 		getStoredValueLabel().setBackground(bg);
227 		getContinuousMode().setBackground(bg);
228 	}
229 
230 	/*
231 	 * (non-Javadoc)
232 	 * 
233 	 * @see javax.swing.JComponent#setForeground(java.awt.Color)
234 	 */
235 	@Override
236 	public void setForeground(Color fg) {
237 		super.setForeground(fg);
238 		getSyncButton().setForeground(fg);
239 		getStoreButton().setForeground(fg);
240 		getSetButton().setForeground(fg);
241 		getRestoreButton().setForeground(fg);
242 		getTitleComponent().setForeground(fg);
243 		getStoredValueLabel().setForeground(fg);
244 		getContinuousMode().setForeground(fg);
245 	}
246 
247 	/**
248 	 * Sets the title for slider.
249 	 * 
250 	 * @param title
251 	 *            of slider.
252 	 */
253 	public void setTitle(String title) {
254 		getTitleComponent().setText(title);
255 	}
256 
257 	/**
258 	 * Returns title of slider.
259 	 * 
260 	 * @return current title of slider.
261 	 */
262 	public String getTitle() {
263 		return getTitleComponent().getText();
264 	}
265 
266 	/*
267 	 * (non-Javadoc)
268 	 * 
269 	 * @see javax.swing.JComponent#setFont(java.awt.Font)
270 	 */
271 	public void setFont(Font font) {
272 		super.setFont(font);
273 		getTitleComponent().setFont(font);
274 	}
275 
276 	/*
277 	 * (non-Javadoc)
278 	 * 
279 	 * @see java.awt.Component#getFont()
280 	 */
281 	public Font getFont() {
282 		return getTitleComponent().getFont();
283 	}
284 
285 	/*
286 	 * (non-Javadoc)
287 	 * 
288 	 * @see javax.swing.JComponent#setEnabled(boolean)
289 	 */
290 	@Override
291 	public void setEnabled(boolean enabled) {
292 		super.setEnabled(enabled);
293 		getSyncButton().setEnabled(enabled);
294 		getSetButton().setEnabled(enabled);
295 		getContinuousMode().setEnabled(enabled);
296 		getRestoreButton().setEnabled(enabled);
297 		getStoreButton().setEnabled(enabled);
298 		getStoredValueLabel().setEnabled(enabled);
299 	}
300 
301 	/**
302 	 * Sets the store value as tooltip to the Restore button.
303 	 * 
304 	 * @param value
305 	 */
306 	public void setStoredValue(String value) {
307 		if (value == null) {
308 			getRestoreButton().setToolTipText("Set value to: <no value>");
309 			getStoredValueLabel().setText("<no value>");
310 			getStoredValueLabel().setToolTipText("<no value>");
311 		} else {
312 			getRestoreButton().setToolTipText("Set value to: " + value);
313 			getStoredValueLabel().setText(value);
314 			getStoredValueLabel().setToolTipText(value);
315 		}
316 	}
317 
318 	/**
319 	 * Sets the title of the restore button.
320 	 * 
321 	 * @param title
322 	 */
323 	public void setRestoreButtonTitle(String title) {
324 		getRestoreButton().setText(title);
325 	}
326 
327 	/**
328 	 * Returns the title of the restore button;
329 	 * 
330 	 * @return
331 	 */
332 	public String getRestoreButtonTitle() {
333 		return getRestoreButton().getText();
334 	}
335 
336 	/**
337 	 * Sets the visibility of the store/restore button.
338 	 * 
339 	 * @param button
340 	 *            the button
341 	 * @param visible
342 	 *            true if button should be visible
343 	 */
344 	public void setStorageButtonsVisible(boolean visible) {
345 		getStoreButton().setVisible(visible);
346 		getRestoreButton().setVisible(visible);
347 		getStoredValueLabel().setVisible(
348 				storedValueLabelVisible && getStoreButton().isVisible());
349 	}
350 
351 	/**
352 	 * Sets the visibility of the storeed value label.
353 	 * 
354 	 * @param button
355 	 *            the button
356 	 * @param visible
357 	 *            true if button should be visible
358 	 */
359 	public void setStoredValueLabelVisible(boolean visible) {
360 		storedValueLabelVisible = visible;
361 		getStoredValueLabel().setVisible(
362 				storedValueLabelVisible && getStoreButton().isVisible());
363 	}
364 
365 	/**
366 	 * Returns the visibility of the store/restore buttons.
367 	 * 
368 	 * @param button
369 	 * @return visibility of the button
370 	 */
371 	public boolean isStorageButtonsVisible() {
372 		return getStoreButton().isVisible();
373 	}
374 
375 	/**
376 	 * Sets the visibility of the sync button.
377 	 * 
378 	 * @param button
379 	 *            the button
380 	 * @param visible
381 	 *            true if button should be visible
382 	 */
383 	public void setSyncButtonVisible(boolean visible) {
384 		getSyncButton().setVisible(visible);
385 	}
386 
387 	/**
388 	 * Returns the visibility of the sync button.
389 	 * 
390 	 * @param button
391 	 * @return visibility of the button
392 	 */
393 	public boolean isSyncButtonVisible() {
394 		return getSyncButton().isVisible();
395 	}
396 
397 	/**
398 	 * Returns true if the stored value label is visible.
399 	 * 
400 	 * @return true if label is visible or false otherwise
401 	 */
402 	public boolean isStoredValueLabelVisible() {
403 		return storedValueLabelVisible;
404 	}
405 
406 	/**
407 	 * Returns the continuousMode combobox
408 	 * 
409 	 * @param continuousMode
410 	 */
411 	private JCheckBox getContinuousMode() {
412 		if (continuousMode == null) {
413 			continuousMode = new JCheckBox("Continuous");
414 			continuousMode.setFont(new Font(continuousMode.getFont().getName(),Font.PLAIN, 9));
415 			continuousMode.setOpaque(false);			
416 			continuousMode.addActionListener(new ActionListener() {
417 				public void actionPerformed(ActionEvent ae) {
418 					if(continuousMode.isSelected()){
419 						slider.setContinuousModeEnabled(true);
420 					} else {
421 						slider.setContinuousModeEnabled(false);
422 					}
423 				}
424 			});
425 		}
426 		return continuousMode;
427 	}
428 	
429 	/**
430 	 * Sets the continuous mode. If true the continuous mode is enabled otherwise
431 	 * it is false.
432 	 * 
433 	 * @see Slider#setContinuousModeEnabled(boolean)
434 	 * @param b true if enabled
435 	 */
436 	public void setContinuousMode(boolean b) {
437 		if (b!=getContinuousMode().isSelected()) {
438 			getContinuousMode().setSelected(b);
439 		}
440 	}
441 	
442 	/**
443 	 * Sets the visibility of the continuous mode button.
444 	 * 
445 	 * @param b true if visible or false otherwise
446 	 */
447 	public void setContinuousModeVisible(boolean b) {
448 		if (b!=getContinuousMode().isVisible()) {
449 			getContinuousMode().setVisible(b);
450 		}
451 	}
452 	
453 	/**
454 	 * Returns true if continuous mode is turned on.
455 	 * 
456 	 * @return true if continuous mode or false otherwise
457 	 */
458 	public boolean isContinuousMode() {
459 		return getContinuousMode().isSelected();
460 	}
461 
462 	/**
463 	 * Returns true if the continuous mode button is visible.
464 	 * 
465 	 * @return true if continuous mode button is visible
466 	 */
467 	public boolean isContinuousModeVisible() {
468 		return getContinuousMode().isVisible();
469 	}
470 	
471 }
472 
473 /* __oOo__ */