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;
043:
044: import java.beans.*;
045: import java.util.*;
046: import java.awt.*;
047:
048: /** The PropertyPicker is a form which allows user to choose from property set
049: * of specified required class.
050: *
051: * @author Ian Formanek
052: * @version 1.00, Aug 29, 1998
053: */
054: public class PropertyPicker extends javax.swing.JPanel {
055:
056: public static final int CANCEL = 0;
057: public static final int OK = 1;
058:
059: static final long serialVersionUID = 5689122601606238081L;
060:
061: /** Initializes the Form */
062: public PropertyPicker(FormModel formModel,
063: RADComponent componentToSelect, Class requiredType) {
064: this .formModel = formModel;
065: this .requiredType = requiredType;
066: initComponents();
067:
068: java.util.List componentsList = formModel.getMetaComponents();
069: Collections.sort(componentsList,
070: new ParametersPicker.ComponentComparator());
071: components = new RADComponent[componentsList.size()];
072: componentsList.toArray(components);
073:
074: int selIndex = -1;
075: for (Iterator it = componentsList.iterator(); it.hasNext();) {
076: RADComponent radComp = (RADComponent) it.next();
077: if (componentToSelect != null
078: && componentToSelect == radComp)
079: selIndex = componentsCombo.getItemCount();
080: if (radComp == formModel.getTopRADComponent())
081: componentsCombo.addItem(FormUtils
082: .getBundleString("CTL_FormTopContainerName")); // NOI18N
083: else
084: componentsCombo.addItem(radComp.getName());
085: }
086: if (selIndex >= 0)
087: componentsCombo.setSelectedIndex(selIndex);
088:
089: propertyList
090: .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
091: updatePropertyList();
092:
093: // localize components
094: componentLabel.setDisplayedMnemonic(FormUtils.getBundleString(
095: "CTL_CW_Component_Mnemonic").charAt(0)); // NOI18N
096: listLabel.setDisplayedMnemonic(FormUtils.getBundleString(
097: "CTL_CW_PropertyList_Mnemonic").charAt(0)); // NOI18N
098:
099: componentsCombo
100: .getAccessibleContext()
101: .setAccessibleDescription(
102: FormUtils
103: .getBundleString("ACSD_CTL_CW_Component")); // NOI18N
104: propertyList.getAccessibleContext().setAccessibleDescription(
105: FormUtils.getBundleString("ACSD_CTL_CW_PropertyList")); // NOI18N
106: getAccessibleContext().setAccessibleDescription(
107: FormUtils.getBundleString("ACSD_PropertyPicker")); // NOI18N
108:
109: // HelpCtx.setHelpIDString(this, "gui.connecting.code"); // NOI18N
110: }
111:
112: public boolean isPickerValid() {
113: return pickerValid;
114: }
115:
116: private void setPickerValid(boolean v) {
117: boolean old = pickerValid;
118: pickerValid = v;
119: firePropertyChange("pickerValid", old, pickerValid); // NOI18N
120: }
121:
122: RADComponent getSelectedComponent() {
123: return selectedComponent;
124: }
125:
126: void setSelectedComponent(RADComponent selectedComponent) {
127: if (selectedComponent != null)
128: componentsCombo
129: .setSelectedItem(selectedComponent.getName());
130: }
131:
132: PropertyDescriptor getSelectedProperty() {
133: if ((selectedComponent == null)
134: || (propertyList.getSelectedIndex() == -1))
135: return null;
136: return descriptors[propertyList.getSelectedIndex()];
137: }
138:
139: void setSelectedProperty(PropertyDescriptor selectedProperty) {
140: if (selectedProperty == null) {
141: propertyList.setSelectedIndex(-1);
142: } else {
143: propertyList.setSelectedValue(selectedProperty.getName(),
144: true);
145: }
146: }
147:
148: // ----------------------------------------------------------------------------
149: // private methods
150:
151: private void updatePropertyList() {
152: RADComponent sel = getSelectedComponent();
153: if (sel == null) {
154: propertyList.setListData(new Object[0]);
155: propertyList.revalidate();
156: propertyList.repaint();
157: } else {
158: PropertyDescriptor[] descs = sel.getBeanInfo()
159: .getPropertyDescriptors();
160: ArrayList filtered = new ArrayList();
161: for (int i = 0; i < descs.length; i++) {
162: if ((descs[i].getReadMethod() != null) && // filter out non-readable properties
163: (descs[i].getPropertyType() != null) && // indexed properties return null from getPropertyType
164: requiredType.isAssignableFrom(descs[i]
165: .getPropertyType())) {
166: filtered.add(descs[i]);
167: }
168: }
169:
170: // sort the properties by name
171: Collections.sort(filtered, new Comparator() {
172: public int compare(Object o1, Object o2) {
173: return ((PropertyDescriptor) o1)
174: .getName()
175: .compareTo(
176: ((PropertyDescriptor) o2).getName());
177: }
178: });
179:
180: descriptors = new PropertyDescriptor[filtered.size()];
181: filtered.toArray(descriptors);
182:
183: String[] items = new String[descriptors.length];
184: for (int i = 0; i < descriptors.length; i++)
185: items[i] = descriptors[i].getName();
186: propertyList.setListData(items);
187: propertyList.revalidate();
188: propertyList.repaint();
189: }
190: }
191:
192: private void updateState() {
193: setPickerValid((getSelectedComponent() != null)
194: && (getSelectedProperty() != null));
195: }
196:
197: /** This method is called from within the constructor to
198: * initialize the form.
199: * WARNING: Do NOT modify this code. The content of this method is
200: * always regenerated by the FormEditor.
201: */
202: private void initComponents() {//GEN-BEGIN:initComponents
203: java.awt.GridBagConstraints gridBagConstraints;
204:
205: componentsCombo = new javax.swing.JComboBox();
206: propertiesScrollPane = new javax.swing.JScrollPane();
207: propertyList = new javax.swing.JList();
208: componentLabel = new javax.swing.JLabel();
209: listLabel = new javax.swing.JLabel();
210:
211: setLayout(new java.awt.GridBagLayout());
212:
213: setBorder(new javax.swing.border.EmptyBorder(
214: new java.awt.Insets(12, 12, 0, 11)));
215: componentsCombo
216: .addItemListener(new java.awt.event.ItemListener() {
217: public void itemStateChanged(
218: java.awt.event.ItemEvent evt) {
219: componentsComboItemStateChanged(evt);
220: }
221: });
222:
223: gridBagConstraints = new java.awt.GridBagConstraints();
224: gridBagConstraints.gridx = 1;
225: gridBagConstraints.gridy = 0;
226: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
227: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
228: gridBagConstraints.weightx = 1.0;
229: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
230: add(componentsCombo, gridBagConstraints);
231:
232: propertyList
233: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
234: public void valueChanged(
235: javax.swing.event.ListSelectionEvent evt) {
236: propertyListValueChanged(evt);
237: }
238: });
239:
240: propertiesScrollPane.setViewportView(propertyList);
241:
242: gridBagConstraints = new java.awt.GridBagConstraints();
243: gridBagConstraints.gridx = 0;
244: gridBagConstraints.gridy = 2;
245: gridBagConstraints.gridwidth = 2;
246: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
247: gridBagConstraints.weightx = 1.0;
248: gridBagConstraints.weighty = 1.0;
249: add(propertiesScrollPane, gridBagConstraints);
250:
251: componentLabel.setLabelFor(componentsCombo);
252: componentLabel.setText(FormUtils
253: .getBundleString("CTL_Component"));
254: gridBagConstraints = new java.awt.GridBagConstraints();
255: gridBagConstraints.gridx = 0;
256: gridBagConstraints.gridy = 0;
257: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
258: gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 6);
259: add(componentLabel, gridBagConstraints);
260:
261: listLabel.setLabelFor(propertyList);
262: listLabel.setText(FormUtils
263: .getBundleString("CTL_CW_PropertyList"));
264: gridBagConstraints = new java.awt.GridBagConstraints();
265: gridBagConstraints.gridx = 0;
266: gridBagConstraints.gridy = 1;
267: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
268: gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0);
269: add(listLabel, gridBagConstraints);
270:
271: }//GEN-END:initComponents
272:
273: private void propertyListValueChanged(
274: javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_propertyListValueChanged
275: if (propertyList.getSelectedIndex() == -1)
276: selectedProperty = null;
277: else
278: selectedProperty = descriptors[propertyList
279: .getSelectedIndex()];
280: updateState();
281: }//GEN-LAST:event_propertyListValueChanged
282:
283: private void componentsComboItemStateChanged(
284: java.awt.event.ItemEvent evt) {//GEN-FIRST:event_componentsComboItemStateChanged
285: if (componentsCombo.getSelectedIndex() == -1)
286: selectedComponent = null;
287: else
288: selectedComponent = components[componentsCombo
289: .getSelectedIndex()];
290: updatePropertyList();
291: }//GEN-LAST:event_componentsComboItemStateChanged
292:
293: /** Closes the dialog */
294: private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:closeDialog
295: }//GEN-LAST:closeDialog
296:
297: // Variables declaration - do not modify//GEN-BEGIN:variables
298: private javax.swing.JList propertyList;
299: private javax.swing.JLabel listLabel;
300: private javax.swing.JLabel componentLabel;
301: private javax.swing.JComboBox componentsCombo;
302: private javax.swing.JScrollPane propertiesScrollPane;
303: // End of variables declaration//GEN-END:variables
304:
305: private FormModel formModel;
306: private boolean pickerValid = false;
307:
308: private RADComponent[] components;
309: private Class requiredType;
310: private PropertyDescriptor[] descriptors;
311: private RADComponent selectedComponent;
312: private PropertyDescriptor selectedProperty;
313:
314: }
|