KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > beaninfo > 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.beaninfo.editors;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.awt.event.KeyAdapter JavaDoc;
27 import java.awt.event.KeyEvent JavaDoc;
28 import java.util.Vector JavaDoc;
29 import java.util.ResourceBundle JavaDoc;
30
31 import javax.swing.*;
32 import javax.swing.border.*;
33 import org.openide.awt.Mnemonics;
34
35 //import org.openide.util.HelpCtx;
36
import org.openide.util.NbBundle;
37
38 /** A custom editor for array of Strings.
39 *
40 * @author Ian Formanek
41 */

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

79         getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(
80             StringArrayCustomEditor.class, "ACSD_StringArrayCustomEditor")); //NOI18N
81
itemField.getAccessibleContext().setAccessibleDescription(
82             NbBundle.getMessage(
83             StringArrayCustomEditor.class, "ACSD_CTL_Item")); //NOI18N
84
itemList.getAccessibleContext().setAccessibleDescription(
85             NbBundle.getMessage(
86             StringArrayCustomEditor.class, "ACSD_CTL_ItemList")); //NOI18N
87
addButton.getAccessibleContext().setAccessibleDescription(
88             NbBundle.getMessage(StringArrayCustomEditor.class,
89             "ACSD_CTL_Add_StringArrayCustomEditor")); //NOI18N
90
changeButton.getAccessibleContext().setAccessibleDescription(
91             NbBundle.getMessage(StringArrayCustomEditor.class,
92             "ACSD_CTL_Change_StringArrayCustomEditor")); //NOI18N
93
removeButton.getAccessibleContext().setAccessibleDescription(
94             NbBundle.getMessage(StringArrayCustomEditor.class,
95             "ACSD_CTL_Remove")); //NOI18N
96
moveUpButton.getAccessibleContext().setAccessibleDescription(
97             NbBundle.getMessage(StringArrayCustomEditor.class,
98             "ACSD_CTL_MoveUp")); //NOI18N
99
moveDownButton.getAccessibleContext().setAccessibleDescription(
100             NbBundle.getMessage(StringArrayCustomEditor.class,
101             "ACSD_CTL_MoveDown")); //NOI18N
102

103         updateButtons ();
104         itemField.addKeyListener(new KeyAdapter JavaDoc() {
105            public void keyReleased(KeyEvent JavaDoc event) {
106                 boolean containsCurrent = containsCurrent();
107                 String JavaDoc txt = itemField.getText().trim();
108                 boolean en = itemField.isEnabled() &&
109                     txt.length() > 0 &&
110                     !containsCurrent;
111                 addButton.setEnabled(en);
112                 changeButton.setEnabled(en && itemList.getSelectedIndex() != -1);
113                 if (containsCurrent) {
114                     itemList.setSelectedIndex(idxOfCurrent());
115                 }
116            }
117         });
118         itemField.addActionListener(new ActionListener JavaDoc() {
119             public void actionPerformed (ActionEvent JavaDoc ae) {
120                 if (addButton.isEnabled()) {
121                     addButtonActionPerformed(ae);
122                 }
123             }
124         });
125         addButton.setEnabled(false);
126         changeButton.setEnabled(false);
127 // HelpCtx.setHelpIDString (this, StringArrayCustomEditor.class.getName ());
128
setMinimumSize(new Dimension JavaDoc (200, 400));
129     }
130     
131     /** Determine if the text of the text field matches an item in the
132      * list */

133     private boolean containsCurrent() {
134         return idxOfCurrent() != -1;
135     }
136     
137     private int idxOfCurrent() {
138         String JavaDoc txt = itemField.getText().trim();
139         if (txt.length() > 0) {
140             int max = itemList.getModel().getSize();
141             for (int i=0; i < max; i++) {
142                 if (txt.equals(itemList.getModel().getElementAt(i))) return i;
143             }
144         }
145         return -1;
146     }
147
148     /** This method is called from within the constructor to
149      * initialize the form.
150      * WARNING: Do NOT modify this code. The content of this method is
151      * always regenerated by the FormEditor.
152      */

153     private void initComponents() {//GEN-BEGIN:initComponents
154
editPanel = new javax.swing.JPanel JavaDoc();
155         itemListScroll = new javax.swing.JScrollPane JavaDoc();
156         itemList = new javax.swing.JList JavaDoc();
157         itemLabel = new javax.swing.JLabel JavaDoc();
158         itemField = new javax.swing.JTextField JavaDoc();
159         itemListLabel = new javax.swing.JLabel JavaDoc();
160         buttonsPanel = new javax.swing.JPanel JavaDoc();
161         addButton = new javax.swing.JButton JavaDoc();
162         changeButton = new javax.swing.JButton JavaDoc();
163         removeButton = new javax.swing.JButton JavaDoc();
164         jSeparator1 = new javax.swing.JSeparator JavaDoc();
165         moveUpButton = new javax.swing.JButton JavaDoc();
166         moveDownButton = new javax.swing.JButton JavaDoc();
167         paddingPanel = new javax.swing.JPanel JavaDoc();
168
169         setLayout(new java.awt.BorderLayout JavaDoc());
170
171         editPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
172         java.awt.GridBagConstraints JavaDoc gridBagConstraints2;
173
174         itemList.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
175             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
176                 itemListValueChanged(evt);
177             }
178         });
179
180         itemListScroll.setViewportView(itemList);
181
182         gridBagConstraints2 = new java.awt.GridBagConstraints JavaDoc();
183         gridBagConstraints2.gridx = 0;
184         gridBagConstraints2.gridy = 2;
185         gridBagConstraints2.gridwidth = 2;
186         gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
187         gridBagConstraints2.weightx = 1.0;
188         gridBagConstraints2.weighty = 1.0;
189         editPanel.add(itemListScroll, gridBagConstraints2);
190
191         itemLabel.setText("item");
192         itemLabel.setLabelFor(itemField);
193         gridBagConstraints2 = new java.awt.GridBagConstraints JavaDoc();
194         gridBagConstraints2.insets = new java.awt.Insets JavaDoc(0, 0, 11, 12);
195         gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST;
196         editPanel.add(itemLabel, gridBagConstraints2);
197
198         gridBagConstraints2 = new java.awt.GridBagConstraints JavaDoc();
199         gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
200         gridBagConstraints2.insets = new java.awt.Insets JavaDoc(0, 0, 11, 0);
201         editPanel.add(itemField, gridBagConstraints2);
202
203         itemListLabel.setText("jLabel1");
204         itemListLabel.setLabelFor(itemList);
205         gridBagConstraints2 = new java.awt.GridBagConstraints JavaDoc();
206         gridBagConstraints2.gridx = 0;
207         gridBagConstraints2.gridy = 1;
208         gridBagConstraints2.gridwidth = 2;
209         gridBagConstraints2.insets = new java.awt.Insets JavaDoc(0, 0, 2, 0);
210         gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST;
211         editPanel.add(itemListLabel, gridBagConstraints2);
212
213         add(editPanel, java.awt.BorderLayout.CENTER);
214
215         buttonsPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
216         java.awt.GridBagConstraints JavaDoc gridBagConstraints1;
217
218         addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
219             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
220                 addButtonActionPerformed(evt);
221             }
222         });
223
224         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
225         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
226         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
227         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(0, 8, 0, 8);
228         gridBagConstraints1.weightx = 1.0;
229         buttonsPanel.add(addButton, gridBagConstraints1);
230
231         changeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
232             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
233                 changeButtonActionPerformed(evt);
234             }
235         });
236
237         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
238         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
239         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
240         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(8, 8, 0, 8);
241         gridBagConstraints1.weightx = 1.0;
242         buttonsPanel.add(changeButton, gridBagConstraints1);
243
244         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
245             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
246                 removeButtonActionPerformed(evt);
247             }
248         });
249
250         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
251         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
252         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
253         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(8, 8, 8, 8);
254         gridBagConstraints1.weightx = 1.0;
255         buttonsPanel.add(removeButton, gridBagConstraints1);
256
257         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
258         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
259         gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
260         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(0, 4, 0, 4);
261         buttonsPanel.add(jSeparator1, gridBagConstraints1);
262
263         moveUpButton.setEnabled(false);
264         moveUpButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
265             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
266                 moveUpButtonActionPerformed(evt);
267             }
268         });
269
270         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
271         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
272         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
273         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(8, 8, 0, 8);
274         gridBagConstraints1.weightx = 1.0;
275         buttonsPanel.add(moveUpButton, gridBagConstraints1);
276
277         moveDownButton.setEnabled(false);
278         moveDownButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
279             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
280                 moveDownButtonActionPerformed(evt);
281             }
282         });
283
284         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
285         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
286         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
287         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(8, 8, 0, 8);
288         gridBagConstraints1.weightx = 1.0;
289         buttonsPanel.add(moveDownButton, gridBagConstraints1);
290
291         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
292         gridBagConstraints1.weighty = 1.0;
293         buttonsPanel.add(paddingPanel, gridBagConstraints1);
294
295         add(buttonsPanel, java.awt.BorderLayout.EAST);
296
297     }//GEN-END:initComponents
298

299     private void changeButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_changeButtonActionPerformed
300
int sel = itemList.getSelectedIndex ();
301         String JavaDoc s = itemsVector.elementAt(sel);
302         itemsVector.removeElementAt (sel);
303         itemsVector.insertElementAt (itemField.getText (), sel);
304         itemList.setListData (itemsVector);
305         itemList.setSelectedIndex (sel);
306         itemList.repaint ();
307         updateValue ();
308     }//GEN-LAST:event_changeButtonActionPerformed
309

310     private void moveDownButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_moveDownButtonActionPerformed
311
int sel = itemList.getSelectedIndex ();
312         String JavaDoc s = itemsVector.elementAt(sel);
313         itemsVector.removeElementAt (sel);
314         itemsVector.insertElementAt (s, sel + 1);
315         itemList.setListData (itemsVector);
316         itemList.setSelectedIndex (sel + 1);
317         itemList.repaint ();
318         updateValue ();
319     }//GEN-LAST:event_moveDownButtonActionPerformed
320

321     private void moveUpButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_moveUpButtonActionPerformed
322
int sel = itemList.getSelectedIndex ();
323         String JavaDoc s = itemsVector.elementAt(sel);
324         itemsVector.removeElementAt (sel);
325         itemsVector.insertElementAt (s, sel - 1);
326         itemList.setListData (itemsVector);
327         itemList.setSelectedIndex (sel - 1);
328         itemList.repaint ();
329         updateValue ();
330     }//GEN-LAST:event_moveUpButtonActionPerformed
331

332     private void removeButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
333
// Add your handling code here:
334
int currentIndex = itemList.getSelectedIndex ();
335         itemsVector.removeElementAt (currentIndex);
336         itemList.setListData (itemsVector);
337
338         // set new selection
339
if (itemsVector.size () != 0) {
340             if (currentIndex >= itemsVector.size ())
341                 currentIndex = itemsVector.size () - 1;
342             itemList.setSelectedIndex (currentIndex);
343         }
344
345         itemList.repaint ();
346
347         updateValue ();
348     }//GEN-LAST:event_removeButtonActionPerformed
349

350     private void itemListValueChanged (javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_itemListValueChanged
351
// Add your handling code here:
352
updateButtons ();
353         int sel = itemList.getSelectedIndex ();
354         if (sel != -1) {
355             itemField.setText(itemsVector.elementAt(sel));
356         }
357     }//GEN-LAST:event_itemListValueChanged
358

359     private void addButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
360
// Add your handling code here:
361
itemsVector.addElement (itemField.getText ());
362         itemList.setListData (itemsVector);
363         itemList.setSelectedIndex (itemsVector.size () - 1);
364         itemList.repaint ();
365         updateValue ();
366     }//GEN-LAST:event_addButtonActionPerformed
367

368     public void setEnabled (boolean val) {
369         Component JavaDoc[] c = getComponents();
370         super.setEnabled(val);
371         setChildrenEnabled (this, val);
372     }
373     
374     private void setChildrenEnabled(JPanel parent, boolean val) {
375         Component JavaDoc[] c = parent.getComponents();
376         for (int i=0; i < c.length; i++) {
377             c[i].setEnabled(val);
378             if (c[i] instanceof JPanel) {
379                 setChildrenEnabled((JPanel) c[i], val);
380             }
381         }
382     }
383     
384     private void updateButtons () {
385         int sel = itemList.getSelectedIndex ();
386         boolean enVal = isEnabled();
387         if (sel == -1) {
388             removeButton.setEnabled (false);
389             moveUpButton.setEnabled (false);
390             moveDownButton.setEnabled (false);
391             changeButton.setEnabled (false);
392         } else {
393             removeButton.setEnabled (enVal && true);
394             moveUpButton.setEnabled (enVal && (sel != 0));
395             moveDownButton.setEnabled (enVal && (sel != itemsVector.size () - 1));
396             changeButton.setEnabled (enVal && true);
397         }
398         itemField.setEnabled(enVal);
399         // #62803: String[] editor keeps text in the textfield after removing all items
400
boolean containsCurrent = containsCurrent();
401         String JavaDoc txt = itemField.getText().trim();
402         boolean en = itemField.isEnabled() &&
403             txt.length() > 0 &&
404             !containsCurrent;
405         addButton.setEnabled(en);
406     }
407
408     private void updateValue () {
409         String JavaDoc [] value = new String JavaDoc [itemsVector.size()];
410         itemsVector.copyInto (value);
411         editor.setStringArray (value);
412     }
413
414     // Variables declaration - do not modify//GEN-BEGIN:variables
415
private javax.swing.JPanel JavaDoc editPanel;
416     private javax.swing.JScrollPane JavaDoc itemListScroll;
417     private javax.swing.JList JavaDoc itemList;
418     private javax.swing.JLabel JavaDoc itemLabel;
419     private javax.swing.JTextField JavaDoc itemField;
420     private javax.swing.JLabel JavaDoc itemListLabel;
421     private javax.swing.JPanel JavaDoc buttonsPanel;
422     private javax.swing.JButton JavaDoc addButton;
423     private javax.swing.JButton JavaDoc changeButton;
424     private javax.swing.JButton JavaDoc removeButton;
425     private javax.swing.JSeparator JavaDoc jSeparator1;
426     private javax.swing.JButton JavaDoc moveUpButton;
427     private javax.swing.JButton JavaDoc moveDownButton;
428     private javax.swing.JPanel JavaDoc paddingPanel;
429     // End of variables declaration//GEN-END:variables
430

431     static class EmptyStringListCellRenderer extends JLabel implements ListCellRenderer {
432
433         protected static Border hasFocusBorder;
434         protected static Border noFocusBorder;
435
436         static {
437             hasFocusBorder = new LineBorder(UIManager.getColor("List.focusCellHighlight")); // NOI18N
438
noFocusBorder = new EmptyBorder(1, 1, 1, 1);
439         }
440
441         static final long serialVersionUID =487512296465844339L;
442         /** Creates a new NodeListCellRenderer */
443         public EmptyStringListCellRenderer () {
444             setOpaque (true);
445             setBorder (noFocusBorder);
446         }
447
448         /** This is the only method defined by ListCellRenderer. We just
449         * reconfigure the Jlabel each time we're called.
450         */

451         public java.awt.Component JavaDoc getListCellRendererComponent(
452             JList list,
453             Object JavaDoc value, // value to display
454
int index, // cell index
455
boolean isSelected, // is the cell selected
456
boolean cellHasFocus) // the list and the cell have the focus
457
{
458             if (!(value instanceof String JavaDoc)) return this;
459             String JavaDoc text = (String JavaDoc)value;
460             if ("".equals (text)) text = NbBundle.getMessage(StringArrayCustomEditor.class, "CTL_Empty");
461
462             setText(text);
463             if (isSelected){
464                 setBackground(UIManager.getColor("List.selectionBackground")); // NOI18N
465
setForeground(UIManager.getColor("List.selectionForeground")); // NOI18N
466
}
467             else {
468                 setBackground(list.getBackground());
469                 setForeground(list.getForeground());
470             }
471
472             setBorder(cellHasFocus ? hasFocusBorder : noFocusBorder);
473
474             return this;
475         }
476     }
477 }
478
Popular Tags