KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > wizard > ConnectionPanel2


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 package org.netbeans.modules.form.wizard;
21
22 import java.beans.*;
23 import java.util.*;
24 import javax.swing.event.*;
25
26 import org.openide.util.NbBundle;
27 import org.netbeans.modules.form.*;
28
29 /**
30  * The UI component of the ConnectionWizardPanel2.
31  *
32  * @author Tomas Pavek
33  */

34
35 class ConnectionPanel2 extends javax.swing.JPanel JavaDoc {
36
37     private ConnectionWizardPanel2 wizardPanel;
38
39     private Object JavaDoc[] propertyListData;
40     private Object JavaDoc[] methodListData;
41     private MethodDescriptor[] methodDescriptors;
42     private PropertyDescriptor[] propDescriptors;
43
44     java.util.ResourceBundle JavaDoc bundle;
45
46     /** Creates new form ConnectionPanel2 */
47     ConnectionPanel2(ConnectionWizardPanel2 wizardPanel) {
48         this.wizardPanel = wizardPanel;
49
50         initComponents();
51
52         bundle = NbBundle.getBundle(ConnectionPanel2.class);
53
54         setName(bundle.getString("CTL_CW_Step2_Title")); // NOI18N
55

56         javax.swing.ButtonGroup JavaDoc gr = new javax.swing.ButtonGroup JavaDoc();
57         gr.add(propertyButton);
58         gr.add(methodButton);
59         gr.add(codeButton);
60
61         targetComponentName.setText(wizardPanel.getTargetComponent().getName());
62
63         actionList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
64         actionList.addListSelectionListener(new ListSelectionListener() {
65             public void valueChanged(ListSelectionEvent evt) {
66                 if (!evt.getValueIsAdjusting())
67                     ConnectionPanel2.this.wizardPanel.fireStateChanged();
68             }
69         });
70
71         // localization code
72
targetNameLabel.setText(
73             bundle.getString("CTL_CW_TargetComponent")); // NOI18N
74
targetNameLabel.setDisplayedMnemonic(
75             bundle.getString("CTL_CW_TargetComponent_Mnemonic").charAt(0)); // NOI18N
76
targetComponentName.setToolTipText(
77             bundle.getString("CTL_CW_TargetComponent_Hint")); // NOI18N
78
propertyButton.setText(bundle.getString("CTL_CW_SetProperty")); // NOI18N
79
propertyButton.setMnemonic(
80             bundle.getString("CTL_CW_SetProperty_Mnemonic").charAt(0)); // NOI18N
81
propertyButton.setToolTipText(
82             bundle.getString("CTL_CW_SetProperty_Hint")); // NOI18N
83
methodButton.setText(bundle.getString("CTL_CW_MethodCall")); // NOI18N
84
methodButton.setMnemonic(
85             bundle.getString("CTL_CW_MethodCall_Mnemonic").charAt(0)); // NOI18N
86
methodButton.setToolTipText(bundle.getString("CTL_CW_MethodCall_Hint"));
87         codeButton.setText(bundle.getString("CTL_CW_XUserCode")); // NOI18N
88
codeButton.setMnemonic(
89             bundle.getString("CTL_CW_XUserCode_Mnemonic").charAt(0)); // NOI18N
90
codeButton.setToolTipText(
91             bundle.getString("CTL_CW_XUserCode_Hint")); // NOI18N
92

93         targetComponentName.getAccessibleContext().setAccessibleDescription(
94             bundle.getString("ACSD_CW_TargetComponent")); // NOI18N
95
propertyButton.getAccessibleContext().setAccessibleDescription(
96             bundle.getString("ACSD_CW_SetProperty")); // NOI18N
97
methodButton.getAccessibleContext().setAccessibleDescription(
98             bundle.getString("ACSD_CW_MethodCall")); // NOI18N
99
codeButton.getAccessibleContext().setAccessibleDescription(
100             bundle.getString("ACSD_CW_XUserCode")); // NOI18N
101
actionList.getAccessibleContext().setAccessibleDescription(
102             bundle.getString("ACSD_CW_ActionList")); // NOI18N
103
getAccessibleContext().setAccessibleDescription(
104             bundle.getString("ACSD_CW_ConnectionPanel2")); // NOI18N
105

106         updateActionList();
107
108         putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(1)); // NOI18N
109
}
110
111     public java.awt.Dimension JavaDoc getPreferredSize() {
112         return new java.awt.Dimension JavaDoc(450, 300);
113     }
114
115     int getActionType() {
116         if (methodButton.isSelected())
117             return ConnectionWizardPanel2.METHOD_TYPE;
118         else if (propertyButton.isSelected())
119             return ConnectionWizardPanel2.PROPERTY_TYPE;
120         else
121             return ConnectionWizardPanel2.CODE_TYPE;
122     }
123
124     MethodDescriptor getSelectedMethod() {
125         if (!methodButton.isSelected() || actionList.getSelectedIndex() == -1)
126             return null;
127         return methodDescriptors[actionList.getSelectedIndex()];
128     }
129
130     PropertyDescriptor getSelectedProperty() {
131         if (!propertyButton.isSelected() || actionList.getSelectedIndex() == -1)
132             return null;
133         return propDescriptors[actionList.getSelectedIndex()];
134     }
135
136     private void updateActionList() {
137         if (codeButton.isSelected()) {
138             actionList.setListData(new String JavaDoc [] {
139                 bundle.getString("CTL_CW_UserCodeText1"), // NOI18N
140
bundle.getString("CTL_CW_UserCodeText2") }); // NOI18N
141
actionList.setEnabled(false);
142             actionList.getAccessibleContext().setAccessibleName(codeButton.getText());
143         }
144         else if (propertyButton.isSelected()) {
145             // properties list
146
actionList.setEnabled(true);
147             if (propertyListData == null) {
148                 BeanInfo targetBeanInfo =
149                     wizardPanel.getTargetComponent().getBeanInfo();
150                 PropertyDescriptor[] descs = targetBeanInfo.getPropertyDescriptors();
151
152                 // filter out read-only properties // [FUTURE: provide also indexed properties]
153
ArrayList list = new ArrayList();
154                 for (int i = 0; i < descs.length; i++) {
155                     if (descs[i].getWriteMethod() != null) {
156                         list.add(descs[i]);
157                     }
158                 }
159
160                 // sort the properties by name
161
Collections.sort(list, new Comparator() {
162                     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
163                         return((PropertyDescriptor)o1).getName().compareTo(((PropertyDescriptor)o2).getName());
164                     }
165                 });
166
167                 propDescriptors = new PropertyDescriptor [list.size()];
168                 list.toArray(propDescriptors);
169
170                 propertyListData = new String JavaDoc [propDescriptors.length];
171                 for (int i = 0; i < propDescriptors.length; i++) {
172                     propertyListData [i] = propDescriptors [i].getName();
173                 }
174             }
175             actionList.setListData(propertyListData);
176             actionList.getAccessibleContext().setAccessibleName(propertyButton.getText());
177         }
178         else {
179             // methods list
180
actionList.setEnabled(true);
181             if (methodListData == null) {
182                 BeanInfo targetBeanInfo =
183                     wizardPanel.getTargetComponent().getBeanInfo();
184                 methodDescriptors = targetBeanInfo.getMethodDescriptors();
185                 ArrayList list = new ArrayList();
186                 for (int i = 0; i < methodDescriptors.length; i++) {
187                     list.add(methodDescriptors[i]);
188                 }
189
190                 // sort the methods by name
191
Collections.sort(list, new Comparator() {
192                     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
193                         return((MethodDescriptor)o1).getName().compareTo(((MethodDescriptor)o2).getName());
194                     }
195                 });
196
197                 // copy it back to the array as it is used later
198
list.toArray(methodDescriptors);
199
200                 methodListData = new String JavaDoc [list.size()];
201                 int i = 0;
202                 for (Iterator it = list.iterator(); it.hasNext();) {
203                     methodListData [i++] = getMethodName((MethodDescriptor)it.next());
204                 }
205             }
206             actionList.setListData(methodListData);
207             actionList.getAccessibleContext().setAccessibleName(methodButton.getText());
208         }
209         actionList.revalidate();
210         actionList.repaint();
211     }
212
213     private static String JavaDoc getMethodName(MethodDescriptor desc) {
214         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(desc.getName());
215         Class JavaDoc[] params = desc.getMethod().getParameterTypes();
216         if ((params == null) ||(params.length == 0)) {
217             sb.append("()"); // NOI18N
218
} else {
219             for (int i = 0; i < params.length; i++) {
220                 if (i == 0) sb.append("("); // NOI18N
221
else sb.append(", "); // NOI18N
222
sb.append(org.openide.util.Utilities.getShortClassName(params[i]));
223             }
224             sb.append(")"); // NOI18N
225
}
226
227         return sb.toString();
228     }
229
230     /** This method is called from within the constructor to
231      * initialize the form.
232      * WARNING: Do NOT modify this code. The content of this method is
233      * always regenerated by the Form Editor.
234      */

235     private void initComponents() {//GEN-BEGIN:initComponents
236
targerInfoPanel = new javax.swing.JPanel JavaDoc();
237         targetNamePanel = new javax.swing.JPanel JavaDoc();
238         targetNameLabel = new javax.swing.JLabel JavaDoc();
239         targetComponentName = new javax.swing.JTextField JavaDoc();
240         actionTypePanel = new javax.swing.JPanel JavaDoc();
241         propertyButton = new javax.swing.JRadioButton JavaDoc();
242         methodButton = new javax.swing.JRadioButton JavaDoc();
243         codeButton = new javax.swing.JRadioButton JavaDoc();
244         actionPanel = new javax.swing.JScrollPane JavaDoc();
245         actionList = new javax.swing.JList JavaDoc();
246
247         setLayout(new java.awt.BorderLayout JavaDoc(0, 2));
248
249         targerInfoPanel.setLayout(new java.awt.BorderLayout JavaDoc(0, 6));
250
251         targetNamePanel.setLayout(new java.awt.FlowLayout JavaDoc(java.awt.FlowLayout.LEFT, 0, 0));
252
253         targetNameLabel.setLabelFor(targetComponentName);
254         targetNameLabel.setText("Target Component:");
255         targetNameLabel.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(0, 0, 0, 6)));
256         targetNamePanel.add(targetNameLabel);
257
258         targetComponentName.setEditable(false);
259         targetComponentName.setText("jTextField1");
260         targetNamePanel.add(targetComponentName);
261
262         targerInfoPanel.add(targetNamePanel, java.awt.BorderLayout.NORTH);
263
264         actionTypePanel.setLayout(new java.awt.FlowLayout JavaDoc(java.awt.FlowLayout.LEFT, 0, 0));
265
266         propertyButton.setSelected(true);
267         propertyButton.setText("Set Property");
268         propertyButton.setMargin(new java.awt.Insets JavaDoc(2, 2, 2, 10));
269         propertyButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
270             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
271                 ConnectionPanel2.this.actionTypeButtonPressed(evt);
272             }
273         });
274
275         actionTypePanel.add(propertyButton);
276
277         methodButton.setText("Method Call");
278         methodButton.setMargin(new java.awt.Insets JavaDoc(2, 2, 2, 10));
279         methodButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
280             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
281                 ConnectionPanel2.this.actionTypeButtonPressed(evt);
282             }
283         });
284
285         actionTypePanel.add(methodButton);
286
287         codeButton.setText("User Code");
288         codeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
289             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
290                 ConnectionPanel2.this.actionTypeButtonPressed(evt);
291             }
292         });
293
294         actionTypePanel.add(codeButton);
295
296         targerInfoPanel.add(actionTypePanel, java.awt.BorderLayout.CENTER);
297
298         add(targerInfoPanel, java.awt.BorderLayout.NORTH);
299
300         actionPanel.setViewportView(actionList);
301
302         add(actionPanel, java.awt.BorderLayout.CENTER);
303
304     }//GEN-END:initComponents
305

306     private void actionTypeButtonPressed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_actionTypeButtonPressed
307
updateActionList();
308         wizardPanel.fireStateChanged();
309         if (evt.getSource() != codeButton)
310             actionList.requestFocus();
311     }//GEN-LAST:event_actionTypeButtonPressed
312

313     // Variables declaration - do not modify//GEN-BEGIN:variables
314
private javax.swing.JRadioButton JavaDoc codeButton;
315     private javax.swing.JList JavaDoc actionList;
316     private javax.swing.JRadioButton JavaDoc methodButton;
317     private javax.swing.JScrollPane JavaDoc actionPanel;
318     private javax.swing.JPanel JavaDoc targetNamePanel;
319     private javax.swing.JRadioButton JavaDoc propertyButton;
320     private javax.swing.JPanel JavaDoc targerInfoPanel;
321     private javax.swing.JLabel JavaDoc targetNameLabel;
322     private javax.swing.JTextField JavaDoc targetComponentName;
323     private javax.swing.JPanel JavaDoc actionTypePanel;
324     // End of variables declaration//GEN-END:variables
325
}
326
Popular Tags