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.Dimension;
044: import java.awt.GridBagConstraints;
045: import java.util.Iterator;
046: import javax.swing.DefaultListModel;
047: import javax.swing.JLabel;
048: import javax.swing.JList;
049: import javax.swing.JScrollPane;
050: import javax.swing.JTextField;
051: import javax.swing.ListSelectionModel;
052: import javax.swing.event.ListSelectionEvent;
053: import javax.swing.event.ListSelectionListener;
054: import com.sun.rave.designtime.DesignProperty;
055:
056: /**
057: * @author eric
058: *
059: * TODO To change the template for this generated type comment go to
060: * Window - Preferences - Java - Code Generation - Code and Comments
061: */
062: public class SingleChoiceListPanel extends AbstractPropertyJPanel {
063:
064: protected java.util.List choices;
065: protected JList choicesJList;
066: protected DefaultListModel choicesJListModel;
067: protected JScrollPane choicesJListScrollPane;
068: protected JLabel valueLabelControl;
069: protected JTextField valueTextControl;
070:
071: /**
072: *
073: */
074: public SingleChoiceListPanel(
075: SingleChoiceListPropertyEditor propertyEditor,
076: DesignProperty liveProperty) {
077:
078: super (propertyEditor, liveProperty);
079: }
080:
081: public void doLayout() {
082:
083: super .doLayout();
084: choicesJList.ensureIndexIsVisible(choicesJList
085: .getSelectedIndex());
086: }
087:
088: protected java.util.List getChoices() {
089:
090: return choices;
091: }
092:
093: protected SingleChoiceListPropertyEditor getSingleChoiceListPropertyEditor() {
094:
095: return (SingleChoiceListPropertyEditor) getPropertyEditor();
096: }
097:
098: protected void handleChoicesJListSelectionChanged(
099: ListSelectionEvent event) {
100:
101: if (event.getValueIsAdjusting()) {
102: return;
103: }
104:
105: int index;
106: Object selectedChoice;
107:
108: index = choicesJList.getSelectedIndex();
109: if (index == -1) {
110: selectedChoice = null;
111: } else {
112: selectedChoice = choices.get(index);
113: }
114: getSingleChoiceListPropertyEditor().setValueChoice(
115: selectedChoice);
116: if (valueTextControl != null) {
117: valueTextControl
118: .setText(getSingleChoiceListPropertyEditor()
119: .getStringForChoice(selectedChoice));
120: }
121: }
122:
123: protected void initializeChoices() {
124:
125: choices = getSingleChoiceListPropertyEditor().getChoices();
126: }
127:
128: protected void initializeComponents() {
129: GridBagConstraints gridBagConstraints;
130:
131: setLayout(new java.awt.GridBagLayout());
132:
133: // add Value label
134: valueLabelControl = new javax.swing.JLabel();
135: valueLabelControl.setText(BundleHolder.bundle
136: .getMessage("value")); //NOI18N
137: gridBagConstraints = new java.awt.GridBagConstraints();
138: gridBagConstraints.gridx = 0;
139: gridBagConstraints.gridy = 0;
140: gridBagConstraints.ipadx = 36;
141: gridBagConstraints.ipady = 5;
142: // gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 0);
143: add(valueLabelControl, gridBagConstraints);
144:
145: // add Value entry field
146: valueTextControl = new javax.swing.JTextField();
147: gridBagConstraints = new java.awt.GridBagConstraints();
148: gridBagConstraints.gridx = 1;
149: gridBagConstraints.gridy = 0;
150: gridBagConstraints.gridwidth = 3;
151: gridBagConstraints.fill = GridBagConstraints.BOTH;
152: // gridBagConstraints.ipadx = 118;
153: gridBagConstraints.insets = new java.awt.Insets(10, 20, 10, 0);
154: add(valueTextControl, gridBagConstraints);
155:
156: int selectedIndex;
157:
158: // add the list control
159: choicesJListModel = new DefaultListModel();
160: populateChoicesJListModel();
161: choicesJList = new JList(choicesJListModel);
162: choicesJList
163: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
164: choicesJList.setLayoutOrientation(JList.VERTICAL);
165: choicesJList.setVisibleRowCount(-1);
166: choicesJList
167: .addListSelectionListener(new ListSelectionListener() {
168: public void valueChanged(ListSelectionEvent event) {
169: handleChoicesJListSelectionChanged(event);
170: }
171: });
172: selectedIndex = choices
173: .indexOf(getSingleChoiceListPropertyEditor()
174: .getValueChoice());
175: choicesJList.setSelectedIndex(selectedIndex);
176: // wrap the list control in scrolling pane
177: choicesJListScrollPane = new JScrollPane(choicesJList);
178: choicesJListScrollPane
179: .setPreferredSize(new Dimension(200, 200));
180:
181: gridBagConstraints = new java.awt.GridBagConstraints();
182: gridBagConstraints.gridx = 0;
183: gridBagConstraints.gridy = 1;
184: gridBagConstraints.gridwidth = 4;
185: gridBagConstraints.fill = GridBagConstraints.BOTH;
186: // gridBagConstraints.ipadx = 5;
187: // gridBagConstraints.insets = new java.awt.Insets(10, 20, 10, 10);
188: add(choicesJListScrollPane, gridBagConstraints);
189:
190: return;
191: }
192:
193: protected void populateChoicesJListModel() {
194:
195: for (Iterator iterator = getChoices().iterator(); iterator
196: .hasNext();) {
197: Object object;
198: String string;
199:
200: object = iterator.next();
201: string = getSingleChoiceListPropertyEditor()
202: .getStringForChoice(object);
203: if (string.length() == 0) {
204: string = " "; //NOI18N
205: }
206: choicesJListModel.addElement(string);
207: }
208: }
209:
210: protected void setPropertyEditorAndDesignProperty(
211: AbstractPropertyEditor propertyEditor,
212: DesignProperty liveProperty) {
213:
214: super.setPropertyEditorAndDesignProperty(propertyEditor,
215: liveProperty);
216: initializeChoices();
217: }
218:
219: }
|