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 com.sun.jsfcl.std.property;
042:
043: import java.awt.Component;
044: import java.awt.Dimension;
045: import java.awt.GridBagConstraints;
046: import java.awt.event.ActionEvent;
047: import java.util.IdentityHashMap;
048: import java.util.Map;
049: import javax.swing.Box;
050: import javax.swing.BoxLayout;
051: import javax.swing.DefaultListModel;
052: import javax.swing.JButton;
053: import javax.swing.JLabel;
054: import javax.swing.JList;
055: import javax.swing.JPanel;
056: import javax.swing.JScrollPane;
057: import javax.swing.ListSelectionModel;
058: import javax.swing.event.ListSelectionEvent;
059: import com.sun.jsfcl.std.reference.ReferenceDataItem;
060: import com.sun.rave.designtime.DesignProperty;
061:
062: /**
063: * @author eric
064: *
065: * TODO To change the template for this generated type comment go to
066: * Window - Preferences - Java - Code Generation - Code and Comments
067: */
068: public class ChooseManyReferenceDataPanel extends
069: ChooseOneReferenceDataPanel {
070:
071: protected static final String DESELECT_ACTION = "deselect"; //NOI18N
072: protected static final String DESELECT_ALL_ACTION = "deselect-all"; //NOI18N
073: protected static final String DOWN_ACTION = "down"; //NOI18N
074: protected static final String SELECT_ACTION = "select"; //NOI18N
075: protected static final String UP_ACTION = "up"; //NOI18N
076:
077: protected JButton downJButton;
078: protected JButton deselectJButton;
079: protected JButton deselectAllJButton;
080: protected JButton selectJButton;
081: protected JList selectedJList;
082: protected DefaultListModel selectedJListModel;
083: protected JButton upJButton;
084:
085: /**
086: * @param propertyEditor
087: * @param liveProperty
088: */
089: public ChooseManyReferenceDataPanel(
090: ChooseManyReferenceDataPropertyEditor propertyEditor,
091: DesignProperty liveProperty) {
092:
093: super (propertyEditor, liveProperty);
094: }
095:
096: public void actionPerformed(ActionEvent event) {
097:
098: super .actionPerformed(event);
099: if (DESELECT_ACTION.equals(event.getActionCommand())) {
100: handleDeselectAction(event);
101: return;
102: }
103: if (DESELECT_ALL_ACTION.equals(event.getActionCommand())) {
104: handleDeselectAllAction(event);
105: return;
106: }
107: if (SELECT_ACTION.equals(event.getActionCommand())) {
108: handleSelectAction(event);
109: return;
110: }
111: if (DOWN_ACTION.equals(event.getActionCommand())) {
112: handleDownAction(event);
113: return;
114: }
115: if (UP_ACTION.equals(event.getActionCommand())) {
116: handleUpAction(event);
117: return;
118: }
119: }
120:
121: protected void adjustLeftColumnWidthIfNecessary(
122: ReferenceDataItem item) {
123:
124: ReferenceDataTwoColumnListCellRenderer renderer = (ReferenceDataTwoColumnListCellRenderer) selectedJList
125: .getCellRenderer();
126: renderer.getListCellRendererComponent(choicesJList, item, -1,
127: false, false);
128: renderer.adjustLeftColumnWidthIfNecessary();
129: }
130:
131: protected ChooseManyReferenceDataPropertyEditor getChooseManyReferenceDataPropertyEditor() {
132:
133: return (ChooseManyReferenceDataPropertyEditor) getPropertyEditor();
134: }
135:
136: protected int getListSelectionStyle() {
137:
138: return ListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
139: }
140:
141: public Object getPropertyValue() {
142: ReferenceDataItem[] selectedItems;
143:
144: selectedItems = getSelectedItems();
145: if (selectedItems == null || selectedItems.length == 0) {
146: return null;
147: } else {
148: String string = getChooseManyReferenceDataPropertyEditor()
149: .getStringForManyItems(selectedItems);
150: return string;
151: }
152: }
153:
154: protected ReferenceDataItem[] getSelectedItems() {
155: ReferenceDataItem[] result;
156:
157: result = new ReferenceDataItem[selectedJListModel.size()];
158: for (int i = 0; i < result.length; i++) {
159: result[i] = (ReferenceDataItem) selectedJListModel.get(i);
160: }
161: return result;
162: }
163:
164: protected Map getSelectedItemsMap() {
165: IdentityHashMap map;
166:
167: map = new IdentityHashMap();
168: ReferenceDataItem items[] = getSelectedItems();
169: Object dummy = new Object();
170: for (int i = 0; i < items.length; i++) {
171: ReferenceDataItem item = items[i];
172: map.put(item, dummy);
173: }
174: return map;
175: }
176:
177: protected String getTopLabel() {
178:
179: return getCompositeReferenceData().getChooseManyTitle();
180: }
181:
182: protected int getValueLabelGridWidth() {
183:
184: return 6;
185: }
186:
187: protected void grabCurrentValueFromPropertyEditor() {
188:
189: }
190:
191: protected void handleDeselectAction(ActionEvent event) {
192: Object values[];
193:
194: values = selectedJList.getSelectedValues();
195: for (int i = 0; i < values.length; i++) {
196: selectedJListModel.removeElement(values[i]);
197: }
198: updateButtonsState();
199: }
200:
201: protected void handleDeselectAllAction(ActionEvent event) {
202:
203: selectedJListModel.clear();
204: updateButtonsState();
205: }
206:
207: protected void handleDownAction(ActionEvent event) {
208: int[] indices;
209: int index, maxIndex;
210: Object swap;
211:
212: indices = selectedJList.getSelectedIndices();
213: maxIndex = selectedJListModel.size() - 1;
214: for (int i = 0; i < indices.length; i++) {
215: index = indices[i];
216: if (index < maxIndex) {
217: swap = selectedJListModel.elementAt(index + 1);
218: selectedJListModel.set(index + 1, selectedJListModel
219: .elementAt(index));
220: selectedJListModel.set(index, swap);
221: // so we can select the freshly moved up items
222: indices[i]++;
223: }
224: }
225: selectedJList.setSelectedIndices(indices);
226: }
227:
228: protected void handleSelectAction(ActionEvent event) {
229:
230: Object values[] = choicesJList.getSelectedValues();
231: int[] toSelect = new int[values.length];
232: int lastToSelect = 0;
233: for (int i = 0; i < values.length; i++) {
234: ReferenceDataItem item = (ReferenceDataItem) values[i];
235: if (getChooseManyReferenceDataPropertyEditor()
236: .getAllowDuplicates()
237: || !getSelectedItemsMap().containsKey(item)) {
238: toSelect[lastToSelect] = selectedJListModel.size();
239: lastToSelect++;
240: selectedJListModel.addElement(item);
241: adjustLeftColumnWidthIfNecessary(item);
242: }
243: }
244: if (lastToSelect != values.length) {
245: System.arraycopy(toSelect, 0,
246: toSelect = new int[lastToSelect], 0, lastToSelect);
247: }
248: updateButtonsState();
249: selectedJList.setSelectedIndices(toSelect);
250: if (toSelect.length > 0) {
251: selectedJList.ensureIndexIsVisible(toSelect[0]);
252: }
253: }
254:
255: protected void handleSelectedJListSelectionChanged(
256: ListSelectionEvent event) {
257:
258: if (event.getValueIsAdjusting()) {
259: return;
260: }
261: if (selectedJList != null) {
262: updateButtonsState();
263: }
264: }
265:
266: protected void handleUpAction(ActionEvent event) {
267: int[] indices;
268: int index;
269: Object swap;
270:
271: indices = selectedJList.getSelectedIndices();
272: for (int i = 0; i < indices.length; i++) {
273: index = indices[i];
274: if (index <= 0) {
275: break;
276: } else {
277: swap = selectedJListModel.elementAt(index - 1);
278: selectedJListModel.set(index - 1, selectedJListModel
279: .elementAt(index));
280: selectedJListModel.set(index, swap);
281: // so we can select the freshly moved up items
282: indices[i]--;
283: }
284: }
285: selectedJList.setSelectedIndices(indices);
286: }
287:
288: protected boolean includeInChoicesJList(ReferenceDataItem item) {
289:
290: if (super .includeInChoicesJList(item)) {
291: return item.getName().length() > 0;
292: }
293: return false;
294: }
295:
296: protected void initializeComponents() {
297: GridBagConstraints gridBagConstraints;
298: JScrollPane selectedJListScrollPane;
299: JPanel selectButtonsPanel, moveButtonsPanel;
300:
301: super .initializeComponents();
302:
303: // add select, deselect, deselectSelected buttons
304: selectButtonsPanel = new JPanel();
305: selectButtonsPanel.setLayout(new BoxLayout(selectButtonsPanel,
306: BoxLayout.Y_AXIS));
307:
308: selectJButton = new JButton(">"); //NOI18N
309: // some ridiculous size so it does not get in the way
310: //selectJButton.setMaximumSize(new Dimension(200, 200));
311: selectJButton.setActionCommand(SELECT_ACTION);
312: selectJButton.addActionListener(this );
313: selectJButton.setEnabled(false);
314: selectButtonsPanel.add(selectJButton, null);
315: selectButtonsPanel
316: .add(Box.createRigidArea(new Dimension(0, 5)));
317:
318: deselectJButton = new JButton("<"); //NOI18N
319: //deselectJButton.setMaximumSize(new Dimension(200, 200));
320: deselectJButton.setActionCommand(DESELECT_ACTION);
321: deselectJButton.addActionListener(this );
322: deselectJButton.setEnabled(false);
323: selectButtonsPanel.add(deselectJButton, null);
324: selectButtonsPanel
325: .add(Box.createRigidArea(new Dimension(0, 5)));
326:
327: deselectAllJButton = new JButton("<<"); //NOI18N
328: // deselectAllJButton.setMaximumSize(new Dimension(200, 200));
329: deselectAllJButton.setActionCommand(DESELECT_ALL_ACTION);
330: deselectAllJButton.addActionListener(this );
331: selectButtonsPanel.add(deselectAllJButton, null);
332:
333: gridBagConstraints = new java.awt.GridBagConstraints();
334: gridBagConstraints.gridx = 2;
335: gridBagConstraints.gridy = 3;
336: gridBagConstraints.gridwidth = 1;
337: gridBagConstraints.anchor = GridBagConstraints.PAGE_START;
338: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
339: gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
340: add(selectButtonsPanel, gridBagConstraints);
341:
342: // add Avaliable label
343: JLabel label = new javax.swing.JLabel();
344: label.setText(BundleHolder.bundle.getMessage("Available")); //NOI18N
345: gridBagConstraints = new java.awt.GridBagConstraints();
346: gridBagConstraints.gridx = 0;
347: gridBagConstraints.gridy = 1;
348: gridBagConstraints.gridwidth = 2;
349: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
350: gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 10);
351: add(label, gridBagConstraints);
352:
353: // add Selected label
354: label = new javax.swing.JLabel();
355: label.setText(BundleHolder.bundle.getMessage("sel")); //NOI18N
356: gridBagConstraints = new java.awt.GridBagConstraints();
357: gridBagConstraints.gridx = 3;
358: gridBagConstraints.gridy = 1;
359: gridBagConstraints.gridwidth = 2;
360: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
361: gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 10);
362: add(label, gridBagConstraints);
363:
364: // add the selected list control
365: Component component;
366: component = initializeSelectedListComponent();
367:
368: // wrap the list control in scrolling pane
369: selectedJListScrollPane = new JScrollPane(component);
370: selectedJListScrollPane
371: .setPreferredSize(new Dimension(200, 200));
372:
373: gridBagConstraints = new java.awt.GridBagConstraints();
374: gridBagConstraints.gridx = 3;
375: gridBagConstraints.gridy = 3;
376: gridBagConstraints.gridwidth = 2;
377: gridBagConstraints.weightx = 0.5;
378: gridBagConstraints.weighty = 1.0;
379: gridBagConstraints.fill = GridBagConstraints.BOTH;
380: gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 10);
381: add(selectedJListScrollPane, gridBagConstraints);
382:
383: if (getCompositeReferenceData().canOrderItems()) {
384: // add move Up, Down buttons
385: moveButtonsPanel = new JPanel();
386: moveButtonsPanel.setLayout(new BoxLayout(moveButtonsPanel,
387: BoxLayout.Y_AXIS));
388:
389: upJButton = new JButton(BundleHolder.bundle
390: .getMessage("up")); //NOI18N
391: // upJButton.setMaximumSize(new Dimension(200, 200));
392: upJButton.setActionCommand(UP_ACTION);
393: upJButton.addActionListener(this );
394: upJButton.setEnabled(false);
395: moveButtonsPanel.add(upJButton);
396: moveButtonsPanel.add(Box
397: .createRigidArea(new Dimension(0, 5)));
398:
399: downJButton = new JButton(BundleHolder.bundle
400: .getMessage("down")); //NOI18N
401: // downJButton.setMaximumSize(new Dimension(200, 200));
402: downJButton.setActionCommand(DOWN_ACTION);
403: downJButton.setEnabled(false);
404: downJButton.addActionListener(this );
405: moveButtonsPanel.add(downJButton);
406:
407: gridBagConstraints = new java.awt.GridBagConstraints();
408: gridBagConstraints.gridx = 5;
409: gridBagConstraints.gridy = 3;
410: gridBagConstraints.gridwidth = 1;
411: gridBagConstraints.anchor = GridBagConstraints.PAGE_START;
412: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
413: gridBagConstraints.insets = new java.awt.Insets(0, 10, 0,
414: 10);
415: add(moveButtonsPanel, gridBagConstraints);
416: }
417: }
418:
419: protected Component initializeSelectedListComponent() {
420:
421: selectedJListModel = new DefaultListModel();
422: selectedJList = new JList(selectedJListModel);
423: selectedJList.setSelectionMode(getListSelectionStyle());
424: selectedJList.setLayoutOrientation(JList.VERTICAL);
425: selectedJList.setVisibleRowCount(-1);
426: selectedJList
427: .setCellRenderer(new ReferenceDataTwoColumnListCellRenderer());
428: populateSelectedJListModel();
429: selectedJList.addListSelectionListener(this );
430: return selectedJList;
431: }
432:
433: protected void populateSelectedJListModel() {
434: ReferenceDataItem items[];
435:
436: selectedJListModel.clear();
437: ReferenceDataTwoColumnListCellRenderer renderer = (ReferenceDataTwoColumnListCellRenderer) selectedJList
438: .getCellRenderer();
439: renderer.resetLeftColumnWidth();
440: items = getChooseManyReferenceDataPropertyEditor()
441: .getValueReferenceDataItems();
442: if (items != null) {
443: for (int i = 0; i < items.length; i++) {
444: ReferenceDataItem item = items[i];
445: selectedJListModel.addElement(item);
446: renderer.getListCellRendererComponent(choicesJList,
447: item, -1, false, false);
448: renderer.adjustLeftColumnWidthIfNecessary();
449: }
450: }
451: }
452:
453: protected void updateButtonsState() {
454:
455: super .updateButtonsState();
456: boolean hasSelections = choicesJList.getSelectedIndices().length > 0;
457: selectJButton.setEnabled(hasSelections);
458:
459: if (selectedJList != null) {
460: hasSelections = selectedJList.getSelectedIndices().length > 0;
461: deselectJButton.setEnabled(hasSelections);
462: deselectAllJButton
463: .setEnabled(selectedJListModel.size() > 0);
464: if (getCompositeReferenceData().canOrderItems()) {
465: upJButton.setEnabled(selectedJList
466: .getMinSelectionIndex() > 0);
467: downJButton
468: .setEnabled(selectedJList
469: .getMaxSelectionIndex() >= 0
470: && selectedJList.getMaxSelectionIndex() < (selectedJListModel
471: .size() - 1));
472: }
473: }
474: }
475:
476: public void valueChanged(ListSelectionEvent event) {
477:
478: super.valueChanged(event);
479: if (event.getSource() == selectedJList) {
480: handleSelectedJListSelectionChanged(event);
481: }
482: }
483: }
|