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.JLabel;
33  import javax.swing.JPanel;
34  import javax.swing.event.CaretEvent;
35  import javax.swing.event.CaretListener;
36  
37  import com.cosylab.gui.components.customizer.Editor;
38  
39  /**
40   * <code>LabelledNumberFieldCustomizer</code> is a customizer for 
41   * <code>LabelledNumberField</code> which allows customization of its most
42   * common properties.
43   * 
44   * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
45   * @version $Id: LabelledNumberFieldCustomizer.java,v 1.14 2008-04-22 12:28:40 jbobnar Exp $
46   * @see LabelledNumberField
47   *
48   */
49  public class LabelledNumberFieldCustomizer extends
50  		AbstractNumericDisplayerPanelCustomizer {
51  	private static final long serialVersionUID = 1L;
52  
53  	/** Aspect for bounds. */
54  	public static final String VALUE_DISPLAY_BOUNDS = "Value Display/Bounds";
55  	public static final String[] VALUE_DISPLAY_BOUNDS_PROPERTIES = {"minimumValue","maximumValue"};
56  	
57  	
58  	public static class WPanel extends JPanel implements Customizer
59  	{
60  		private static final long serialVersionUID = 1L;
61  		private LabelledNumberField displayer = null;
62  		NumberField min;
63  		NumberField max;
64  
65  		/**
66  		 * Creates a new WPanel object.
67  		 */
68  		public WPanel()
69  		{
70  			super();
71  
72  			setLayout(new GridBagLayout());
73  
74  			min = new NumberField();
75  			min.addCaretListener(new CaretListener(){
76  				public void caretUpdate(CaretEvent arg0) {
77  					applySettings();
78  				}
79  				
80  			});
81  			min.setNumberType(Double.class);
82  			min.setPreferredSize(new Dimension(100, 21));
83  			min.setToolTipText("Minimal allowed value");
84  			if (displayer != null) min.setValue(displayer.getMinimumValue());
85  			add(new JLabel("Minimum"),
86  			    new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
87  			        GridBagConstraints.WEST, GridBagConstraints.NONE,
88  			        new Insets(4, 11, 4, 4), 0, 0));
89  			add(min,
90  			    new GridBagConstraints(1, 2, 1, 1, 1.0, 1.0,
91  			        GridBagConstraints.WEST, GridBagConstraints.NONE,
92  			        new Insets(4, 4, 4, 4), 0, 0));
93  
94  			max = new NumberField();
95  			max.addCaretListener(new CaretListener(){
96  				public void caretUpdate(CaretEvent arg0) {
97  					applySettings();
98  				}
99  				
100 			});
101 			max.setNumberType(Double.class);
102 			max.setPreferredSize(new Dimension(100, 21));
103 			max.setToolTipText("Maximal allowed value");
104 			if (displayer != null) max.setValue(displayer.getMaximumValue());
105 			ActionListener l = new ActionListener(){
106 
107 				public void actionPerformed(ActionEvent arg0) {
108 					applySettings();
109 				}
110 			};
111 			max.addActionListener(l);
112 			min.addActionListener(l);
113 			
114 			add(new JLabel("Maximum"),
115 			    new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
116 			        GridBagConstraints.WEST, GridBagConstraints.NONE,
117 			        new Insets(4, 11, 4, 4), 0, 0));
118 			add(max,
119 			    new GridBagConstraints(1, 3, 1, 1, 1.0, 1.0,
120 			        GridBagConstraints.WEST, GridBagConstraints.NONE,
121 			        new Insets(4, 4, 4, 4), 0, 0));
122 		}
123 
124 		public void setObject(Object arg0) {
125 			initialize((LabelledNumberField)arg0);
126 		}
127 
128 		private void initialize(LabelledNumberField newDisplayer)
129 		{
130 			if (newDisplayer == null) {
131 				return;
132 			}
133 			
134 			displayer = newDisplayer;
135 			displayer.addPropertyChangeListener("maximumValue", new PropertyChangeListener(){
136 				public void propertyChange(PropertyChangeEvent evt) {
137 					max.setValue(displayer.getMaximumValue());
138 				}
139 			});
140 			displayer.addPropertyChangeListener("minimumValue", new PropertyChangeListener(){
141 				public void propertyChange(PropertyChangeEvent evt) {
142 					min.setValue(displayer.getMinimumValue());
143 				}
144 			});
145 			max.setNumberType(displayer.getNumberType());
146 			max.setValue(displayer.getMaximumValue());
147 			min.setNumberType(displayer.getNumberType());
148 			min.setValue(displayer.getMinimumValue());
149 		}
150 
151 		/**
152 		 * @see Editor#applySettings()
153 		 */
154 		public void applySettings()
155 		{
156 			if (displayer == null) {
157 				return;
158 			}
159 
160 			displayer.setMaximumValue(max.getValue());
161 			displayer.setMinimumValue(min.getValue());
162 			initialize(displayer);
163 			firePropertyChange("maximumValue", null, max.getValue());
164 			firePropertyChange("minimumValue", null, min.getValue());
165 		}
166 
167 	}
168 
169 	public LabelledNumberFieldCustomizer() {
170 //		addCustomizerTable(VALUE_DISPLAY_BOUNDS, new String[]{"minimumValue","maximumValue"});
171 		addCustomizer(VALUE_DISPLAY_BOUNDS, new WPanel());
172 		setSize(445, 185);
173 	}
174 }