KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > 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.openide.explorer.propertysheet.editors;
21
22 import java.beans.*;
23 import java.util.Locale JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25 import javax.swing.*;
26
27 import org.openide.*;
28 import org.openide.explorer.propertysheet.PropertyEnv;
29 import org.openide.util.NbBundle;
30
31
32 // XXX Copy from openide/src org.openide.explorer.propertysheeet.editors.ObjectArrayPanel.
33
/**
34  * Panel for editing the arrays of any kind of Object.
35  * This is an abstract class which should be subclassed
36  * for the specified types of arrays (e.g. Identifier[],
37  * MethodParameter[], ...)
38  *
39  * @author Petr Hamernik
40  */

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

50     /** Resource bundle. */
51     static final ResourceBundle JavaDoc bundle = NbBundle.getBundle("org.openide.explorer.propertysheet.editors.Bundle2", Locale.getDefault(), ObjectArrayPanel2.class.getClassLoader()); // NOI18N
52

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

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

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

264     private void listValueChanged (javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_listValueChanged
265
updateButtons();
266     }//GEN-LAST:event_listValueChanged
267

268     private void downButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_downButtonActionPerformed
269
int selIndex = list.getSelectedIndex();
270         model.set(selIndex + 1, model.set(selIndex, model.getElementAt(selIndex + 1)));
271         list.setSelectedIndex(selIndex + 1);
272         modelChanged();
273         updateButtons();
274     }//GEN-LAST:event_downButtonActionPerformed
275

276     private void upButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_upButtonActionPerformed
277
int selIndex = list.getSelectedIndex();
278         model.set(selIndex - 1, model.set(selIndex, model.getElementAt(selIndex - 1)));
279         list.setSelectedIndex(selIndex - 1);
280         modelChanged();
281         updateButtons();
282     }//GEN-LAST:event_upButtonActionPerformed
283

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

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

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

317     // Variables declaration - do not modify//GEN-BEGIN:variables
318
private javax.swing.JButton JavaDoc addButton;
319     private javax.swing.JButton JavaDoc downButton;
320     private javax.swing.JList JavaDoc list;
321     private javax.swing.JScrollPane JavaDoc jScrollPane1;
322     private javax.swing.JLabel JavaDoc jLabel1;
323     private javax.swing.JButton JavaDoc changeButton;
324     private javax.swing.JButton JavaDoc upButton;
325     private javax.swing.JButton JavaDoc removeButton;
326     // End of variables declaration//GEN-END:variables
327

328     /** Method for subclasses.
329     * @return the main JList component in the panel
330     */

331     protected JList getListComponent() {
332         return list;
333     }
334
335     /** Updates the 'enable' status of the buttons in the panel.
336     */

337     private void updateButtons() {
338         int selIndex = list.getSelectedIndex();
339         boolean isSelected = (selIndex != -1);
340
341         removeButton.setEnabled(isSelected);
342         changeButton.setEnabled(isSelected);
343         upButton.setEnabled(isSelected && (selIndex > 0));
344         downButton.setEnabled(isSelected && (selIndex < model.size() - 1));
345     }
346
347     /** Ask user for new value.
348     * @return new value or <CODE>null</CODE> when
349     * operation was canceled.
350     */

351     protected abstract Object JavaDoc insertNewValue();
352
353     /** Ask user for edit value.
354     * @param oldValue The previous value to be edited
355     * @return new value or <CODE>null</CODE> when
356     * operation was canceled.
357     */

358     protected abstract Object JavaDoc editValue(Object JavaDoc oldValue);
359
360     /** Notification about the changes in the data array.
361     */

362     protected void modelChanged() {
363     }
364 }
365
Popular Tags