KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
23 import java.beans.EventSetDescriptor JavaDoc;
24 import javax.swing.tree.*;
25 import javax.swing.event.*;
26
27 import org.openide.awt.Mnemonics;
28 import org.openide.util.NbBundle;
29 import org.netbeans.modules.form.*;
30
31
32 /**
33  * The UI component of the ConnectionWizardPanel1.
34  *
35  * @author Tomas Pavek
36  */

37
38 class ConnectionPanel1 extends javax.swing.JPanel JavaDoc {
39
40     private ConnectionWizardPanel1 wizardPanel;
41
42     /** Creates new form ConnectionPanel1 */
43     ConnectionPanel1(ConnectionWizardPanel1 wizardPanel) {
44         this.wizardPanel = wizardPanel;
45
46         initComponents();
47
48         RADComponent source = wizardPanel.getSourceComponent();
49
50         java.util.ResourceBundle JavaDoc bundle = NbBundle.getBundle(ConnectionPanel1.class);
51
52         setName(bundle.getString("CTL_CW_Step1_Title")); // NOI18N
53
sourceComponentName.setText(source.getName());
54
55         eventNameCombo.setEnabled(wizardPanel.getSelectedEvent() != null);
56
57         eventNameCombo.addActionListener(new java.awt.event.ActionListener JavaDoc() {
58             public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
59                 ConnectionPanel1.this.wizardPanel.fireStateChanged();
60             }
61         });
62
63         eventNameCombo.getEditor().addActionListener(new java.awt.event.ActionListener JavaDoc() {
64             public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
65                 ConnectionPanel1.this.wizardPanel.fireStateChanged();
66             }
67         });
68
69          // populate event tree
70
final Vector eventNodes = new Vector();
71         TreeNode rootNode = new TreeNode() {
72             public TreeNode getChildAt(int childIndex) {
73                 return(TreeNode) eventNodes.elementAt(childIndex);
74             }
75             public int getChildCount() {
76                 return eventNodes.size();
77             }
78             public TreeNode getParent() {
79                 return null;
80             }
81             public int getIndex(TreeNode node) {
82                 return eventNodes.indexOf(node);
83             }
84             public boolean getAllowsChildren() {
85                 return true;
86             }
87             public boolean isLeaf() {
88                 return false;
89             }
90             public Enumeration children() {
91                 return eventNodes.elements();
92             }
93         };
94
95         EventSetDescriptor JavaDoc lastEventSetDesc = null;
96         TreeNode eventSetNode = null;
97         List eventSetEvents = null;
98
99         Event[] events = source.getAllEvents();
100         for (int i=0; i < events.length; i++) {
101             Event event = events[i];
102             EventSetDescriptor JavaDoc eventSetDesc = event.getEventSetDescriptor();
103
104             if (eventSetDesc != lastEventSetDesc) {
105                 eventSetEvents = new ArrayList();
106                 eventSetNode = new EventSetNode(eventSetDesc.getName(),
107                                                 eventSetEvents);
108                 eventNodes.add(eventSetNode);
109                 lastEventSetDesc = eventSetDesc;
110             }
111
112             eventSetEvents.add(new EventNode(eventSetNode, event));
113         }
114
115         DefaultTreeSelectionModel treeSelectionModel = new DefaultTreeSelectionModel();
116         treeSelectionModel.addTreeSelectionListener(new TreeSelectionListener() {
117             public void valueChanged(TreeSelectionEvent evt) {
118                 TreePath[] paths = eventSelectTree.getSelectionPaths();
119                 if ((paths != null) &&(paths.length == 1)) {
120                     TreeNode node =(TreeNode) paths[0].getLastPathComponent();
121                     if ((node != null) &&(node instanceof EventNode)) {
122                         setSelectedEvent(((EventNode)node).getEvent());
123                         return;
124                     }
125                 }
126                 setSelectedEvent(null);
127             }
128         });
129
130         treeSelectionModel.setSelectionMode(DefaultTreeSelectionModel.SINGLE_TREE_SELECTION);
131         eventSelectTree.setModel(new DefaultTreeModel(rootNode));
132         eventSelectTree.setSelectionModel(treeSelectionModel);
133
134         // localization code
135
Mnemonics.setLocalizedText(sourceNameLabel, bundle.getString("CTL_CW_SourceComponent")); // NOI18N
136
sourceComponentName.setToolTipText(
137             bundle.getString("CTL_CW_SourceComponent_Hint")); // NOI18N
138
Mnemonics.setLocalizedText(eventSelectLabel, bundle.getString("CTL_CW_Event")); // NOI18N
139
eventSelectTree.setToolTipText(bundle.getString("CTL_CW_Event_Hint"));
140         sourcePanel.setToolTipText(bundle.getString("CTL_CW_Event_Hint"));
141         eventHandlerPanel.setBorder(new javax.swing.border.CompoundBorder JavaDoc(
142                 new javax.swing.border.TitledBorder JavaDoc(
143                     new javax.swing.border.EtchedBorder JavaDoc(),
144                     bundle.getString("CTL_CW_EventHandlerMethod")), // NOI18N
145
new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(5, 5, 5, 5))));
146         Mnemonics.setLocalizedText(eventNameLabel, bundle.getString("CTL_CW_MethodName")); // NOI18N
147

148         eventSelectTree.getAccessibleContext().setAccessibleDescription(
149             bundle.getString("ACSD_CW_EventTree")); // NOI18N
150
eventNameCombo.getAccessibleContext().setAccessibleDescription(
151             bundle.getString("ACSD_CW_MethodName")); // NOI18N
152
sourceComponentName.getAccessibleContext().setAccessibleDescription(
153             bundle.getString("ACSD_CW_SourceComponent")); // NOI18N
154
getAccessibleContext().setAccessibleDescription(
155             bundle.getString("ACSD_CW_ConnectionPanel1")); // NOI18N
156
putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(0)); // NOI18N
157
}
158
159     public java.awt.Dimension JavaDoc getPreferredSize() {
160         return new java.awt.Dimension JavaDoc(450, 300);
161     }
162
163     String JavaDoc getEventName() {
164         return (String JavaDoc) eventNameCombo.getEditor().getItem();
165     }
166
167     private void setSelectedEvent(Event event) {
168         eventNameCombo.removeAllItems();
169
170         if (event != null) {
171             eventNameCombo.setEnabled(true);
172
173             FormEvents formEvents = wizardPanel.getSourceComponent()
174                                        .getFormModel().getFormEvents();
175             String JavaDoc defaultName = formEvents.findFreeHandlerName(
176                                      event, wizardPanel.getSourceComponent());
177
178             eventNameCombo.addItem(defaultName);
179
180             if (event.hasEventHandlers()) {
181                 String JavaDoc[] handlers = event.getEventHandlers();
182                 for (int i=0; i < handlers.length; i++)
183                     eventNameCombo.addItem(handlers[i]);
184             }
185
186             eventNameCombo.setSelectedIndex(0);
187         }
188         else eventNameCombo.setEnabled(false);
189
190         wizardPanel.setSelectedEvent(event);
191     }
192
193     /** This method is called from within the constructor to
194      * initialize the form.
195      * WARNING: Do NOT modify this code. The content of this method is
196      * always regenerated by the Form Editor.
197      */

198     private void initComponents() {//GEN-BEGIN:initComponents
199
sourcePanel = new javax.swing.JPanel JavaDoc();
200         sourceInfoPanel = new javax.swing.JPanel JavaDoc();
201         sourceNamePanel = new javax.swing.JPanel JavaDoc();
202         sourceNameLabel = new javax.swing.JLabel JavaDoc();
203         sourceComponentName = new javax.swing.JTextField JavaDoc();
204         eventSelectLabelPanel = new javax.swing.JPanel JavaDoc();
205         eventSelectLabel = new javax.swing.JLabel JavaDoc();
206         eventSelectScroll = new javax.swing.JScrollPane JavaDoc();
207         eventSelectTree = new javax.swing.JTree JavaDoc();
208         eventHandlerPanel = new javax.swing.JPanel JavaDoc();
209         eventNameLabel = new javax.swing.JLabel JavaDoc();
210         eventNameCombo = new javax.swing.JComboBox JavaDoc();
211
212         setLayout(new java.awt.BorderLayout JavaDoc(0, 11));
213
214         sourcePanel.setLayout(new java.awt.BorderLayout JavaDoc());
215
216         sourceInfoPanel.setLayout(new java.awt.GridLayout JavaDoc(2, 1));
217
218         sourceNamePanel.setLayout(new java.awt.FlowLayout JavaDoc(java.awt.FlowLayout.LEFT, 0, 0));
219
220         sourceNameLabel.setLabelFor(sourceComponentName);
221         sourceNameLabel.setText("Source Component:");
222         sourceNameLabel.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(0, 0, 0, 6)));
223         sourceNamePanel.add(sourceNameLabel);
224
225         sourceComponentName.setEditable(false);
226         sourceComponentName.setText("jTextField1");
227         sourceNamePanel.add(sourceComponentName);
228
229         sourceInfoPanel.add(sourceNamePanel);
230
231         eventSelectLabelPanel.setLayout(new java.awt.FlowLayout JavaDoc(java.awt.FlowLayout.LEFT, 0, 3));
232
233         eventSelectLabel.setLabelFor(eventSelectTree);
234         eventSelectLabel.setText("Events");
235         eventSelectLabelPanel.add(eventSelectLabel);
236
237         sourceInfoPanel.add(eventSelectLabelPanel);
238
239         sourcePanel.add(sourceInfoPanel, java.awt.BorderLayout.NORTH);
240
241         eventSelectScroll.setMaximumSize(new java.awt.Dimension JavaDoc(32767, 100));
242         eventSelectTree.setRootVisible(false);
243         eventSelectTree.setShowsRootHandles(true);
244         eventSelectScroll.setViewportView(eventSelectTree);
245
246         sourcePanel.add(eventSelectScroll, java.awt.BorderLayout.CENTER);
247
248         add(sourcePanel, java.awt.BorderLayout.CENTER);
249
250         eventHandlerPanel.setLayout(new java.awt.BorderLayout JavaDoc(8, 0));
251
252         eventHandlerPanel.setBorder(new javax.swing.border.TitledBorder JavaDoc("Event Handler Method"));
253         eventNameLabel.setLabelFor(eventNameCombo);
254         eventNameLabel.setText("Method Name:");
255         eventHandlerPanel.add(eventNameLabel, java.awt.BorderLayout.WEST);
256
257         eventNameCombo.setEditable(true);
258         eventHandlerPanel.add(eventNameCombo, java.awt.BorderLayout.CENTER);
259
260         add(eventHandlerPanel, java.awt.BorderLayout.SOUTH);
261
262     }//GEN-END:initComponents
263

264     // Variables declaration - do not modify//GEN-BEGIN:variables
265
private javax.swing.JTree JavaDoc eventSelectTree;
266     private javax.swing.JLabel JavaDoc eventNameLabel;
267     private javax.swing.JPanel JavaDoc sourcePanel;
268     private javax.swing.JScrollPane JavaDoc eventSelectScroll;
269     private javax.swing.JLabel JavaDoc eventSelectLabel;
270     private javax.swing.JPanel JavaDoc sourceNamePanel;
271     private javax.swing.JPanel JavaDoc sourceInfoPanel;
272     private javax.swing.JPanel JavaDoc eventHandlerPanel;
273     private javax.swing.JLabel JavaDoc sourceNameLabel;
274     private javax.swing.JTextField JavaDoc sourceComponentName;
275     private javax.swing.JComboBox JavaDoc eventNameCombo;
276     private javax.swing.JPanel JavaDoc eventSelectLabelPanel;
277     // End of variables declaration//GEN-END:variables
278

279     // --------
280
// Innerclasses
281

282     static class EventSetNode implements TreeNode {
283         private String JavaDoc eventSetName;
284         private List subNodes;
285
286         public EventSetNode(String JavaDoc eventSetName, List subNodes) {
287             this.eventSetName = eventSetName;
288             this.subNodes = subNodes;
289         }
290
291         public TreeNode getChildAt(int childIndex) {
292             return (TreeNode) subNodes.get(childIndex);
293         }
294         public int getChildCount() {
295             return subNodes.size();
296         }
297         public TreeNode getParent() {
298             return null;
299         }
300         public int getIndex(TreeNode node) {
301             return subNodes.indexOf(node);
302         }
303         public boolean getAllowsChildren() {
304             return true;
305         }
306         public boolean isLeaf() {
307             return false;
308         }
309         public Enumeration children() {
310             return Collections.enumeration(subNodes);
311         }
312         public String JavaDoc toString() {
313             return eventSetName;
314         }
315     }
316
317     static class EventNode implements TreeNode {
318         private TreeNode parent;
319         private Event event;
320         public EventNode(TreeNode parent, Event event) {
321             this.parent = parent;
322             this.event = event;
323         }
324         public TreeNode getChildAt(int childIndex) {
325             return null;
326         }
327         public int getChildCount() {
328             return 0;
329         }
330         public TreeNode getParent() {
331             return parent;
332         }
333         public int getIndex(TreeNode node) {
334             return -1;
335         }
336         public boolean getAllowsChildren() {
337             return false;
338         }
339         public boolean isLeaf() {
340             return true;
341         }
342         public Enumeration children() {
343             return null;
344         }
345         public String JavaDoc toString() {
346             if (!event.hasEventHandlers())
347                 return event.getName();
348             String JavaDoc[] handlers = event.getEventHandlers();
349             if (handlers.length == 1)
350                 return event.getName() + " ["+ handlers[0] +"]"; // NOI18N
351
return event.getName() + " [...]"; // NOI18N
352
}
353         Event getEvent() {
354             return event;
355         }
356     }
357 }
358
Popular Tags