1 | /* |
---|
2 | * $Id: LabelPropertyPanel.java,v 1.4 2003/09/16 22:02:14 dwd Exp $ |
---|
3 | * |
---|
4 | * This software is provided by NOAA for full, free and open release. It is |
---|
5 | * understood by the recipient/user that NOAA assumes no liability for any |
---|
6 | * errors contained in the code. Although this software is released without |
---|
7 | * conditions or restrictions in its use, it is expected that appropriate |
---|
8 | * credit be given to its author and to the National Oceanic and Atmospheric |
---|
9 | * Administration should the software be included by the recipient as an |
---|
10 | * element in other product development. |
---|
11 | */ |
---|
12 | |
---|
13 | package gov.noaa.pmel.sgt.beans; |
---|
14 | |
---|
15 | import java.awt.*; |
---|
16 | import javax.swing.*; |
---|
17 | import java.awt.event.ActionEvent; |
---|
18 | import java.awt.event.ActionListener; |
---|
19 | import javax.swing.event.ChangeEvent; |
---|
20 | import javax.swing.event.ChangeListener; |
---|
21 | import java.text.DecimalFormat; |
---|
22 | import java.awt.event.FocusEvent; |
---|
23 | import java.awt.event.FocusListener; |
---|
24 | |
---|
25 | import gov.noaa.pmel.sgt.swing.prop.ColorDialog; |
---|
26 | import gov.noaa.pmel.sgt.swing.prop.FontDialog; |
---|
27 | import gov.noaa.pmel.sgt.SGLabel; |
---|
28 | import gov.noaa.pmel.util.Point2D; |
---|
29 | |
---|
30 | /** |
---|
31 | * @author Donald Denbo |
---|
32 | * @version $Revision: 1.4 $, $Date: 2003/09/16 22:02:14 $ |
---|
33 | * @since 3.0 |
---|
34 | **/ |
---|
35 | class LabelPropertyPanel extends PropertyPanel implements ActionListener, ChangeListener, FocusListener { |
---|
36 | private boolean expert_ = false; |
---|
37 | private Label label_; |
---|
38 | private static DecimalFormat format_ = new DecimalFormat("#.###"); |
---|
39 | private String[] pNames_ = {"Color", "Font", "Height", "Id", "Justification", |
---|
40 | "Location", "Orientation", "Selectable", "Text", "Visible", "Width"}; |
---|
41 | private JComponent[] comps_ = new JComponent[pNames_.length]; |
---|
42 | private String[] justType = {"Left", "Center", "Right"}; |
---|
43 | private String[] orientType = {"Horizontal", "Vertical"}; |
---|
44 | |
---|
45 | public LabelPropertyPanel(Label label, boolean expert) { |
---|
46 | super(); |
---|
47 | label_ = label; |
---|
48 | label_.addChangeListener(this); |
---|
49 | expert_ = expert; |
---|
50 | create(); |
---|
51 | } |
---|
52 | |
---|
53 | public void setLabel(Label label, boolean expert) { |
---|
54 | if(label_ != null) label_.removeChangeListener(this); |
---|
55 | label_ = label; |
---|
56 | label_.addChangeListener(this); |
---|
57 | expert_ = expert; |
---|
58 | reset(); |
---|
59 | } |
---|
60 | |
---|
61 | void update() { |
---|
62 | int item = -1; |
---|
63 | int i = -1; |
---|
64 | updateColor((JButton)comps_[++i], label_.getColor()); |
---|
65 | updateFont((JButton)comps_[++i], label_.getFont()); |
---|
66 | ((JTextField)comps_[++i]).setText(format(label_.getHeightP(), format_)); |
---|
67 | ((JTextField)comps_[++i]).setText(label_.getId()); |
---|
68 | switch(label_.getJustification()) { |
---|
69 | default: |
---|
70 | case SGLabel.LEFT: |
---|
71 | item = 0; |
---|
72 | break; |
---|
73 | case SGLabel.CENTER: |
---|
74 | item = 1; |
---|
75 | break; |
---|
76 | case SGLabel.RIGHT: |
---|
77 | item = 2; |
---|
78 | break; |
---|
79 | } |
---|
80 | ((JComboBox)comps_[++i]).setSelectedIndex(item); |
---|
81 | ((JLabel)comps_[++i]).setText(format(label_.getLocationP(), true)); |
---|
82 | switch(label_.getOrientation()) { |
---|
83 | default: |
---|
84 | case SGLabel.HORIZONTAL: |
---|
85 | item = 0; |
---|
86 | break; |
---|
87 | case SGLabel.VERTICAL: |
---|
88 | item = 1; |
---|
89 | break; |
---|
90 | } |
---|
91 | ((JComboBox)comps_[++i]).setSelectedIndex(item); |
---|
92 | ((JCheckBox)comps_[++i]).setSelected(label_.isSelectable()); |
---|
93 | ((JTextField)comps_[++i]).setText(label_.getText()); |
---|
94 | ((JCheckBox)comps_[++i]).setSelected(label_.isVisible()); |
---|
95 | ((JTextField)comps_[++i]).setText(format(label_.getWidthP(), format_)); |
---|
96 | } |
---|
97 | |
---|
98 | void create() { |
---|
99 | int i = -1; |
---|
100 | int item = -1; |
---|
101 | comps_[++i] = createColor(label_.getColor(), pNames_[i], this); |
---|
102 | comps_[++i] = createFont(label_.getFont(), pNames_[i], this); |
---|
103 | comps_[++i] = createTextField(format(label_.getHeightP(), format_), pNames_[i], this, true); |
---|
104 | comps_[++i] = createTextField(label_.getId(), pNames_[i], this, !label_.isInstantiated()); |
---|
105 | switch(label_.getJustification()) { |
---|
106 | default: |
---|
107 | case SGLabel.LEFT: |
---|
108 | item = 0; |
---|
109 | break; |
---|
110 | case SGLabel.CENTER: |
---|
111 | item = 1; |
---|
112 | break; |
---|
113 | case SGLabel.RIGHT: |
---|
114 | item = 2; |
---|
115 | break; |
---|
116 | } |
---|
117 | comps_[++i] = createComboBox(justType, item, pNames_[i], this, true); |
---|
118 | comps_[++i] = createLabel(format(label_.getLocationP(), true)); |
---|
119 | switch(label_.getOrientation()) { |
---|
120 | default: |
---|
121 | case SGLabel.HORIZONTAL: |
---|
122 | item = 0; |
---|
123 | break; |
---|
124 | case SGLabel.VERTICAL: |
---|
125 | item = 1; |
---|
126 | break; |
---|
127 | } |
---|
128 | comps_[++i] = createComboBox(orientType, item, pNames_[i], this, true); |
---|
129 | comps_[++i] = createCheckBox(label_.isSelectable(), pNames_[i], this); |
---|
130 | comps_[++i] = createTextField(label_.getText(), pNames_[i], this, true); |
---|
131 | comps_[++i] = createCheckBox(label_.isVisible(), pNames_[i], this); |
---|
132 | comps_[++i] = createTextField(format(label_.getWidthP(), format_), pNames_[i], this, true); |
---|
133 | for(i=0; i < comps_.length; i++) { |
---|
134 | addProperty(i+1, pNames_[i], comps_[i], false); |
---|
135 | } |
---|
136 | addProperty(comps_.length + 1, " ", new JLabel(" "), true); |
---|
137 | } |
---|
138 | |
---|
139 | private void processEvent(Object obj, String command) { |
---|
140 | if(command.equals("Id")) { |
---|
141 | String oldId = label_.getId(); |
---|
142 | label_.getPanelHolder().getLabels().remove(oldId); |
---|
143 | label_.setId(((JTextField)obj).getText()); |
---|
144 | label_.getPanelHolder().getLabels().put(label_.getId(), label_); |
---|
145 | } else if(command.equals("Justification")) { |
---|
146 | String str = (String)((JComboBox)obj).getSelectedItem(); |
---|
147 | int item = -1; |
---|
148 | if(str.equals("Left")) { |
---|
149 | item = SGLabel.LEFT; |
---|
150 | } else if(str.equals("Center")) { |
---|
151 | item = SGLabel.CENTER; |
---|
152 | } else if(str.equals("Right")) { |
---|
153 | item = SGLabel.RIGHT; |
---|
154 | } |
---|
155 | label_.setJustification(item); |
---|
156 | } else if(command.equals("Text")) { |
---|
157 | label_.setText(((JTextField)obj).getText()); |
---|
158 | } else if(command.equals("Location")) { |
---|
159 | label_.setLocationP(parsePoint2D(((JTextField)obj).getText())); |
---|
160 | } else if(command.equals("Height")) { |
---|
161 | label_.setHeightP(Float.parseFloat(((JTextField)obj).getText())); |
---|
162 | } else if(command.equals("Width")) { |
---|
163 | label_.setWidthP(Float.parseFloat(((JTextField)obj).getText())); |
---|
164 | } else if(command.equals("Visible")) { |
---|
165 | label_.setVisible(((JCheckBox)obj).isSelected()); |
---|
166 | } else if(command.equals("Color")) { |
---|
167 | ColorDialog cd = new ColorDialog(getFrame(), "Select Label Color", true); |
---|
168 | cd.setColor(label_.getColor()); |
---|
169 | cd.setVisible(true); |
---|
170 | Color newcolor = cd.getColor(); |
---|
171 | if(newcolor != null) label_.setColor(newcolor); |
---|
172 | } else if(command.equals("Font")) { |
---|
173 | FontDialog fd = new FontDialog("Label Font"); |
---|
174 | int result = fd.showDialog(label_.getFont()); |
---|
175 | if(result == fd.OK_RESPONSE) { |
---|
176 | label_.setFont(fd.getFont()); |
---|
177 | } |
---|
178 | } else if(command.equals("Orientation")) { |
---|
179 | int old = label_.getOrientation(); |
---|
180 | String str = (String)((JComboBox)obj).getSelectedItem(); |
---|
181 | int item = -1; |
---|
182 | if(str.equals("Horizontal")) { |
---|
183 | item = SGLabel.HORIZONTAL; |
---|
184 | } else if(str.equals("Vertical")) { |
---|
185 | item = SGLabel.VERTICAL; |
---|
186 | } |
---|
187 | label_.setOrientation(item); |
---|
188 | /** |
---|
189 | * if orientation has changed redefine DragBox. |
---|
190 | */ |
---|
191 | if(old != item) { |
---|
192 | Point2D.Double loc = label_.getLocationP(); |
---|
193 | double w = label_.getWidthP(); |
---|
194 | double h = label_.getHeightP(); |
---|
195 | double x; |
---|
196 | double y; |
---|
197 | label_.setWidthP(label_.getHeightP()); |
---|
198 | label_.setHeightP(w); |
---|
199 | switch(label_.getJustification()) { |
---|
200 | case SGLabel.CENTER: |
---|
201 | if(item == SGLabel.VERTICAL) { |
---|
202 | x = loc.x + w*0.5 - h; |
---|
203 | y = loc.y - w*0.5; |
---|
204 | } else { |
---|
205 | x = loc.x - h*0.5 + w; |
---|
206 | y = loc.y + h*0.5; |
---|
207 | } |
---|
208 | break; |
---|
209 | default: |
---|
210 | case SGLabel.LEFT: |
---|
211 | if(item == SGLabel.VERTICAL) { |
---|
212 | x = loc.x - h; |
---|
213 | y = loc.y; |
---|
214 | } else { |
---|
215 | x = loc.x + w; |
---|
216 | y = loc.y; |
---|
217 | } |
---|
218 | break; |
---|
219 | case SGLabel.RIGHT: |
---|
220 | if(item == SGLabel.VERTICAL) { |
---|
221 | x = loc.x + w - h; |
---|
222 | y = loc.y - w; |
---|
223 | } else { |
---|
224 | x = loc.x - h + w; |
---|
225 | y = loc.y + h; |
---|
226 | } |
---|
227 | } |
---|
228 | label_.setLocationP(new Point2D.Double(x,y)); |
---|
229 | } |
---|
230 | } else if(command.equals("Selectable")) { |
---|
231 | label_.setSelectable(((JCheckBox)obj).isSelected()); |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | void resetFields() { |
---|
236 | for(int i=0; i < comps_.length; i++) { |
---|
237 | if(comps_[i] instanceof JTextField) { |
---|
238 | ((JTextField)comps_[i]).removeActionListener(this); |
---|
239 | ((JTextField)comps_[i]).removeFocusListener(this); |
---|
240 | } else if(comps_[i] instanceof JCheckBox) { |
---|
241 | ((JCheckBox)comps_[i]).removeActionListener(this); |
---|
242 | ((JCheckBox)comps_[i]).removeFocusListener(this); |
---|
243 | } else if(comps_[i] instanceof JComboBox) { |
---|
244 | ((JComboBox)comps_[i]).removeActionListener(this); |
---|
245 | ((JComboBox)comps_[i]).removeFocusListener(this); |
---|
246 | } else if(comps_[i] instanceof JButton) { |
---|
247 | ((JButton)comps_[i]).removeActionListener(this); |
---|
248 | ((JButton)comps_[i]).removeFocusListener(this); |
---|
249 | } |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | public void actionPerformed(ActionEvent e) { |
---|
254 | Object obj = e.getSource(); |
---|
255 | String command = e.getActionCommand(); |
---|
256 | processEvent(obj, command); |
---|
257 | } |
---|
258 | public void stateChanged(ChangeEvent e) { |
---|
259 | update(); |
---|
260 | } |
---|
261 | public void focusGained(FocusEvent e) { |
---|
262 | } |
---|
263 | public void focusLost(FocusEvent e) { |
---|
264 | Object obj = e.getSource(); |
---|
265 | if(obj instanceof JTextField) { |
---|
266 | JTextField tf = (JTextField)obj; |
---|
267 | String name = tf.getName(); |
---|
268 | processEvent(obj, name); |
---|
269 | } |
---|
270 | } |
---|
271 | public void setExpert(boolean expert) { |
---|
272 | expert_ = expert; |
---|
273 | } |
---|
274 | |
---|
275 | public boolean isExpert() { |
---|
276 | return expert_; |
---|
277 | } |
---|
278 | } |
---|