1 19 package org.netbeans.modules.j2ee.sun.share.configbean.templates; 20 21 import java.awt.Component ; 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.Collections ; 25 import java.util.NoSuchElementException ; 26 import java.util.Set ; 27 import javax.swing.JComponent ; 28 import javax.swing.event.ChangeListener ; 29 30 import org.openide.DialogDisplayer; 31 import org.openide.ErrorManager; 32 import org.openide.NotifyDescriptor; 33 import org.openide.WizardDescriptor; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileSystem; 36 import org.openide.filesystems.FileUtil; 37 import org.openide.filesystems.Repository; 38 import org.openide.util.NbBundle; 39 40 import org.netbeans.api.project.Project; 41 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 42 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 43 import org.netbeans.modules.j2ee.sun.share.configbean.SunONEDeploymentConfiguration; 44 45 46 50 public final class SunDDWizardIterator implements WizardDescriptor.InstantiatingIterator { 51 52 private int index; 53 54 private WizardDescriptor wizard; 55 private WizardDescriptor.Panel[] panels; 56 57 61 private WizardDescriptor.Panel[] getPanels() { 62 if (panels == null) { 63 panels = new WizardDescriptor.Panel[] { 64 new SunDDWizardPanel() 65 }; 66 String [] steps = createSteps(); 67 for (int i = 0; i < panels.length; i++) { 68 Component c = panels[i].getComponent(); 69 if (steps[i] == null) { 70 steps[i] = c.getName(); 74 } 75 if (c instanceof JComponent ) { JComponent jc = (JComponent ) c; 77 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty("WizardPanel_contentData", steps); jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE); jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE); } 88 } 89 } 90 return panels; 91 } 92 93 public Set instantiate() throws IOException { 94 Set result = Collections.EMPTY_SET; 95 SunDDWizardPanel wizardPanel = (SunDDWizardPanel) panels[0]; 96 97 File configDir = wizardPanel.getSelectedLocation(); 98 if(!configDir.exists()) { 99 configDir.mkdirs(); 100 } 101 FileObject configFolder = FileUtil.toFileObject(configDir); 102 Project project = wizardPanel.getProject(); 103 String sunDDFileName = wizardPanel.getFileName(); 104 105 J2eeModuleProvider j2eeModuleProvider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class); 106 J2eeModule j2eeModule = j2eeModuleProvider.getJ2eeModule(); 107 if(configFolder != null) { 108 String resource = "org-netbeans-modules-j2ee-sun-ddui/" + sunDDFileName; FileObject sunDDTemplate = Repository.getDefault().getDefaultFileSystem().findResource(resource); 110 if(sunDDTemplate != null) { 111 FileSystem fs = configFolder.getFileSystem(); 112 XmlFileCreator creator = new XmlFileCreator(sunDDTemplate, configFolder, 113 sunDDTemplate.getName(), sunDDTemplate.getExt()); 114 fs.runAtomicAction(creator); 115 FileObject sunDDFO = creator.getResult(); 116 if(sunDDFO != null) { 117 SunONEDeploymentConfiguration config = 118 SunONEDeploymentConfiguration.getConfiguration(FileUtil.toFile(sunDDFO)); 119 if(config != null) { 120 config.setAppServerVersion(config.getMaxASVersion()); 122 } else { 123 NotifyDescriptor nd = new NotifyDescriptor.Message( 124 NbBundle.getMessage(SunDDWizardIterator.class,"ERR_NoDeploymentConfiguration"), NotifyDescriptor.ERROR_MESSAGE); 126 DialogDisplayer.getDefault().notify(nd); 127 } 128 result = Collections.singleton(creator.getResult()); 129 } else { 130 NotifyDescriptor nd = new NotifyDescriptor.Message( 131 NbBundle.getMessage(SunDDWizardIterator.class,"ERR_FileCreationFailed", sunDDFileName), NotifyDescriptor.ERROR_MESSAGE); 133 DialogDisplayer.getDefault().notify(nd); 134 } 135 } else { 136 NotifyDescriptor nd = new NotifyDescriptor.Message( 137 NbBundle.getMessage(SunDDWizardIterator.class,"ERR_TemplateNotFound", resource), NotifyDescriptor.ERROR_MESSAGE); 139 DialogDisplayer.getDefault().notify(nd); 140 } 141 } else { 142 NotifyDescriptor nd = new NotifyDescriptor.Message( 143 NbBundle.getMessage(SunDDWizardIterator.class,"ERR_LocationNotFound", configDir.getAbsolutePath()), NotifyDescriptor.ERROR_MESSAGE); 145 DialogDisplayer.getDefault().notify(nd); 146 } 147 return result; 148 } 149 150 private static class XmlFileCreator implements FileSystem.AtomicAction { 151 152 private final FileObject source; 153 private final FileObject destFolder; 154 private final String name; 155 private final String ext; 156 private FileObject result; 157 158 XmlFileCreator(final FileObject source, final FileObject destFolder, final String name, final String ext) { 159 this.source = source; 160 this.destFolder = destFolder; 161 this.name = name; 162 this.ext = ext; 163 this.result = null; 164 } 165 166 public void run() throws IOException { 167 result = FileUtil.copyFile(source, destFolder, name, ext); 168 } 169 170 public FileObject getResult() { 171 return result; 172 } 173 } 174 175 public void initialize(WizardDescriptor wizard) { 176 this.wizard = wizard; 177 } 178 179 public void uninitialize(WizardDescriptor wizard) { 180 panels = null; 181 } 182 183 public WizardDescriptor.Panel current() { 184 return getPanels()[index]; 185 } 186 187 public String name() { 188 return index + 1 + ". from " + getPanels().length; 189 } 190 191 public boolean hasNext() { 192 return index < getPanels().length - 1; 193 } 194 195 public boolean hasPrevious() { 196 return index > 0; 197 } 198 199 public void nextPanel() { 200 if (!hasNext()) { 201 throw new NoSuchElementException (); 202 } 203 index++; 204 } 205 206 public void previousPanel() { 207 if (!hasPrevious()) { 208 throw new NoSuchElementException (); 209 } 210 index--; 211 } 212 213 public void addChangeListener(ChangeListener l) {} 215 public void removeChangeListener(ChangeListener l) {} 216 217 243 244 private String [] createSteps() { 249 String [] beforeSteps = null; 250 Object prop = wizard.getProperty("WizardPanel_contentData"); if (prop != null && prop instanceof String []) { 252 beforeSteps = (String []) prop; 253 } 254 255 if (beforeSteps == null) { 256 beforeSteps = new String [0]; 257 } 258 259 String [] res = new String [(beforeSteps.length - 1) + panels.length]; 260 for (int i = 0; i < res.length; i++) { 261 if (i < (beforeSteps.length - 1)) { 262 res[i] = beforeSteps[i]; 263 } else { 264 res[i] = panels[i - beforeSteps.length + 1].getComponent().getName(); 265 } 266 } 267 return res; 268 } 269 } 270 | Popular Tags |