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.HashSet;
046: import javax.swing.event.*;
047: import java.lang.reflect.Method;
048:
049: import java.util.Set;
050: import org.openide.*;
051: import org.openide.util.NbBundle;
052: import org.netbeans.modules.form.*;
053:
054: /** This class manages the Connection Wizard. The wizard has three (or two)
055: * steps in which user can specify event on source component, operation
056: * on target component and (if needed) parameters of the operation (method).
057: *
058: * @author Tomas Pavek
059: */
060: public class ConnectionWizard extends WizardDescriptor {
061:
062: ConnectionIterator iterator;
063:
064: private boolean finished = false; // whether the wizard was finished sucessfully
065:
066: private Event selEvent; // selected activating event
067: private String eventName; // selected event handler name
068: private int actionType; // type of action on target
069: private Method targetMethod; // method selected to be invoked on target
070: private String paramsText; // text of parameters for target method
071: private Object[] paramValues; // values for target method's parameters
072:
073: // constructor
074: public ConnectionWizard(FormModel model, RADComponent source,
075: RADComponent target) {
076: this (model, source, target, new ConnectionIterator(model,
077: source, target));
078: }
079:
080: // constructor
081: private ConnectionWizard(FormModel model, RADComponent source,
082: RADComponent target, ConnectionIterator it) {
083: super (it);
084: iterator = it;
085: putProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); // NOI18N
086: putProperty("WizardPanel_contentDisplayed", Boolean.TRUE); // NOI18N
087: putProperty("WizardPanel_contentNumbered", Boolean.TRUE); // NOI18N
088: setTitle(NbBundle.getBundle(ConnectionWizard.class).getString(
089: "CTL_CW_Title")); // NOI18N
090: java.text.MessageFormat format = new java.text.MessageFormat(
091: "{0}"); // NOI18N
092: setTitleFormat(format);
093: }
094:
095: /** Shows the wizard to the user.
096: * @returns whether the wizard was finished (not canceled)
097: */
098: public boolean show() {
099: java.awt.Dialog d = DialogDisplayer.getDefault().createDialog(
100: this );
101:
102: finished = false;
103: d.setVisible(true);
104: finished = getValue() == FINISH_OPTION;
105:
106: if (finished) {
107: selEvent = iterator.panel1.getSelectedEvent();
108: eventName = iterator.panel1.getEventName();
109: actionType = iterator.panel2.getActionType();
110: targetMethod = iterator.getMethodFromPanel2();
111: paramsText = iterator.anyParameters() ? iterator.panel3
112: .getParametersText() : null;
113: paramValues = iterator.anyParameters() ? iterator.panel3
114: .getParameters() : null;
115: }
116:
117: d.dispose();
118:
119: return finished;
120: }
121:
122: /** @returns whether the wizard was finished (processed and not canceled)
123: */
124: public boolean isFinished() {
125: return finished;
126: }
127:
128: // -----------------
129:
130: public RADComponent getSource() {
131: return iterator.source;
132: }
133:
134: public RADComponent getTarget() {
135: return iterator.target;
136: }
137:
138: public FormModel getFormModel() {
139: return iterator.formModel;
140: }
141:
142: public Event getSelectedEvent() {
143: return finished ? selEvent : null;
144: }
145:
146: public String getEventName() {
147: return finished ? eventName : null;
148: }
149:
150: /** @returns source code for the connection
151: */
152: public String getGeneratedCode() {
153: if (!finished || actionType == ConnectionWizardPanel2.CODE_TYPE)
154: return null;
155:
156: StringBuffer buf = new StringBuffer();
157: Set<Class> allExceptions = new HashSet<Class>();
158:
159: // params can be specified as method calls which in turn may produce exceptions
160: if (paramValues != null) {
161: for (int i = 0; i < paramValues.length; i++) {
162: if (paramValues[i] instanceof RADConnectionPropertyEditor.RADConnectionDesignValue) {
163: RADConnectionPropertyEditor.RADConnectionDesignValue val = (RADConnectionPropertyEditor.RADConnectionDesignValue) paramValues[i];
164: if (val.getType() == RADConnectionPropertyEditor.RADConnectionDesignValue.TYPE_METHOD) {
165: Class[] except = val.getMethod().getMethod()
166: .getExceptionTypes();
167: for (int j = 0; j < except.length; j++) {
168: allExceptions.add(except[j]);
169: }
170: }
171: }
172: }
173: }
174:
175: Class[] methodExceptions = targetMethod.getExceptionTypes();
176: for (int k = 0; k < methodExceptions.length; k++) {
177: allExceptions.add(methodExceptions[k]);
178: }
179:
180: // if either the setter or some of the methods that get parameters
181: // throw checked exceptions, we must generate try/catch block around it
182: if (allExceptions.size() > 0) {
183: buf.append("try {\n "); // NOI18N
184: }
185:
186: if (getTarget() != getFormModel().getTopRADComponent()) { // not generated for the form
187: buf.append(getTarget().getName());
188: buf.append("."); // NOI18N
189: }
190: buf.append(targetMethod.getName());
191: buf.append("("); // NOI18N
192: if (paramsText != null)
193: buf.append(paramsText);
194: buf.append(");\n"); // NOI18N
195:
196: int varCount = 1;
197:
198: // add the catch for all checked exceptions
199: for (java.util.Iterator it = allExceptions.iterator(); it
200: .hasNext();) {
201: Class exceptionClass = (Class) it.next();
202: buf.append("} catch ("); // NOI18N
203: buf.append(exceptionClass.getName());
204: buf.append(" "); // NOI18N
205: String excName = "e" + varCount; // NOI18N
206: varCount++;
207: /* VariablePool varPool = getFormModel().getVariablePool();
208: while (varPool.isReserved(excName)) {
209: excName = "e"+varCount; // NOI18N
210: varCount++;
211: } */
212: buf.append(excName);
213: buf.append(") {\n"); // NOI18N
214: buf.append(" " + excName); // NOI18N
215: buf.append(".printStackTrace();\n"); // NOI18N
216: }
217: if (!allExceptions.isEmpty())
218: buf.append("}\n"); // NOI18N
219:
220: return buf.toString();
221: }
222:
223: @Override
224: protected void updateState() {
225: super .updateState();
226: java.util.ResourceBundle bundle = NbBundle
227: .getBundle(ConnectionWizard.class);
228: if (iterator.getPanelsCount() > 2) {
229: putProperty("WizardPanel_contentData", // NOI18N
230: new String[] {
231: bundle.getString("CTL_CW_Step1_Title"), // NOI18N
232: bundle.getString("CTL_CW_Step2_Title"), // NOI18N
233: bundle.getString("CTL_CW_Step3_Title") // NOI18N
234: });
235: } else {
236: putProperty("WizardPanel_contentData", // NOI18N
237: new String[] {
238: bundle.getString("CTL_CW_Step1_Title"), // NOI18N
239: bundle.getString("CTL_CW_Step2_Title") // NOI18N
240: });
241: }
242: }
243:
244: /** This class manages connection wizard panels.
245: */
246: static class ConnectionIterator implements
247: WizardDescriptor.Iterator, ChangeListener {
248:
249: FormModel formModel;
250: RADComponent source;
251: RADComponent target;
252:
253: ConnectionWizardPanel1 panel1;
254: ConnectionWizardPanel2 panel2;
255: ConnectionWizardPanel3 panel3;
256: WizardDescriptor.Panel[] panels;
257:
258: boolean panel2Changed = false;
259:
260: int stage;
261:
262: private EventListenerList listenerList = null;
263:
264: // constructor
265: ConnectionIterator(FormModel model, RADComponent source,
266: RADComponent target) {
267: formModel = model;
268: this .source = source;
269: this .target = target;
270:
271: stage = 1;
272:
273: panel1 = new ConnectionWizardPanel1(source);
274: panel2 = new ConnectionWizardPanel2(target);
275: panel2.addChangeListener(this );
276: panel3 = new ConnectionWizardPanel3(formModel);
277: panels = new WizardDescriptor.Panel[] { panel1, panel2,
278: panel3 };
279: }
280:
281: public int getPanelsCount() {
282: // the number of panels depends on whether any parameters must
283: // be entered (in the last panel)
284: return anyParameters() ? 3 : 2;
285: }
286:
287: public WizardDescriptor.Panel current() {
288: return panels[stage - 1];
289: }
290:
291: public boolean hasNext() {
292: return stage < getPanelsCount();
293: }
294:
295: public boolean hasPrevious() {
296: return stage > 1;
297: }
298:
299: public java.lang.String name() {
300: return ""; // NOI18N
301: }
302:
303: public void nextPanel() {
304: if (stage < getPanelsCount()) {
305: if (stage == 1 && panel1.handlerAlreadyExists()) {
306: if (DialogDisplayer
307: .getDefault()
308: .notify(
309: new NotifyDescriptor.Confirmation(
310: NbBundle
311: .getBundle(
312: ConnectionWizard.class)
313: .getString(
314: "MSG_RewritingEvent"), // NOI18N
315: NotifyDescriptor.OK_CANCEL_OPTION,
316: NotifyDescriptor.WARNING_MESSAGE)) == NotifyDescriptor.CANCEL_OPTION)
317: return;
318: }
319:
320: stage++;
321:
322: if (stage == 3 && panel2Changed) {
323: Method method = getMethodFromPanel2();
324:
325: if (method != null)
326: panel3.setMethod(method);
327:
328: panel2Changed = false;
329: }
330: }
331: }
332:
333: public void previousPanel() {
334: if (stage > 1)
335: stage--;
336: }
337:
338: public void addChangeListener(ChangeListener listener) {
339: if (listenerList == null)
340: listenerList = new EventListenerList();
341: listenerList.add(ChangeListener.class, listener);
342: }
343:
344: public void removeChangeListener(ChangeListener listener) {
345: if (listenerList != null)
346: listenerList.remove(ChangeListener.class, listener);
347: }
348:
349: public void stateChanged(ChangeEvent p1) {
350: if (stage == 2) {
351: panel2Changed = true;
352: }
353: }
354:
355: private Method getMethodFromPanel2() {
356: Method method = null;
357:
358: if (panel2.getActionType() == ConnectionWizardPanel2.METHOD_TYPE) {
359: MethodDescriptor desc = panel2.getSelectedMethod();
360: if (desc != null)
361: method = desc.getMethod();
362: } else if (panel2.getActionType() == ConnectionWizardPanel2.PROPERTY_TYPE) {
363: PropertyDescriptor desc = panel2.getSelectedProperty();
364: if (desc != null)
365: method = desc.getWriteMethod();
366: }
367:
368: return method;
369: }
370:
371: private boolean anyParameters() {
372: Method m = getMethodFromPanel2();
373: return m != null && m.getParameterTypes().length > 0;
374: }
375: }
376: }
|