1 19 20 package org.netbeans.modules.web.wizards; 21 22 import java.awt.Component ; 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 import org.openide.cookies.SaveCookie; 30 31 import org.openide.filesystems.FileObject; 32 import org.openide.WizardDescriptor; 33 import org.openide.loaders.*; 34 import org.openide.util.NbBundle; 35 36 import org.netbeans.spi.project.ui.templates.support.Templates; 37 import org.netbeans.api.project.Project; 38 import org.netbeans.api.project.Sources; 39 import org.netbeans.api.project.SourceGroup; 40 import org.netbeans.modules.web.api.webmodule.WebProjectConstants; 41 import org.netbeans.modules.web.api.webmodule.WebModule; 42 import org.netbeans.api.java.project.JavaProjectConstants; 43 import org.netbeans.modules.web.core.Util; 44 45 import org.netbeans.modules.web.taglib.TLDDataObject; 46 import org.netbeans.modules.web.taglib.model.Taglib; 47 import org.netbeans.modules.web.taglib.model.TagFileType; 48 49 50 58 public class PageIterator implements TemplateWizard.Iterator { 59 60 private static final long serialVersionUID = -7586964579556513549L; 61 62 private transient FileType fileType; 63 private WizardDescriptor.Panel folderPanel; 64 private transient SourceGroup[] sourceGroups; 65 66 public static PageIterator createJspIterator() { 67 return new PageIterator(FileType.JSP); 68 } 69 70 public static PageIterator createTagIterator() { 71 return new PageIterator(FileType.TAG); 72 } 73 74 public static PageIterator createTagLibraryIterator() { 75 return new PageIterator(FileType.TAGLIBRARY); 76 } 77 78 public static PageIterator createHtmlIterator() { 79 return new PageIterator(FileType.HTML); 80 } 81 82 public static PageIterator createXHtmlIterator() { 83 return new PageIterator(FileType.XHTML); 84 } 85 86 private PageIterator(FileType fileType) { 87 this.fileType = fileType; 88 } 89 90 protected WizardDescriptor.Panel[] createPanels (Project project) { 92 Sources sources = (Sources) project.getLookup().lookup(org.netbeans.api.project.Sources.class); 93 if (fileType.equals(FileType.JSP)) { 94 sourceGroups = sources.getSourceGroups(WebProjectConstants.TYPE_DOC_ROOT); 95 if (sourceGroups==null || sourceGroups.length==0) 96 sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC); 97 folderPanel=new TargetChooserPanel(project,sourceGroups,fileType); 98 return new WizardDescriptor.Panel[] { 99 folderPanel 100 }; 101 } 102 else if (fileType.equals(FileType.HTML) || fileType.equals(FileType.XHTML)) { 103 SourceGroup[] docRoot = sources.getSourceGroups(WebProjectConstants.TYPE_DOC_ROOT); 104 SourceGroup[] srcRoots = Util.getJavaSourceGroups(project); 105 if (docRoot != null && srcRoots != null) { 106 sourceGroups = new SourceGroup[docRoot.length + srcRoots.length]; 107 System.arraycopy(docRoot, 0, sourceGroups, 0, docRoot.length); 108 System.arraycopy(srcRoots, 0, sourceGroups, docRoot.length, srcRoots.length); 109 } 110 if (sourceGroups==null || sourceGroups.length==0) 111 sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC); 112 folderPanel=new TargetChooserPanel(project,sourceGroups,fileType); 113 return new WizardDescriptor.Panel[] { 114 folderPanel 115 }; 116 } 117 else if (fileType.equals(FileType.TAG)) { 118 sourceGroups = sources.getSourceGroups(WebProjectConstants.TYPE_DOC_ROOT); 119 if (sourceGroups==null || sourceGroups.length==0) 120 sourceGroups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 121 if (sourceGroups==null || sourceGroups.length==0) 122 sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC); 123 folderPanel=new TargetChooserPanel(project,sourceGroups,fileType); 124 return new WizardDescriptor.Panel[] { 125 folderPanel 126 }; 127 } 128 else if (fileType.equals(FileType.TAGLIBRARY)) { 129 sourceGroups = sources.getSourceGroups(WebProjectConstants.TYPE_DOC_ROOT); 130 if (sourceGroups==null || sourceGroups.length==0) 131 sourceGroups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 132 if (sourceGroups==null || sourceGroups.length==0) 133 sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC); 134 folderPanel=new TargetChooserPanel(project,sourceGroups,fileType); 135 return new WizardDescriptor.Panel[] { 136 folderPanel 137 }; 138 } 139 return new WizardDescriptor.Panel[] { 140 Templates.createSimpleTargetChooser(project,sourceGroups) 141 }; 142 } 143 144 public Set instantiate (TemplateWizard wiz) throws IOException { 145 154 org.openide.filesystems.FileObject dir = Templates.getTargetFolder( wiz ); 155 DataFolder df = DataFolder.findFolder( dir ); 156 FileObject template = Templates.getTemplate( wiz ); 157 FileObject templateParent = template.getParent(); 158 TargetChooserPanel panel = (TargetChooserPanel)folderPanel; 159 160 if (FileType.JSP.equals(fileType)) { 161 if (panel.isSegment()) { 162 if (panel.isXml()) { 163 template = templateParent.getFileObject("JSPFX","jspf"); } else { 165 template = templateParent.getFileObject("JSPF","jspf"); } 167 } else { 168 if (panel.isXml()) { 169 template = templateParent.getFileObject("JSPX","jspx"); } 171 } 172 } else if (FileType.TAG.equals(fileType)) { 173 if (panel.isSegment()) { 174 if (panel.isXml()) { 175 template = templateParent.getFileObject("TagFileFX","tagf"); } else { 177 template = templateParent.getFileObject("TagFileF","tagf"); } 179 } else { 180 if (panel.isXml()) { 181 template = templateParent.getFileObject("TagFileX","tagx"); } 183 } 184 } else if (FileType.TAGLIBRARY.equals(fileType)) { 185 WebModule wm = WebModule.getWebModule (dir); 186 if (wm!=null) { 187 String j2eeVersion = wm.getJ2eePlatformVersion(); 188 if (WebModule.J2EE_13_LEVEL.equals(j2eeVersion)) { 189 template = templateParent.getFileObject("TagLibrary_1_2","tld"); } 191 } 192 } 193 DataObject dTemplate = DataObject.find( template ); 194 DataObject dobj = dTemplate.createFromTemplate( df, Templates.getTargetName( wiz ) ); 195 if (dobj!=null) { 196 if (FileType.TAGLIBRARY.equals(fileType)) { TLDDataObject tldDO = (TLDDataObject)dobj; 198 Taglib taglib = tldDO.getTaglib(); 199 taglib.setUri(panel.getUri()); 200 taglib.setShortName(panel.getPrefix()); 201 tldDO.write(taglib); 202 } else if (FileType.TAG.equals(fileType) && panel.isTldCheckBoxSelected()) { FileObject tldFo = panel.getTldFileObject(); 204 if (tldFo!=null) { 205 if (!tldFo.canWrite()) { 206 String mes = java.text.MessageFormat.format ( 207 NbBundle.getMessage (PageIterator.class, "MSG_tldRO"), 208 new Object [] {tldFo.getNameExt()}); 209 org.openide.NotifyDescriptor desc = new org.openide.NotifyDescriptor.Message(mes, 210 org.openide.NotifyDescriptor.Message.ERROR_MESSAGE); 211 org.openide.DialogDisplayer.getDefault().notify(desc); 212 } else { 213 TLDDataObject tldDO = (TLDDataObject)DataObject.find(tldFo); 214 Taglib taglib=null; 215 try { 216 taglib = tldDO.getTaglib(); 217 } catch (IOException ex) { 218 String mes = java.text.MessageFormat.format ( 219 NbBundle.getMessage (PageIterator.class, "MSG_tldCorrupted"), 220 new Object [] {tldFo.getNameExt()}); 221 org.openide.NotifyDescriptor desc = new org.openide.NotifyDescriptor.Message(mes, 222 org.openide.NotifyDescriptor.Message.ERROR_MESSAGE); 223 org.openide.DialogDisplayer.getDefault().notify(desc); 224 } 225 if (taglib!=null) { 226 TagFileType tag = new TagFileType(); 227 tag.setName(panel.getTagName()); 228 String packageName=null; 229 for (int i = 0; i < sourceGroups.length && packageName == null; i++) { 230 packageName = org.openide.filesystems.FileUtil.getRelativePath (sourceGroups [i].getRootFolder (), dobj.getPrimaryFile()); 231 } 232 tag.setPath("/"+packageName); taglib.addTagFile(tag); 234 SaveCookie save = (SaveCookie)tldDO.getCookie(SaveCookie.class); 235 if (save!=null) save.save(); 236 try { 237 tldDO.write(taglib); 238 } catch (IOException ex) { 239 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.EXCEPTION,ex); 240 } 241 } 242 } 243 } 244 } 245 246 253 } 254 return Collections.singleton(dobj); 255 } 256 257 259 private transient int index; 260 private transient WizardDescriptor.Panel[] panels; 261 private transient TemplateWizard wiz; 262 263 public void initialize (TemplateWizard wiz) { 268 this.wiz = wiz; 269 index = 0; 270 Project project = Templates.getProject( wiz ); 271 panels = createPanels (project); 272 273 Object prop = wiz.getProperty ("WizardPanel_contentData"); String [] beforeSteps = null; 276 if (prop != null && prop instanceof String []) { 277 beforeSteps = (String [])prop; 278 } 279 String [] steps = Utilities.createSteps (beforeSteps, panels); 280 281 for (int i = 0; i < panels.length; i++) { 282 Component c = panels[i].getComponent (); 283 if (steps[i] == null) { 284 steps[i] = c.getName (); 288 } 289 if (c instanceof JComponent ) { JComponent jc = (JComponent ) c; 291 jc.putClientProperty ("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty ("WizardPanel_contentData", steps); } 296 } 297 } 298 public void uninitialize (TemplateWizard wiz) { 299 this.wiz = null; 300 panels = null; 301 } 302 303 308 public String name () { 309 return NbBundle.getMessage(PageIterator.class, "TITLE_x_of_y", 310 new Integer (index + 1), new Integer (panels.length)); 311 } 312 313 public boolean hasNext () { 314 return index < panels.length - 1; 315 } 316 public boolean hasPrevious () { 317 return index > 0; 318 } 319 public void nextPanel () { 320 if (! hasNext ()) throw new NoSuchElementException (); 321 index++; 322 } 323 public void previousPanel () { 324 if (! hasPrevious ()) throw new NoSuchElementException (); 325 index--; 326 } 327 public WizardDescriptor.Panel current () { 328 return panels[index]; 329 } 330 331 public final void addChangeListener (ChangeListener l) {} 333 public final void removeChangeListener (ChangeListener l) {} 334 } 339 | Popular Tags |