1 19 24 25 package org.netbeans.modules.j2ee.sun.ide.sunresources.wizards; 26 27 import org.openide.WizardDescriptor; 28 import org.openide.util.NbBundle; 29 import org.openide.filesystems.FileObject; 30 import org.netbeans.api.project.Project; 31 import org.netbeans.spi.project.ui.templates.support.Templates; 32 33 import java.util.Set ; 34 import java.awt.Component ; 35 import java.io.InputStream ; 36 import javax.swing.JComponent ; 37 import javax.swing.event.ChangeListener ; 38 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ResourceUtils; 39 40 import org.netbeans.modules.j2ee.sun.sunresources.beans.Wizard; 41 import org.netbeans.modules.j2ee.sun.sunresources.beans.WizardConstants; 42 import org.openide.ErrorManager; 43 44 48 public final class PMFWizard implements WizardDescriptor.InstantiatingIterator, ChangeListener , WizardConstants{ 49 50 private static Project project; 51 52 53 private WizardDescriptor.Panel panels[]; 54 private transient WizardDescriptor.Panel[] morePanels = null; private transient WizardDescriptor.Panel[] moreCPPanels = null; 56 private transient WizardDescriptor.Panel[] pmPanels = null; 57 private transient int index; 58 59 61 private static final String DATAFILE = "org/netbeans/modules/j2ee/sun/sunresources/beans/PMFWizard.xml"; private static final String DS_DATAFILE = "org/netbeans/modules/j2ee/sun/sunresources/beans/DSWizard.xml"; private static final String CP_DATAFILE = "org/netbeans/modules/j2ee/sun/sunresources/beans/CPWizard.xml"; 65 private static final int IN_PM = 1; 66 private static final int IN_DS = 2; 67 private static final int IN_CP = 3; 68 private int stage = IN_PM; 69 70 private ResourceConfigHelper helper; 71 private ResourceConfigHelper dshelper; 72 private ResourceConfigHelper cphelper; 73 private ResourceConfigHelperHolder holder; 74 75 private Wizard wizardInfo; 76 private Wizard dsWizardInfo; 77 private Wizard cpWizardInfo; 78 79 private boolean addSteps = false; 80 82 private transient String [] steps = null; 83 private transient String [] moreSteps = null; 84 private transient String [] moreCPSteps = null; 85 private transient String [] pmSteps = null; 86 87 88 public static PMFWizard create () { 89 return new PMFWizard (); 90 } 91 92 private WizardDescriptor.Panel[] createPanels() { 93 WizardDescriptor.Panel[] tempPanels = null; 94 WizardDescriptor.Panel panel = new CommonAttributePanel(helper, wizardInfo, new String [] {"general"}); panel.addChangeListener(this); 96 if(stage == IN_PM){ 97 morePanels = null; 98 tempPanels = new WizardDescriptor.Panel[] { 99 panel, 100 new CommonPropertyPanel(this.helper, this.wizardInfo), 101 }; 102 }else if(stage == IN_DS){ 103 moreCPPanels = null; 104 CommonAttributePanel dspan = new CommonAttributePanel(this.dshelper, this.dsWizardInfo, new String [] {"general"}); dspan.addChangeListener(this); 106 tempPanels = new WizardDescriptor.Panel[] { 107 panel, 108 new CommonPropertyPanel(this.helper, this.wizardInfo), 109 dspan, 110 new CommonPropertyPanel(this.dshelper, this.wizardInfo), 111 }; 112 } 113 return tempPanels; 114 } 115 116 static private String CONST_ADD_PROPERTY = "LBL_AddProperty"; 118 private String [] createSteps() { 119 String [] tempSteps = null; 120 if(stage == IN_PM){ 121 moreSteps = null; 122 tempSteps = new String [] { 123 NbBundle.getMessage(PMFWizard.class,__FirstStepChoose), 124 NbBundle.getMessage(PMFWizard.class, "LBL_GeneralAttributes_PM"), NbBundle.getMessage(PMFWizard.class, CONST_ADD_PROPERTY), 126 }; 127 }else if(stage == IN_DS){ 128 moreCPSteps = null; 129 tempSteps = new String [] { 130 NbBundle.getMessage(PMFWizard.class,__FirstStepChoose), 131 NbBundle.getMessage(PMFWizard.class, "LBL_GeneralAttributes_PM"), NbBundle.getMessage(PMFWizard.class, CONST_ADD_PROPERTY), 133 NbBundle.getMessage(PMFWizard.class, "LBL_GeneralAttributes_DS"), NbBundle.getMessage(PMFWizard.class, CONST_ADD_PROPERTY), 135 }; 136 } 137 return tempSteps; 138 } 139 140 public Set instantiate(){ 141 try{ 142 if(this.holder.hasDSHelper()){ 143 FileObject fo = this.helper.getData().getTargetFileObject(); 144 String jdbcName = this.dshelper.getData().getString(__JndiName); 145 this.helper.getData().setString(__JdbcResourceJndiName, jdbcName); 146 this.dshelper.getData().setTargetFile(jdbcName); 147 this.dshelper.getData().setTargetFileObject(fo); 148 149 if(this.holder.hasCPHelper()){ 150 String poolName = this.cphelper.getData().getString(__Name); 151 this.dshelper.getData().setString(__PoolName, poolName); 152 this.cphelper.getData().setTargetFile(poolName); 153 this.cphelper.getData().setTargetFileObject(fo); 154 ResourceUtils.savePMFResourceDatatoXml(this.helper.getData(), this.dshelper.getData(), this.cphelper.getData()); 155 }else{ 156 ResourceUtils.savePMFResourceDatatoXml(this.helper.getData(), this.dshelper.getData(), null); 159 } 160 }else{ 161 ResourceUtils.savePMFResourceDatatoXml(this.helper.getData(), null, null); 162 } 163 }catch (Exception ex){ 164 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 165 ex); 166 } 167 return java.util.Collections.EMPTY_SET; 168 } 169 170 public void initialize(WizardDescriptor wiz){ 171 this.wizardInfo = getWizardInfo(DATAFILE); 172 this.holder = new ResourceConfigHelperHolder(); 173 this.helper = holder.getPMFHelper(); 174 175 wiz.putProperty("NewFileWizard_Title", NbBundle.getMessage(PMFWizard.class, "Templates/SunResources/Persistence_Resource")); index = 0; 178 panels = createPanels(); 179 180 steps = createSteps(); 182 183 project = Templates.getProject(wiz); 184 try{ 185 FileObject pkgLocation = project.getProjectDirectory(); 186 if (pkgLocation != null) { 187 this.helper.getData().setTargetFileObject(pkgLocation); 188 } 189 }catch (Exception ex){ 190 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 191 ex); 192 } 193 this.helper.getData().setHolder(this.holder); 194 195 for (int i = 0; i < panels.length; i++) { 196 Component c = panels[i].getComponent(); 197 if (c instanceof JComponent ) { JComponent jc = (JComponent )c; 199 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty("WizardPanel_contentData", steps); } 204 } 205 } 206 207 public void uninitialize(WizardDescriptor wiz){ 208 panels = null; 210 } 211 212 public Wizard getWizardInfo(String filePath){ 213 Wizard wizInfo = null; 214 try{ 215 InputStream in = Wizard.class.getClassLoader().getResourceAsStream(filePath); 216 wizInfo = Wizard.createGraph(in); 217 }catch(Exception ex){ 218 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 219 ex); 220 } 221 return wizInfo; 222 } 223 224 public String name(){ 225 return NbBundle.getMessage(PMFWizard.class, "Templates/SunResources/Persistence_Resource"); } 227 228 public boolean hasNext(){ 229 boolean retVal = false; 230 if (stage == IN_PM && index == 0 && addSteps) 231 retVal = true; 232 else 233 retVal = index < panels.length - 1; 234 return retVal; 235 } 236 237 public boolean hasPrevious(){ 238 return index > 0; 239 } 240 241 public synchronized void nextPanel(){ 242 if (index + 1 == panels.length) { 243 throw new java.util.NoSuchElementException (); 244 } 245 if (index == 0) { 246 ((CommonPropertyPanel) panels[1]).setInitialFocus(); 247 }else if(index == 1){ 248 ((CommonAttributePanel) panels[2]).setInitialFocus(); 249 }else if (index == 2) { 250 ((CommonPropertyPanel) panels[3]).setInitialFocus(); 251 }else if (index == 3) { 252 ((CPVendorPanel) panels[4]).setInitialFocus(); 253 }else if (index == 4){ 254 ((CPPropertiesPanelPanel) panels[5]).refreshFields(); 255 }else if (index == 5){ 256 ((CommonAttributePanel) panels[6]).setPropInitialFocus(); 257 } 258 259 index ++; 260 261 if (stage == IN_PM && index == 2) { 262 stage = IN_DS; 263 }else if(stage == IN_DS && index == 4){ 264 stage = IN_CP; 265 } 266 } 267 268 public synchronized void previousPanel(){ 269 if (index == 0) { 270 throw new java.util.NoSuchElementException (); 271 } 272 273 index--; 274 if( (stage == IN_DS) && (index < 2)){ 276 stage = IN_PM; 277 }else{ 278 if((stage == IN_CP) && (index < 4)) { 279 stage = IN_DS; 280 } 281 } 282 } 283 284 public WizardDescriptor.Panel current(){ 285 return (WizardDescriptor.Panel)panels[index]; 286 } 287 288 public final void addChangeListener(ChangeListener l) { 289 } 290 public final void removeChangeListener(ChangeListener l) { 291 } 292 293 public void setResourceConfigHelper(ResourceConfigHelper helper){ 294 this.helper = helper; 295 } 296 297 public ResourceConfigHelper getResourceConfigHelper(){ 298 return this.helper; 299 } 300 301 public void stateChanged(javax.swing.event.ChangeEvent e) { 302 if( (e.getSource().getClass() == CommonAttributePanel.class) || (e.getSource().getClass() == CommonAttributeVisualPanel.class) ) { 303 CommonAttributePanel commonPane = (CommonAttributePanel)this.current(); 304 CommonAttributeVisualPanel visPane = (CommonAttributeVisualPanel)commonPane.getComponent(); 305 this.addSteps = visPane.isNewResourceSelected(); 307 308 WizardDescriptor.Panel[] tempPanels = null; 309 if(this.addSteps){ 310 if (stage == IN_PM){ 311 this.holder.setHasDSHelper(this.addSteps); 312 tempPanels = morePanels; 313 }else if(stage == IN_DS){ 314 this.holder.setHasCPHelper(this.addSteps); 315 tempPanels = moreCPPanels; 316 } 317 }else{ 318 if (stage == IN_PM){ 319 this.holder.setHasDSHelper(this.addSteps); 320 tempPanels = morePanels; 321 }else if(stage == IN_DS){ 322 this.holder.setHasCPHelper(this.addSteps); 323 tempPanels = moreCPPanels; 324 } 325 } 326 327 if (addSteps && tempPanels == null) { 328 addPanels(); 329 addSteps(); 330 for (int i = 0; i < panels.length; i++) { 331 Component c = panels[i].getComponent(); 332 if (c instanceof JComponent ) { 333 JComponent jc = (JComponent )c; 334 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty("WizardPanel_contentData", steps); } 337 } 338 }else if((!addSteps) && (tempPanels != null) ){ 339 if((stage == IN_PM) && (pmPanels != null)){ 340 morePanels = null; 341 panels = pmPanels; 342 }else if((stage == IN_DS) && (morePanels != null)){ 343 moreCPPanels = null; 344 panels = morePanels; 345 }else 346 panels = createPanels(); 347 348 if((stage == IN_PM) && (pmSteps != null)){ 349 moreSteps = null; 350 steps = pmSteps; 351 }else if((stage == IN_DS) && (moreSteps != null)){ 352 moreCPSteps = null; 353 steps = moreSteps; 354 }else 355 steps = createSteps(); 356 357 for (int i = 0; i < panels.length; i++) { 358 Component c = panels[i].getComponent(); 359 if (c instanceof JComponent ) { 360 JComponent jc = (JComponent )c; 361 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty("WizardPanel_contentData", steps); } 364 } 365 366 if(stage == IN_PM){ 367 ((CommonAttributePanel)panels[0]).setInitialFocus(); 368 }else if(stage == IN_DS){ 369 ((CommonAttributePanel)panels[2]).setInitialFocus(); 370 } 371 } 372 } 374 } 375 376 protected void addPanels() { 377 if (stage == IN_PM){ 378 if (panels != null && morePanels == null) { 379 this.dshelper = this.holder.addAssociatedHelper(); 380 this.dshelper.getData().setResourceName(__JdbcResource); 381 this.holder.setHasDSHelper(true); 382 this.dsWizardInfo = getWizardInfo(DS_DATAFILE); 383 this.dshelper.getData().setTargetFileObject(this.helper.getData().getTargetFileObject()); 384 this.dshelper.getData().setString(__DynamicWizPanel, "true"); 386 CommonAttributePanel dspan = new CommonAttributePanel(this.dshelper, this.dsWizardInfo, new String [] {"general"}); dspan.addChangeListener(this); 388 morePanels = new WizardDescriptor.Panel[] { 389 panels[0], 390 panels[1], 391 dspan, 392 new CommonPropertyPanel(this.dshelper, this.wizardInfo), 393 }; 394 } 395 pmPanels = panels; 396 panels = morePanels; 397 }else if(stage == IN_DS){ 398 if (panels != null && moreCPPanels == null) { 399 this.cphelper = this.holder.addAssociatedHelper(); 400 this.cphelper.getData().setResourceName(__JdbcConnectionPool); 401 this.holder.setHasCPHelper(true); 402 this.cpWizardInfo = getWizardInfo(CP_DATAFILE); 403 this.cphelper.getData().setTargetFileObject(this.helper.getData().getTargetFileObject()); 404 this.cphelper.getData().setString(__DynamicWizPanel, "true"); 406 moreCPPanels = new WizardDescriptor.Panel[] { 407 panels[0], 408 panels[1], 409 panels[2], 410 panels[3], 411 new CPVendorPanel(this.cphelper, this.cpWizardInfo), 412 new CPPropertiesPanelPanel(this.cphelper, this.cpWizardInfo), 413 new CommonAttributePanel(this.cphelper, this.cpWizardInfo, new String [] {"pool-setting", "pool-setting-2", "pool-setting-3"}), }; 415 } 416 panels = moreCPPanels; 418 } 419 } 420 421 protected void addSteps() { 422 if (stage == IN_PM){ 423 if (steps != null && moreSteps == null) { 424 moreSteps = new String [] { 425 steps[0], 426 steps[1], 427 steps[2], 428 NbBundle.getMessage(PMFWizard.class, "LBL_GeneralAttributes_DS"), NbBundle.getMessage(PMFWizard.class, CONST_ADD_PROPERTY), 430 }; 431 } 432 pmSteps = steps; 433 steps = moreSteps; 434 }else if(stage == IN_DS){ 435 if (steps != null && moreCPSteps == null) { 436 moreCPSteps = new String [] { 437 steps[0], 438 steps[1], 439 steps[2], 440 steps[3], 441 steps[4], 442 NbBundle.getMessage(PMFWizard.class, "TITLE_ConnPoolWizardPanel_dbConn"), NbBundle.getMessage(PMFWizard.class, "TITLE_ConnPoolWizardPanel_properties"), NbBundle.getMessage(PMFWizard.class, "TITLE_ConnPoolWizardPanel_optionalProps") }; 446 } 447 steps = moreCPSteps; 449 } 450 } 451 452 } 453 | Popular Tags |