KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > ui > nodes > editors > ObjectArrayPanel2


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.ui.nodes.editors;
21
22 import java.beans.*;
23 import java.util.ResourceBundle JavaDoc;
24 import javax.swing.*;
25
26 import org.openide.explorer.propertysheet.PropertyEnv;
27 import org.openide.util.NbBundle;
28
29
30 // XXX Copy from openide/src org.openide.explorer.propertysheeet.editors.ObjectArrayPanel.
31
/**
32  * Panel for editing the arrays of any kind of Object.
33  * This is an abstract class which should be subclassed
34  * for the specified types of arrays (e.g. Identifier[],
35  * MethodParameter[], ...)
36  *
37  * @author Petr Hamernik
38  */

39 abstract class ObjectArrayPanel2 extends javax.swing.JPanel JavaDoc {
40     static final String JavaDoc PROP_MNEMONIC_ADD = "mnemonic_Add"; // NOI18N
41
static final String JavaDoc PROP_MNEMONIC_REMOVE = "mnemonic_Remove"; // NOI18N
42
static final String JavaDoc PROP_MNEMONIC_UP = "mnemonic_Up"; // NOI18N
43
static final String JavaDoc PROP_MNEMONIC_DOWN = "mnemonic_Down"; // NOI18N
44
static final String JavaDoc PROP_MNEMONIC_EDIT = "mnemonic_Edit"; // NOI18N
45
static final String JavaDoc PROP_MNEMONIC_LIST = "mnemonic_List"; // NOI18N
46
static final String JavaDoc PROP_LABEL_LIST = "label_List"; // NOI18N
47

48     /** Resource bundle. */
49     static final ResourceBundle JavaDoc bundle = NbBundle.getBundle(ObjectArrayPanel2.class);
50
51     /** The list model for the edited data. */
52     protected DefaultListModel model;
53     
54     protected final boolean isWritable;
55
56     /** Creates new form ListPanel */
57     public ObjectArrayPanel2() {
58         this(true);
59     }
60     
61     public ObjectArrayPanel2(boolean isWritable) {
62         this.isWritable = isWritable;
63         initComponents ();
64
65         model = new DefaultListModel();
66         list.setModel(model);
67         updateButtons();
68
69         getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ObjectArrayPanel")); // NOI18N
70
list.getAccessibleContext().setAccessibleName(bundle.getString("ACS_ObjectArrayPanel_List")); // NOI18N
71
list.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ObjectArrayPanel_List")); // NOI18N
72
upButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_MoveUp")); // NOI18N
73
downButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_MoveDown")); // NOI18N
74
addButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_Add")); // NOI18N
75
removeButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Remove")); // NOI18N
76
changeButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_Change")); // NOI18N
77
}
78     
79     /* package private */void setMnemonics(PropertyEnv env) {
80         FeatureDescriptor desc = env.getFeatureDescriptor();
81         Object JavaDoc o = desc.getValue(PROP_MNEMONIC_ADD);
82         if (o instanceof String JavaDoc) {
83             addButton.setMnemonic(((String JavaDoc)o).charAt(0));
84         } else {
85             addButton.setMnemonic(bundle.getString("CTL_Add_Mnemonic").charAt(0)); // NOI18N
86
}
87         o = desc.getValue(PROP_MNEMONIC_REMOVE);
88         if (o instanceof String JavaDoc) {
89             removeButton.setMnemonic(((String JavaDoc)o).charAt(0));
90         } else {
91             removeButton.setMnemonic(bundle.getString("CTL_Remove_Mnemonic").charAt(0)); // NOI18N
92
}
93         o = desc.getValue(PROP_MNEMONIC_EDIT);
94         if (o instanceof String JavaDoc) {
95             changeButton.setMnemonic(((String JavaDoc)o).charAt(0));
96         } else {
97             changeButton.setMnemonic(bundle.getString("CTL_Change_Mnemonic").charAt(0)); // NOI18N
98
}
99         o = desc.getValue(PROP_MNEMONIC_UP);
100         if (o instanceof String JavaDoc) {
101             upButton.setMnemonic(((String JavaDoc)o).charAt(0));
102         } else {
103             upButton.setMnemonic(bundle.getString("CTL_MoveUp_Mnemonic").charAt(0)); // NOI18N
104
}
105         o = desc.getValue(PROP_MNEMONIC_DOWN);
106         if (o instanceof String JavaDoc) {
107             downButton.setMnemonic(((String JavaDoc)o).charAt(0));
108         } else {
109             downButton.setMnemonic(bundle.getString("CTL_MoveDown_Mnemonic").charAt(0)); // NOI18N
110
}
111         o = desc.getValue(PROP_LABEL_LIST);
112         if (o instanceof String JavaDoc) {
113             jLabel1.setText((String JavaDoc)o);
114             o = desc.getValue(PROP_MNEMONIC_LIST);
115             if (o instanceof String JavaDoc) {
116                 jLabel1.setDisplayedMnemonic(((String JavaDoc)o).charAt(0));
117             }
118         } else {
119             jLabel1.setText(bundle.getString("LAB_ObjectList")); // NOI18N
120
jLabel1.setDisplayedMnemonic(bundle.getString("MNEMO_ObjectList").charAt(0)); // NOI18N
121
}
122     }
123
124     /** This method is called from within the constructor to
125      * initialize the form.
126      * WARNING: Do NOT modify this code. The content of this method is
127      * always regenerated by the FormEditor.
128      */

129     private void initComponents() {//GEN-BEGIN:initComponents
130
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
131
132         jLabel1 = new javax.swing.JLabel JavaDoc();
133         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
134         list = new javax.swing.JList JavaDoc();
135         upButton = new javax.swing.JButton JavaDoc();
136         downButton = new javax.swing.JButton JavaDoc();
137         addButton = new javax.swing.JButton JavaDoc();
138         removeButton = new javax.swing.JButton JavaDoc();
139         changeButton = new javax.swing.JButton JavaDoc();
140
141         setLayout(new java.awt.GridBagLayout JavaDoc());
142
143         jLabel1.setLabelFor(list);
144         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
145         gridBagConstraints.gridx = 0;
146         gridBagConstraints.gridy = 0;
147         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 5, 0);
148         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
149         gridBagConstraints.weighty = 1.0;
150         add(jLabel1, gridBagConstraints);
151
152         jScrollPane1.setPreferredSize(new java.awt.Dimension JavaDoc(200, 3));
153         list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
154         list.setVisibleRowCount(3);
155         list.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
156             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
157                 ObjectArrayPanel2.this.listValueChanged(evt);
158             }
159         });
160         list.addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
161             public void mouseClicked(java.awt.event.MouseEvent JavaDoc evt) {
162                 ObjectArrayPanel2.this.listMouseClicked(evt);
163             }
164         });
165
166         jScrollPane1.setViewportView(list);
167
168         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
169         gridBagConstraints.gridx = 0;
170         gridBagConstraints.gridy = 1;
171         gridBagConstraints.gridheight = 6;
172         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
173         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 0, 11);
174         gridBagConstraints.weightx = 1.0;
175         gridBagConstraints.weighty = 1.0;
176         add(jScrollPane1, gridBagConstraints);
177
178         upButton.setText(bundle.getString("CTL_MoveUp"));
179         upButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
180             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
181                 ObjectArrayPanel2.this.upButtonActionPerformed(evt);
182             }
183         });
184
185         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
186         gridBagConstraints.gridx = 1;
187         gridBagConstraints.gridy = 4;
188         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
189         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 11);
190         gridBagConstraints.weighty = 1.0;
191         add(upButton, gridBagConstraints);
192
193         downButton.setText(bundle.getString("CTL_MoveDown"));
194         downButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
195             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
196                 ObjectArrayPanel2.this.downButtonActionPerformed(evt);
197             }
198         });
199
200         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
201         gridBagConstraints.gridx = 1;
202         gridBagConstraints.gridy = 5;
203         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
204         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 11);
205         gridBagConstraints.weighty = 1.0;
206         add(downButton, gridBagConstraints);
207
208         addButton.setText(bundle.getString("CTL_Add"));
209         addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
210             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
211                 ObjectArrayPanel2.this.addButtonActionPerformed(evt);
212             }
213         });
214
215         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
216         gridBagConstraints.gridx = 1;
217         gridBagConstraints.gridy = 1;
218         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
219         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 5, 11);
220         gridBagConstraints.weighty = 1.0;
221         add(addButton, gridBagConstraints);
222
223         removeButton.setText(bundle.getString("CTL_Remove"));
224         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
225             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
226                 ObjectArrayPanel2.this.removeButtonActionPerformed(evt);
227             }
228         });
229
230         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
231         gridBagConstraints.gridx = 1;
232         gridBagConstraints.gridy = 3;
233         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
234         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 11);
235         gridBagConstraints.weighty = 1.0;
236         add(removeButton, gridBagConstraints);
237
238         changeButton.setText(bundle.getString("CTL_Change"));
239         changeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
240             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
241                 ObjectArrayPanel2.this.changeButtonActionPerformed(evt);
242             }
243         });
244
245         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
246         gridBagConstraints.gridx = 1;
247         gridBagConstraints.gridy = 2;
248         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
249         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 11);
250         gridBagConstraints.weighty = 1.0;
251         add(changeButton, gridBagConstraints);
252
253     }//GEN-END:initComponents
254

255     private void listMouseClicked (java.awt.event.MouseEvent JavaDoc evt) {//GEN-FIRST:event_listMouseClicked
256
if (org.openide.awt.MouseUtils.isDoubleClick(evt) && this.isWritable) {
257             int selIndex = list.getSelectedIndex();
258             if (selIndex != -1) {
259                 Object JavaDoc newValue = editValue(model.getElementAt(selIndex));
260                 if (newValue != null) {
261                     model.setElementAt(newValue,selIndex);
262                     modelChanged();
263                     updateButtons();
264                 }
265             }
266         }
267     }//GEN-LAST:event_listMouseClicked
268

269     private void listValueChanged (javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_listValueChanged
270
updateButtons();
271     }//GEN-LAST:event_listValueChanged
272

273     private void downButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_downButtonActionPerformed
274
int selIndex = list.getSelectedIndex();
275         model.set(selIndex + 1, model.set(selIndex, model.getElementAt(selIndex + 1)));
276         list.setSelectedIndex(selIndex + 1);
277         modelChanged();
278         updateButtons();
279     }//GEN-LAST:event_downButtonActionPerformed
280

281     private void upButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_upButtonActionPerformed
282
int selIndex = list.getSelectedIndex();
283         model.set(selIndex - 1, model.set(selIndex, model.getElementAt(selIndex - 1)));
284         list.setSelectedIndex(selIndex - 1);
285         modelChanged();
286         updateButtons();
287     }//GEN-LAST:event_upButtonActionPerformed
288

289     private void changeButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_changeButtonActionPerformed
290
int selIndex = list.getSelectedIndex();
291         Object JavaDoc newValue = editValue(model.getElementAt(selIndex));
292         if (newValue != null) {
293             model.setElementAt(newValue,selIndex);
294             modelChanged();
295             updateButtons();
296         }
297     }//GEN-LAST:event_changeButtonActionPerformed
298

299     private void removeButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
300
int selIndex = list.getSelectedIndex();
301         model.remove(selIndex);
302         int size = model.size();
303         if (size > 0) {
304             selIndex = Math.min(size - 1, selIndex);
305             list.setSelectedIndex(selIndex);
306         }
307
308         modelChanged();
309         updateButtons();
310     }//GEN-LAST:event_removeButtonActionPerformed
311

312     private void addButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
313
Object JavaDoc newValue = insertNewValue();
314         if (newValue != null) {
315             model.addElement(newValue);
316             list.setSelectedIndex(model.size() - 1);
317             modelChanged();
318             updateButtons();
319         }
320     }//GEN-LAST:event_addButtonActionPerformed
321

322     // Variables declaration - do not modify//GEN-BEGIN:variables
323
private javax.swing.JButton JavaDoc addButton;
324     private javax.swing.JButton JavaDoc downButton;
325     private javax.swing.JList JavaDoc list;
326     private javax.swing.JScrollPane JavaDoc jScrollPane1;
327     private javax.swing.JLabel JavaDoc jLabel1;
328     private javax.swing.JButton JavaDoc changeButton;
329     private javax.swing.JButton JavaDoc upButton;
330     private javax.swing.JButton JavaDoc removeButton;
331     // End of variables declaration//GEN-END:variables
332

333     /** Method for subclasses.
334     * @return the main JList component in the panel
335     */

336     protected JList getListComponent() {
337         return list;
338     }
339
340     /** Updates the 'enable' status of the buttons in the panel.
341     */

342     private void updateButtons() {
343         boolean isEmpty = model.isEmpty();
344         int selIndex = list.getSelectedIndex();
345         boolean isSelected = (selIndex != -1) && this.isWritable;
346         
347         addButton.setEnabled(this.isWritable);
348         removeButton.setEnabled(isSelected);
349         changeButton.setEnabled(isSelected);
350         upButton.setEnabled(isSelected && (selIndex > 0));
351         downButton.setEnabled(isSelected && (selIndex < model.size() - 1));
352     }
353
354     /** Ask user for new value.
355     * @return new value or <CODE>null</CODE> when
356     * operation was canceled.
357     */

358     protected abstract Object JavaDoc insertNewValue();
359
360     /** Ask user for edit value.
361     * @param oldValue The previous value to be edited
362     * @return new value or <CODE>null</CODE> when
363     * operation was canceled.
364     */

365     protected abstract Object JavaDoc editValue(Object JavaDoc oldValue);
366
367     /** Notification about the changes in the data array.
368     */

369     protected void modelChanged() {
370     }
371 }
372
Popular Tags