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-2007 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: package org.netbeans.modules.form;
042:
043: import java.util.*;
044: import javax.swing.*;
045: import javax.swing.event.ListDataEvent;
046: import javax.swing.event.ListDataListener;
047: import org.openide.util.Utilities;
048:
049: /**
050: * Component that allows you to select (reordered) sublist from given list of items.
051: *
052: * @author Jan Stola
053: */
054: public class ListSelector extends javax.swing.JPanel {
055:
056: public ListSelector() {
057: initComponents();
058: ListDataListener listener = new ListDataListener() {
059: public void contentsChanged(ListDataEvent e) {
060: if (e.getSource() == availableList.getModel()) {
061: addAllButton.setEnabled(availableList.getModel()
062: .getSize() != 0);
063: } else if (e.getSource() == selectedList.getModel()) {
064: removeAllButton.setEnabled(selectedList.getModel()
065: .getSize() != 0);
066: }
067: }
068:
069: public void intervalAdded(ListDataEvent e) {
070: contentsChanged(e);
071: }
072:
073: public void intervalRemoved(ListDataEvent e) {
074: contentsChanged(e);
075: }
076: };
077: availableList.getModel().addListDataListener(listener);
078: selectedList.getModel().addListDataListener(listener);
079: }
080:
081: public void setItems(List available, List selected) {
082: DefaultListModel model = (DefaultListModel) availableList
083: .getModel();
084: model.clear();
085: for (Object item : available) {
086: model.addElement(item);
087: }
088: model = (DefaultListModel) selectedList.getModel();
089: model.clear();
090: for (Object item : selected) {
091: model.addElement(item);
092: }
093: }
094:
095: public List getSelectedItems() {
096: DefaultListModel model = (DefaultListModel) selectedList
097: .getModel();
098: return Arrays.asList(model.toArray());
099: }
100:
101: /** This method is called from within the constructor to
102: * initialize the form.
103: * WARNING: Do NOT modify this code. The content of this method is
104: * always regenerated by the Form Editor.
105: */
106: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
107: private void initComponents() {
108:
109: availableLabel = new javax.swing.JLabel();
110: availableScrollPane = new javax.swing.JScrollPane();
111: availableList = new javax.swing.JList();
112: addButton = new javax.swing.JButton();
113: removeButton = new javax.swing.JButton();
114: addAllButton = new javax.swing.JButton();
115: removeAllButton = new javax.swing.JButton();
116: selectedScrollPane = new javax.swing.JScrollPane();
117: selectedList = new javax.swing.JList();
118: upButton = new javax.swing.JButton();
119: downButton = new javax.swing.JButton();
120: selectedLabel = new javax.swing.JLabel();
121:
122: availableLabel.setLabelFor(availableList);
123: org.openide.awt.Mnemonics.setLocalizedText(availableLabel,
124: org.openide.util.NbBundle.getMessage(
125: ListSelector.class,
126: "MSG_ListSelector_Available")); // NOI18N
127:
128: availableList.setModel(new DefaultListModel());
129: availableList
130: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
131: public void valueChanged(
132: javax.swing.event.ListSelectionEvent evt) {
133: availableListValueChanged(evt);
134: }
135: });
136: availableScrollPane.setViewportView(availableList);
137: availableList.getAccessibleContext().setAccessibleName(
138: org.openide.util.NbBundle.getMessage(
139: ListSelector.class,
140: "MSG_ListSelector_Available_ACSN")); // NOI18N
141: availableList.getAccessibleContext().setAccessibleDescription(
142: org.openide.util.NbBundle.getMessage(
143: ListSelector.class,
144: "MSG_ListSelector_Available_ACSD")); // NOI18N
145:
146: addButton.setText(org.openide.util.NbBundle.getMessage(
147: ListSelector.class, "MSG_ListSelector_Add")); // NOI18N
148: addButton.setEnabled(false);
149: addButton
150: .addActionListener(new java.awt.event.ActionListener() {
151: public void actionPerformed(
152: java.awt.event.ActionEvent evt) {
153: addButtonActionPerformed(evt);
154: }
155: });
156:
157: removeButton.setText(org.openide.util.NbBundle.getMessage(
158: ListSelector.class, "MSG_ListSelector_Remove")); // NOI18N
159: removeButton.setEnabled(false);
160: removeButton
161: .addActionListener(new java.awt.event.ActionListener() {
162: public void actionPerformed(
163: java.awt.event.ActionEvent evt) {
164: removeButtonActionPerformed(evt);
165: }
166: });
167:
168: addAllButton.setText(org.openide.util.NbBundle.getMessage(
169: ListSelector.class, "MSG_ListSelector_AddAll")); // NOI18N
170: addAllButton.setEnabled(false);
171: addAllButton
172: .addActionListener(new java.awt.event.ActionListener() {
173: public void actionPerformed(
174: java.awt.event.ActionEvent evt) {
175: addAllButtonActionPerformed(evt);
176: }
177: });
178:
179: removeAllButton.setText(org.openide.util.NbBundle.getMessage(
180: ListSelector.class, "MSG_ListSelector_RemoveAll")); // NOI18N
181: removeAllButton.setEnabled(false);
182: removeAllButton
183: .addActionListener(new java.awt.event.ActionListener() {
184: public void actionPerformed(
185: java.awt.event.ActionEvent evt) {
186: removeAllButtonActionPerformed(evt);
187: }
188: });
189:
190: selectedList.setModel(new DefaultListModel());
191: selectedList
192: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
193: public void valueChanged(
194: javax.swing.event.ListSelectionEvent evt) {
195: selectedListValueChanged(evt);
196: }
197: });
198: selectedScrollPane.setViewportView(selectedList);
199: selectedList.getAccessibleContext().setAccessibleName(
200: org.openide.util.NbBundle.getMessage(
201: ListSelector.class,
202: "MSG_ListSelector_Selected_ACSN")); // NOI18N
203: selectedList.getAccessibleContext().setAccessibleDescription(
204: org.openide.util.NbBundle.getMessage(
205: ListSelector.class,
206: "MSG_ListSelector_Selected_ACSD")); // NOI18N
207:
208: upButton
209: .setIcon(new javax.swing.ImageIcon(
210: getClass()
211: .getResource(
212: "/org/netbeans/modules/form/resources/up.gif"))); // NOI18N
213: org.openide.awt.Mnemonics.setLocalizedText(upButton,
214: org.openide.util.NbBundle.getMessage(
215: ListSelector.class, "MSG_ListSelector_Up")); // NOI18N
216: upButton.setEnabled(false);
217: upButton
218: .setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
219: if (!Utilities.isMac()) {
220: upButton.setMargin(new java.awt.Insets(2, 6, 2, 6));
221: }
222: upButton.addActionListener(new java.awt.event.ActionListener() {
223: public void actionPerformed(java.awt.event.ActionEvent evt) {
224: upButtonActionPerformed(evt);
225: }
226: });
227:
228: downButton
229: .setIcon(new javax.swing.ImageIcon(
230: getClass()
231: .getResource(
232: "/org/netbeans/modules/form/resources/down.gif"))); // NOI18N
233: org.openide.awt.Mnemonics.setLocalizedText(downButton,
234: org.openide.util.NbBundle.getMessage(
235: ListSelector.class, "MSG_ListSelector_Down")); // NOI18N
236: downButton.setEnabled(false);
237: downButton
238: .setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
239: if (!Utilities.isMac()) {
240: downButton.setMargin(new java.awt.Insets(2, 6, 2, 6));
241: }
242: downButton
243: .addActionListener(new java.awt.event.ActionListener() {
244: public void actionPerformed(
245: java.awt.event.ActionEvent evt) {
246: downButtonActionPerformed(evt);
247: }
248: });
249:
250: selectedLabel.setLabelFor(selectedList);
251: org.openide.awt.Mnemonics.setLocalizedText(selectedLabel,
252: org.openide.util.NbBundle
253: .getMessage(ListSelector.class,
254: "MSG_ListSelector_Selected")); // NOI18N
255:
256: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
257: this );
258: this .setLayout(layout);
259: layout
260: .setHorizontalGroup(layout
261: .createParallelGroup(
262: org.jdesktop.layout.GroupLayout.LEADING)
263: .add(
264: org.jdesktop.layout.GroupLayout.TRAILING,
265: layout
266: .createSequentialGroup()
267: .add(
268: layout
269: .createParallelGroup(
270: org.jdesktop.layout.GroupLayout.LEADING)
271: .add(
272: availableLabel)
273: .add(
274: availableScrollPane,
275: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
276: 207,
277: Short.MAX_VALUE))
278: .addPreferredGap(
279: org.jdesktop.layout.LayoutStyle.RELATED)
280: .add(
281: layout
282: .createParallelGroup(
283: org.jdesktop.layout.GroupLayout.LEADING,
284: false)
285: .add(
286: addButton,
287: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
288: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
289: Short.MAX_VALUE)
290: .add(
291: removeButton,
292: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
293: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
294: Short.MAX_VALUE)
295: .add(
296: addAllButton,
297: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
298: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
299: Short.MAX_VALUE)
300: .add(
301: removeAllButton,
302: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
303: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
304: Short.MAX_VALUE))
305: .addPreferredGap(
306: org.jdesktop.layout.LayoutStyle.RELATED)
307: .add(
308: layout
309: .createParallelGroup(
310: org.jdesktop.layout.GroupLayout.LEADING)
311: .add(
312: selectedScrollPane,
313: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
314: 207,
315: Short.MAX_VALUE)
316: .add(
317: selectedLabel))
318: .addPreferredGap(
319: org.jdesktop.layout.LayoutStyle.RELATED)
320: .add(
321: layout
322: .createParallelGroup(
323: org.jdesktop.layout.GroupLayout.LEADING,
324: false)
325: .add(
326: downButton,
327: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
328: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
329: Short.MAX_VALUE)
330: .add(
331: upButton,
332: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
333: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
334: Short.MAX_VALUE))));
335: layout
336: .setVerticalGroup(layout
337: .createParallelGroup(
338: org.jdesktop.layout.GroupLayout.LEADING)
339: .add(
340: layout
341: .createSequentialGroup()
342: .add(
343: layout
344: .createParallelGroup(
345: org.jdesktop.layout.GroupLayout.BASELINE)
346: .add(
347: availableLabel)
348: .add(
349: selectedLabel))
350: .addPreferredGap(
351: org.jdesktop.layout.LayoutStyle.RELATED)
352: .add(
353: layout
354: .createParallelGroup(
355: org.jdesktop.layout.GroupLayout.LEADING)
356: .add(
357: layout
358: .createSequentialGroup()
359: .addPreferredGap(
360: org.jdesktop.layout.LayoutStyle.RELATED,
361: 33,
362: Short.MAX_VALUE)
363: .add(
364: upButton)
365: .addPreferredGap(
366: org.jdesktop.layout.LayoutStyle.RELATED)
367: .add(
368: downButton)
369: .addContainerGap(
370: 43,
371: Short.MAX_VALUE))
372: .add(
373: layout
374: .createSequentialGroup()
375: .addPreferredGap(
376: org.jdesktop.layout.LayoutStyle.RELATED,
377: 5,
378: Short.MAX_VALUE)
379: .add(
380: addButton)
381: .addPreferredGap(
382: org.jdesktop.layout.LayoutStyle.RELATED)
383: .add(
384: addAllButton)
385: .addPreferredGap(
386: org.jdesktop.layout.LayoutStyle.RELATED)
387: .add(
388: removeButton)
389: .addPreferredGap(
390: org.jdesktop.layout.LayoutStyle.RELATED)
391: .add(
392: removeAllButton)
393: .addContainerGap(
394: 17,
395: Short.MAX_VALUE))
396: .add(
397: availableScrollPane)
398: .add(
399: selectedScrollPane))));
400:
401: addButton.getAccessibleContext().setAccessibleDescription(
402: org.openide.util.NbBundle
403: .getMessage(ListSelector.class,
404: "MSG_ListSelector_Add_ACSD")); // NOI18N
405: removeButton.getAccessibleContext().setAccessibleDescription(
406: org.openide.util.NbBundle.getMessage(
407: ListSelector.class,
408: "MSG_ListSelector_Remove_ACSD")); // NOI18N
409: addAllButton.getAccessibleContext().setAccessibleDescription(
410: org.openide.util.NbBundle.getMessage(
411: ListSelector.class,
412: "MSG_ListSelector_AddAll_ACSD")); // NOI18N
413: removeAllButton.getAccessibleContext()
414: .setAccessibleDescription(
415: org.openide.util.NbBundle.getMessage(
416: ListSelector.class,
417: "MSG_ListSelector_RemoveAll_ACSD")); // NOI18N
418: upButton.getAccessibleContext()
419: .setAccessibleDescription(
420: org.openide.util.NbBundle.getMessage(
421: ListSelector.class,
422: "MSG_ListSelector_Up_ACSD")); // NOI18N
423: downButton.getAccessibleContext().setAccessibleDescription(
424: org.openide.util.NbBundle.getMessage(
425: ListSelector.class,
426: "MSG_ListSelector_Down_ACSD")); // NOI18N
427: }// </editor-fold>//GEN-END:initComponents
428:
429: private void downButtonActionPerformed(
430: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downButtonActionPerformed
431: DefaultListModel model = (DefaultListModel) selectedList
432: .getModel();
433: int index = selectedList.getSelectedIndex();
434: Object item = model.remove(index);
435: model.add(index + 1, item);
436: selectedList.setSelectedIndex(index + 1);
437: }//GEN-LAST:event_downButtonActionPerformed
438:
439: private void upButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_upButtonActionPerformed
440: DefaultListModel model = (DefaultListModel) selectedList
441: .getModel();
442: int index = selectedList.getSelectedIndex();
443: Object item = model.remove(index);
444: model.add(index - 1, item);
445: selectedList.setSelectedIndex(index - 1);
446: }//GEN-LAST:event_upButtonActionPerformed
447:
448: private void removeAllButtonActionPerformed(
449: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeAllButtonActionPerformed
450: moveListItems(selectedList, availableList, false);
451: }//GEN-LAST:event_removeAllButtonActionPerformed
452:
453: private void addAllButtonActionPerformed(
454: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addAllButtonActionPerformed
455: moveListItems(availableList, selectedList, false);
456: }//GEN-LAST:event_addAllButtonActionPerformed
457:
458: private void removeButtonActionPerformed(
459: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
460: moveListItems(selectedList, availableList, true);
461: }//GEN-LAST:event_removeButtonActionPerformed
462:
463: private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
464: moveListItems(availableList, selectedList, true);
465: }//GEN-LAST:event_addButtonActionPerformed
466:
467: private void selectedListValueChanged(
468: javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_selectedListValueChanged
469: removeButton.setEnabled(selectedList.getSelectedIndex() != -1);
470: int[] index = selectedList.getSelectedIndices();
471: upButton.setEnabled((index.length == 1) && (index[0] != 0));
472: downButton.setEnabled((index.length == 1)
473: && (index[0] != selectedList.getModel().getSize() - 1));
474: }//GEN-LAST:event_selectedListValueChanged
475:
476: private void availableListValueChanged(
477: javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_availableListValueChanged
478: addButton.setEnabled(availableList.getSelectedIndex() != -1);
479: }//GEN-LAST:event_availableListValueChanged
480:
481: // Variables declaration - do not modify//GEN-BEGIN:variables
482: private javax.swing.JButton addAllButton;
483: private javax.swing.JButton addButton;
484: private javax.swing.JLabel availableLabel;
485: private javax.swing.JList availableList;
486: private javax.swing.JScrollPane availableScrollPane;
487: private javax.swing.JButton downButton;
488: private javax.swing.JButton removeAllButton;
489: private javax.swing.JButton removeButton;
490: private javax.swing.JLabel selectedLabel;
491: private javax.swing.JList selectedList;
492: private javax.swing.JScrollPane selectedScrollPane;
493: private javax.swing.JButton upButton;
494:
495: // End of variables declaration//GEN-END:variables
496:
497: /**
498: * Moves items of <code>fromList</code> into <code>toList</code>.
499: *
500: * @param fromList list to move the items from.
501: * @param toList list to move the items to.
502: * @param selected determines whether to move all items or just the selected ones.
503: */
504: private static void moveListItems(JList fromList, JList toList,
505: boolean selected) {
506: DefaultListModel fromModel = (DefaultListModel) fromList
507: .getModel();
508: DefaultListModel toModel = (DefaultListModel) toList.getModel();
509: if (selected) {
510: int[] index = fromList.getSelectedIndices();
511: for (int i = 0; i < index.length; i++) {
512: Object item = fromModel.getElementAt(index[i]);
513: toModel.addElement(item);
514: }
515: for (int i = index.length - 1; i >= 0; i--) {
516: fromModel.removeElementAt(index[i]);
517: }
518: } else {
519: Enumeration items = fromModel.elements();
520: while (items.hasMoreElements()) {
521: toModel.addElement(items.nextElement());
522: }
523: fromModel.clear();
524: }
525: }
526:
527: }
|