2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.beaninfo.editors;
44
import java.awt.event.ActionEvent;
45
import java.awt.event.ActionListener;
46
import java.awt.event.KeyAdapter;
47
import java.awt.event.KeyEvent;
48
import java.util.ArrayList;
49
import java.util.Enumeration;
50
import java.util.List;
51
import java.util.ResourceBundle;
52
import java.util.StringTokenizer;
55
import javax.swing.border.*;
56
import org.openide.loaders.ExtensionList;
58
import org.openide.util.NbBundle;
60
/** A custom editor for array of Strings.
62
* @author Ian Formanek
64
public class ExtensionListCustomEditor extends javax.swing.JPanel {
66
private ExtensionList value;
67
private ExtensionListEditor editor;
69
static final long serialVersionUID =-4347656479280614636L;
71
@SuppressWarnings("unchecked")
72
private String[] getStrings() {
73
List<String> l = new ArrayList<String> ();
74
if (value == null) return new String[0];
76
Enumeration<String> e = (Enumeration<String>)value.extensions ();
77
while (e.hasMoreElements ()) l.add (e.nextElement ());
79
e = (Enumeration<String>)value.mimeTypes ();
80
while (e.hasMoreElements ()) l.add (e.nextElement ());
82
return l.toArray (new String[l.size ()]);
85
/** Initializes the Form */
86
public ExtensionListCustomEditor(ExtensionListEditor ed) {
88
value = (ExtensionList)((ExtensionList)editor.getValue()).clone ();
90
itemList.setCellRenderer (new EmptyStringListCellRenderer ());
91
itemList.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
92
itemList.setListData (getStrings());
94
ResourceBundle bundle = NbBundle.getBundle (
95
ExtensionListCustomEditor.class);
97
getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ELCE"));
98
itemField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ELCE_Item"));
99
itemList.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ELCE_ItemList"));
100
addButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ELCE_Add"));
101
changeButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ELCE_Change"));
102
removeButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ELCE_Remove"));
104
if ( ! editor.isEditable() ) {
106
itemField.setEnabled( false );
107
addButton.setEnabled( false );
108
changeButton.setEnabled( false );
109
removeButton.setEnabled( false );
112
itemField.addKeyListener(new KeyAdapter() {
113
public void keyReleased(KeyEvent event) {
114
boolean containsCurrent = containsCurrent();
115
String txt = itemField.getText().trim();
116
boolean en = itemField.isEnabled() &&
119
addButton.setEnabled(en);
120
changeButton.setEnabled(en && itemList.getSelectedIndex() != -1);
121
if (containsCurrent) {
122
itemList.setSelectedIndex(idxOfCurrent());
126
itemField.addActionListener(new ActionListener() {
127
public void actionPerformed (ActionEvent ae) {
128
if (addButton.isEnabled()) {
133
addButton.setEnabled(false);
134
changeButton.setEnabled(false);
137
/** Determine if the text of the text field matches an item in the
139
private boolean containsCurrent() {
140
return idxOfCurrent() != -1;
143
private int idxOfCurrent() {
144
String txt = itemField.getText().trim();
145
if (txt.length() > 0) {
146
int max = itemList.getModel().getSize();
147
for (int i=0; i < max; i++) {
148
if (txt.equals(itemList.getModel().getElementAt(i))) return i;
154
/** This method is called from within the constructor to
155
* initialize the form.
156
* WARNING: Do NOT modify this code. The content of this method is
157
* always regenerated by the FormEditor.
159
private void initComponents() {//GEN-BEGIN:initComponents
160
java.awt.GridBagConstraints gridBagConstraints;
162
addButton = new javax.swing.JButton();
163
changeButton = new javax.swing.JButton();
164
removeButton = new javax.swing.JButton();
165
itemListScroll = new javax.swing.JScrollPane();
166
itemList = new javax.swing.JList();
167
itemLabel = new javax.swing.JLabel();
168
itemField = new javax.swing.JTextField();
169
itemListLabel = new javax.swing.JLabel();
171
setLayout(new java.awt.GridBagLayout());
173
org.openide.awt.Mnemonics.setLocalizedText(addButton, org.openide.util.NbBundle.getMessage(ExtensionListCustomEditor.class, "CTL_ELCE_Add", new Object[] {}));
174
addButton.addActionListener(new java.awt.event.ActionListener() {
175
public void actionPerformed(java.awt.event.ActionEvent evt) {
176
addButtonActionPerformed(evt);
180
gridBagConstraints = new java.awt.GridBagConstraints();
181
gridBagConstraints.gridx = 3;
182
gridBagConstraints.gridy = 0;
183
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
184
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
185
gridBagConstraints.insets = new java.awt.Insets(4, 8, 0, 8);
186
add(addButton, gridBagConstraints);
188
org.openide.awt.Mnemonics.setLocalizedText(changeButton, org.openide.util.NbBundle.getMessage(ExtensionListCustomEditor.class, "CTL_ELCE_Change", new Object[] {}));
189
changeButton.addActionListener(new java.awt.event.ActionListener() {
190
public void actionPerformed(java.awt.event.ActionEvent evt) {
191
changeButtonActionPerformed(evt);
195
gridBagConstraints = new java.awt.GridBagConstraints();
196
gridBagConstraints.gridx = 3;
197
gridBagConstraints.gridy = 1;
198
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
199
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
200
gridBagConstraints.insets = new java.awt.Insets(8, 8, 0, 8);
201
add(changeButton, gridBagConstraints);
203
org.openide.awt.Mnemonics.setLocalizedText(removeButton, org.openide.util.NbBundle.getMessage(ExtensionListCustomEditor.class, "CTL_ELCE_Remove", new Object[] {}));
204
removeButton.addActionListener(new java.awt.event.ActionListener() {
205
public void actionPerformed(java.awt.event.ActionEvent evt) {
206
removeButtonActionPerformed(evt);
210
gridBagConstraints = new java.awt.GridBagConstraints();
211
gridBagConstraints.gridx = 3;
212
gridBagConstraints.gridy = 2;
213
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
214
gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 8);
215
add(removeButton, gridBagConstraints);
217
itemList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
218
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
219
itemListValueChanged(evt);
223
itemListScroll.setViewportView(itemList);
225
gridBagConstraints = new java.awt.GridBagConstraints();
226
gridBagConstraints.gridx = 0;
227
gridBagConstraints.gridy = 2;
228
gridBagConstraints.gridwidth = 2;
229
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
230
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
231
gridBagConstraints.insets = new java.awt.Insets(0, 8, 0, 0);
232
add(itemListScroll, gridBagConstraints);
234
itemLabel.setLabelFor(itemField);
235
org.openide.awt.Mnemonics.setLocalizedText(itemLabel, org.openide.util.NbBundle.getMessage(ExtensionListCustomEditor.class, "CTL_ELCE_Item", new Object[] {}));
236
gridBagConstraints = new java.awt.GridBagConstraints();
237
gridBagConstraints.gridx = 0;
238
gridBagConstraints.gridy = 0;
239
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
240
gridBagConstraints.insets = new java.awt.Insets(4, 8, 0, 8);
241
add(itemLabel, gridBagConstraints);
243
itemField.setColumns(10);
244
gridBagConstraints = new java.awt.GridBagConstraints();
245
gridBagConstraints.gridx = 1;
246
gridBagConstraints.gridy = 0;
247
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
248
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
249
gridBagConstraints.weightx = 1.0;
250
gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
251
add(itemField, gridBagConstraints);
253
itemListLabel.setLabelFor(itemList);
254
org.openide.awt.Mnemonics.setLocalizedText(itemListLabel, org.openide.util.NbBundle.getMessage(ExtensionListCustomEditor.class, "CTL_ItemList", new Object[] {}));
255
gridBagConstraints = new java.awt.GridBagConstraints();
256
gridBagConstraints.gridx = 0;
257
gridBagConstraints.gridy = 1;
258
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
259
gridBagConstraints.insets = new java.awt.Insets(0, 8, 0, 0);
260
add(itemListLabel, gridBagConstraints);
262
}//GEN-END:initComponents
264
private String addTexts() {
265
StringTokenizer st = new StringTokenizer (itemField.getText (), ",. \n\t"); // NOI18N
267
while(st.hasMoreTokens()) {
268
last = st.nextToken();
269
if (last.indexOf('/') >= 0) { // mime type!?
270
value.addMimeType(last);
272
value.addExtension (last);
278
private void changeButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changeButtonActionPerformed
279
int sel = itemList.getSelectedIndex ();
280
String s = (String) itemList.getModel().getElementAt(sel);
281
if (s.indexOf('/') >= 0) { // mime type!?
282
value.removeMimeType(s);
284
value.removeExtension(s);
287
}//GEN-LAST:event_changeButtonActionPerformed
289
private int indexOf(String[] array, String item) {
290
for (int i = 0; i<array.length; i++) {
291
if (array[i].equals(item)) return i;
296
private void removeButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
297
int sel = itemList.getSelectedIndex ();
298
String s = (String) itemList.getModel().getElementAt(sel);
299
if (s.indexOf('/') >= 0) { // mime type!?
300
value.removeMimeType(s);
302
value.removeExtension(s);
304
itemList.setListData (getStrings());
306
int count = itemList.getModel().getSize();
309
if (sel >= count) sel = count - 1;
310
itemList.setSelectedIndex (sel);
315
}//GEN-LAST:event_removeButtonActionPerformed
317
private void itemListValueChanged (javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_itemListValueChanged
318
// Add your handling code here:
320
int sel = itemList.getSelectedIndex ();
322
itemField.setText ((String) itemList.getModel().getElementAt(sel));
323
changeButton.setEnabled(false);
324
addButton.setEnabled (false);
326
}//GEN-LAST:event_itemListValueChanged
328
private void doAdd() {
329
String last = addTexts();
330
String[] values = getStrings();
331
int index = indexOf(values, last);
332
itemList.setListData (values);
333
if (index >= 0) itemList.setSelectedIndex (index);
338
private void addButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
340
}//GEN-LAST:event_addButtonActionPerformed
342
private void updateButtons () {
343
int sel = itemList.getSelectedIndex ();
344
if (sel == -1 || !editor.isEditable()) {
345
removeButton.setEnabled (false);
346
changeButton.setEnabled (false);
348
removeButton.setEnabled (true);
349
changeButton.setEnabled (true);
351
// #62803: String[] editor keeps text in the textfield after removing all items
352
boolean containsCurrent = containsCurrent();
353
String txt = itemField.getText().trim();
354
boolean en = itemField.isEnabled() &&
357
addButton.setEnabled(en);
360
private void updateValue () {
361
editor.setValue(value);
364
// Variables declaration - do not modify//GEN-BEGIN:variables
365
private javax.swing.JButton addButton;
366
private javax.swing.JButton changeButton;
367
private javax.swing.JTextField itemField;
368
private javax.swing.JLabel itemLabel;
369
private javax.swing.JList itemList;
370
private javax.swing.JLabel itemListLabel;
371
private javax.swing.JScrollPane itemListScroll;
372
private javax.swing.JButton removeButton;
373
// End of variables declaration//GEN-END:variables
375
static class EmptyStringListCellRenderer extends JLabel implements ListCellRenderer {
377
protected static Border hasFocusBorder;
378
protected static Border noFocusBorder;
381
hasFocusBorder = new LineBorder(UIManager.getColor("List.focusCellHighlight")); // NOI18N
382
noFocusBorder = new EmptyBorder(1, 1, 1, 1);
385
static final long serialVersionUID =487512296465844339L;
386
/** Creates a new NodeListCellRenderer */
387
public EmptyStringListCellRenderer () {
389
setBorder (noFocusBorder);
392
/** This is the only method defined by ListCellRenderer. We just
393
* reconfigure the Jlabel each time we're called.
395
public java.awt.Component getListCellRendererComponent(
397
Object value, // value to display
398
int index, // cell index
399
boolean isSelected, // is the cell selected
400
boolean cellHasFocus) // the list and the cell have the focus
402
if (!(value instanceof String)) return this;
403
String text = (String)value;
404
if ("".equals (text)) text = NbBundle.getMessage (
405
ExtensionListCustomEditor.class, "CTL_ELCE_Empty");
409
setBackground(UIManager.getColor("List.selectionBackground")); // NOI18N
410
setForeground(UIManager.getColor("List.selectionForeground")); // NOI18N
413
setBackground(list.getBackground());
414
setForeground(list.getForeground());
417
setBorder(cellHasFocus ? hasFocusBorder : noFocusBorder);