File: ConnectionPanel2.java

package info (click to toggle)
libnb-platform18-java 12.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 729,624 kB
  • sloc: java: 5,058,967; xml: 574,432; php: 78,788; javascript: 29,039; ansic: 10,278; sh: 6,386; cpp: 4,612; jsp: 3,643; sql: 1,097; makefile: 540; objc: 288; perl: 277; haskell: 93
file content (325 lines) | stat: -rw-r--r-- 13,359 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.netbeans.modules.form.wizard;

import java.beans.*;
import java.util.*;
import javax.swing.event.*;

import org.openide.util.NbBundle;
import org.netbeans.modules.form.*;

/**
 * The UI component of the ConnectionWizardPanel2.
 *
 * @author Tomas Pavek
 */

class ConnectionPanel2 extends javax.swing.JPanel {

    private ConnectionWizardPanel2 wizardPanel;

    private Object[] propertyListData;
    private Object[] methodListData;
    private MethodDescriptor[] methodDescriptors;
    private PropertyDescriptor[] propDescriptors;

    java.util.ResourceBundle bundle;

    /** Creates new form ConnectionPanel2 */
    ConnectionPanel2(ConnectionWizardPanel2 wizardPanel) {
        this.wizardPanel = wizardPanel;

        initComponents();

        bundle = NbBundle.getBundle(ConnectionPanel2.class);

        setName(bundle.getString("CTL_CW_Step2_Title")); // NOI18N

        javax.swing.ButtonGroup gr = new javax.swing.ButtonGroup();
        gr.add(propertyButton);
        gr.add(methodButton);
        gr.add(codeButton);

        targetComponentName.setText(wizardPanel.getTargetComponent().getName());

        actionList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        actionList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent evt) {
                if (!evt.getValueIsAdjusting())
                    ConnectionPanel2.this.wizardPanel.fireStateChanged();
            }
        });

        // localization code
        targetNameLabel.setText(
            bundle.getString("CTL_CW_TargetComponent")); // NOI18N
        targetNameLabel.setDisplayedMnemonic(
            bundle.getString("CTL_CW_TargetComponent_Mnemonic").charAt(0)); // NOI18N
        targetComponentName.setToolTipText(
            bundle.getString("CTL_CW_TargetComponent_Hint")); // NOI18N
        propertyButton.setText(bundle.getString("CTL_CW_SetProperty")); // NOI18N
        propertyButton.setMnemonic(
            bundle.getString("CTL_CW_SetProperty_Mnemonic").charAt(0)); // NOI18N
        propertyButton.setToolTipText(
            bundle.getString("CTL_CW_SetProperty_Hint")); // NOI18N
        methodButton.setText(bundle.getString("CTL_CW_MethodCall")); // NOI18N
        methodButton.setMnemonic(
            bundle.getString("CTL_CW_MethodCall_Mnemonic").charAt(0)); // NOI18N
        methodButton.setToolTipText(bundle.getString("CTL_CW_MethodCall_Hint"));
        codeButton.setText(bundle.getString("CTL_CW_XUserCode")); // NOI18N
        codeButton.setMnemonic(
            bundle.getString("CTL_CW_XUserCode_Mnemonic").charAt(0)); // NOI18N
        codeButton.setToolTipText(
            bundle.getString("CTL_CW_XUserCode_Hint")); // NOI18N

        targetComponentName.getAccessibleContext().setAccessibleDescription(
            bundle.getString("ACSD_CW_TargetComponent")); // NOI18N
        propertyButton.getAccessibleContext().setAccessibleDescription(
            bundle.getString("ACSD_CW_SetProperty")); // NOI18N
        methodButton.getAccessibleContext().setAccessibleDescription(
            bundle.getString("ACSD_CW_MethodCall")); // NOI18N
        codeButton.getAccessibleContext().setAccessibleDescription(
            bundle.getString("ACSD_CW_XUserCode")); // NOI18N
        actionList.getAccessibleContext().setAccessibleDescription(
            bundle.getString("ACSD_CW_ActionList")); // NOI18N
        getAccessibleContext().setAccessibleDescription(
            bundle.getString("ACSD_CW_ConnectionPanel2")); // NOI18N

        updateActionList();

        putClientProperty(WizardDescriptor.PROP_CONTENT_SELECTED_INDEX, new Integer(1)); // NOI18N
    }

    public java.awt.Dimension getPreferredSize() {
        return new java.awt.Dimension(450, 300);
    }

    int getActionType() {
        if (methodButton.isSelected())
            return ConnectionWizardPanel2.METHOD_TYPE;
        else if (propertyButton.isSelected())
            return ConnectionWizardPanel2.PROPERTY_TYPE;
        else
            return ConnectionWizardPanel2.CODE_TYPE;
    }

    MethodDescriptor getSelectedMethod() {
        if (!methodButton.isSelected() || actionList.getSelectedIndex() == -1)
            return null;
        return methodDescriptors[actionList.getSelectedIndex()];
    }

    PropertyDescriptor getSelectedProperty() {
        if (!propertyButton.isSelected() || actionList.getSelectedIndex() == -1)
            return null;
        return propDescriptors[actionList.getSelectedIndex()];
    }

    private void updateActionList() {
        if (codeButton.isSelected()) {
            actionList.setListData(new String [] {
                bundle.getString("CTL_CW_UserCodeText1"), // NOI18N
                bundle.getString("CTL_CW_UserCodeText2") }); // NOI18N
            actionList.setEnabled(false);
            actionList.getAccessibleContext().setAccessibleName(codeButton.getText());
        } 
        else if (propertyButton.isSelected()) {
            // properties list
            actionList.setEnabled(true);
            if (propertyListData == null) {
                BeanInfo targetBeanInfo =
                    wizardPanel.getTargetComponent().getBeanInfo();
                PropertyDescriptor[] descs = targetBeanInfo.getPropertyDescriptors();

                // filter out read-only properties // [FUTURE: provide also indexed properties]
                ArrayList list = new ArrayList();
                for (int i = 0; i < descs.length; i++) {
                    if (descs[i].getWriteMethod() != null) {
                        list.add(descs[i]);
                    }
                }

                // sort the properties by name
                Collections.sort(list, new Comparator() {
                    public int compare(Object o1, Object o2) {
                        return((PropertyDescriptor)o1).getName().compareTo(((PropertyDescriptor)o2).getName());
                    }
                });

                propDescriptors = new PropertyDescriptor [list.size()];
                list.toArray(propDescriptors);

                propertyListData = new String [propDescriptors.length];
                for (int i = 0; i < propDescriptors.length; i++) {
                    propertyListData [i] = propDescriptors [i].getName();
                }
            }
            actionList.setListData(propertyListData);
            actionList.getAccessibleContext().setAccessibleName(propertyButton.getText());
        } 
        else {
            // methods list
            actionList.setEnabled(true);
            if (methodListData == null) {
                BeanInfo targetBeanInfo =
                    wizardPanel.getTargetComponent().getBeanInfo();
                methodDescriptors = targetBeanInfo.getMethodDescriptors();
                ArrayList list = new ArrayList();
                for (int i = 0; i < methodDescriptors.length; i++) {
                    list.add(methodDescriptors[i]);
                }

                // sort the methods by name
                Collections.sort(list, new Comparator() {
                    public int compare(Object o1, Object o2) {
                        return((MethodDescriptor)o1).getName().compareTo(((MethodDescriptor)o2).getName());
                    }
                });

                // copy it back to the array as it is used later
                list.toArray(methodDescriptors);

                methodListData = new String [list.size()];
                int i = 0;
                for (Iterator it = list.iterator(); it.hasNext();) {
                    methodListData [i++] = getMethodName((MethodDescriptor)it.next());
                }
            }
            actionList.setListData(methodListData);
            actionList.getAccessibleContext().setAccessibleName(methodButton.getText());
        }
        actionList.revalidate();
        actionList.repaint();
    }

    private static String getMethodName(MethodDescriptor desc) {
        StringBuffer sb = new StringBuffer(desc.getName());
        Class[] params = desc.getMethod().getParameterTypes();
        if ((params == null) ||(params.length == 0)) {
            sb.append("()"); // NOI18N
        } else {
            for (int i = 0; i < params.length; i++) {
                if (i == 0) sb.append("("); // NOI18N
                else sb.append(", "); // NOI18N
                sb.append(org.openide.util.Utilities.getShortClassName(params[i]));
            }
            sb.append(")"); // NOI18N
        }

        return sb.toString();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        targerInfoPanel = new javax.swing.JPanel();
        targetNamePanel = new javax.swing.JPanel();
        targetNameLabel = new javax.swing.JLabel();
        targetComponentName = new javax.swing.JTextField();
        actionTypePanel = new javax.swing.JPanel();
        propertyButton = new javax.swing.JRadioButton();
        methodButton = new javax.swing.JRadioButton();
        codeButton = new javax.swing.JRadioButton();
        actionPanel = new javax.swing.JScrollPane();
        actionList = new javax.swing.JList();

        setLayout(new java.awt.BorderLayout(0, 2));

        targerInfoPanel.setLayout(new java.awt.BorderLayout(0, 6));

        targetNamePanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));

        targetNameLabel.setLabelFor(targetComponentName);
        targetNameLabel.setText("Target Component:");
        targetNameLabel.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(0, 0, 0, 6)));
        targetNamePanel.add(targetNameLabel);

        targetComponentName.setEditable(false);
        targetComponentName.setText("jTextField1");
        targetNamePanel.add(targetComponentName);

        targerInfoPanel.add(targetNamePanel, java.awt.BorderLayout.NORTH);

        actionTypePanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));

        propertyButton.setSelected(true);
        propertyButton.setText("Set Property");
        propertyButton.setMargin(new java.awt.Insets(2, 2, 2, 10));
        propertyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConnectionPanel2.this.actionTypeButtonPressed(evt);
            }
        });

        actionTypePanel.add(propertyButton);

        methodButton.setText("Method Call");
        methodButton.setMargin(new java.awt.Insets(2, 2, 2, 10));
        methodButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConnectionPanel2.this.actionTypeButtonPressed(evt);
            }
        });

        actionTypePanel.add(methodButton);

        codeButton.setText("User Code");
        codeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConnectionPanel2.this.actionTypeButtonPressed(evt);
            }
        });

        actionTypePanel.add(codeButton);

        targerInfoPanel.add(actionTypePanel, java.awt.BorderLayout.CENTER);

        add(targerInfoPanel, java.awt.BorderLayout.NORTH);

        actionPanel.setViewportView(actionList);

        add(actionPanel, java.awt.BorderLayout.CENTER);

    }//GEN-END:initComponents

    private void actionTypeButtonPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actionTypeButtonPressed
        updateActionList();
        wizardPanel.fireStateChanged();
        if (evt.getSource() != codeButton)
            actionList.requestFocus();
    }//GEN-LAST:event_actionTypeButtonPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton codeButton;
    private javax.swing.JList actionList;
    private javax.swing.JRadioButton methodButton;
    private javax.swing.JScrollPane actionPanel;
    private javax.swing.JPanel targetNamePanel;
    private javax.swing.JRadioButton propertyButton;
    private javax.swing.JPanel targerInfoPanel;
    private javax.swing.JLabel targetNameLabel;
    private javax.swing.JTextField targetComponentName;
    private javax.swing.JPanel actionTypePanel;
    // End of variables declaration//GEN-END:variables
}