KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > editors > StringArrayCustomEditor


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.form.editors;
21
22 import java.util.Vector JavaDoc;
23 import java.util.ResourceBundle JavaDoc;
24
25 import javax.swing.*;
26 import javax.swing.border.*;
27
28 import org.openide.util.NbBundle;
29 import org.openide.awt.Mnemonics;
30
31
32 /** A custom editor for array of Strings.
33 *
34 * @author Ian Formanek
35 */

36 public class StringArrayCustomEditor extends javax.swing.JPanel JavaDoc {
37
38     // the bundle to use
39
private ResourceBundle JavaDoc bundle = NbBundle.getBundle (
40                                        StringArrayCustomEditor.class);
41
42     private Vector JavaDoc itemsVector;
43     private StringArrayCustomizable editor;
44
45     private final static int DEFAULT_WIDTH = 400;
46
47     static final long serialVersionUID =-4347656479280614636L;
48
49     /** Initializes the Form */
50     public StringArrayCustomEditor(StringArrayCustomizable sac) {
51         editor = sac;
52         itemsVector = new Vector JavaDoc ();
53         String JavaDoc[] array = editor.getStringArray ();
54         if (array != null)
55             for (int i = 0; i < array.length; i++)
56                 itemsVector.addElement (array[i]);
57         initComponents ();
58         itemList.setCellRenderer (new EmptyStringListCellRenderer ());
59         itemList.setListData (itemsVector);
60         itemList.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
61
62         setBorder (new javax.swing.border.EmptyBorder JavaDoc (new java.awt.Insets JavaDoc(16, 8, 8, 0)));
63         buttonsPanel.setBorder (new javax.swing.border.EmptyBorder JavaDoc (new java.awt.Insets JavaDoc(0, 5, 5, 5)));
64
65         Mnemonics.setLocalizedText(itemLabel, bundle.getString("CTL_Item")); // NOI18N
66
Mnemonics.setLocalizedText(itemListLabel, bundle.getString("CTL_ItemList")); // NOI18N
67
Mnemonics.setLocalizedText(addButton, bundle.getString("CTL_Add_StringArrayCustomEditor")); // NOI18N
68
Mnemonics.setLocalizedText(changeButton, bundle.getString("CTL_Change_StringArrayCustomEditor")); // NOI18N
69
Mnemonics.setLocalizedText(removeButton, bundle.getString("CTL_Remove")); // NOI18N
70
Mnemonics.setLocalizedText(moveUpButton, bundle.getString("CTL_MoveUp")); // NOI18N
71
Mnemonics.setLocalizedText(moveDownButton, bundle.getString("CTL_MoveDown")); // NOI18N
72

73         getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_StringArrayCustomEditor")); // NOI18N
74
itemField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Item")); // NOI18N
75
itemList.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_ItemList")); // NOI18N
76
addButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Add_StringArrayCustomEditor")); // NOI18N
77
changeButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Change_StringArrayCustomEditor")); // NOI18N
78
removeButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_Remove")); // NOI18N
79
moveUpButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_MoveUp")); // NOI18N
80
moveDownButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_MoveDown")); // NOI18N
81

82         updateButtons ();
83     }
84
85     public java.awt.Dimension JavaDoc getPreferredSize () {
86         // ensure minimum width
87
java.awt.Dimension JavaDoc sup = super.getPreferredSize ();
88         return new java.awt.Dimension JavaDoc (Math.max (sup.width, DEFAULT_WIDTH), sup.height);
89     }
90
91     /** This method is called from within the constructor to
92      * initialize the form.
93      * WARNING: Do NOT modify this code. The content of this method is
94      * always regenerated by the FormEditor.
95      */

96     private void initComponents() {//GEN-BEGIN:initComponents
97
editPanel = new javax.swing.JPanel JavaDoc();
98         itemListScroll = new javax.swing.JScrollPane JavaDoc();
99         itemList = new javax.swing.JList JavaDoc();
100         itemLabel = new javax.swing.JLabel JavaDoc();
101         itemField = new javax.swing.JTextField JavaDoc();
102         itemListLabel = new javax.swing.JLabel JavaDoc();
103         buttonsPanel = new javax.swing.JPanel JavaDoc();
104         addButton = new javax.swing.JButton JavaDoc();
105         changeButton = new javax.swing.JButton JavaDoc();
106         removeButton = new javax.swing.JButton JavaDoc();
107         jSeparator1 = new javax.swing.JSeparator JavaDoc();
108         moveUpButton = new javax.swing.JButton JavaDoc();
109         moveDownButton = new javax.swing.JButton JavaDoc();
110         paddingPanel = new javax.swing.JPanel JavaDoc();
111
112         setLayout(new java.awt.BorderLayout JavaDoc());
113
114         editPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
115         java.awt.GridBagConstraints JavaDoc gridBagConstraints2;
116
117         itemList.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
118             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
119                 itemListValueChanged(evt);
120             }
121         });
122
123         itemListScroll.setViewportView(itemList);
124
125         gridBagConstraints2 = new java.awt.GridBagConstraints JavaDoc();
126         gridBagConstraints2.gridx = 0;
127         gridBagConstraints2.gridy = 2;
128         gridBagConstraints2.gridwidth = 2;
129         gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
130         gridBagConstraints2.weightx = 1.0;
131         gridBagConstraints2.weighty = 1.0;
132         editPanel.add(itemListScroll, gridBagConstraints2);
133
134         itemLabel.setText("item");
135         itemLabel.setLabelFor(itemField);
136         gridBagConstraints2 = new java.awt.GridBagConstraints JavaDoc();
137         gridBagConstraints2.insets = new java.awt.Insets JavaDoc(0, 0, 11, 12);
138         gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST;
139         editPanel.add(itemLabel, gridBagConstraints2);
140
141         gridBagConstraints2 = new java.awt.GridBagConstraints JavaDoc();
142         gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
143         gridBagConstraints2.insets = new java.awt.Insets JavaDoc(0, 0, 11, 0);
144         editPanel.add(itemField, gridBagConstraints2);
145
146         itemListLabel.setText("jLabel1");
147         itemListLabel.setLabelFor(itemList);
148         gridBagConstraints2 = new java.awt.GridBagConstraints JavaDoc();
149         gridBagConstraints2.gridx = 0;
150         gridBagConstraints2.gridy = 1;
151         gridBagConstraints2.gridwidth = 2;
152         gridBagConstraints2.insets = new java.awt.Insets JavaDoc(0, 0, 2, 0);
153         gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST;
154         editPanel.add(itemListLabel, gridBagConstraints2);
155
156         add(editPanel, java.awt.BorderLayout.CENTER);
157
158         buttonsPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
159         java.awt.GridBagConstraints JavaDoc gridBagConstraints1;
160
161         addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
162             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
163                 addButtonActionPerformed(evt);
164             }
165         });
166
167         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
168         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
169         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
170         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(0, 8, 0, 8);
171         gridBagConstraints1.weightx = 1.0;
172         buttonsPanel.add(addButton, gridBagConstraints1);
173
174         changeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
175             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
176                 changeButtonActionPerformed(evt);
177             }
178         });
179
180         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
181         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
182         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
183         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(8, 8, 0, 8);
184         gridBagConstraints1.weightx = 1.0;
185         buttonsPanel.add(changeButton, gridBagConstraints1);
186
187         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
188             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
189                 removeButtonActionPerformed(evt);
190             }
191         });
192
193         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
194         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
195         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
196         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(8, 8, 8, 8);
197         gridBagConstraints1.weightx = 1.0;
198         buttonsPanel.add(removeButton, gridBagConstraints1);
199
200         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
201         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
202         gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
203         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(0, 4, 0, 4);
204         buttonsPanel.add(jSeparator1, gridBagConstraints1);
205
206         moveUpButton.setEnabled(false);
207         moveUpButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
208             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
209                 moveUpButtonActionPerformed(evt);
210             }
211         });
212
213         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
214         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
215         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
216         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(8, 8, 0, 8);
217         gridBagConstraints1.weightx = 1.0;
218         buttonsPanel.add(moveUpButton, gridBagConstraints1);
219
220         moveDownButton.setEnabled(false);
221         moveDownButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
222             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
223                 moveDownButtonActionPerformed(evt);
224             }
225         });
226
227         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
228         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
229         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
230         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(8, 8, 0, 8);
231         gridBagConstraints1.weightx = 1.0;
232         buttonsPanel.add(moveDownButton, gridBagConstraints1);
233
234         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
235         gridBagConstraints1.weighty = 1.0;
236         buttonsPanel.add(paddingPanel, gridBagConstraints1);
237
238         add(buttonsPanel, java.awt.BorderLayout.EAST);
239
240     }//GEN-END:initComponents
241

242     private void changeButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_changeButtonActionPerformed
243
int sel = itemList.getSelectedIndex ();
244         itemsVector.removeElementAt (sel);
245         itemsVector.insertElementAt (itemField.getText (), sel);
246         itemList.setListData (itemsVector);
247         itemList.setSelectedIndex (sel);
248         itemList.repaint ();
249         updateValue ();
250     }//GEN-LAST:event_changeButtonActionPerformed
251

252     private void moveDownButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_moveDownButtonActionPerformed
253
int sel = itemList.getSelectedIndex ();
254         String JavaDoc s = (String JavaDoc) itemsVector.elementAt (sel);
255         itemsVector.removeElementAt (sel);
256         itemsVector.insertElementAt (s, sel + 1);
257         itemList.setListData (itemsVector);
258         itemList.setSelectedIndex (sel + 1);
259         itemList.repaint ();
260         updateValue ();
261     }//GEN-LAST:event_moveDownButtonActionPerformed
262

263     private void moveUpButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_moveUpButtonActionPerformed
264
int sel = itemList.getSelectedIndex ();
265         String JavaDoc s = (String JavaDoc) itemsVector.elementAt (sel);
266         itemsVector.removeElementAt (sel);
267         itemsVector.insertElementAt (s, sel - 1);
268         itemList.setListData (itemsVector);
269         itemList.setSelectedIndex (sel - 1);
270         itemList.repaint ();
271         updateValue ();
272     }//GEN-LAST:event_moveUpButtonActionPerformed
273

274     private void removeButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
275
// Add your handling code here:
276
int currentIndex = itemList.getSelectedIndex ();
277         itemsVector.removeElementAt (currentIndex);
278         itemList.setListData (itemsVector);
279
280         // set new selection
281
if (itemsVector.size () != 0) {
282             if (currentIndex >= itemsVector.size ())
283                 currentIndex = itemsVector.size () - 1;
284             itemList.setSelectedIndex (currentIndex);
285         }
286
287         itemList.repaint ();
288
289         updateValue ();
290     }//GEN-LAST:event_removeButtonActionPerformed
291

292     private void itemListValueChanged (javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_itemListValueChanged
293
// Add your handling code here:
294
updateButtons ();
295         int sel = itemList.getSelectedIndex ();
296         if (sel != -1) {
297             itemField.setText ((String JavaDoc) itemsVector.elementAt (sel));
298         }
299     }//GEN-LAST:event_itemListValueChanged
300

301     private void addButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
302
// Add your handling code here:
303
itemsVector.addElement (itemField.getText ());
304         itemList.setListData (itemsVector);
305         itemList.setSelectedIndex (itemsVector.size () - 1);
306         itemList.repaint ();
307         updateValue ();
308     }//GEN-LAST:event_addButtonActionPerformed
309

310     private void updateButtons () {
311         int sel = itemList.getSelectedIndex ();
312         if (sel == -1) {
313             removeButton.setEnabled (false);
314             moveUpButton.setEnabled (false);
315             moveDownButton.setEnabled (false);
316             changeButton.setEnabled (false);
317         } else {
318             removeButton.setEnabled (true);
319             moveUpButton.setEnabled (sel != 0);
320             moveDownButton.setEnabled (sel != itemsVector.size () - 1);
321             changeButton.setEnabled (true);
322         }
323     }
324
325     private void updateValue () {
326         String JavaDoc [] value = new String JavaDoc [itemsVector.size()];
327         itemsVector.copyInto (value);
328         editor.setStringArray (value);
329     }
330
331     // Variables declaration - do not modify//GEN-BEGIN:variables
332
private javax.swing.JPanel JavaDoc editPanel;
333     private javax.swing.JScrollPane JavaDoc itemListScroll;
334     private javax.swing.JList JavaDoc itemList;
335     private javax.swing.JLabel JavaDoc itemLabel;
336     private javax.swing.JTextField JavaDoc itemField;
337     private javax.swing.JLabel JavaDoc itemListLabel;
338     private javax.swing.JPanel JavaDoc buttonsPanel;
339     private javax.swing.JButton JavaDoc addButton;
340     private javax.swing.JButton JavaDoc changeButton;
341     private javax.swing.JButton JavaDoc removeButton;
342     private javax.swing.JSeparator JavaDoc jSeparator1;
343     private javax.swing.JButton JavaDoc moveUpButton;
344     private javax.swing.JButton JavaDoc moveDownButton;
345     private javax.swing.JPanel JavaDoc paddingPanel;
346     // End of variables declaration//GEN-END:variables
347

348     static class EmptyStringListCellRenderer extends JLabel implements ListCellRenderer {
349
350         protected static Border hasFocusBorder;
351         protected static Border noFocusBorder;
352
353         static {
354             hasFocusBorder = new LineBorder(UIManager.getColor("List.focusCellHighlight")); // NOI18N
355
noFocusBorder = new EmptyBorder(1, 1, 1, 1);
356         }
357
358         static final long serialVersionUID =487512296465844339L;
359         /** Creates a new NodeListCellRenderer */
360         public EmptyStringListCellRenderer () {
361             setOpaque (true);
362             setBorder (noFocusBorder);
363         }
364
365         /** This is the only method defined by ListCellRenderer. We just
366         * reconfigure the Jlabel each time we're called.
367         */

368         public java.awt.Component JavaDoc getListCellRendererComponent(
369             JList list,
370             Object JavaDoc value, // value to display
371
int index, // cell index
372
boolean isSelected, // is the cell selected
373
boolean cellHasFocus) // the list and the cell have the focus
374
{
375             if (!(value instanceof String JavaDoc)) return this;
376             String JavaDoc text = (String JavaDoc)value;
377             if ("".equals (text)) text = NbBundle.getMessage (EmptyStringListCellRenderer.class, "CTL_Empty"); // NOI18N
378

379             setText(text);
380             if (isSelected){
381                 setBackground(UIManager.getColor("List.selectionBackground")); // NOI18N
382
setForeground(UIManager.getColor("List.selectionForeground")); // NOI18N
383
}
384             else {
385                 setBackground(list.getBackground());
386                 setForeground(list.getForeground());
387             }
388
389             setBorder(cellHasFocus ? hasFocusBorder : noFocusBorder);
390
391             return this;
392         }
393     }
394 }
395
Popular Tags