1 19 20 package org.netbeans.modules.web.wizards; 21 22 import java.io.IOException ; 23 import java.util.Collections ; 24 import java.util.NoSuchElementException ; 25 import java.util.Set ; 26 import javax.swing.JComponent ; 27 import javax.swing.event.ChangeListener ; 28 import org.netbeans.api.java.project.JavaProjectConstants; 29 import org.netbeans.api.project.ProjectUtils; 30 import org.netbeans.api.project.Sources; 31 import org.netbeans.modules.web.api.webmodule.WebModule; 32 import org.openide.WizardDescriptor; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.loaders.DataFolder; 35 import org.openide.loaders.DataObject; 36 import org.openide.loaders.TemplateWizard; 37 import org.openide.util.NbBundle; 38 import org.openide.filesystems.FileObject; 39 40 import org.netbeans.api.project.Project; 41 42 import org.netbeans.spi.project.ui.templates.support.Templates; 43 import org.netbeans.api.project.SourceGroup; 44 import org.netbeans.modules.web.core.Util; 45 import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates; 46 47 54 55 public class ServletIterator implements TemplateWizard.Iterator { 56 57 private static final long serialVersionUID = -4147344271705652643L; 58 59 private static final boolean debug = false; 60 61 private transient FileType fileType; 62 private transient Evaluator evaluator = null; 63 private transient DeployData deployData = null; 64 65 private transient int index; 66 private transient WizardDescriptor.Panel[] panels; 67 private transient TemplateWizard wizard; 68 private transient ServletPanel servletPanel; 69 private transient WizardDescriptor.Panel customPanel; 70 71 private ServletIterator(FileType fileType) { 72 this.fileType = fileType; 73 } 74 75 public static ServletIterator createServletIterator() { 76 return new ServletIterator(FileType.SERVLET); 77 } 78 79 public static ServletIterator createFilterIterator() { 80 return new ServletIterator(FileType.FILTER); 81 } 82 83 public void initialize (TemplateWizard wizard) { 84 85 this.wizard = wizard; 86 index = 0; 87 88 if(fileType.equals(FileType.SERVLET) || 89 fileType.equals(FileType.FILTER)) { 90 deployData = new ServletData(fileType); 91 evaluator = new TargetEvaluator(fileType, deployData); 92 } 93 Project project = Templates.getProject( wizard ); 94 DataFolder targetFolder=null; 95 try { 96 targetFolder = wizard.getTargetFolder(); 97 } catch (IOException ex) { 98 targetFolder = DataFolder.findFolder(project.getProjectDirectory()); 99 } 100 evaluator.setInitialFolder(targetFolder,project); 101 102 if(fileType == FileType.SERVLET) { 103 panels = new WizardDescriptor.Panel[] { 104 new FinishableProxyWizardPanel(createPackageChooserPanel(wizard,null)), 106 ServletPanel.createServletPanel((TargetEvaluator)evaluator, wizard) 107 }; 108 } 109 else if(fileType == FileType.FILTER) { 110 customPanel = new WrapperSelection(wizard); 111 panels = new WizardDescriptor.Panel[] { 112 createPackageChooserPanel(wizard,customPanel), 114 servletPanel=ServletPanel.createServletPanel((TargetEvaluator)evaluator, wizard), 115 ServletPanel.createFilterPanel((TargetEvaluator)evaluator, wizard) 116 }; 117 } 118 119 Object prop = wizard.getProperty ("WizardPanel_contentData"); String [] beforeSteps = null; 122 if (prop != null && prop instanceof String []) { 123 beforeSteps = (String [])prop; 124 } 125 String [] steps = Utilities.createSteps (beforeSteps, panels); 126 127 for (int i = 0; i < panels.length; i++) { 128 JComponent jc = (JComponent )panels[i].getComponent (); 129 if (steps[i] == null) { 130 steps[i] = jc.getName (); 134 } 135 jc.putClientProperty ("WizardPanel_contentSelectedIndex", new Integer (i)); 137 jc.putClientProperty ("WizardPanel_contentData", steps); } 139 } 140 141 private WizardDescriptor.Panel createPackageChooserPanel(TemplateWizard wizard, WizardDescriptor.Panel customPanel) { 142 Project project = Templates.getProject(wizard); 143 SourceGroup[] sourceGroups = Util.getJavaSourceGroups(project); 144 if (customPanel==null) { 145 if (sourceGroups.length == 0) 146 return Templates.createSimpleTargetChooser(project, sourceGroups); 147 else 148 return JavaTemplates.createPackageChooser(project, sourceGroups); 149 } else { 150 if (sourceGroups.length == 0) 151 return Templates.createSimpleTargetChooser(project, sourceGroups, customPanel); 152 else 153 return JavaTemplates.createPackageChooser(project, sourceGroups, customPanel); 154 } 155 } 156 157 public Set instantiate(TemplateWizard wizard) throws IOException { 158 159 if(debug) log("::instantiate()"); 160 166 168 FileObject dir = Templates.getTargetFolder( wizard ); 169 DataFolder df = DataFolder.findFolder( dir ); 170 171 FileObject template = Templates.getTemplate( wizard ); 172 if (FileType.FILTER.equals(fileType) && ((WrapperSelection)customPanel).isWrapper()) { 173 template = Templates.getTemplate( wizard ); 174 FileObject templateParent = template.getParent(); 175 template = templateParent.getFileObject("AdvancedFilter","java"); } 177 178 DataObject dTemplate = DataObject.find( template ); 179 DataObject dobj = dTemplate.createFromTemplate( df, Templates.getTargetName( wizard ) ); 180 181 if(debug) log("\topened file"); 183 if(!deployData.makeEntry()) { 186 return Collections.singleton(dobj); 187 } 188 189 TargetEvaluator te = (TargetEvaluator)evaluator; 190 191 if(debug) log("\tcreate dd entries"); 193 if (deployData.getClassName().length()==0) { 195 String targetName = wizard.getTargetName(); 196 FileObject targetFolder = Templates.getTargetFolder(wizard); 197 String packageName = null; 198 Project project = Templates.getProject( wizard ); 199 Sources sources = ProjectUtils.getSources(project); 200 SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 201 for (int i = 0; i < groups.length && packageName == null; i++) { 202 if (WebModule.getWebModule (groups [i].getRootFolder ()) != null) { 203 packageName = FileUtil.getRelativePath (groups [i].getRootFolder (), targetFolder); 204 } 205 } 206 if (packageName!=null) 207 packageName = packageName.replace('/','.'); 208 else packageName=""; 209 deployData.setClassName(packageName.length()==0?targetName:packageName+"."+targetName); 211 String servletName = ((ServletData)deployData).createDDServletName(targetName); 213 ((ServletData)deployData).createDDServletMapping(servletName); 214 } 215 deployData.createDDEntries(); 216 217 if(debug) log("\tURI param"); return Collections.singleton(dobj); 219 } 220 221 private void log(String s) { 222 System.out.println("ServletIterator" + s); 223 } 224 225 226 public void uninitialize (TemplateWizard wizard) { 227 this.wizard = null; 228 panels = null; 229 } 230 231 233 public String name () { 234 return NbBundle.getMessage (ServletIterator.class, "TITLE_x_of_y", 235 new Integer (index + 1), new Integer (panels.length)); 236 } 237 238 public boolean hasNext () { 242 if(debug) log("::hasNext()"); if(debug) log("\tindex is " + index); return index < panels.length - 1 && deployData.hasDD(); 245 } 246 247 public boolean hasPrevious () { 248 return index > 0; 249 } 250 public void nextPanel () { 251 if (! hasNext ()) throw new NoSuchElementException (); 252 index++; 253 } 254 public void previousPanel () { 255 if (! hasPrevious ()) throw new NoSuchElementException (); 256 index--; 257 } 258 public WizardDescriptor.Panel current () { 259 return panels[index]; 260 } 261 262 268 public final void addChangeListener (ChangeListener l) {} 270 public final void removeChangeListener (ChangeListener l) {} 271 302 303 } 304 | Popular Tags |