1 | /* |
---|
2 | * $Id: ArrayEditDialog.java,v 1.7 2001/02/08 00:29:38 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 javax.swing.*; |
---|
15 | import javax.swing.event.*; |
---|
16 | import java.awt.*; |
---|
17 | import java.util.Enumeration; |
---|
18 | /** |
---|
19 | * This dialog accepts an array of float's then via a graphical |
---|
20 | * interface allows this array to be modified. For example, this |
---|
21 | * dialog is used to edit the dash array of the <code>LineAttribute</code>. |
---|
22 | * |
---|
23 | * <p> Example of <code>ArrayEditDialog</code> use: |
---|
24 | * <pre> |
---|
25 | * |
---|
26 | * public float[] editArray(float[] inArray) { |
---|
27 | * ArrayEditDialog aed = new ArrayEditDialog(); |
---|
28 | * aed.setTitle("ArrayEdit"); |
---|
29 | * aed.setArray(inArray); |
---|
30 | * if(aed.showDialog() == ArrayEditDialog.CANCEL_RESPONSE) { |
---|
31 | * return inArray; |
---|
32 | * } else { |
---|
33 | * return aed.getFloatArray(); |
---|
34 | * } |
---|
35 | * } |
---|
36 | * </pre> |
---|
37 | * |
---|
38 | * @author Donald Denbo |
---|
39 | * @version $Revision: 1.7 $, $Date: 2001/02/08 00:29:38 $ |
---|
40 | * @since 2.0 |
---|
41 | * @LineAttribute |
---|
42 | */ |
---|
43 | public class ArrayEditDialog extends JDialog implements ListSelectionListener { |
---|
44 | private DefaultListModel model_; |
---|
45 | private int result_; |
---|
46 | /** OK button was selected */ |
---|
47 | public static int OK_RESPONSE = 1; |
---|
48 | /** Cancel button was selected */ |
---|
49 | public static int CANCEL_RESPONSE = 2; |
---|
50 | /** |
---|
51 | * Constructor. |
---|
52 | */ |
---|
53 | public ArrayEditDialog(Frame parent) { |
---|
54 | super(parent); |
---|
55 | try { |
---|
56 | jbInit(); |
---|
57 | pack(); |
---|
58 | } catch(Exception ex) { |
---|
59 | ex.printStackTrace(); |
---|
60 | } |
---|
61 | } |
---|
62 | void jbInit() throws Exception { |
---|
63 | getContentPane().setLayout(new BorderLayout(0,0)); |
---|
64 | setSize(322,349); |
---|
65 | setVisible(false); |
---|
66 | buttonPanel.setBorder(etchedBorder1); |
---|
67 | buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER,5,5)); |
---|
68 | getContentPane().add(buttonPanel, "South"); |
---|
69 | okButton.setText("OK"); |
---|
70 | okButton.setActionCommand("OK"); |
---|
71 | buttonPanel.add(okButton); |
---|
72 | cancelButton.setText("Cancel"); |
---|
73 | cancelButton.setActionCommand("Cancel"); |
---|
74 | buttonPanel.add(cancelButton); |
---|
75 | //$$ etchedBorder1.move(48,396); |
---|
76 | mainPanel.setLayout(new GridBagLayout()); |
---|
77 | getContentPane().add(mainPanel, "Center"); |
---|
78 | mainPanel.add(JScrollPane1, new GridBagConstraints(0,0,1,3,1.0,1.0, |
---|
79 | GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(5,5,5,5),155,0)); |
---|
80 | arrayList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); |
---|
81 | JScrollPane1.getViewport().add(arrayList); |
---|
82 | arrayList.setBounds(0,0,173,295); |
---|
83 | JPanel2.setBorder(titledBorder2); |
---|
84 | JPanel2.setLayout(new GridBagLayout()); |
---|
85 | mainPanel.add(JPanel2, new GridBagConstraints(1,0,1,1,0.0,1.0, |
---|
86 | GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,5),0,0)); |
---|
87 | JPanel2.add(editTextField,new GridBagConstraints(0,0,1,1,0.0,0.0, |
---|
88 | GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(5,5,0,5),0,0)); |
---|
89 | editButton.setToolTipText("Change value of selected element."); |
---|
90 | editButton.setText("Change Value"); |
---|
91 | JPanel2.add(editButton,new GridBagConstraints(0,1,1,1,1.0,1.0, |
---|
92 | GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(5,5,5,5),0,0)); |
---|
93 | JPanel3.setBorder(titledBorder3); |
---|
94 | JPanel3.setLayout(new GridBagLayout()); |
---|
95 | mainPanel.add(JPanel3, new GridBagConstraints(1,2,1,1,1.0,1.0, |
---|
96 | GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(5,0,0,0),0,0)); |
---|
97 | deleteButton.setToolTipText("Delete selected element."); |
---|
98 | deleteButton.setText("Delete"); |
---|
99 | JPanel3.add(deleteButton,new GridBagConstraints(0,0,1,1,1.0,1.0, |
---|
100 | GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(5,5,5,5),0,0)); |
---|
101 | JPanel1.setBorder(titledBorder1); |
---|
102 | JPanel1.setLayout(new GridBagLayout()); |
---|
103 | mainPanel.add(JPanel1, new GridBagConstraints(1,1,1,1,1.0,1.0, |
---|
104 | GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(5,0,0,0),0,0)); |
---|
105 | JPanel1.add(insertTextField, new GridBagConstraints(0,0,1,1,1.0,0.0, |
---|
106 | GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(5,5,0,5),0,0)); |
---|
107 | beforeButton.setToolTipText("Insert new item before selected element."); |
---|
108 | beforeButton.setText("Before"); |
---|
109 | JPanel1.add(beforeButton, new GridBagConstraints(0,1,1,1,0.0,0.0, |
---|
110 | GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(5,5,0,5),0,0)); |
---|
111 | afterButton.setToolTipText("Insert new item after selected element."); |
---|
112 | afterButton.setText("After"); |
---|
113 | JPanel1.add(afterButton, new GridBagConstraints(0,2,1,1,0.0,0.0, |
---|
114 | GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(0,5,5,5),0,0)); |
---|
115 | setTitle("Edit Array"); |
---|
116 | |
---|
117 | SymWindow aSymWindow = new SymWindow(); |
---|
118 | this.addWindowListener(aSymWindow); |
---|
119 | SymAction lSymAction = new SymAction(); |
---|
120 | cancelButton.addActionListener(lSymAction); |
---|
121 | okButton.addActionListener(lSymAction); |
---|
122 | editButton.addActionListener(lSymAction); |
---|
123 | beforeButton.addActionListener(lSymAction); |
---|
124 | afterButton.addActionListener(lSymAction); |
---|
125 | deleteButton.addActionListener(lSymAction); |
---|
126 | |
---|
127 | arrayList.addListSelectionListener(this); |
---|
128 | } |
---|
129 | /** |
---|
130 | * Used internally. |
---|
131 | */ |
---|
132 | public void addNotify() { |
---|
133 | // Record the size of the window prior to calling parents addNotify. |
---|
134 | Dimension d = getSize(); |
---|
135 | |
---|
136 | super.addNotify(); |
---|
137 | |
---|
138 | if (fComponentsAdjusted) |
---|
139 | return; |
---|
140 | |
---|
141 | // Adjust components according to the insets |
---|
142 | Insets ins = getInsets(); |
---|
143 | setSize(ins.left + ins.right + d.width, ins.top + ins.bottom + d.height); |
---|
144 | Component components[] = getContentPane().getComponents(); |
---|
145 | for (int i = 0; i < components.length; i++) { |
---|
146 | Point p = components[i].getLocation(); |
---|
147 | p.translate(ins.left, ins.top); |
---|
148 | components[i].setLocation(p); |
---|
149 | } |
---|
150 | fComponentsAdjusted = true; |
---|
151 | } |
---|
152 | |
---|
153 | // Used for addNotify check. |
---|
154 | boolean fComponentsAdjusted = false; |
---|
155 | /** |
---|
156 | * Constructor. |
---|
157 | */ |
---|
158 | public ArrayEditDialog(String title) { |
---|
159 | this(); |
---|
160 | setTitle(title); |
---|
161 | } |
---|
162 | /** Default constructor */ |
---|
163 | public ArrayEditDialog() { |
---|
164 | this((Frame)null); |
---|
165 | } |
---|
166 | /** |
---|
167 | * Make the dialog visible. |
---|
168 | */ |
---|
169 | public void setVisible(boolean b) { |
---|
170 | if(b) { |
---|
171 | setLocation(50, 50); |
---|
172 | } |
---|
173 | super.setVisible(b); |
---|
174 | } |
---|
175 | |
---|
176 | class SymWindow extends java.awt.event.WindowAdapter { |
---|
177 | public void windowClosing(java.awt.event.WindowEvent event) { |
---|
178 | Object object = event.getSource(); |
---|
179 | if (object == ArrayEditDialog.this) |
---|
180 | ArrayEditDialog_WindowClosing(event); |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | void ArrayEditDialog_WindowClosing(java.awt.event.WindowEvent event) { |
---|
185 | dispose(); |
---|
186 | } |
---|
187 | |
---|
188 | javax.swing.JPanel buttonPanel = new javax.swing.JPanel(); |
---|
189 | javax.swing.JButton okButton = new javax.swing.JButton(); |
---|
190 | javax.swing.JButton cancelButton = new javax.swing.JButton(); |
---|
191 | javax.swing.border.EtchedBorder etchedBorder1 = new javax.swing.border.EtchedBorder(); |
---|
192 | javax.swing.JPanel mainPanel = new javax.swing.JPanel(); |
---|
193 | javax.swing.JScrollPane JScrollPane1 = new javax.swing.JScrollPane(); |
---|
194 | javax.swing.JList arrayList = new javax.swing.JList(); |
---|
195 | javax.swing.JPanel JPanel2 = new javax.swing.JPanel(); |
---|
196 | javax.swing.JTextField editTextField = new javax.swing.JTextField(); |
---|
197 | javax.swing.JButton editButton = new javax.swing.JButton(); |
---|
198 | javax.swing.JPanel JPanel3 = new javax.swing.JPanel(); |
---|
199 | javax.swing.JButton deleteButton = new javax.swing.JButton(); |
---|
200 | javax.swing.JPanel JPanel1 = new javax.swing.JPanel(); |
---|
201 | javax.swing.JTextField insertTextField = new javax.swing.JTextField(); |
---|
202 | javax.swing.JButton beforeButton = new javax.swing.JButton(); |
---|
203 | javax.swing.JButton afterButton = new javax.swing.JButton(); |
---|
204 | javax.swing.border.TitledBorder titledBorder1 = new javax.swing.border.TitledBorder("Insert Element"); |
---|
205 | javax.swing.border.TitledBorder titledBorder2 = new javax.swing.border.TitledBorder("Edit Element"); |
---|
206 | javax.swing.border.TitledBorder titledBorder3 = new javax.swing.border.TitledBorder("Delete Element"); |
---|
207 | |
---|
208 | |
---|
209 | class SymAction implements java.awt.event.ActionListener { |
---|
210 | public void actionPerformed(java.awt.event.ActionEvent event) { |
---|
211 | Object object = event.getSource(); |
---|
212 | if (object == cancelButton) |
---|
213 | cancelButton_actionPerformed(event); |
---|
214 | else if (object == okButton) |
---|
215 | okButton_actionPerformed(event); |
---|
216 | else if (object == editButton) |
---|
217 | editButton_actionPerformed(event); |
---|
218 | else if (object == beforeButton) |
---|
219 | beforeButton_actionPerformed(event); |
---|
220 | else if (object == afterButton) |
---|
221 | afterButton_actionPerformed(event); |
---|
222 | if (object == deleteButton) |
---|
223 | deleteButton_actionPerformed(event); |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | void cancelButton_actionPerformed(java.awt.event.ActionEvent event) { |
---|
228 | this.setVisible(false); |
---|
229 | result_ = CANCEL_RESPONSE; |
---|
230 | } |
---|
231 | |
---|
232 | void okButton_actionPerformed(java.awt.event.ActionEvent event) { |
---|
233 | result_ = OK_RESPONSE; |
---|
234 | this.setVisible(false); |
---|
235 | } |
---|
236 | /** |
---|
237 | * Test entry point. |
---|
238 | */ |
---|
239 | public static void main(String[] args) { |
---|
240 | float[] array = {0.0f, 2.0f, 2.5f, 3.0f, 4.5f}; |
---|
241 | ArrayEditDialog aed = new ArrayEditDialog(); |
---|
242 | aed.setTitle("Test ArrayEdit Dialog"); |
---|
243 | aed.setArray(array); |
---|
244 | if(aed.showDialog() == CANCEL_RESPONSE) { |
---|
245 | System.out.println("Dialog Cancelled"); |
---|
246 | } else { |
---|
247 | float[] out = aed.getFloatArray(); |
---|
248 | for(int i=0; i < out.length; i++) { |
---|
249 | System.out.println("x["+i+"] = " + out[i]); |
---|
250 | } |
---|
251 | } |
---|
252 | aed.setVisible(false); |
---|
253 | aed.dispose(); |
---|
254 | System.exit(0); |
---|
255 | } |
---|
256 | /** |
---|
257 | * Show the dialog and wait for a response. |
---|
258 | * |
---|
259 | * @return CANCEL_RESPONSE or OK_RESPONSE |
---|
260 | */ |
---|
261 | public int showDialog() { |
---|
262 | result_ = CANCEL_RESPONSE; |
---|
263 | setModal(true); |
---|
264 | super.setVisible(true); |
---|
265 | return result_; |
---|
266 | } |
---|
267 | /** |
---|
268 | * Initialize the array. |
---|
269 | */ |
---|
270 | public void setArray(float[] array) { |
---|
271 | model_ = new DefaultListModel(); |
---|
272 | for(int i=0; i < array.length; i++) { |
---|
273 | model_.addElement(Float.toString(array[i])); |
---|
274 | } |
---|
275 | arrayList.setModel(model_); |
---|
276 | } |
---|
277 | /** |
---|
278 | * Get the edited array. |
---|
279 | */ |
---|
280 | public float[] getFloatArray() { |
---|
281 | float[] array = new float[model_.size()]; |
---|
282 | Enumeration e = model_.elements(); |
---|
283 | int index = 0; |
---|
284 | while(e.hasMoreElements()) { |
---|
285 | array[index] = new Float((String)e.nextElement()).floatValue(); |
---|
286 | index++; |
---|
287 | } |
---|
288 | return array; |
---|
289 | } |
---|
290 | |
---|
291 | void editButton_actionPerformed(java.awt.event.ActionEvent event) { |
---|
292 | for(int i=0; i < model_.size(); i++) { |
---|
293 | if(arrayList.isSelectedIndex(i)) { |
---|
294 | model_.set(i, editTextField.getText()); |
---|
295 | return; |
---|
296 | } |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | void beforeButton_actionPerformed(java.awt.event.ActionEvent event) { |
---|
301 | for(int i=0; i < model_.size(); i++) { |
---|
302 | if(arrayList.isSelectedIndex(i)) { |
---|
303 | model_.insertElementAt(insertTextField.getText(), i); |
---|
304 | return; |
---|
305 | } |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | void afterButton_actionPerformed(java.awt.event.ActionEvent event) { |
---|
310 | for(int i=0; i < model_.size(); i++) { |
---|
311 | if(arrayList.isSelectedIndex(i)) { |
---|
312 | model_.insertElementAt(insertTextField.getText(), i+1); |
---|
313 | return; |
---|
314 | } |
---|
315 | } |
---|
316 | } |
---|
317 | /** |
---|
318 | * Internal event listener |
---|
319 | */ |
---|
320 | public void valueChanged(ListSelectionEvent e) { |
---|
321 | if(e.getValueIsAdjusting()) return; |
---|
322 | int first = e.getFirstIndex(); |
---|
323 | int last = e.getLastIndex(); |
---|
324 | for(int i=first; i <= last; i++) { |
---|
325 | if(arrayList.isSelectedIndex(i)) |
---|
326 | editTextField.setText((String)model_.get(i)); |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | void deleteButton_actionPerformed(java.awt.event.ActionEvent event) { |
---|
331 | for(int i=0; i < model_.size(); i++) { |
---|
332 | if(arrayList.isSelectedIndex(i)) { |
---|
333 | model_.removeElementAt(i); |
---|
334 | return; |
---|
335 | } |
---|
336 | } |
---|
337 | } |
---|
338 | } |
---|