1 | /* |
---|
2 | * $Id: NewLevelsDialog.java,v 1.4 2001/02/08 00:29: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 | package gov.noaa.pmel.sgt.swing.prop; |
---|
13 | |
---|
14 | import gov.noaa.pmel.sgt.dm.SGTGrid; |
---|
15 | import gov.noaa.pmel.util.Range2D; |
---|
16 | import gov.noaa.pmel.sgt.Graph; |
---|
17 | |
---|
18 | import javax.swing.*; |
---|
19 | import java.awt.*; |
---|
20 | |
---|
21 | import gov.noaa.pmel.swing.JSlider2Double; |
---|
22 | |
---|
23 | /** |
---|
24 | * Computes the range for creating a <code>ContourLevels</code> |
---|
25 | * object. If a <code>SGTGrid</code> object is provided |
---|
26 | * <code>NewLevelsDialog</code> can use the actual data range to help |
---|
27 | * in creating the contour levels. |
---|
28 | * |
---|
29 | * @author Donald Denbo |
---|
30 | * @version $Revision: 1.4 $, $Date: 2001/02/08 00:29:39 $ |
---|
31 | * @since 2.0 |
---|
32 | */ |
---|
33 | public class NewLevelsDialog extends JDialog { |
---|
34 | private int result_; |
---|
35 | private SGTGrid grid_ = null; |
---|
36 | private boolean useSpacing_ = false; |
---|
37 | private boolean rangeComputed_ = false; |
---|
38 | /** OK button selected */ |
---|
39 | public static int OK_RESPONSE = 1; |
---|
40 | /** Cancel button selected */ |
---|
41 | public static int CANCEL_RESPONSE = 2; |
---|
42 | /** |
---|
43 | * Constructor. |
---|
44 | */ |
---|
45 | public NewLevelsDialog(Frame parent) { |
---|
46 | super(parent); |
---|
47 | try { |
---|
48 | jbInit(); |
---|
49 | pack(); |
---|
50 | } catch(Exception ex) { |
---|
51 | ex.printStackTrace(); |
---|
52 | } |
---|
53 | } |
---|
54 | void jbInit() throws Exception { |
---|
55 | getContentPane().setLayout(new BorderLayout(0,0)); |
---|
56 | setSize(282,366); |
---|
57 | setVisible(false); |
---|
58 | buttonPanel.setBorder(etchedBorder1); |
---|
59 | buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER,5,5)); |
---|
60 | getContentPane().add(buttonPanel, "South"); |
---|
61 | okButton.setText("OK"); |
---|
62 | okButton.setActionCommand("OK"); |
---|
63 | buttonPanel.add(okButton); |
---|
64 | cancelButton.setText("Cancel"); |
---|
65 | cancelButton.setActionCommand("Cancel"); |
---|
66 | buttonPanel.add(cancelButton); |
---|
67 | //$$ etchedBorder1.move(0,444); |
---|
68 | mainPanel.setLayout(new GridBagLayout()); |
---|
69 | getContentPane().add(mainPanel, "Center"); |
---|
70 | mainPanel.setBackground(new java.awt.Color(204,204,204)); |
---|
71 | JPanel1.setBorder(titledBorder1); |
---|
72 | JPanel1.setLayout(new GridBagLayout()); |
---|
73 | mainPanel.add(JPanel1, new GridBagConstraints(0,0,1,1,0.0,0.0, |
---|
74 | GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(5,5,5,5),0,0)); |
---|
75 | JLabel1.setText("Minimum:"); |
---|
76 | JPanel1.add(JLabel1,new GridBagConstraints(0,0,1,1,0.0,0.0, |
---|
77 | GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(5,10,5,2),0,0)); |
---|
78 | minTextField.setColumns(15); |
---|
79 | JPanel1.add(minTextField,new GridBagConstraints(1,0,1,1,1.0,1.0, |
---|
80 | GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(5,0,5,10),0,0)); |
---|
81 | JLabel2.setText("Maximum:"); |
---|
82 | JPanel1.add(JLabel2,new GridBagConstraints(0,1,1,1,0.0,0.0, |
---|
83 | GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(5,5,5,2),0,0)); |
---|
84 | maxTextField.setColumns(15); |
---|
85 | JPanel1.add(maxTextField,new GridBagConstraints(1,1,1,1,1.0,1.0, |
---|
86 | GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(5,0,5,10),0,0)); |
---|
87 | JPanel3.setBorder(titledBorder3); |
---|
88 | JPanel3.setLayout(new GridBagLayout()); |
---|
89 | mainPanel.add(JPanel3, new GridBagConstraints(0,1,1,1,0.0,0.0, |
---|
90 | GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(0,5,0,5),0,0)); |
---|
91 | JLabel3.setText("Number:"); |
---|
92 | JPanel3.add(JLabel3,new GridBagConstraints(0,0,1,1,0.0,0.0, |
---|
93 | GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(5,10,0,2),0,0)); |
---|
94 | nlevelsTextField.setColumns(5); |
---|
95 | JPanel3.add(nlevelsTextField,new GridBagConstraints(1,0,1,1,0.0,1.0, |
---|
96 | GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(5,0,0,10),0,0)); |
---|
97 | JLabel5.setText("or"); |
---|
98 | JPanel3.add(JLabel5,new GridBagConstraints(0,1,1,1,0.0,0.0, |
---|
99 | GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(0,0,0,5),0,0)); |
---|
100 | JLabel5.setBackground(new java.awt.Color(204,204,204)); |
---|
101 | JLabel5.setForeground(new java.awt.Color(102,102,153)); |
---|
102 | JLabel4.setText("Spacing:"); |
---|
103 | JPanel3.add(JLabel4,new GridBagConstraints(0,2,1,1,0.0,0.0, |
---|
104 | GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(0,10,5,2),0,0)); |
---|
105 | spacingTextField.setColumns(15); |
---|
106 | JPanel3.add(spacingTextField,new GridBagConstraints(1,2,1,1,0.0,1.0, |
---|
107 | GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(0,0,5,10),0,0)); |
---|
108 | JPanel4.setLayout(new FlowLayout(FlowLayout.CENTER,5,5)); |
---|
109 | mainPanel.add(JPanel4, new GridBagConstraints(0,2,1,1,0.0,0.0, |
---|
110 | GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); |
---|
111 | computeButton.setText("Compute \"nice\" Range"); |
---|
112 | JPanel4.add(computeButton); |
---|
113 | JPanel2.setBorder(titledBorder2); |
---|
114 | JPanel2.setLayout(new GridBagLayout()); |
---|
115 | mainPanel.add(JPanel2, new GridBagConstraints(0,3,1,1,0.0,0.0, |
---|
116 | GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0)); |
---|
117 | JLabel6.setText("Minimum:"); |
---|
118 | JPanel2.add(JLabel6,new GridBagConstraints(0,0,1,1,0.0,0.0, |
---|
119 | GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(5,10,5,2),0,0)); |
---|
120 | minCompTextField.setEditable(false); |
---|
121 | minCompTextField.setColumns(15); |
---|
122 | minCompTextField.setEnabled(false); |
---|
123 | JPanel2.add(minCompTextField,new GridBagConstraints(1,0,1,1,1.0,1.0, |
---|
124 | GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(5,0,5,10),0,0)); |
---|
125 | JLabel7.setText("Minimum:"); |
---|
126 | JPanel2.add(JLabel7,new GridBagConstraints(0,1,1,1,0.0,0.0, |
---|
127 | GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(5,5,5,2),0,0)); |
---|
128 | maxCompTextField.setEditable(false); |
---|
129 | maxCompTextField.setColumns(15); |
---|
130 | maxCompTextField.setEnabled(false); |
---|
131 | JPanel2.add(maxCompTextField,new GridBagConstraints(1,1,1,1,0.0,1.0, |
---|
132 | GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(5,0,5,10),0,0)); |
---|
133 | |
---|
134 | SymWindow aSymWindow = new SymWindow(); |
---|
135 | this.addWindowListener(aSymWindow); |
---|
136 | SymAction lSymAction = new SymAction(); |
---|
137 | cancelButton.addActionListener(lSymAction); |
---|
138 | okButton.addActionListener(lSymAction); |
---|
139 | minTextField.addActionListener(lSymAction); |
---|
140 | maxTextField.addActionListener(lSymAction); |
---|
141 | nlevelsTextField.addActionListener(lSymAction); |
---|
142 | spacingTextField.addActionListener(lSymAction); |
---|
143 | SymPropertyChange lSymPropertyChange = new SymPropertyChange(); |
---|
144 | computeButton.addActionListener(lSymAction); |
---|
145 | } |
---|
146 | /** Used internally */ |
---|
147 | public void addNotify() { |
---|
148 | // Record the size of the window prior to calling parents addNotify. |
---|
149 | Dimension d = getSize(); |
---|
150 | |
---|
151 | super.addNotify(); |
---|
152 | |
---|
153 | if (fComponentsAdjusted) |
---|
154 | return; |
---|
155 | |
---|
156 | // Adjust components according to the insets |
---|
157 | Insets ins = getInsets(); |
---|
158 | setSize(ins.left + ins.right + d.width, ins.top + ins.bottom + d.height); |
---|
159 | Component components[] = getContentPane().getComponents(); |
---|
160 | for (int i = 0; i < components.length; i++) { |
---|
161 | Point p = components[i].getLocation(); |
---|
162 | p.translate(ins.left, ins.top); |
---|
163 | components[i].setLocation(p); |
---|
164 | } |
---|
165 | fComponentsAdjusted = true; |
---|
166 | } |
---|
167 | |
---|
168 | // Used for addNotify check. |
---|
169 | boolean fComponentsAdjusted = false; |
---|
170 | /** |
---|
171 | * Constructor. |
---|
172 | */ |
---|
173 | public NewLevelsDialog(String title) { |
---|
174 | this(); |
---|
175 | setTitle(title); |
---|
176 | } |
---|
177 | /** |
---|
178 | * Default constructor. |
---|
179 | */ |
---|
180 | public NewLevelsDialog() { |
---|
181 | this((Frame)null); |
---|
182 | } |
---|
183 | /** |
---|
184 | * Make the dialog visible |
---|
185 | */ |
---|
186 | public void setVisible(boolean b) { |
---|
187 | if(b) { |
---|
188 | setLocation(50, 50); |
---|
189 | } |
---|
190 | super.setVisible(b); |
---|
191 | } |
---|
192 | |
---|
193 | class SymWindow extends java.awt.event.WindowAdapter { |
---|
194 | public void windowClosing(java.awt.event.WindowEvent event) { |
---|
195 | Object object = event.getSource(); |
---|
196 | if (object == NewLevelsDialog.this) |
---|
197 | NewLevelsDialog_WindowClosing(event); |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | void NewLevelsDialog_WindowClosing(java.awt.event.WindowEvent event) { |
---|
202 | dispose(); |
---|
203 | } |
---|
204 | |
---|
205 | javax.swing.JPanel buttonPanel = new javax.swing.JPanel(); |
---|
206 | javax.swing.JButton okButton = new javax.swing.JButton(); |
---|
207 | javax.swing.JButton cancelButton = new javax.swing.JButton(); |
---|
208 | javax.swing.border.EtchedBorder etchedBorder1 = new javax.swing.border.EtchedBorder(); |
---|
209 | javax.swing.JPanel mainPanel = new javax.swing.JPanel(); |
---|
210 | javax.swing.JPanel JPanel1 = new javax.swing.JPanel(); |
---|
211 | javax.swing.JLabel JLabel1 = new javax.swing.JLabel(); |
---|
212 | javax.swing.JTextField minTextField = new javax.swing.JTextField(); |
---|
213 | javax.swing.JLabel JLabel2 = new javax.swing.JLabel(); |
---|
214 | javax.swing.JTextField maxTextField = new javax.swing.JTextField(); |
---|
215 | javax.swing.JPanel JPanel3 = new javax.swing.JPanel(); |
---|
216 | javax.swing.JLabel JLabel3 = new javax.swing.JLabel(); |
---|
217 | javax.swing.JTextField nlevelsTextField = new javax.swing.JTextField(); |
---|
218 | javax.swing.JLabel JLabel5 = new javax.swing.JLabel(); |
---|
219 | javax.swing.JLabel JLabel4 = new javax.swing.JLabel(); |
---|
220 | javax.swing.JTextField spacingTextField = new javax.swing.JTextField(); |
---|
221 | javax.swing.JPanel JPanel4 = new javax.swing.JPanel(); |
---|
222 | javax.swing.JButton computeButton = new javax.swing.JButton(); |
---|
223 | javax.swing.JPanel JPanel2 = new javax.swing.JPanel(); |
---|
224 | javax.swing.JLabel JLabel6 = new javax.swing.JLabel(); |
---|
225 | javax.swing.JTextField minCompTextField = new javax.swing.JTextField(); |
---|
226 | javax.swing.JLabel JLabel7 = new javax.swing.JLabel(); |
---|
227 | javax.swing.JTextField maxCompTextField = new javax.swing.JTextField(); |
---|
228 | javax.swing.border.TitledBorder titledBorder1 = new javax.swing.border.TitledBorder("Data Range"); |
---|
229 | javax.swing.border.TitledBorder titledBorder3 = new javax.swing.border.TitledBorder("Select"); |
---|
230 | javax.swing.border.TitledBorder titledBorder2 = new javax.swing.border.TitledBorder("Computed Range"); |
---|
231 | |
---|
232 | |
---|
233 | class SymAction implements java.awt.event.ActionListener { |
---|
234 | public void actionPerformed(java.awt.event.ActionEvent event) { |
---|
235 | Object object = event.getSource(); |
---|
236 | if (object == cancelButton) |
---|
237 | cancelButton_actionPerformed(event); |
---|
238 | else if (object == okButton) |
---|
239 | okButton_actionPerformed(event); |
---|
240 | else if (object == minTextField) |
---|
241 | minTextField_actionPerformed(event); |
---|
242 | else if (object == maxTextField) |
---|
243 | maxTextField_actionPerformed(event); |
---|
244 | else if (object == nlevelsTextField) |
---|
245 | nlevelsTextField_actionPerformed(event); |
---|
246 | else if (object == spacingTextField) |
---|
247 | spacingTextField_actionPerformed(event); |
---|
248 | else if (object == computeButton) |
---|
249 | computeButton_actionPerformed(event); |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | void cancelButton_actionPerformed(java.awt.event.ActionEvent event) { |
---|
254 | this.setVisible(false); |
---|
255 | result_ = CANCEL_RESPONSE; |
---|
256 | } |
---|
257 | |
---|
258 | void okButton_actionPerformed(java.awt.event.ActionEvent event) { |
---|
259 | result_ = OK_RESPONSE; |
---|
260 | this.setVisible(false); |
---|
261 | } |
---|
262 | /** |
---|
263 | * Test entry point |
---|
264 | */ |
---|
265 | public static void main(String[] args) { |
---|
266 | NewLevelsDialog la = new NewLevelsDialog(); |
---|
267 | la.setTitle("Test New Levels Dialog"); |
---|
268 | la.setVisible(true); |
---|
269 | } |
---|
270 | /** |
---|
271 | * Show the dialog and wait for a response. |
---|
272 | * |
---|
273 | * @param grid the data grid |
---|
274 | * @return result, either CANCEL_RESPONSE or OK_RESPONSE |
---|
275 | */ |
---|
276 | public int showDialog(SGTGrid grid) { |
---|
277 | setGrid(grid); |
---|
278 | result_ = CANCEL_RESPONSE; |
---|
279 | setModal(true); |
---|
280 | super.setVisible(true); |
---|
281 | return result_; |
---|
282 | } |
---|
283 | /** |
---|
284 | * Return the computed or entered range. |
---|
285 | */ |
---|
286 | public Range2D getRange() { |
---|
287 | double min; |
---|
288 | double max; |
---|
289 | if(rangeComputed_) { |
---|
290 | min = (new Double(minCompTextField.getText())).doubleValue(); |
---|
291 | max = (new Double(maxCompTextField.getText())).doubleValue(); |
---|
292 | } else { |
---|
293 | min = (new Double(minTextField.getText())).doubleValue(); |
---|
294 | max = (new Double(maxTextField.getText())).doubleValue(); |
---|
295 | } |
---|
296 | double delta = (new Double(spacingTextField.getText())).doubleValue(); |
---|
297 | Range2D range = new Range2D(min, max, delta); |
---|
298 | return range; |
---|
299 | } |
---|
300 | /** |
---|
301 | * Set the data grid for the computed range. |
---|
302 | */ |
---|
303 | public void setGrid(SGTGrid grid) { |
---|
304 | double zmin, zmax; |
---|
305 | grid_ = grid; |
---|
306 | if(grid_ == null) { |
---|
307 | titledBorder1.setTitle("Enter Range"); |
---|
308 | zmin = 0.0; |
---|
309 | zmax = 10.0; |
---|
310 | } else { |
---|
311 | zmin = Double.MAX_VALUE; |
---|
312 | zmax = -Double.MAX_VALUE; |
---|
313 | double[] z = grid_.getZArray(); |
---|
314 | for(int i=0; i < z.length; i++) { |
---|
315 | if(Double.isNaN(z[i])) continue; |
---|
316 | zmin = Math.min(zmin, z[i]); |
---|
317 | zmax = Math.max(zmax, z[i]); |
---|
318 | } |
---|
319 | } |
---|
320 | minTextField.setText(Double.toString(zmin)); |
---|
321 | maxTextField.setText(Double.toString(zmax)); |
---|
322 | useSpacing_ = false; |
---|
323 | nlevelsTextField.setText("10"); |
---|
324 | updateLevSpac(); |
---|
325 | } |
---|
326 | |
---|
327 | private void updateLevSpac() { |
---|
328 | int levels; |
---|
329 | double delta = (new Double(maxTextField.getText())).doubleValue() - |
---|
330 | (new Double(minTextField.getText())).doubleValue(); |
---|
331 | double spacing; |
---|
332 | if(useSpacing_) { |
---|
333 | levels = (int)(delta/(new Double(spacingTextField.getText())).doubleValue()); |
---|
334 | nlevelsTextField.setText(Integer.toString(levels)); |
---|
335 | } else { |
---|
336 | spacing = delta/(new Integer(nlevelsTextField.getText())).intValue(); |
---|
337 | spacingTextField.setText(Double.toString(spacing)); |
---|
338 | } |
---|
339 | } |
---|
340 | |
---|
341 | void minTextField_actionPerformed(java.awt.event.ActionEvent event) { |
---|
342 | updateLevSpac(); |
---|
343 | } |
---|
344 | |
---|
345 | void maxTextField_actionPerformed(java.awt.event.ActionEvent event) { |
---|
346 | updateLevSpac(); |
---|
347 | } |
---|
348 | |
---|
349 | void nlevelsTextField_actionPerformed(java.awt.event.ActionEvent event) { |
---|
350 | useSpacing_ = false; |
---|
351 | updateLevSpac(); |
---|
352 | } |
---|
353 | |
---|
354 | void spacingTextField_actionPerformed(java.awt.event.ActionEvent event) { |
---|
355 | useSpacing_ = true; |
---|
356 | updateLevSpac(); |
---|
357 | } |
---|
358 | |
---|
359 | class SymPropertyChange implements java.beans.PropertyChangeListener { |
---|
360 | public void propertyChange(java.beans.PropertyChangeEvent event) { |
---|
361 | } |
---|
362 | } |
---|
363 | |
---|
364 | void computeButton_actionPerformed(java.awt.event.ActionEvent event) { |
---|
365 | rangeComputed_ = true; |
---|
366 | minCompTextField.setEnabled(true); |
---|
367 | maxCompTextField.setEnabled(true); |
---|
368 | double min = (new Double(minTextField.getText())).doubleValue(); |
---|
369 | double max = (new Double(maxTextField.getText())).doubleValue(); |
---|
370 | Range2D range = new Range2D(min,max); |
---|
371 | int levels = Integer.parseInt(nlevelsTextField.getText()); |
---|
372 | Range2D newRange = Graph.computeRange(range, levels); |
---|
373 | spacingTextField.setText(Double.toString(newRange.delta)); |
---|
374 | minCompTextField.setText(Double.toString(newRange.start)); |
---|
375 | maxCompTextField.setText(Double.toString(newRange.end)); |
---|
376 | } |
---|
377 | } |
---|