1 19 24 25 package org.netbeans.modules.j2ee.sun.ide.sunresources.wizards; 26 27 31 32 import java.awt.Component ; 33 import java.util.Set ; 34 import javax.swing.JComponent ; 35 import java.io.InputStream ; 36 import javax.swing.event.ChangeListener ; 37 38 import org.openide.util.NbBundle; 39 import org.openide.WizardDescriptor; 40 import org.netbeans.api.project.Project; 41 import org.openide.filesystems.FileObject; 42 import org.netbeans.spi.project.ui.templates.support.Templates; 43 44 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ResourceUtils; 45 46 import org.netbeans.modules.j2ee.sun.sunresources.beans.Wizard; 47 import org.netbeans.modules.j2ee.sun.sunresources.beans.WizardConstants; 48 import org.openide.ErrorManager; 49 50 public final class DataSourceWizard implements WizardDescriptor.InstantiatingIterator, ChangeListener , WizardConstants{ 51 52 53 54 private static final String 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"; 57 private transient int index; 58 private transient WizardDescriptor.Panel[] panels; 59 private transient String [] steps; 61 62 private ResourceConfigHelper helper; 63 private Wizard wizardInfo; 64 65 private boolean addSteps = false; 66 68 private ResourceConfigHelper cphelper; 69 private ResourceConfigHelperHolder holder; 70 private Wizard cpWizardInfo; 71 72 private transient WizardDescriptor.Panel[] morePanels = null; 73 private transient WizardDescriptor.Panel[] dsPanels = null; 74 75 private transient String [] dsSteps = null; 76 private transient String [] moreSteps = null; 77 78 79 public static DataSourceWizard create () { 80 return new DataSourceWizard (); 81 } 82 83 private WizardDescriptor.Panel[] createPanels() { 84 morePanels = null; 85 WizardDescriptor.Panel panel = new CommonAttributePanel(helper, wizardInfo, new String [] {"general"}); panel.addChangeListener(this); 87 return new WizardDescriptor.Panel[] { 88 panel, 89 new CommonPropertyPanel(this.helper, this.wizardInfo) 90 }; 91 } 92 93 private String [] createSteps() { 94 return new String [] { 95 NbBundle.getMessage(DataSourceWizard.class,__FirstStepChoose), 96 NbBundle.getMessage(DataSourceWizard.class, "LBL_GeneralAttributes_DS"), NbBundle.getMessage(DataSourceWizard.class, "LBL_AddProperty") }; 99 } 100 101 public Set instantiate(){ 102 try{ 103 if(this.holder.hasCPHelper()){ 104 String poolName = this.cphelper.getData().getString(__Name); 105 this.helper.getData().setString(__PoolName, poolName); 106 this.cphelper.getData().setTargetFile(poolName); 107 this.cphelper.getData().setTargetFileObject(this.helper.getData().getTargetFileObject()); 108 ResourceUtils.saveJDBCResourceDatatoXml(this.helper.getData(), this.cphelper.getData()); 109 }else{ 110 ResourceUtils.saveJDBCResourceDatatoXml(this.helper.getData(), null); 111 } 112 }catch (Exception ex){ 113 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 114 ex); 115 } 116 return java.util.Collections.EMPTY_SET; 117 } 118 119 public void initialize(WizardDescriptor wiz){ 120 this.wizardInfo = getWizardInfo(DATAFILE); 121 this.holder = new ResourceConfigHelperHolder(); 122 this.helper = holder.getDataSourceHelper(); 123 124 wiz.putProperty("NewFileWizard_Title", NbBundle.getMessage(ConnPoolWizard.class, "Templates/SunResources/JDBC_Resource")); index = 0; 127 128 Project project = Templates.getProject(wiz); 129 130 panels = createPanels(); 131 steps = createSteps(); 133 134 try{ 135 FileObject pkgLocation = project.getProjectDirectory(); 136 if (pkgLocation != null) { 137 this.helper.getData().setTargetFileObject(pkgLocation); 138 } 139 }catch (Exception ex){ 140 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 141 ex); 142 } 143 144 for (int i = 0; i < panels.length; i++) { 145 Component c = panels[i].getComponent(); 146 if (c instanceof JComponent ) { JComponent jc = (JComponent )c; 148 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty("WizardPanel_contentData", steps); } 153 } 154 155 } 156 157 public void uninitialize(WizardDescriptor wiz){ 158 panels = null; 160 } 161 162 public Wizard getWizardInfo(String filePath){ 163 try{ 164 InputStream in = Wizard.class.getClassLoader().getResourceAsStream(filePath); 165 this.wizardInfo = Wizard.createGraph(in); 166 }catch(Exception ex){ 167 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 168 ex); 169 } 170 return this.wizardInfo; 171 } 172 173 public String name(){ 174 return NbBundle.getMessage(DataSourceWizard.class, "Templates/SunResources/JDBC_Resource"); } 176 177 public boolean hasNext(){ 178 return index < panels.length - 1; 179 } 180 181 public boolean hasPrevious(){ 182 return index > 0; 183 } 184 185 public synchronized void nextPanel(){ 186 if (index + 1 == panels.length) { 187 throw new java.util.NoSuchElementException (); 188 } 189 190 if (index == 0) { 191 ((CommonPropertyPanel) panels[1]).setInitialFocus(); 192 }else if (index == 1) { 193 ((CPVendorPanel) panels[2]).setInitialFocus(); 194 }else if (index == 2){ 195 ((CPPropertiesPanelPanel) panels[3]).refreshFields(); 196 }else if (index == 3){ 197 ((CommonAttributePanel) panels[4]).setPropInitialFocus(); 198 } 199 200 index ++; 201 } 202 203 public synchronized void previousPanel(){ 204 if (index == 0) { 205 throw new java.util.NoSuchElementException (); 206 } 207 208 index--; 209 } 210 211 public WizardDescriptor.Panel current(){ 212 return (WizardDescriptor.Panel)panels[index]; 213 } 214 215 public final void addChangeListener(ChangeListener l) { 216 } 217 public final void removeChangeListener(ChangeListener l) { 218 } 219 220 public void setResourceConfigHelper(ResourceConfigHelper helper){ 221 this.helper = helper; 222 } 223 224 public ResourceConfigHelper getResourceConfigHelper(){ 225 return this.helper; 226 } 227 228 public void stateChanged(javax.swing.event.ChangeEvent e) { 229 if( (e.getSource().getClass() == CommonAttributePanel.class) || (e.getSource().getClass() == CommonAttributeVisualPanel.class) ) { 230 CommonAttributePanel commonPane = (CommonAttributePanel)this.current(); 231 CommonAttributeVisualPanel visPane = (CommonAttributeVisualPanel)commonPane.getComponent(); 232 boolean oldVal = addSteps; 233 this.addSteps = visPane.isNewResourceSelected(); 234 235 if((!oldVal && addSteps) || (oldVal && !addSteps)){ 236 this.holder.setHasCPHelper(this.addSteps); 237 if (addSteps && morePanels == null) { 238 addPanels(); 239 addSteps(); 240 for (int i = 0; i < panels.length; i++) { 241 Component c = panels[i].getComponent(); 242 if (steps[i] == null) { 243 steps[i] = c.getName(); 244 } 245 if (c instanceof JComponent ) { 246 JComponent jc = (JComponent )c; 247 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty("WizardPanel_contentData", steps); } 250 } 251 }else if((!addSteps) && (morePanels != null)){ 252 if(dsPanels != null){ 253 panels = dsPanels; 254 morePanels = null; 255 }else 256 panels = createPanels(); 257 if(dsSteps != null) 258 steps = dsSteps; 259 else 260 steps = createSteps(); 261 262 for (int i = 0; i < panels.length; i++) { 263 Component c = panels[i].getComponent(); 264 if (steps[i] == null) { 265 steps[i] = c.getName(); 266 } 267 if (c instanceof JComponent ) { 268 JComponent jc = (JComponent )c; 269 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty("WizardPanel_contentData", steps); } 272 } 273 ((CommonAttributePanel)panels[0]).setInitialFocus(); 274 } 275 } 276 } 277 } 278 279 protected void addPanels() { 280 if (panels != null && morePanels == null) { 281 this.cphelper = this.holder.addAssociatedHelper(); 282 this.cphelper.getData().setResourceName(__JdbcConnectionPool); 283 this.holder.setHasCPHelper(true); 284 this.cpWizardInfo = getWizardInfo(CP_DATAFILE); 285 this.cphelper.getData().setTargetFileObject(this.helper.getData().getTargetFileObject()); 286 this.cphelper.getData().setString(__DynamicWizPanel, "true"); 288 morePanels = new WizardDescriptor.Panel[] { 289 panels[0], 290 panels[1], 291 new CPVendorPanel(this.cphelper, this.cpWizardInfo), 292 new CPPropertiesPanelPanel(this.cphelper, this.cpWizardInfo), 293 new CommonAttributePanel(this.cphelper, this.cpWizardInfo, new String [] {"pool-setting", "pool-setting-2", "pool-setting-3"}), }; 295 } 296 dsPanels = panels; 297 panels = morePanels; 298 } 299 300 protected void addSteps() { 301 if (steps != null && moreSteps == null) { 302 moreSteps = new String [] { 303 steps[0], 304 steps[1], 305 steps[2], 306 NbBundle.getMessage(DataSourceWizard.class, "TITLE_ConnPoolWizardPanel_dbConn"), NbBundle.getMessage(DataSourceWizard.class, "TITLE_ConnPoolWizardPanel_properties"), NbBundle.getMessage(DataSourceWizard.class, "TITLE_ConnPoolWizardPanel_optionalProps") }; 310 } 311 dsSteps = steps; 312 steps = moreSteps; 313 } 314 } 315 | Popular Tags |