KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > beaninfo > editors > ExtensionListCustomEditor


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.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.awt.event.KeyAdapter JavaDoc;
25 import java.awt.event.KeyEvent JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.ResourceBundle JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31
32 import javax.swing.*;
33 import javax.swing.border.*;
34 import org.openide.loaders.ExtensionList;
35
36 import org.openide.util.NbBundle;
37
38 /** A custom editor for array of Strings.
39 *
40 * @author Ian Formanek
41 */

42 public class ExtensionListCustomEditor extends javax.swing.JPanel JavaDoc {
43
44     private ExtensionList value;
45     private ExtensionListEditor editor;
46     
47     static final long serialVersionUID =-4347656479280614636L;
48     
49     @SuppressWarnings JavaDoc("unchecked")
50     private String JavaDoc[] getStrings() {
51         List JavaDoc<String JavaDoc> l = new ArrayList JavaDoc<String JavaDoc> ();
52         if (value == null) return new String JavaDoc[0];
53
54         Enumeration JavaDoc<String JavaDoc> e = (Enumeration JavaDoc<String JavaDoc>)value.extensions ();
55         while (e.hasMoreElements ()) l.add (e.nextElement ());
56                 
57         e = (Enumeration JavaDoc<String JavaDoc>)value.mimeTypes ();
58         while (e.hasMoreElements ()) l.add (e.nextElement ());
59         
60         return l.toArray (new String JavaDoc[l.size ()]);
61     }
62
63     /** Initializes the Form */
64     public ExtensionListCustomEditor(ExtensionListEditor ed) {
65         editor = ed;
66         value = (ExtensionList)((ExtensionList)editor.getValue()).clone ();
67         initComponents ();
68         itemList.setCellRenderer (new EmptyStringListCellRenderer ());
69         itemList.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
70         itemList.setListData (getStrings());
71
72         ResourceBundle JavaDoc bundle = NbBundle.getBundle (
73                                        ExtensionListCustomEditor.class);
74
75         getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ELCE"));
76         itemField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ELCE_Item"));
77         itemList.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ELCE_ItemList"));
78         addButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ELCE_Add"));
79         changeButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ELCE_Change"));
80         removeButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ELCE_Remove"));
81
82         if ( ! editor.isEditable() ) {
83             // set read-only
84
itemField.setEnabled( false );
85             addButton.setEnabled( false );
86             changeButton.setEnabled( false );
87             removeButton.setEnabled( false );
88         }
89         updateButtons ();
90         itemField.addKeyListener(new KeyAdapter JavaDoc() {
91            public void keyReleased(KeyEvent JavaDoc event) {
92                 boolean containsCurrent = containsCurrent();
93                 String JavaDoc txt = itemField.getText().trim();
94                 boolean en = itemField.isEnabled() &&
95                     txt.length() > 0 &&
96                     !containsCurrent;
97                 addButton.setEnabled(en);
98                 changeButton.setEnabled(en && itemList.getSelectedIndex() != -1);
99                 if (containsCurrent) {
100                     itemList.setSelectedIndex(idxOfCurrent());
101                 }
102            }
103         });
104         itemField.addActionListener(new ActionListener JavaDoc() {
105             public void actionPerformed (ActionEvent JavaDoc ae) {
106                 if (addButton.isEnabled()) {
107                     doAdd();
108                 }
109             }
110         });
111         addButton.setEnabled(false);
112         changeButton.setEnabled(false);
113     }
114     
115     /** Determine if the text of the text field matches an item in the
116      * list */

117     private boolean containsCurrent() {
118         return idxOfCurrent() != -1;
119     }
120     
121     private int idxOfCurrent() {
122         String JavaDoc txt = itemField.getText().trim();
123         if (txt.length() > 0) {
124             int max = itemList.getModel().getSize();
125             for (int i=0; i < max; i++) {
126                 if (txt.equals(itemList.getModel().getElementAt(i))) return i;
127             }
128         }
129         return -1;
130     }
131
132     /** This method is called from within the constructor to
133      * initialize the form.
134      * WARNING: Do NOT modify this code. The content of this method is
135      * always regenerated by the FormEditor.
136      */

137     private void initComponents() {//GEN-BEGIN:initComponents
138
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
139
140         addButton = new javax.swing.JButton JavaDoc();
141         changeButton = new javax.swing.JButton JavaDoc();
142         removeButton = new javax.swing.JButton JavaDoc();
143         itemListScroll = new javax.swing.JScrollPane JavaDoc();
144         itemList = new javax.swing.JList JavaDoc();
145         itemLabel = new javax.swing.JLabel JavaDoc();
146         itemField = new javax.swing.JTextField JavaDoc();
147         itemListLabel = new javax.swing.JLabel JavaDoc();
148
149         setLayout(new java.awt.GridBagLayout JavaDoc());
150
151         org.openide.awt.Mnemonics.setLocalizedText(addButton, org.openide.util.NbBundle.getMessage(ExtensionListCustomEditor.class, "CTL_ELCE_Add", new Object JavaDoc[] {}));
152         addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
153             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
154                 addButtonActionPerformed(evt);
155             }
156         });
157
158         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
159         gridBagConstraints.gridx = 3;
160         gridBagConstraints.gridy = 0;
161         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
162         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
163         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 8, 0, 8);
164         add(addButton, gridBagConstraints);
165
166         org.openide.awt.Mnemonics.setLocalizedText(changeButton, org.openide.util.NbBundle.getMessage(ExtensionListCustomEditor.class, "CTL_ELCE_Change", new Object JavaDoc[] {}));
167         changeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
168             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
169                 changeButtonActionPerformed(evt);
170             }
171         });
172
173         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
174         gridBagConstraints.gridx = 3;
175         gridBagConstraints.gridy = 1;
176         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
177         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
178         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 8, 0, 8);
179         add(changeButton, gridBagConstraints);
180
181         org.openide.awt.Mnemonics.setLocalizedText(removeButton, org.openide.util.NbBundle.getMessage(ExtensionListCustomEditor.class, "CTL_ELCE_Remove", new Object JavaDoc[] {}));
182         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
183             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
184                 removeButtonActionPerformed(evt);
185             }
186         });
187
188         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
189         gridBagConstraints.gridx = 3;
190         gridBagConstraints.gridy = 2;
191         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
192         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 8, 8, 8);
193         add(removeButton, gridBagConstraints);
194
195         itemList.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
196             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
197                 itemListValueChanged(evt);
198             }
199         });
200
201         itemListScroll.setViewportView(itemList);
202
203         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
204         gridBagConstraints.gridx = 0;
205         gridBagConstraints.gridy = 2;
206         gridBagConstraints.gridwidth = 2;
207         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
208         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
209         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 8, 0, 0);
210         add(itemListScroll, gridBagConstraints);
211
212         itemLabel.setLabelFor(itemField);
213         org.openide.awt.Mnemonics.setLocalizedText(itemLabel, org.openide.util.NbBundle.getMessage(ExtensionListCustomEditor.class, "CTL_ELCE_Item", new Object JavaDoc[] {}));
214         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
215         gridBagConstraints.gridx = 0;
216         gridBagConstraints.gridy = 0;
217         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
218         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 8, 0, 8);
219         add(itemLabel, gridBagConstraints);
220
221         itemField.setColumns(10);
222         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
223         gridBagConstraints.gridx = 1;
224         gridBagConstraints.gridy = 0;
225         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
226         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
227         gridBagConstraints.weightx = 1.0;
228         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 0, 0, 0);
229         add(itemField, gridBagConstraints);
230
231         itemListLabel.setLabelFor(itemList);
232         org.openide.awt.Mnemonics.setLocalizedText(itemListLabel, org.openide.util.NbBundle.getMessage(ExtensionListCustomEditor.class, "CTL_ItemList", new Object JavaDoc[] {}));
233         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
234         gridBagConstraints.gridx = 0;
235         gridBagConstraints.gridy = 1;
236         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
237         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 8, 0, 0);
238         add(itemListLabel, gridBagConstraints);
239
240     }//GEN-END:initComponents
241

242     private String JavaDoc addTexts() {
243         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (itemField.getText (), ",. \n\t"); // NOI18N
244
String JavaDoc last = null;
245         while(st.hasMoreTokens()) {
246             last = st.nextToken();
247             if (last.indexOf('/') >= 0) { // mime type!?
248
value.addMimeType(last);
249             } else {
250                 value.addExtension (last);
251             }
252         }
253         return last;
254     }
255     
256     private void changeButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_changeButtonActionPerformed
257
int sel = itemList.getSelectedIndex ();
258         String JavaDoc s = (String JavaDoc) itemList.getModel().getElementAt(sel);
259         if (s.indexOf('/') >= 0) { // mime type!?
260
value.removeMimeType(s);
261         } else {
262             value.removeExtension(s);
263         }
264         doAdd();
265     }//GEN-LAST:event_changeButtonActionPerformed
266

267     private int indexOf(String JavaDoc[] array, String JavaDoc item) {
268         for (int i = 0; i<array.length; i++) {
269             if (array[i].equals(item)) return i;
270         }
271         return -1;
272     }
273     
274     private void removeButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
275
int sel = itemList.getSelectedIndex ();
276         String JavaDoc s = (String JavaDoc) itemList.getModel().getElementAt(sel);
277         if (s.indexOf('/') >= 0) { // mime type!?
278
value.removeMimeType(s);
279         } else {
280             value.removeExtension(s);
281         }
282         itemList.setListData (getStrings());
283
284         int count = itemList.getModel().getSize();
285         // set new selection
286
if (count != 0) {
287             if (sel >= count) sel = count - 1;
288             itemList.setSelectedIndex (sel);
289         }
290
291         itemList.repaint ();
292         updateValue ();
293     }//GEN-LAST:event_removeButtonActionPerformed
294

295     private void itemListValueChanged (javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_itemListValueChanged
296
// Add your handling code here:
297
updateButtons ();
298         int sel = itemList.getSelectedIndex ();
299         if (sel != -1) {
300             itemField.setText ((String JavaDoc) itemList.getModel().getElementAt(sel));
301             changeButton.setEnabled(false);
302             addButton.setEnabled (false);
303         }
304     }//GEN-LAST:event_itemListValueChanged
305

306     private void doAdd() {
307         String JavaDoc last = addTexts();
308         String JavaDoc[] values = getStrings();
309         int index = indexOf(values, last);
310         itemList.setListData (values);
311         if (index >= 0) itemList.setSelectedIndex (index);
312         itemList.repaint ();
313         updateValue ();
314     }
315     
316     private void addButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
317
doAdd();
318     }//GEN-LAST:event_addButtonActionPerformed
319

320     private void updateButtons () {
321         int sel = itemList.getSelectedIndex ();
322         if (sel == -1 || !editor.isEditable()) {
323             removeButton.setEnabled (false);
324             changeButton.setEnabled (false);
325         } else {
326             removeButton.setEnabled (true);
327             changeButton.setEnabled (true);
328         }
329         // #62803: String[] editor keeps text in the textfield after removing all items
330
boolean containsCurrent = containsCurrent();
331         String JavaDoc txt = itemField.getText().trim();
332         boolean en = itemField.isEnabled() &&
333             txt.length() > 0 &&
334             !containsCurrent;
335         addButton.setEnabled(en);
336     }
337
338     private void updateValue () {
339         editor.setValue(value);
340     }
341
342     // Variables declaration - do not modify//GEN-BEGIN:variables
343
private javax.swing.JButton JavaDoc addButton;
344     private javax.swing.JButton JavaDoc changeButton;
345     private javax.swing.JTextField JavaDoc itemField;
346     private javax.swing.JLabel JavaDoc itemLabel;
347     private javax.swing.JList JavaDoc itemList;
348     private javax.swing.JLabel JavaDoc itemListLabel;
349     private javax.swing.JScrollPane JavaDoc itemListScroll;
350     private javax.swing.JButton JavaDoc removeButton;
351     // End of variables declaration//GEN-END:variables
352

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

373         public java.awt.Component JavaDoc getListCellRendererComponent(
374             JList list,
375             Object JavaDoc value, // value to display
376
int index, // cell index
377
boolean isSelected, // is the cell selected
378
boolean cellHasFocus) // the list and the cell have the focus
379
{
380             if (!(value instanceof String JavaDoc)) return this;
381             String JavaDoc text = (String JavaDoc)value;
382             if ("".equals (text)) text = NbBundle.getMessage (
383                     ExtensionListCustomEditor.class, "CTL_ELCE_Empty");
384
385             setText(text);
386             if (isSelected){
387                 setBackground(UIManager.getColor("List.selectionBackground")); // NOI18N
388
setForeground(UIManager.getColor("List.selectionForeground")); // NOI18N
389
}
390             else {
391                 setBackground(list.getBackground());
392                 setForeground(list.getForeground());
393             }
394
395             setBorder(cellHasFocus ? hasFocusBorder : noFocusBorder);
396
397             return this;
398         }
399     }
400 }
401
Popular Tags