1 | /* |
---|
2 | * $Id: LegendPropertyPanel.java,v 1.3 2003/09/02 22:40:39 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 javax.swing.*; |
---|
16 | |
---|
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.awt.event.FocusEvent; |
---|
22 | import java.awt.event.FocusListener; |
---|
23 | import java.awt.Color; |
---|
24 | import java.awt.Font; |
---|
25 | import java.text.DecimalFormat; |
---|
26 | |
---|
27 | import gov.noaa.pmel.sgt.Ruler; |
---|
28 | import gov.noaa.pmel.sgt.swing.prop.FontDialog; |
---|
29 | import gov.noaa.pmel.sgt.swing.prop.ColorDialog; |
---|
30 | |
---|
31 | /** |
---|
32 | * @author Donald Denbo |
---|
33 | * @version $Revision: 1.3 $, $Date: 2003/09/02 22:40:39 $ |
---|
34 | * @since 3.0 |
---|
35 | **/ |
---|
36 | class LegendPropertyPanel extends PropertyPanel implements ActionListener, ChangeListener, FocusListener { |
---|
37 | private boolean expert_ = false; |
---|
38 | private Legend legend_; |
---|
39 | private String[] pNames_ = |
---|
40 | { "Border", "Columns", "Height", |
---|
41 | "Id", "Key Label HeightP", "Line Length", |
---|
42 | "Location", |
---|
43 | "Type", "Scale Color", "Scale Label Font", |
---|
44 | "Scale Label Format", "Scale Label HeightP", "Scale Label Interval", |
---|
45 | "Scale Large Tic HeightP", "Scale Num Small Tics", "Scale Significant Digits", |
---|
46 | "Scale Small Tic HeightP", "Scale Visible", "Visible", |
---|
47 | "Width"}; |
---|
48 | private boolean[] inColor_ = |
---|
49 | { true, false, true, |
---|
50 | true, true, false, |
---|
51 | true, |
---|
52 | true, true, true, |
---|
53 | true, true, true, |
---|
54 | true, true, true, |
---|
55 | true, true, true, |
---|
56 | true}; |
---|
57 | private boolean[] inColorOnly_ = |
---|
58 | { false, false, false, |
---|
59 | false, false, false, |
---|
60 | false, |
---|
61 | false, true, true, |
---|
62 | true, true, true, |
---|
63 | true, true, true, |
---|
64 | true, true, false, |
---|
65 | false}; |
---|
66 | private boolean[] expertItem_ = |
---|
67 | { false, false, false, |
---|
68 | false, true, false, |
---|
69 | false, |
---|
70 | false, true, true, |
---|
71 | true, true, false, |
---|
72 | true, true, false, |
---|
73 | true, true, false, |
---|
74 | false}; |
---|
75 | private JComponent[] comps_ = new JComponent[pNames_.length]; |
---|
76 | private String[] keyType = {"Line", "Color", "Vector", "Point"}; |
---|
77 | private String[] borderType = {"Plain Line", "Raised", "No Border"}; |
---|
78 | private static DecimalFormat format_ = new DecimalFormat("#.###"); |
---|
79 | |
---|
80 | public LegendPropertyPanel(Legend legend, boolean expert) { |
---|
81 | super(); |
---|
82 | legend_ = legend; |
---|
83 | expert_ = expert; |
---|
84 | legend_.addChangeListener(this); |
---|
85 | reset(); |
---|
86 | } |
---|
87 | |
---|
88 | public void setLegend(Legend legend, boolean expert) { |
---|
89 | if(legend_ != null) legend_.removeChangeListener(this); |
---|
90 | legend_ = legend; |
---|
91 | legend_.addChangeListener(this); |
---|
92 | expert_ = expert; |
---|
93 | // update(); |
---|
94 | reset(); |
---|
95 | } |
---|
96 | |
---|
97 | void update() { |
---|
98 | int item = -1; |
---|
99 | int i = -1; |
---|
100 | switch(legend_.getBorderStyle()) { |
---|
101 | default: |
---|
102 | case Legend.PLAIN_LINE: |
---|
103 | item = 0; |
---|
104 | break; |
---|
105 | case Legend.RAISED: |
---|
106 | item = 1; |
---|
107 | break; |
---|
108 | case Legend.NO_BORDER: |
---|
109 | item = 2; |
---|
110 | break; |
---|
111 | } |
---|
112 | ((JComboBox)comps_[++i]).setSelectedIndex(item); |
---|
113 | ((JTextField)comps_[++i]).setText(Integer.toString(legend_.getColumns())); |
---|
114 | ((JTextField)comps_[++i]).setText(format(legend_.getHeightP(), format_)); |
---|
115 | ((JTextField)comps_[++i]).setText(legend_.getId()); |
---|
116 | ((JTextField)comps_[++i]).setText(format(legend_.getKeyLabelHeightP())); |
---|
117 | ((JTextField)comps_[++i]).setText(format(legend_.getLineLength())); |
---|
118 | ((JTextField)comps_[++i]).setText(format(legend_.getLocationP(), false)); |
---|
119 | switch(legend_.getType()) { |
---|
120 | default: |
---|
121 | case Legend.LINE: |
---|
122 | item = 0; |
---|
123 | break; |
---|
124 | case Legend.COLOR: |
---|
125 | item = 1; |
---|
126 | break; |
---|
127 | case Legend.VECTOR: |
---|
128 | item = 2; |
---|
129 | break; |
---|
130 | case Legend.POINT: |
---|
131 | item = 3; |
---|
132 | break; |
---|
133 | } |
---|
134 | ((JComboBox)comps_[++i]).setSelectedIndex(item); |
---|
135 | updateColor((JButton)comps_[++i], legend_.getScaleColor()); |
---|
136 | updateFont((JButton)comps_[++i], legend_.getScaleLabelFont()); |
---|
137 | ((JTextField)comps_[++i]).setText(legend_.getScaleLabelFormat()); |
---|
138 | ((JTextField)comps_[++i]).setText(format(legend_.getScaleLabelHeightP())); |
---|
139 | ((JTextField)comps_[++i]).setText(format(legend_.getScaleLabelInterval())); |
---|
140 | ((JTextField)comps_[++i]).setText(format(legend_.getScaleLargeTicHeightP())); |
---|
141 | ((JTextField)comps_[++i]).setText(format(legend_.getScaleNumberSmallTics())); |
---|
142 | ((JTextField)comps_[++i]).setText(format(legend_.getScaleSignificantDigits())); |
---|
143 | ((JTextField)comps_[++i]).setText(format(legend_.getScaleSmallTicHeightP())); |
---|
144 | ((JCheckBox)comps_[++i]).setSelected(legend_.isScaleVisible()); |
---|
145 | ((JCheckBox)comps_[++i]).setSelected(legend_.isVisible()); |
---|
146 | ((JTextField)comps_[++i]).setText(format(legend_.getWidthP(), format_)); |
---|
147 | } |
---|
148 | |
---|
149 | void create() { |
---|
150 | int item = -1; |
---|
151 | int i = -1; |
---|
152 | switch(legend_.getBorderStyle()) { |
---|
153 | default: |
---|
154 | case Legend.PLAIN_LINE: |
---|
155 | item = 0; |
---|
156 | break; |
---|
157 | case Legend.RAISED: |
---|
158 | item = 1; |
---|
159 | break; |
---|
160 | case Legend.NO_BORDER: |
---|
161 | item = 2; |
---|
162 | break; |
---|
163 | } |
---|
164 | comps_[++i] = createComboBox(borderType, item, pNames_[i], this, true); |
---|
165 | comps_[++i] = createTextField(Integer.toString(legend_.getColumns()), pNames_[i], this, true); |
---|
166 | comps_[++i] = createTextField(format(legend_.getHeightP(), format_), pNames_[i], this, true); |
---|
167 | comps_[++i] = createTextField(legend_.getId(), pNames_[i], this, !legend_.isInstantiated()); |
---|
168 | comps_[++i] = createTextField(format(legend_.getKeyLabelHeightP()), pNames_[i], this, true); |
---|
169 | comps_[++i] = createTextField(format(legend_.getLineLength()), pNames_[i], this, true); |
---|
170 | comps_[++i] = createTextField(format(legend_.getLocationP(), false), pNames_[i], this, true); |
---|
171 | String[] axisPosition; |
---|
172 | switch(legend_.getType()) { |
---|
173 | default: |
---|
174 | case Legend.LINE: |
---|
175 | item = 0; |
---|
176 | break; |
---|
177 | case Legend.COLOR: |
---|
178 | item = 1; |
---|
179 | break; |
---|
180 | case Legend.VECTOR: |
---|
181 | item = 2; |
---|
182 | break; |
---|
183 | case Legend.POINT: |
---|
184 | item = 3; |
---|
185 | break; |
---|
186 | } |
---|
187 | comps_[++i] = createComboBox(keyType, item, pNames_[i], this, !legend_.isInstantiated()); |
---|
188 | comps_[++i] = createColor(legend_.getScaleColor(), pNames_[i], this); |
---|
189 | comps_[++i] = createFont(legend_.getScaleLabelFont(), pNames_[i], this); |
---|
190 | comps_[++i] = createTextField(legend_.getScaleLabelFormat(), pNames_[i], this, true); |
---|
191 | comps_[++i] = createTextField(format(legend_.getScaleLabelHeightP()), pNames_[i], this, true); |
---|
192 | comps_[++i] = createTextField(format(legend_.getScaleLabelInterval()), pNames_[i], this, true); |
---|
193 | comps_[++i] = createTextField(format(legend_.getScaleLargeTicHeightP()), pNames_[i], this, true); |
---|
194 | comps_[++i] = createTextField(format(legend_.getScaleNumberSmallTics()), pNames_[i], this, true); |
---|
195 | comps_[++i] = createTextField(format(legend_.getScaleSignificantDigits()), pNames_[i], this, true); |
---|
196 | comps_[++i] = createTextField(format(legend_.getScaleSmallTicHeightP()), pNames_[i], this, true); |
---|
197 | comps_[++i] = createCheckBox(legend_.isScaleVisible(), pNames_[i], this); |
---|
198 | comps_[++i] = createCheckBox(legend_.isVisible(), pNames_[i], this); |
---|
199 | comps_[++i] = createTextField(format(legend_.getWidthP(), format_), pNames_[i], this, true); |
---|
200 | |
---|
201 | for(i=0; i < comps_.length; i++) { |
---|
202 | if(expert_ || ! expertItem_[i]) |
---|
203 | if(legend_.isColor()) { |
---|
204 | if(inColor_[i]) { |
---|
205 | addProperty(i+1, pNames_[i], comps_[i], false); |
---|
206 | } |
---|
207 | } else { |
---|
208 | if(!inColorOnly_[i]) { |
---|
209 | addProperty(i+1, pNames_[i], comps_[i], false); |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | addProperty(comps_.length + 1, " ", new JLabel(" "), true); |
---|
214 | } |
---|
215 | |
---|
216 | void resetFields() { |
---|
217 | for(int i=0; i < comps_.length; i++) { |
---|
218 | if(comps_[i] instanceof JTextField) { |
---|
219 | ((JTextField)comps_[i]).removeActionListener(this); |
---|
220 | ((JTextField)comps_[i]).removeFocusListener(this); |
---|
221 | } else if(comps_[i] instanceof JCheckBox) { |
---|
222 | ((JCheckBox)comps_[i]).removeActionListener(this); |
---|
223 | ((JCheckBox)comps_[i]).removeFocusListener(this); |
---|
224 | } else if(comps_[i] instanceof JComboBox) { |
---|
225 | ((JComboBox)comps_[i]).removeActionListener(this); |
---|
226 | ((JComboBox)comps_[i]).removeFocusListener(this); |
---|
227 | } else if(comps_[i] instanceof JButton) { |
---|
228 | ((JButton)comps_[i]).removeActionListener(this); |
---|
229 | ((JButton)comps_[i]).removeFocusListener(this); |
---|
230 | } |
---|
231 | } |
---|
232 | } |
---|
233 | private void processEvent(Object obj, String command) { |
---|
234 | if(command.equals("Height")) { |
---|
235 | legend_.setHeightP(Float.parseFloat(((JTextField)obj).getText())); |
---|
236 | } else if(command.equals("Id")) { |
---|
237 | String oldId = legend_.getId(); |
---|
238 | legend_.getPanelHolder().getLegends().remove(oldId); |
---|
239 | legend_.setId(((JTextField)obj).getText()); |
---|
240 | legend_.getPanelHolder().getLegends().put(legend_.getId(), legend_); |
---|
241 | } else if(command.equals("Key Label HeightP")) { |
---|
242 | legend_.setKeyLabelHeightP(Double.parseDouble(((JTextField)obj).getText())); |
---|
243 | } else if(command.equals("Border")) { |
---|
244 | String str = (String)((JComboBox)obj).getSelectedItem(); |
---|
245 | int item = -1; |
---|
246 | if(str.equals("Plain Line")) { |
---|
247 | item = Legend.PLAIN_LINE; |
---|
248 | } else if(str.equals("Raised")) { |
---|
249 | item = Legend.RAISED; |
---|
250 | } else if(str.equals("No Border")) { |
---|
251 | item = Legend.NO_BORDER; |
---|
252 | } |
---|
253 | legend_.setBorderStyle(item); |
---|
254 | } else if(command.equals("Columns")) { |
---|
255 | legend_.setColumns(Integer.parseInt(((JTextField)obj).getText())); |
---|
256 | } else if(command.equals("Line Length")) { |
---|
257 | legend_.setLineLength(Double.parseDouble(((JTextField)obj).getText())); |
---|
258 | } else if(command.equals("Location")) { |
---|
259 | legend_.setLocationP(parsePoint2D(((JTextField)obj).getText())); |
---|
260 | } else if(command.equals("Type")) { |
---|
261 | String str = (String)((JComboBox)obj).getSelectedItem(); |
---|
262 | int item = -1; |
---|
263 | if(str.equals("Line")) { |
---|
264 | item = Legend.LINE; |
---|
265 | } else if(str.equals("Color")) { |
---|
266 | item = Legend.COLOR; |
---|
267 | } else if(str.equals("Vector")) { |
---|
268 | item = Legend.VECTOR; |
---|
269 | } else if(str.equals("Point")) { |
---|
270 | item = Legend.POINT; |
---|
271 | } |
---|
272 | legend_.setType(item); |
---|
273 | reset(); |
---|
274 | } else if(command.equals("Scale Color")) { |
---|
275 | ColorDialog cd = new ColorDialog(getFrame(), "Select Axis Color", true); |
---|
276 | cd.setColor(legend_.getScaleColor()); |
---|
277 | cd.setVisible(true); |
---|
278 | Color newcolor = cd.getColor(); |
---|
279 | if(newcolor != null) legend_.setScaleColor(newcolor); |
---|
280 | } else if(command.equals("Scale Label Font")) { |
---|
281 | FontDialog fd = new FontDialog("Label Font"); |
---|
282 | int result = fd.showDialog(legend_.getScaleLabelFont()); |
---|
283 | if(result == fd.OK_RESPONSE) { |
---|
284 | legend_.setScaleLabelFont(fd.getFont()); |
---|
285 | } |
---|
286 | } else if(command.equals("Scale Label Format")) { |
---|
287 | legend_.setScaleLabelFormat(((JTextField)obj).getText()); |
---|
288 | } else if(command.equals("Scale Label HeightP")) { |
---|
289 | legend_.setScaleLabelHeightP(Double.parseDouble(((JTextField)obj).getText())); |
---|
290 | } else if(command.equals("Scale Label Interval")) { |
---|
291 | legend_.setScaleLabelInterval(Integer.parseInt(((JTextField)obj).getText())); |
---|
292 | } else if(command.equals("Scale Large Tic HeightP")) { |
---|
293 | legend_.setScaleLargeTicHeightP(Double.parseDouble(((JTextField)obj).getText())); |
---|
294 | } else if(command.equals("Scale Num Small Tics")) { |
---|
295 | legend_.setScaleNumberSmallTics(Integer.parseInt(((JTextField)obj).getText())); |
---|
296 | } else if(command.equals("Scale Significant Digits")) { |
---|
297 | legend_.setScaleSignificantDigits(Integer.parseInt(((JTextField)obj).getText())); |
---|
298 | } else if(command.equals("Scale Small Tic HeightP")) { |
---|
299 | legend_.setScaleSmallTicHeightP(Integer.parseInt(((JTextField)obj).getText())); |
---|
300 | } else if(command.equals("Scale Visible")) { |
---|
301 | legend_.setScaleVisible(((JCheckBox)obj).isSelected()); |
---|
302 | } else if(command.equals("Visible")) { |
---|
303 | legend_.setVisible(((JCheckBox)obj).isSelected()); |
---|
304 | } else if(command.equals("Width")) { |
---|
305 | legend_.setWidthP(Float.parseFloat(((JTextField)obj).getText())); |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | public void actionPerformed(ActionEvent e) { |
---|
310 | Object obj = e.getSource(); |
---|
311 | String command = e.getActionCommand(); |
---|
312 | processEvent(obj, command); |
---|
313 | } |
---|
314 | |
---|
315 | public void stateChanged(ChangeEvent e) { |
---|
316 | update(); |
---|
317 | } |
---|
318 | public void focusGained(FocusEvent e) { |
---|
319 | } |
---|
320 | public void focusLost(FocusEvent e) { |
---|
321 | Object obj = e.getSource(); |
---|
322 | if(obj instanceof JTextField) { |
---|
323 | JTextField tf = (JTextField)obj; |
---|
324 | String name = tf.getName(); |
---|
325 | processEvent(obj, name); |
---|
326 | } |
---|
327 | } |
---|
328 | public void setExpert(boolean expert) { |
---|
329 | boolean save = expert_; |
---|
330 | expert_ = expert; |
---|
331 | if(expert_ != save) reset(); |
---|
332 | } |
---|
333 | |
---|
334 | public boolean isExpert() { |
---|
335 | return expert_; |
---|
336 | } |
---|
337 | } |
---|