KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet JavaDoc;
24 import javax.swing.event.*;
25 import java.lang.reflect.Method JavaDoc;
26
27 import org.openide.*;
28 import org.openide.util.NbBundle;
29 import org.netbeans.modules.form.*;
30
31
32 /** This class manages the Connection Wizard. The wizard has three (or two)
33  * steps in which user can specify event on source component, operation
34  * on target component and (if needed) parameters of the operation (method).
35  *
36  * @author Tomas Pavek
37  */

38 public class ConnectionWizard extends WizardDescriptor {
39
40     ConnectionIterator iterator;
41
42     private boolean finished = false; // whether the wizard was finished sucessfully
43

44     private Event selEvent; // selected activating event
45
private String JavaDoc eventName; // selected event handler name
46
private int actionType; // type of action on target
47
private Method JavaDoc targetMethod; // method selected to be invoked on target
48
private String JavaDoc paramsText; // text of parameters for target method
49
private Object JavaDoc[] paramValues; // values for target method's parameters
50

51     // constructor
52
public ConnectionWizard(FormModel model, RADComponent source, RADComponent target) {
53         this(model, source, target,
54              new ConnectionIterator(model, source, target));
55     }
56
57     // constructor
58
private ConnectionWizard(FormModel model, RADComponent source, RADComponent target,
59                              ConnectionIterator it) {
60         super(it);
61         iterator = it;
62         putProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); // NOI18N
63
putProperty("WizardPanel_contentDisplayed", Boolean.TRUE); // NOI18N
64
putProperty("WizardPanel_contentNumbered", Boolean.TRUE); // NOI18N
65
setTitle(NbBundle.getBundle(ConnectionWizard.class).getString("CTL_CW_Title")); // NOI18N
66
java.text.MessageFormat JavaDoc format = new java.text.MessageFormat JavaDoc("{0}"); // NOI18N
67
setTitleFormat(format);
68     }
69
70     /** Shows the wizard to the user.
71      * @returns whether the wizard was finished (not canceled)
72      */

73     public boolean show() {
74         java.awt.Dialog JavaDoc d = DialogDisplayer.getDefault().createDialog(this);
75
76         finished = false;
77         d.setVisible(true);
78         finished = getValue() == FINISH_OPTION;
79
80         if (finished) {
81             selEvent = iterator.panel1.getSelectedEvent();
82             eventName = iterator.panel1.getEventName();
83             actionType = iterator.panel2.getActionType();
84             targetMethod = iterator.getMethodFromPanel2();
85             paramsText = iterator.anyParameters() ?
86                          iterator.panel3.getParametersText() : null;
87             paramValues = iterator.anyParameters() ?
88                           iterator.panel3.getParameters() : null;
89         }
90
91         d.dispose();
92
93         return finished;
94     }
95
96     /** @returns whether the wizard was finished (processed and not canceled)
97      */

98     public boolean isFinished() {
99         return finished;
100     }
101
102     // -----------------
103

104     public RADComponent getSource() {
105         return iterator.source;
106     }
107
108     public RADComponent getTarget() {
109         return iterator.target;
110     }
111
112     public FormModel getFormModel() {
113         return iterator.formModel;
114     }
115
116     public Event getSelectedEvent() {
117         return finished ? selEvent : null;
118     }
119
120     public String JavaDoc getEventName() {
121         return finished ? eventName : null;
122     }
123
124     /** @returns source code for the connection
125      */

126     public String JavaDoc getGeneratedCode() {
127         if (!finished || actionType == ConnectionWizardPanel2.CODE_TYPE)
128             return null;
129
130         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
131         HashSet JavaDoc allExceptions = new HashSet JavaDoc();
132
133         // params can be specified as method calls which in turn may produce exceptions
134
if (paramValues != null) {
135             for (int i=0; i < paramValues.length; i++) {
136                 if (paramValues[i] instanceof RADConnectionPropertyEditor.RADConnectionDesignValue) {
137                     RADConnectionPropertyEditor.RADConnectionDesignValue val = (RADConnectionPropertyEditor.RADConnectionDesignValue) paramValues[i];
138                     if (val.getType() == RADConnectionPropertyEditor.RADConnectionDesignValue.TYPE_METHOD) {
139                         Class JavaDoc [] except = val.getMethod().getMethod().getExceptionTypes();
140                         for (int j=0; j < except.length; j++) {
141                             allExceptions.add(except[j]);
142                         }
143                     }
144                 }
145             }
146         }
147
148         Class JavaDoc[] methodExceptions = targetMethod.getExceptionTypes();
149         for (int k=0; k < methodExceptions.length; k++) {
150             allExceptions.add(methodExceptions[k]);
151         }
152
153         // if either the setter or some of the methods that get parameters
154
// throw checked exceptions, we must generate try/catch block around it
155
if (allExceptions.size() > 0) {
156             buf.append("try {\n "); // NOI18N
157
}
158
159         if (getTarget() != getFormModel().getTopRADComponent()) { // not generated for the form
160
buf.append(getTarget().getName());
161             buf.append("."); // NOI18N
162
}
163         buf.append(targetMethod.getName());
164         buf.append("("); // NOI18N
165
if (paramsText != null)
166             buf.append(paramsText);
167         buf.append(");\n"); // NOI18N
168

169         int varCount = 1;
170
171         // add the catch for all checked exceptions
172
for (java.util.Iterator JavaDoc it = allExceptions.iterator(); it.hasNext(); ) {
173             Class JavaDoc exceptionClass = (Class JavaDoc) it.next();
174             buf.append("} catch ("); // NOI18N
175
buf.append(exceptionClass.getName());
176             buf.append(" "); // NOI18N
177
String JavaDoc excName = "e"+varCount; // NOI18N
178
varCount++;
179 /* VariablePool varPool = getFormModel().getVariablePool();
180             while (varPool.isReserved(excName)) {
181                 excName = "e"+varCount; // NOI18N
182                 varCount++;
183             } */

184             buf.append(excName);
185             buf.append(") {\n"); // NOI18N
186
buf.append(" "+excName); // NOI18N
187
buf.append(".printStackTrace();\n"); // NOI18N
188
}
189         if (!allExceptions.isEmpty())
190             buf.append("}\n"); // NOI18N
191

192         return buf.toString();
193     }
194
195     protected void updateState() {
196         super.updateState();
197         java.util.ResourceBundle JavaDoc bundle = NbBundle.getBundle(ConnectionWizard.class);
198         if (iterator.getPanelsCount() > 2) {
199             putProperty("WizardPanel_contentData", // NOI18N
200
new String JavaDoc[] {
201                     bundle.getString("CTL_CW_Step1_Title"), // NOI18N
202
bundle.getString("CTL_CW_Step2_Title"), // NOI18N
203
bundle.getString("CTL_CW_Step3_Title") // NOI18N
204
}
205             );
206         } else {
207             putProperty("WizardPanel_contentData", // NOI18N
208
new String JavaDoc[] {
209                     bundle.getString("CTL_CW_Step1_Title"), // NOI18N
210
bundle.getString("CTL_CW_Step2_Title") // NOI18N
211
}
212             );
213         }
214     }
215     
216     /** This class manages connection wizard panels.
217      */

218     static class ConnectionIterator implements WizardDescriptor.Iterator, ChangeListener {
219
220         FormModel formModel;
221         RADComponent source;
222         RADComponent target;
223
224         ConnectionWizardPanel1 panel1;
225         ConnectionWizardPanel2 panel2;
226         ConnectionWizardPanel3 panel3;
227         WizardDescriptor.Panel[] panels;
228
229         boolean panel2Changed = false;
230
231         int stage;
232
233         private EventListenerList listenerList = null;
234
235         // constructor
236
ConnectionIterator(FormModel model,
237                            RADComponent source,
238                            RADComponent target)
239         {
240             formModel = model;
241             this.source = source;
242             this.target = target;
243
244             stage = 1;
245
246             panel1 = new ConnectionWizardPanel1(source);
247             panel2 = new ConnectionWizardPanel2(target);
248             panel2.addChangeListener(this);
249             panel3 = new ConnectionWizardPanel3(formModel);
250             panels = new WizardDescriptor.Panel[] { panel1, panel2, panel3 };
251         }
252
253         public int getPanelsCount() {
254             // the number of panels depends on whether any parameters must
255
// be entered (in the last panel)
256
return anyParameters() ? 3 : 2;
257         }
258
259         public WizardDescriptor.Panel current() {
260             return panels[stage-1];
261         }
262
263         public boolean hasNext() {
264             return stage < getPanelsCount();
265         }
266
267         public boolean hasPrevious() {
268             return stage > 1;
269         }
270
271         public java.lang.String JavaDoc name() {
272             return ""; // NOI18N
273
}
274
275         public void nextPanel() {
276             if (stage < getPanelsCount()) {
277                 if (stage == 1 && panel1.handlerAlreadyExists()) {
278                     if (DialogDisplayer.getDefault().notify(
279                         new NotifyDescriptor.Confirmation(
280                               NbBundle.getBundle(ConnectionWizard.class)
281                                         .getString("MSG_RewritingEvent"), // NOI18N
282
NotifyDescriptor.OK_CANCEL_OPTION,
283                               NotifyDescriptor.WARNING_MESSAGE))
284                           == NotifyDescriptor.CANCEL_OPTION)
285                         return;
286                 }
287
288                 stage++;
289
290                 if (stage == 3 && panel2Changed) {
291                     Method JavaDoc method = getMethodFromPanel2();
292
293                     if (method != null)
294                         panel3.setMethod(method);
295
296                     panel2Changed = false;
297                 }
298             }
299         }
300
301         public void previousPanel() {
302             if (stage > 1)
303                 stage--;
304         }
305
306         public void addChangeListener(ChangeListener listener) {
307             if (listenerList == null)
308                 listenerList = new EventListenerList();
309             listenerList.add(ChangeListener.class, listener);
310         }
311
312         public void removeChangeListener(ChangeListener listener) {
313             if (listenerList != null)
314                 listenerList.remove(ChangeListener.class, listener);
315         }
316
317         public void stateChanged(ChangeEvent p1) {
318             if (stage == 2) {
319                 panel2Changed = true;
320             }
321         }
322
323         private Method JavaDoc getMethodFromPanel2() {
324             Method JavaDoc method = null;
325
326             if (panel2.getActionType() == ConnectionWizardPanel2.METHOD_TYPE) {
327                 MethodDescriptor desc = panel2.getSelectedMethod();
328                 if (desc != null)
329                     method = desc.getMethod();
330             }
331             else if (panel2.getActionType() == ConnectionWizardPanel2.PROPERTY_TYPE) {
332                 PropertyDescriptor desc = panel2.getSelectedProperty();
333                 if (desc != null)
334                     method = desc.getWriteMethod();
335             }
336
337             return method;
338         }
339
340         private boolean anyParameters() {
341             Method JavaDoc m = getMethodFromPanel2();
342             return m != null && m.getParameterTypes().length > 0;
343         }
344     }
345 }
346
Popular Tags