KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > PropertyPicker


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.form;
22
23 import java.beans.*;
24 import java.util.*;
25 import java.awt.*;
26
27 /** The PropertyPicker is a form which allows user to choose from property set
28  * of specified required class.
29  *
30  * @author Ian Formanek
31  * @version 1.00, Aug 29, 1998
32  */

33 public class PropertyPicker extends javax.swing.JPanel JavaDoc {
34
35     public static final int CANCEL = 0;
36     public static final int OK = 1;
37
38     static final long serialVersionUID =5689122601606238081L;
39     /** Initializes the Form */
40     public PropertyPicker(FormModel formModel, RADComponent componentToSelect, Class JavaDoc requiredType) {
41         this.formModel = formModel;
42         this.requiredType = requiredType;
43         initComponents();
44
45         java.util.List JavaDoc componentsList = formModel.getMetaComponents();
46         Collections.sort(componentsList, new ParametersPicker.ComponentComparator());
47         components = new RADComponent[componentsList.size()];
48         componentsList.toArray(components);
49
50         int selIndex = -1;
51         for (Iterator it = componentsList.iterator(); it.hasNext(); ) {
52             RADComponent radComp = (RADComponent) it.next();
53             if (componentToSelect != null && componentToSelect == radComp)
54                 selIndex = componentsCombo.getItemCount();
55             if (radComp == formModel.getTopRADComponent())
56                 componentsCombo.addItem(
57                     FormUtils.getBundleString("CTL_FormTopContainerName")); // NOI18N
58
else
59                 componentsCombo.addItem(radComp.getName());
60         }
61         if (selIndex >= 0)
62             componentsCombo.setSelectedIndex(selIndex);
63
64         propertyList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
65         updatePropertyList();
66
67         // localize components
68
componentLabel.setDisplayedMnemonic(
69             FormUtils.getBundleString("CTL_CW_Component_Mnemonic").charAt(0)); // NOI18N
70
listLabel.setDisplayedMnemonic(
71             FormUtils.getBundleString("CTL_CW_PropertyList_Mnemonic").charAt(0)); // NOI18N
72

73         componentsCombo.getAccessibleContext().setAccessibleDescription(
74             FormUtils.getBundleString("ACSD_CTL_CW_Component")); // NOI18N
75
propertyList.getAccessibleContext().setAccessibleDescription(
76             FormUtils.getBundleString("ACSD_CTL_CW_PropertyList")); // NOI18N
77
getAccessibleContext().setAccessibleDescription(
78             FormUtils.getBundleString("ACSD_PropertyPicker")); // NOI18N
79

80 // HelpCtx.setHelpIDString(this, "gui.connecting.code"); // NOI18N
81
}
82
83     public boolean isPickerValid() {
84         return pickerValid;
85     }
86     
87     private void setPickerValid(boolean v) {
88         boolean old = pickerValid;
89         pickerValid = v;
90         firePropertyChange("pickerValid", old, pickerValid); // NOI18N
91
}
92
93     RADComponent getSelectedComponent() {
94         return selectedComponent;
95     }
96
97     void setSelectedComponent(RADComponent selectedComponent) {
98         if (selectedComponent != null)
99             componentsCombo.setSelectedItem(selectedComponent.getName());
100     }
101
102     PropertyDescriptor getSelectedProperty() {
103         if ((selectedComponent == null) ||(propertyList.getSelectedIndex() == -1))
104             return null;
105         return descriptors [propertyList.getSelectedIndex()];
106     }
107
108     void setSelectedProperty(PropertyDescriptor selectedProperty) {
109         if (selectedProperty == null) {
110             propertyList.setSelectedIndex(-1);
111         } else {
112             propertyList.setSelectedValue(selectedProperty.getName(), true);
113         }
114     }
115     // ----------------------------------------------------------------------------
116
// private methods
117

118     private void updatePropertyList() {
119         RADComponent sel = getSelectedComponent();
120         if (sel == null) {
121             propertyList.setListData(new Object JavaDoc [0]);
122             propertyList.revalidate();
123             propertyList.repaint();
124         } else {
125             PropertyDescriptor[] descs = sel.getBeanInfo().getPropertyDescriptors();
126             ArrayList filtered = new ArrayList();
127             for (int i = 0; i < descs.length; i ++) {
128                 if ((descs[i].getReadMethod() != null) && // filter out non-readable properties
129
(descs[i].getPropertyType() != null) && // indexed properties return null from getPropertyType
130
requiredType.isAssignableFrom(descs[i].getPropertyType())) {
131                     filtered.add(descs[i]);
132                 }
133             }
134
135             // sort the properties by name
136
Collections.sort(filtered, new Comparator() {
137                 public int compare(Object JavaDoc o1, Object JavaDoc o2) {
138                     return((PropertyDescriptor)o1).getName().compareTo(((PropertyDescriptor)o2).getName());
139                 }
140             }
141                              );
142
143             descriptors = new PropertyDescriptor[filtered.size()];
144             filtered.toArray(descriptors);
145
146             String JavaDoc[] items = new String JavaDoc [descriptors.length];
147             for (int i = 0; i < descriptors.length; i++)
148                 items[i] = descriptors[i].getName();
149             propertyList.setListData(items);
150             propertyList.revalidate();
151             propertyList.repaint();
152         }
153     }
154
155     private void updateState() {
156         setPickerValid((getSelectedComponent() != null) &&(getSelectedProperty() != null));
157     }
158
159     /** This method is called from within the constructor to
160      * initialize the form.
161      * WARNING: Do NOT modify this code. The content of this method is
162      * always regenerated by the FormEditor.
163      */

164     private void initComponents() {//GEN-BEGIN:initComponents
165
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
166
167         componentsCombo = new javax.swing.JComboBox JavaDoc();
168         propertiesScrollPane = new javax.swing.JScrollPane JavaDoc();
169         propertyList = new javax.swing.JList JavaDoc();
170         componentLabel = new javax.swing.JLabel JavaDoc();
171         listLabel = new javax.swing.JLabel JavaDoc();
172
173         setLayout(new java.awt.GridBagLayout JavaDoc());
174
175         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(12, 12, 0, 11)));
176         componentsCombo.addItemListener(new java.awt.event.ItemListener JavaDoc() {
177             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
178                 componentsComboItemStateChanged(evt);
179             }
180         });
181
182         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
183         gridBagConstraints.gridx = 1;
184         gridBagConstraints.gridy = 0;
185         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
186         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
187         gridBagConstraints.weightx = 1.0;
188         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 0);
189         add(componentsCombo, gridBagConstraints);
190
191         propertyList.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
192             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
193                 propertyListValueChanged(evt);
194             }
195         });
196
197         propertiesScrollPane.setViewportView(propertyList);
198
199         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
200         gridBagConstraints.gridx = 0;
201         gridBagConstraints.gridy = 2;
202         gridBagConstraints.gridwidth = 2;
203         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
204         gridBagConstraints.weightx = 1.0;
205         gridBagConstraints.weighty = 1.0;
206         add(propertiesScrollPane, gridBagConstraints);
207
208         componentLabel.setLabelFor(componentsCombo);
209         componentLabel.setText(FormUtils.getBundleString("CTL_Component"));
210         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
211         gridBagConstraints.gridx = 0;
212         gridBagConstraints.gridy = 0;
213         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
214         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 6);
215         add(componentLabel, gridBagConstraints);
216
217         listLabel.setLabelFor(propertyList);
218         listLabel.setText(FormUtils.getBundleString("CTL_CW_PropertyList"));
219         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
220         gridBagConstraints.gridx = 0;
221         gridBagConstraints.gridy = 1;
222         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
223         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 2, 0);
224         add(listLabel, gridBagConstraints);
225
226     }//GEN-END:initComponents
227

228
229     private void propertyListValueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_propertyListValueChanged
230
if (propertyList.getSelectedIndex() == -1)
231             selectedProperty = null;
232         else
233             selectedProperty = descriptors[propertyList.getSelectedIndex()];
234         updateState();
235     }//GEN-LAST:event_propertyListValueChanged
236

237     private void componentsComboItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_componentsComboItemStateChanged
238
if (componentsCombo.getSelectedIndex() == -1)
239             selectedComponent = null;
240         else
241             selectedComponent = components[componentsCombo.getSelectedIndex()];
242         updatePropertyList();
243     }//GEN-LAST:event_componentsComboItemStateChanged
244

245     /** Closes the dialog */
246     private void closeDialog(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:closeDialog
247
}//GEN-LAST:closeDialog
248

249     // Variables declaration - do not modify//GEN-BEGIN:variables
250
private javax.swing.JList JavaDoc propertyList;
251     private javax.swing.JLabel JavaDoc listLabel;
252     private javax.swing.JLabel JavaDoc componentLabel;
253     private javax.swing.JComboBox JavaDoc componentsCombo;
254     private javax.swing.JScrollPane JavaDoc propertiesScrollPane;
255     // End of variables declaration//GEN-END:variables
256

257
258     private FormModel formModel;
259     private boolean pickerValid = false;
260
261     private RADComponent[] components;
262     private Class JavaDoc requiredType;
263     private PropertyDescriptor[] descriptors;
264     private RADComponent selectedComponent;
265     private PropertyDescriptor selectedProperty;
266
267 }
268
Popular Tags