1 19 20 package org.netbeans.modules.form.wizard; 21 22 import java.beans.*; 23 import java.util.HashSet ; 24 import javax.swing.event.*; 25 import java.lang.reflect.Method ; 26 27 import org.openide.*; 28 import org.openide.util.NbBundle; 29 import org.netbeans.modules.form.*; 30 31 32 38 public class ConnectionWizard extends WizardDescriptor { 39 40 ConnectionIterator iterator; 41 42 private boolean finished = false; 44 private Event selEvent; private String eventName; private int actionType; private Method targetMethod; private String paramsText; private Object [] paramValues; 51 public ConnectionWizard(FormModel model, RADComponent source, RADComponent target) { 53 this(model, source, target, 54 new ConnectionIterator(model, source, target)); 55 } 56 57 private ConnectionWizard(FormModel model, RADComponent source, RADComponent target, 59 ConnectionIterator it) { 60 super(it); 61 iterator = it; 62 putProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); putProperty("WizardPanel_contentDisplayed", Boolean.TRUE); putProperty("WizardPanel_contentNumbered", Boolean.TRUE); setTitle(NbBundle.getBundle(ConnectionWizard.class).getString("CTL_CW_Title")); java.text.MessageFormat format = new java.text.MessageFormat ("{0}"); setTitleFormat(format); 68 } 69 70 73 public boolean show() { 74 java.awt.Dialog 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 98 public boolean isFinished() { 99 return finished; 100 } 101 102 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 getEventName() { 121 return finished ? eventName : null; 122 } 123 124 126 public String getGeneratedCode() { 127 if (!finished || actionType == ConnectionWizardPanel2.CODE_TYPE) 128 return null; 129 130 StringBuffer buf = new StringBuffer (); 131 HashSet allExceptions = new HashSet (); 132 133 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 [] 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 [] methodExceptions = targetMethod.getExceptionTypes(); 149 for (int k=0; k < methodExceptions.length; k++) { 150 allExceptions.add(methodExceptions[k]); 151 } 152 153 if (allExceptions.size() > 0) { 156 buf.append("try {\n "); } 158 159 if (getTarget() != getFormModel().getTopRADComponent()) { buf.append(getTarget().getName()); 161 buf.append("."); } 163 buf.append(targetMethod.getName()); 164 buf.append("("); if (paramsText != null) 166 buf.append(paramsText); 167 buf.append(");\n"); 169 int varCount = 1; 170 171 for (java.util.Iterator it = allExceptions.iterator(); it.hasNext(); ) { 173 Class exceptionClass = (Class ) it.next(); 174 buf.append("} catch ("); buf.append(exceptionClass.getName()); 176 buf.append(" "); String excName = "e"+varCount; varCount++; 179 184 buf.append(excName); 185 buf.append(") {\n"); buf.append(" "+excName); buf.append(".printStackTrace();\n"); } 189 if (!allExceptions.isEmpty()) 190 buf.append("}\n"); 192 return buf.toString(); 193 } 194 195 protected void updateState() { 196 super.updateState(); 197 java.util.ResourceBundle bundle = NbBundle.getBundle(ConnectionWizard.class); 198 if (iterator.getPanelsCount() > 2) { 199 putProperty("WizardPanel_contentData", new String [] { 201 bundle.getString("CTL_CW_Step1_Title"), bundle.getString("CTL_CW_Step2_Title"), bundle.getString("CTL_CW_Step3_Title") } 205 ); 206 } else { 207 putProperty("WizardPanel_contentData", new String [] { 209 bundle.getString("CTL_CW_Step1_Title"), bundle.getString("CTL_CW_Step2_Title") } 212 ); 213 } 214 } 215 216 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 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 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 name() { 272 return ""; } 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"), 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 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 getMethodFromPanel2() { 324 Method 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 m = getMethodFromPanel2(); 342 return m != null && m.getParameterTypes().length > 0; 343 } 344 } 345 } 346 | Popular Tags |