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.wizard;
043:
044: import java.beans.*;
045: import java.util.*;
046: import javax.swing.event.*;
047:
048: import org.openide.util.NbBundle;
049: import org.netbeans.modules.form.*;
050:
051: /**
052: * The UI component of the ConnectionWizardPanel2.
053: *
054: * @author Tomas Pavek
055: */
056:
057: class ConnectionPanel2 extends javax.swing.JPanel {
058:
059: private ConnectionWizardPanel2 wizardPanel;
060:
061: private Object[] propertyListData;
062: private Object[] methodListData;
063: private MethodDescriptor[] methodDescriptors;
064: private PropertyDescriptor[] propDescriptors;
065:
066: java.util.ResourceBundle bundle;
067:
068: /** Creates new form ConnectionPanel2 */
069: ConnectionPanel2(ConnectionWizardPanel2 wizardPanel) {
070: this .wizardPanel = wizardPanel;
071:
072: initComponents();
073:
074: bundle = NbBundle.getBundle(ConnectionPanel2.class);
075:
076: setName(bundle.getString("CTL_CW_Step2_Title")); // NOI18N
077:
078: javax.swing.ButtonGroup gr = new javax.swing.ButtonGroup();
079: gr.add(propertyButton);
080: gr.add(methodButton);
081: gr.add(codeButton);
082:
083: targetComponentName.setText(wizardPanel.getTargetComponent()
084: .getName());
085:
086: actionList
087: .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
088: actionList
089: .addListSelectionListener(new ListSelectionListener() {
090: public void valueChanged(ListSelectionEvent evt) {
091: if (!evt.getValueIsAdjusting())
092: ConnectionPanel2.this .wizardPanel
093: .fireStateChanged();
094: }
095: });
096:
097: // localization code
098: targetNameLabel.setText(bundle
099: .getString("CTL_CW_TargetComponent")); // NOI18N
100: targetNameLabel.setDisplayedMnemonic(bundle.getString(
101: "CTL_CW_TargetComponent_Mnemonic").charAt(0)); // NOI18N
102: targetComponentName.setToolTipText(bundle
103: .getString("CTL_CW_TargetComponent_Hint")); // NOI18N
104: propertyButton.setText(bundle.getString("CTL_CW_SetProperty")); // NOI18N
105: propertyButton.setMnemonic(bundle.getString(
106: "CTL_CW_SetProperty_Mnemonic").charAt(0)); // NOI18N
107: propertyButton.setToolTipText(bundle
108: .getString("CTL_CW_SetProperty_Hint")); // NOI18N
109: methodButton.setText(bundle.getString("CTL_CW_MethodCall")); // NOI18N
110: methodButton.setMnemonic(bundle.getString(
111: "CTL_CW_MethodCall_Mnemonic").charAt(0)); // NOI18N
112: methodButton.setToolTipText(bundle
113: .getString("CTL_CW_MethodCall_Hint"));
114: codeButton.setText(bundle.getString("CTL_CW_XUserCode")); // NOI18N
115: codeButton.setMnemonic(bundle.getString(
116: "CTL_CW_XUserCode_Mnemonic").charAt(0)); // NOI18N
117: codeButton.setToolTipText(bundle
118: .getString("CTL_CW_XUserCode_Hint")); // NOI18N
119:
120: targetComponentName.getAccessibleContext()
121: .setAccessibleDescription(
122: bundle.getString("ACSD_CW_TargetComponent")); // NOI18N
123: propertyButton.getAccessibleContext().setAccessibleDescription(
124: bundle.getString("ACSD_CW_SetProperty")); // NOI18N
125: methodButton.getAccessibleContext().setAccessibleDescription(
126: bundle.getString("ACSD_CW_MethodCall")); // NOI18N
127: codeButton.getAccessibleContext().setAccessibleDescription(
128: bundle.getString("ACSD_CW_XUserCode")); // NOI18N
129: actionList.getAccessibleContext().setAccessibleDescription(
130: bundle.getString("ACSD_CW_ActionList")); // NOI18N
131: getAccessibleContext().setAccessibleDescription(
132: bundle.getString("ACSD_CW_ConnectionPanel2")); // NOI18N
133:
134: updateActionList();
135:
136: putClientProperty("WizardPanel_contentSelectedIndex",
137: new Integer(1)); // NOI18N
138: }
139:
140: public java.awt.Dimension getPreferredSize() {
141: return new java.awt.Dimension(450, 300);
142: }
143:
144: int getActionType() {
145: if (methodButton.isSelected())
146: return ConnectionWizardPanel2.METHOD_TYPE;
147: else if (propertyButton.isSelected())
148: return ConnectionWizardPanel2.PROPERTY_TYPE;
149: else
150: return ConnectionWizardPanel2.CODE_TYPE;
151: }
152:
153: MethodDescriptor getSelectedMethod() {
154: if (!methodButton.isSelected()
155: || actionList.getSelectedIndex() == -1)
156: return null;
157: return methodDescriptors[actionList.getSelectedIndex()];
158: }
159:
160: PropertyDescriptor getSelectedProperty() {
161: if (!propertyButton.isSelected()
162: || actionList.getSelectedIndex() == -1)
163: return null;
164: return propDescriptors[actionList.getSelectedIndex()];
165: }
166:
167: private void updateActionList() {
168: if (codeButton.isSelected()) {
169: actionList.setListData(new String[] {
170: bundle.getString("CTL_CW_UserCodeText1"), // NOI18N
171: bundle.getString("CTL_CW_UserCodeText2") }); // NOI18N
172: actionList.setEnabled(false);
173: actionList.getAccessibleContext().setAccessibleName(
174: codeButton.getText());
175: } else if (propertyButton.isSelected()) {
176: // properties list
177: actionList.setEnabled(true);
178: if (propertyListData == null) {
179: BeanInfo targetBeanInfo = wizardPanel
180: .getTargetComponent().getBeanInfo();
181: PropertyDescriptor[] descs = targetBeanInfo
182: .getPropertyDescriptors();
183:
184: // filter out read-only properties // [FUTURE: provide also indexed properties]
185: ArrayList list = new ArrayList();
186: for (int i = 0; i < descs.length; i++) {
187: if (descs[i].getWriteMethod() != null) {
188: list.add(descs[i]);
189: }
190: }
191:
192: // sort the properties by name
193: Collections.sort(list, new Comparator() {
194: public int compare(Object o1, Object o2) {
195: return ((PropertyDescriptor) o1).getName()
196: .compareTo(
197: ((PropertyDescriptor) o2)
198: .getName());
199: }
200: });
201:
202: propDescriptors = new PropertyDescriptor[list.size()];
203: list.toArray(propDescriptors);
204:
205: propertyListData = new String[propDescriptors.length];
206: for (int i = 0; i < propDescriptors.length; i++) {
207: propertyListData[i] = propDescriptors[i].getName();
208: }
209: }
210: actionList.setListData(propertyListData);
211: actionList.getAccessibleContext().setAccessibleName(
212: propertyButton.getText());
213: } else {
214: // methods list
215: actionList.setEnabled(true);
216: if (methodListData == null) {
217: BeanInfo targetBeanInfo = wizardPanel
218: .getTargetComponent().getBeanInfo();
219: methodDescriptors = targetBeanInfo
220: .getMethodDescriptors();
221: ArrayList list = new ArrayList();
222: for (int i = 0; i < methodDescriptors.length; i++) {
223: list.add(methodDescriptors[i]);
224: }
225:
226: // sort the methods by name
227: Collections.sort(list, new Comparator() {
228: public int compare(Object o1, Object o2) {
229: return ((MethodDescriptor) o1).getName()
230: .compareTo(
231: ((MethodDescriptor) o2)
232: .getName());
233: }
234: });
235:
236: // copy it back to the array as it is used later
237: list.toArray(methodDescriptors);
238:
239: methodListData = new String[list.size()];
240: int i = 0;
241: for (Iterator it = list.iterator(); it.hasNext();) {
242: methodListData[i++] = getMethodName((MethodDescriptor) it
243: .next());
244: }
245: }
246: actionList.setListData(methodListData);
247: actionList.getAccessibleContext().setAccessibleName(
248: methodButton.getText());
249: }
250: actionList.revalidate();
251: actionList.repaint();
252: }
253:
254: private static String getMethodName(MethodDescriptor desc) {
255: StringBuffer sb = new StringBuffer(desc.getName());
256: Class[] params = desc.getMethod().getParameterTypes();
257: if ((params == null) || (params.length == 0)) {
258: sb.append("()"); // NOI18N
259: } else {
260: for (int i = 0; i < params.length; i++) {
261: if (i == 0)
262: sb.append("("); // NOI18N
263: else
264: sb.append(", "); // NOI18N
265: sb.append(org.openide.util.Utilities
266: .getShortClassName(params[i]));
267: }
268: sb.append(")"); // NOI18N
269: }
270:
271: return sb.toString();
272: }
273:
274: /** This method is called from within the constructor to
275: * initialize the form.
276: * WARNING: Do NOT modify this code. The content of this method is
277: * always regenerated by the Form Editor.
278: */
279: private void initComponents() {//GEN-BEGIN:initComponents
280: targerInfoPanel = new javax.swing.JPanel();
281: targetNamePanel = new javax.swing.JPanel();
282: targetNameLabel = new javax.swing.JLabel();
283: targetComponentName = new javax.swing.JTextField();
284: actionTypePanel = new javax.swing.JPanel();
285: propertyButton = new javax.swing.JRadioButton();
286: methodButton = new javax.swing.JRadioButton();
287: codeButton = new javax.swing.JRadioButton();
288: actionPanel = new javax.swing.JScrollPane();
289: actionList = new javax.swing.JList();
290:
291: setLayout(new java.awt.BorderLayout(0, 2));
292:
293: targerInfoPanel.setLayout(new java.awt.BorderLayout(0, 6));
294:
295: targetNamePanel.setLayout(new java.awt.FlowLayout(
296: java.awt.FlowLayout.LEFT, 0, 0));
297:
298: targetNameLabel.setLabelFor(targetComponentName);
299: targetNameLabel.setText("Target Component:");
300: targetNameLabel.setBorder(new javax.swing.border.EmptyBorder(
301: new java.awt.Insets(0, 0, 0, 6)));
302: targetNamePanel.add(targetNameLabel);
303:
304: targetComponentName.setEditable(false);
305: targetComponentName.setText("jTextField1");
306: targetNamePanel.add(targetComponentName);
307:
308: targerInfoPanel.add(targetNamePanel,
309: java.awt.BorderLayout.NORTH);
310:
311: actionTypePanel.setLayout(new java.awt.FlowLayout(
312: java.awt.FlowLayout.LEFT, 0, 0));
313:
314: propertyButton.setSelected(true);
315: propertyButton.setText("Set Property");
316: propertyButton.setMargin(new java.awt.Insets(2, 2, 2, 10));
317: propertyButton
318: .addActionListener(new java.awt.event.ActionListener() {
319: public void actionPerformed(
320: java.awt.event.ActionEvent evt) {
321: ConnectionPanel2.this
322: .actionTypeButtonPressed(evt);
323: }
324: });
325:
326: actionTypePanel.add(propertyButton);
327:
328: methodButton.setText("Method Call");
329: methodButton.setMargin(new java.awt.Insets(2, 2, 2, 10));
330: methodButton
331: .addActionListener(new java.awt.event.ActionListener() {
332: public void actionPerformed(
333: java.awt.event.ActionEvent evt) {
334: ConnectionPanel2.this
335: .actionTypeButtonPressed(evt);
336: }
337: });
338:
339: actionTypePanel.add(methodButton);
340:
341: codeButton.setText("User Code");
342: codeButton
343: .addActionListener(new java.awt.event.ActionListener() {
344: public void actionPerformed(
345: java.awt.event.ActionEvent evt) {
346: ConnectionPanel2.this
347: .actionTypeButtonPressed(evt);
348: }
349: });
350:
351: actionTypePanel.add(codeButton);
352:
353: targerInfoPanel.add(actionTypePanel,
354: java.awt.BorderLayout.CENTER);
355:
356: add(targerInfoPanel, java.awt.BorderLayout.NORTH);
357:
358: actionPanel.setViewportView(actionList);
359:
360: add(actionPanel, java.awt.BorderLayout.CENTER);
361:
362: }//GEN-END:initComponents
363:
364: private void actionTypeButtonPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actionTypeButtonPressed
365: updateActionList();
366: wizardPanel.fireStateChanged();
367: if (evt.getSource() != codeButton)
368: actionList.requestFocus();
369: }//GEN-LAST:event_actionTypeButtonPressed
370:
371: // Variables declaration - do not modify//GEN-BEGIN:variables
372: private javax.swing.JRadioButton codeButton;
373: private javax.swing.JList actionList;
374: private javax.swing.JRadioButton methodButton;
375: private javax.swing.JScrollPane actionPanel;
376: private javax.swing.JPanel targetNamePanel;
377: private javax.swing.JRadioButton propertyButton;
378: private javax.swing.JPanel targerInfoPanel;
379: private javax.swing.JLabel targetNameLabel;
380: private javax.swing.JTextField targetComponentName;
381: private javax.swing.JPanel actionTypePanel;
382: // End of variables declaration//GEN-END:variables
383: }
|