KickJava   Java API By Example, From Geeks To Geeks.

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


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 MethodPicker is a form which allows user to pick one of methods
28  * with specified required return type.
29  *
30  * @author Ian Formanek
31  * @version 1.00, Aug 29, 1998
32  */

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

66         componentLabel.setDisplayedMnemonic(
67             FormUtils.getBundleString("CTL_CW_Component_Mnemonic").charAt(0)); // NOI18N
68
listLabel.setDisplayedMnemonic(
69             FormUtils.getBundleString("CTL_CW_MethodList_Mnemonic").charAt(0)); // NOI18N
70

71         componentsCombo.getAccessibleContext().setAccessibleDescription(
72             FormUtils.getBundleString("ACSD_CTL_CW_Component")); // NOI18N
73
methodList.getAccessibleContext().setAccessibleDescription(
74             FormUtils.getBundleString("ACSD_CTL_CW_MethodList")); // NOI18N
75
getAccessibleContext().setAccessibleDescription(
76             FormUtils.getBundleString("ACSD_MethodPicker")); // NOI18N
77
// HelpCtx.setHelpIDString(this, "gui.connecting.code"); // NOI18N
78
}
79
80     public boolean isPickerValid() {
81         return pickerValid;
82     }
83     
84     private void setPickerValid(boolean v) {
85         boolean old = pickerValid;
86         pickerValid = v;
87         firePropertyChange("pickerValid", old, pickerValid); // NOI18N
88
}
89
90     RADComponent getSelectedComponent() {
91         return selectedComponent;
92     }
93
94     void setSelectedComponent(RADComponent selectedComponent) {
95         if (selectedComponent != null)
96             componentsCombo.setSelectedItem(selectedComponent.getName());
97     }
98
99     MethodDescriptor getSelectedMethod() {
100         if ((selectedComponent == null) ||(methodList.getSelectedIndex() == -1))
101             return null;
102         return descriptors [methodList.getSelectedIndex()];
103     }
104
105     void setSelectedMethod(MethodDescriptor selectedMethod) {
106         if (selectedMethod == null) {
107             methodList.setSelectedIndex(-1);
108         } else {
109             methodList.setSelectedValue(FormUtils.getMethodName(selectedMethod), true);
110         }
111     }
112
113     // ----------------------------------------------------------------------------
114
// private methods
115

116     private void addComponentsRecursively(ComponentContainer cont, Vector vect) {
117         RADComponent[] children = cont.getSubBeans();
118         for (int i = 0; i < children.length; i++) {
119             vect.addElement(children[i]);
120             if (children[i] instanceof ComponentContainer)
121                 addComponentsRecursively((ComponentContainer)children[i], vect);
122         }
123     }
124
125     private void updateMethodList() {
126         RADComponent sel = getSelectedComponent();
127         if (sel == null) {
128             methodList.setListData(new Object JavaDoc [0]);
129             methodList.revalidate();
130             methodList.repaint();
131         } else {
132             MethodDescriptor[] descs = sel.getBeanInfo().getMethodDescriptors();
133             ArrayList filtered = new ArrayList();
134             for (int i = 0; i < descs.length; i ++) {
135                 if (requiredType.isAssignableFrom(descs[i].getMethod().getReturnType()) &&
136                     (descs[i].getMethod().getParameterTypes().length == 0)) // [FUTURE: - currently we allow only methods without params]
137
{
138                     filtered.add(descs[i]);
139                 }
140             }
141             // sort the methods by name
142
Collections.sort(filtered, new Comparator() {
143                 public int compare(Object JavaDoc o1, Object JavaDoc o2) {
144                     return((MethodDescriptor)o1).getName().compareTo(((MethodDescriptor)o2).getName());
145                 }
146             }
147                              );
148
149             descriptors = new MethodDescriptor[filtered.size()];
150             filtered.toArray(descriptors);
151
152             String JavaDoc[] items = new String JavaDoc [descriptors.length];
153             for (int i = 0; i < descriptors.length; i++)
154                 items[i] = FormUtils.getMethodName(descriptors[i]);
155             methodList.setListData(items);
156             methodList.revalidate();
157             methodList.repaint();
158         }
159     }
160
161     private void updateState() {
162         if ((getSelectedComponent() == null) || (getSelectedMethod() == null)) {
163             setPickerValid(false);
164         } else {
165             setPickerValid(getSelectedMethod().getMethod().getParameterTypes().length == 0);
166         }
167     }
168
169     /** This method is called from within the constructor to
170      * initialize the form.
171      * WARNING: Do NOT modify this code. The content of this method is
172      * always regenerated by the FormEditor.
173      */

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

233     private void methodListValueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_methodListValueChanged
234
if (methodList.getSelectedIndex() == -1)
235             selectedMethod = null;
236         else
237             selectedMethod = descriptors[methodList.getSelectedIndex()];
238         updateState();
239     }//GEN-LAST:event_methodListValueChanged
240

241     private void componentsComboItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_componentsComboItemStateChanged
242
if (componentsCombo.getSelectedIndex() == -1)
243             selectedComponent = null;
244         else
245             selectedComponent = components[componentsCombo.getSelectedIndex()];
246         updateMethodList();
247     }//GEN-LAST:event_componentsComboItemStateChanged
248

249     /** Closes the dialog */
250     private void closeDialog(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:closeDialog
251
}//GEN-LAST:closeDialog
252

253     // Variables declaration - do not modify//GEN-BEGIN:variables
254
private javax.swing.JLabel JavaDoc listLabel;
255     private javax.swing.JLabel JavaDoc componentLabel;
256     private javax.swing.JList JavaDoc methodList;
257     private javax.swing.JComboBox JavaDoc componentsCombo;
258     private javax.swing.JScrollPane JavaDoc propertiesScrollPane;
259     // End of variables declaration//GEN-END:variables
260

261
262     private FormModel formModel;
263     private boolean pickerValid = false;
264
265     private RADComponent[] components;
266     private Class JavaDoc requiredType;
267     private MethodDescriptor[] descriptors;
268     private RADComponent selectedComponent;
269     private MethodDescriptor selectedMethod;
270
271 }
272
Popular Tags