001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.form.editors;
043:
044: import java.util.Vector;
045: import java.util.ResourceBundle;
046:
047: import javax.swing.*;
048: import javax.swing.border.*;
049:
050: //import org.openide.util.HelpCtx;
051: import org.openide.util.NbBundle;
052:
053: /** A custom editor for array of Strings.
054: *
055: * @author Ian Formanek
056: */
057: public class StringArrayCustomEditor extends javax.swing.JPanel {
058:
059: // the bundle to use
060: private ResourceBundle bundle = NbBundle
061: .getBundle(StringArrayCustomEditor.class);
062:
063: private Vector itemsVector;
064: private StringArrayCustomizable editor;
065:
066: private final static int DEFAULT_WIDTH = 400;
067:
068: static final long serialVersionUID = -4347656479280614636L;
069:
070: /** Initializes the Form */
071: public StringArrayCustomEditor(StringArrayCustomizable sac) {
072: editor = sac;
073: itemsVector = new Vector();
074: String[] array = editor.getStringArray();
075: if (array != null)
076: for (int i = 0; i < array.length; i++)
077: itemsVector.addElement(array[i]);
078: initComponents();
079: itemList.setCellRenderer(new EmptyStringListCellRenderer());
080: itemList.setListData(itemsVector);
081: itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
082:
083: setBorder(new javax.swing.border.EmptyBorder(
084: new java.awt.Insets(16, 8, 8, 0)));
085: buttonsPanel.setBorder(new javax.swing.border.EmptyBorder(
086: new java.awt.Insets(0, 5, 5, 5)));
087:
088: itemLabel.setText(bundle.getString("CTL_Item"));
089: itemListLabel.setText(bundle.getString("CTL_ItemList"));
090: addButton.setText(bundle
091: .getString("CTL_Add_StringArrayCustomEditor"));
092: changeButton.setText(bundle
093: .getString("CTL_Change_StringArrayCustomEditor"));
094: removeButton.setText(bundle.getString("CTL_Remove"));
095: moveUpButton.setText(bundle.getString("CTL_MoveUp"));
096: moveDownButton.setText(bundle.getString("CTL_MoveDown"));
097:
098: itemLabel.setDisplayedMnemonic(bundle.getString(
099: "CTL_Item_Mnemonic").charAt(0));
100: itemListLabel.setDisplayedMnemonic(bundle.getString(
101: "CTL_ItemList_Mnemonic").charAt(0));
102: addButton.setMnemonic(bundle.getString(
103: "CTL_Add_StringArrayCustomEditor_Mnemonic").charAt(0));
104: changeButton.setMnemonic(bundle.getString(
105: "CTL_Change_StringArrayCustomEditor_Mnemonic")
106: .charAt(0));
107: removeButton.setMnemonic(bundle
108: .getString("CTL_Remove_Mnemonic").charAt(0));
109: moveUpButton.setMnemonic(bundle
110: .getString("CTL_MoveUp_Mnemonic").charAt(0));
111: moveDownButton.setMnemonic(bundle.getString(
112: "CTL_MoveDown_Mnemonic").charAt(0));
113:
114: getAccessibleContext().setAccessibleDescription(
115: bundle.getString("ACSD_StringArrayCustomEditor"));
116: itemField.getAccessibleContext().setAccessibleDescription(
117: bundle.getString("ACSD_CTL_Item"));
118: itemList.getAccessibleContext().setAccessibleDescription(
119: bundle.getString("ACSD_CTL_ItemList"));
120: addButton
121: .getAccessibleContext()
122: .setAccessibleDescription(
123: bundle
124: .getString("ACSD_CTL_Add_StringArrayCustomEditor"));
125: changeButton
126: .getAccessibleContext()
127: .setAccessibleDescription(
128: bundle
129: .getString("ACSD_CTL_Change_StringArrayCustomEditor"));
130: removeButton.getAccessibleContext().setAccessibleDescription(
131: bundle.getString("ACSD_CTL_Remove"));
132: moveUpButton.getAccessibleContext().setAccessibleDescription(
133: bundle.getString("ACSD_CTL_MoveUp"));
134: moveDownButton.getAccessibleContext().setAccessibleDescription(
135: bundle.getString("ACSD_CTL_MoveDown"));
136:
137: updateButtons();
138:
139: // HelpCtx.setHelpIDString (this, StringArrayCustomEditor.class.getName ());
140: }
141:
142: public java.awt.Dimension getPreferredSize() {
143: // ensure minimum width
144: java.awt.Dimension sup = super .getPreferredSize();
145: return new java.awt.Dimension(Math
146: .max(sup.width, DEFAULT_WIDTH), sup.height);
147: }
148:
149: /** This method is called from within the constructor to
150: * initialize the form.
151: * WARNING: Do NOT modify this code. The content of this method is
152: * always regenerated by the FormEditor.
153: */
154: private void initComponents() {//GEN-BEGIN:initComponents
155: editPanel = new javax.swing.JPanel();
156: itemListScroll = new javax.swing.JScrollPane();
157: itemList = new javax.swing.JList();
158: itemLabel = new javax.swing.JLabel();
159: itemField = new javax.swing.JTextField();
160: itemListLabel = new javax.swing.JLabel();
161: buttonsPanel = new javax.swing.JPanel();
162: addButton = new javax.swing.JButton();
163: changeButton = new javax.swing.JButton();
164: removeButton = new javax.swing.JButton();
165: jSeparator1 = new javax.swing.JSeparator();
166: moveUpButton = new javax.swing.JButton();
167: moveDownButton = new javax.swing.JButton();
168: paddingPanel = new javax.swing.JPanel();
169:
170: setLayout(new java.awt.BorderLayout());
171:
172: editPanel.setLayout(new java.awt.GridBagLayout());
173: java.awt.GridBagConstraints gridBagConstraints2;
174:
175: itemList
176: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
177: public void valueChanged(
178: javax.swing.event.ListSelectionEvent evt) {
179: itemListValueChanged(evt);
180: }
181: });
182:
183: itemListScroll.setViewportView(itemList);
184:
185: gridBagConstraints2 = new java.awt.GridBagConstraints();
186: gridBagConstraints2.gridx = 0;
187: gridBagConstraints2.gridy = 2;
188: gridBagConstraints2.gridwidth = 2;
189: gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
190: gridBagConstraints2.weightx = 1.0;
191: gridBagConstraints2.weighty = 1.0;
192: editPanel.add(itemListScroll, gridBagConstraints2);
193:
194: itemLabel.setText("item");
195: itemLabel.setLabelFor(itemField);
196: gridBagConstraints2 = new java.awt.GridBagConstraints();
197: gridBagConstraints2.insets = new java.awt.Insets(0, 0, 11, 12);
198: gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST;
199: editPanel.add(itemLabel, gridBagConstraints2);
200:
201: gridBagConstraints2 = new java.awt.GridBagConstraints();
202: gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
203: gridBagConstraints2.insets = new java.awt.Insets(0, 0, 11, 0);
204: editPanel.add(itemField, gridBagConstraints2);
205:
206: itemListLabel.setText("jLabel1");
207: itemListLabel.setLabelFor(itemList);
208: gridBagConstraints2 = new java.awt.GridBagConstraints();
209: gridBagConstraints2.gridx = 0;
210: gridBagConstraints2.gridy = 1;
211: gridBagConstraints2.gridwidth = 2;
212: gridBagConstraints2.insets = new java.awt.Insets(0, 0, 2, 0);
213: gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST;
214: editPanel.add(itemListLabel, gridBagConstraints2);
215:
216: add(editPanel, java.awt.BorderLayout.CENTER);
217:
218: buttonsPanel.setLayout(new java.awt.GridBagLayout());
219: java.awt.GridBagConstraints gridBagConstraints1;
220:
221: addButton
222: .addActionListener(new java.awt.event.ActionListener() {
223: public void actionPerformed(
224: java.awt.event.ActionEvent evt) {
225: addButtonActionPerformed(evt);
226: }
227: });
228:
229: gridBagConstraints1 = new java.awt.GridBagConstraints();
230: gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
231: gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
232: gridBagConstraints1.insets = new java.awt.Insets(0, 8, 0, 8);
233: gridBagConstraints1.weightx = 1.0;
234: buttonsPanel.add(addButton, gridBagConstraints1);
235:
236: changeButton
237: .addActionListener(new java.awt.event.ActionListener() {
238: public void actionPerformed(
239: java.awt.event.ActionEvent evt) {
240: changeButtonActionPerformed(evt);
241: }
242: });
243:
244: gridBagConstraints1 = new java.awt.GridBagConstraints();
245: gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
246: gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
247: gridBagConstraints1.insets = new java.awt.Insets(8, 8, 0, 8);
248: gridBagConstraints1.weightx = 1.0;
249: buttonsPanel.add(changeButton, gridBagConstraints1);
250:
251: removeButton
252: .addActionListener(new java.awt.event.ActionListener() {
253: public void actionPerformed(
254: java.awt.event.ActionEvent evt) {
255: removeButtonActionPerformed(evt);
256: }
257: });
258:
259: gridBagConstraints1 = new java.awt.GridBagConstraints();
260: gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
261: gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
262: gridBagConstraints1.insets = new java.awt.Insets(8, 8, 8, 8);
263: gridBagConstraints1.weightx = 1.0;
264: buttonsPanel.add(removeButton, gridBagConstraints1);
265:
266: gridBagConstraints1 = new java.awt.GridBagConstraints();
267: gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
268: gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
269: gridBagConstraints1.insets = new java.awt.Insets(0, 4, 0, 4);
270: buttonsPanel.add(jSeparator1, gridBagConstraints1);
271:
272: moveUpButton.setEnabled(false);
273: moveUpButton
274: .addActionListener(new java.awt.event.ActionListener() {
275: public void actionPerformed(
276: java.awt.event.ActionEvent evt) {
277: moveUpButtonActionPerformed(evt);
278: }
279: });
280:
281: gridBagConstraints1 = new java.awt.GridBagConstraints();
282: gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
283: gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
284: gridBagConstraints1.insets = new java.awt.Insets(8, 8, 0, 8);
285: gridBagConstraints1.weightx = 1.0;
286: buttonsPanel.add(moveUpButton, gridBagConstraints1);
287:
288: moveDownButton.setEnabled(false);
289: moveDownButton
290: .addActionListener(new java.awt.event.ActionListener() {
291: public void actionPerformed(
292: java.awt.event.ActionEvent evt) {
293: moveDownButtonActionPerformed(evt);
294: }
295: });
296:
297: gridBagConstraints1 = new java.awt.GridBagConstraints();
298: gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
299: gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
300: gridBagConstraints1.insets = new java.awt.Insets(8, 8, 0, 8);
301: gridBagConstraints1.weightx = 1.0;
302: buttonsPanel.add(moveDownButton, gridBagConstraints1);
303:
304: gridBagConstraints1 = new java.awt.GridBagConstraints();
305: gridBagConstraints1.weighty = 1.0;
306: buttonsPanel.add(paddingPanel, gridBagConstraints1);
307:
308: add(buttonsPanel, java.awt.BorderLayout.EAST);
309:
310: }//GEN-END:initComponents
311:
312: private void changeButtonActionPerformed(
313: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changeButtonActionPerformed
314: int sel = itemList.getSelectedIndex();
315: String s = (String) itemsVector.elementAt(sel);
316: itemsVector.removeElementAt(sel);
317: itemsVector.insertElementAt(itemField.getText(), sel);
318: itemList.setListData(itemsVector);
319: itemList.setSelectedIndex(sel);
320: itemList.repaint();
321: updateValue();
322: }//GEN-LAST:event_changeButtonActionPerformed
323:
324: private void moveDownButtonActionPerformed(
325: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveDownButtonActionPerformed
326: int sel = itemList.getSelectedIndex();
327: String s = (String) itemsVector.elementAt(sel);
328: itemsVector.removeElementAt(sel);
329: itemsVector.insertElementAt(s, sel + 1);
330: itemList.setListData(itemsVector);
331: itemList.setSelectedIndex(sel + 1);
332: itemList.repaint();
333: updateValue();
334: }//GEN-LAST:event_moveDownButtonActionPerformed
335:
336: private void moveUpButtonActionPerformed(
337: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveUpButtonActionPerformed
338: int sel = itemList.getSelectedIndex();
339: String s = (String) itemsVector.elementAt(sel);
340: itemsVector.removeElementAt(sel);
341: itemsVector.insertElementAt(s, sel - 1);
342: itemList.setListData(itemsVector);
343: itemList.setSelectedIndex(sel - 1);
344: itemList.repaint();
345: updateValue();
346: }//GEN-LAST:event_moveUpButtonActionPerformed
347:
348: private void removeButtonActionPerformed(
349: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
350: // Add your handling code here:
351: int currentIndex = itemList.getSelectedIndex();
352: itemsVector.removeElementAt(currentIndex);
353: itemList.setListData(itemsVector);
354:
355: // set new selection
356: if (itemsVector.size() != 0) {
357: if (currentIndex >= itemsVector.size())
358: currentIndex = itemsVector.size() - 1;
359: itemList.setSelectedIndex(currentIndex);
360: }
361:
362: itemList.repaint();
363:
364: updateValue();
365: }//GEN-LAST:event_removeButtonActionPerformed
366:
367: private void itemListValueChanged(
368: javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_itemListValueChanged
369: // Add your handling code here:
370: updateButtons();
371: int sel = itemList.getSelectedIndex();
372: if (sel != -1) {
373: itemField.setText((String) itemsVector.elementAt(sel));
374: }
375: }//GEN-LAST:event_itemListValueChanged
376:
377: private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
378: // Add your handling code here:
379: itemsVector.addElement(itemField.getText());
380: itemList.setListData(itemsVector);
381: itemList.setSelectedIndex(itemsVector.size() - 1);
382: itemList.repaint();
383: updateValue();
384: }//GEN-LAST:event_addButtonActionPerformed
385:
386: private void updateButtons() {
387: int sel = itemList.getSelectedIndex();
388: if (sel == -1) {
389: removeButton.setEnabled(false);
390: moveUpButton.setEnabled(false);
391: moveDownButton.setEnabled(false);
392: changeButton.setEnabled(false);
393: } else {
394: removeButton.setEnabled(true);
395: moveUpButton.setEnabled(sel != 0);
396: moveDownButton.setEnabled(sel != itemsVector.size() - 1);
397: changeButton.setEnabled(true);
398: }
399: }
400:
401: private void updateValue() {
402: String[] value = new String[itemsVector.size()];
403: itemsVector.copyInto(value);
404: editor.setStringArray(value);
405: }
406:
407: // Variables declaration - do not modify//GEN-BEGIN:variables
408: private javax.swing.JPanel editPanel;
409: private javax.swing.JScrollPane itemListScroll;
410: private javax.swing.JList itemList;
411: private javax.swing.JLabel itemLabel;
412: private javax.swing.JTextField itemField;
413: private javax.swing.JLabel itemListLabel;
414: private javax.swing.JPanel buttonsPanel;
415: private javax.swing.JButton addButton;
416: private javax.swing.JButton changeButton;
417: private javax.swing.JButton removeButton;
418: private javax.swing.JSeparator jSeparator1;
419: private javax.swing.JButton moveUpButton;
420: private javax.swing.JButton moveDownButton;
421: private javax.swing.JPanel paddingPanel;
422:
423: // End of variables declaration//GEN-END:variables
424:
425: static class EmptyStringListCellRenderer extends JLabel implements
426: ListCellRenderer {
427:
428: protected static Border hasFocusBorder;
429: protected static Border noFocusBorder;
430:
431: static {
432: hasFocusBorder = new LineBorder(UIManager
433: .getColor("List.focusCellHighlight")); // NOI18N
434: noFocusBorder = new EmptyBorder(1, 1, 1, 1);
435: }
436:
437: static final long serialVersionUID = 487512296465844339L;
438:
439: /** Creates a new NodeListCellRenderer */
440: public EmptyStringListCellRenderer() {
441: setOpaque(true);
442: setBorder(noFocusBorder);
443: }
444:
445: /** This is the only method defined by ListCellRenderer. We just
446: * reconfigure the Jlabel each time we're called.
447: */
448: public java.awt.Component getListCellRendererComponent(
449: JList list, Object value, // value to display
450: int index, // cell index
451: boolean isSelected, // is the cell selected
452: boolean cellHasFocus) // the list and the cell have the focus
453: {
454: if (!(value instanceof String))
455: return this ;
456: String text = (String) value;
457: if ("".equals(text))
458: text = NbBundle.getMessage(
459: EmptyStringListCellRenderer.class, "CTL_Empty");
460:
461: setText(text);
462: if (isSelected) {
463: setBackground(UIManager
464: .getColor("List.selectionBackground")); // NOI18N
465: setForeground(UIManager
466: .getColor("List.selectionForeground")); // NOI18N
467: } else {
468: setBackground(list.getBackground());
469: setForeground(list.getForeground());
470: }
471:
472: setBorder(cellHasFocus ? hasFocusBorder : noFocusBorder);
473:
474: return this;
475: }
476: }
477: }
|