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