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 java.text.MessageFormat ; 28 import javax.swing.JComponent ; 29 import javax.swing.event.ChangeListener ; 30 import org.netbeans.api.java.source.JavaSource; 31 import org.netbeans.modules.web.core.Util; 32 33 import org.openide.filesystems.FileObject; 34 import org.openide.WizardDescriptor; 35 import org.openide.cookies.SaveCookie; 36 import org.openide.loaders.*; 37 import org.openide.util.NbBundle; 38 import org.openide.ErrorManager; 39 import org.openide.NotifyDescriptor; 40 import org.openide.DialogDisplayer; 41 42 import org.netbeans.spi.project.ui.templates.support.Templates; 43 import org.netbeans.api.project.Project; 44 import org.netbeans.api.project.Sources; 45 import org.netbeans.api.project.SourceGroup; 46 import org.netbeans.modules.web.api.webmodule.WebProjectConstants; 47 import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates; 48 49 import org.netbeans.modules.web.taglib.TLDDataObject; 50 import org.netbeans.modules.web.taglib.model.Taglib; 51 import org.netbeans.modules.web.taglib.model.TagType; 52 import org.netbeans.modules.web.taglib.model.TldAttributeType; 53 54 62 public class TagHandlerIterator implements TemplateWizard.Iterator { 63 private transient FileType fileType; 64 private WizardDescriptor.Panel packageChooserPanel,tagHandlerSelectionPanel,tagInfoPanel; 65 66 protected WizardDescriptor.Panel[] createPanels (Project project,TemplateWizard wiz) { 68 Sources sources = (Sources) project.getLookup().lookup(org.netbeans.api.project.Sources.class); 69 SourceGroup[] sourceGroups = Util.getJavaSourceGroups(project); 70 tagHandlerSelectionPanel = new TagHandlerSelection(wiz); 71 72 if (sourceGroups.length == 0) 73 packageChooserPanel = Templates.createSimpleTargetChooser(project, sourceGroups, tagHandlerSelectionPanel); 74 else 75 packageChooserPanel = JavaTemplates.createPackageChooser(project,sourceGroups,tagHandlerSelectionPanel); 76 77 sourceGroups = sources.getSourceGroups(WebProjectConstants.TYPE_DOC_ROOT); 78 if (sourceGroups==null || sourceGroups.length==0) 79 sourceGroups = Util.getJavaSourceGroups(project); 80 if (sourceGroups==null || sourceGroups.length==0) 81 sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC); 82 tagInfoPanel = new TagInfoPanel(wiz, project, sourceGroups); 83 return new WizardDescriptor.Panel[] { 84 packageChooserPanel, 85 tagInfoPanel 86 }; 87 } 88 89 public Set instantiate (TemplateWizard wiz) throws IOException { 90 99 org.openide.filesystems.FileObject dir = Templates.getTargetFolder( wiz ); 100 DataFolder df = DataFolder.findFolder( dir ); 101 102 FileObject template = Templates.getTemplate( wiz ); 103 104 if (((TagHandlerSelection)tagHandlerSelectionPanel).isBodyTagSupport()) { 105 FileObject templateParent = template.getParent(); 106 template = templateParent.getFileObject("BodyTagHandler","java"); } 108 DataObject dTemplate = DataObject.find( template ); 109 DataObject dobj = dTemplate.createFromTemplate( df, Templates.getTargetName( wiz ) ); 110 TagInfoPanel tldPanel = (TagInfoPanel)tagInfoPanel; 112 Object [][] attrs = tldPanel.getAttributes(); 113 boolean isBodyTag = ((TagHandlerSelection)tagHandlerSelectionPanel).isBodyTagSupport(); 114 115 if (attrs.length>0 || isBodyTag) { 117 JavaSource clazz = JavaSource.forFileObject(dobj.getPrimaryFile()); 118 boolean evaluateBody = !((TagInfoPanel)tagInfoPanel).isEmpty(); 119 TagHandlerGenerator generator = new TagHandlerGenerator(clazz,attrs,isBodyTag, evaluateBody); 120 try { 121 generator.generate(); 122 } catch (IOException ex){ 123 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex); 124 } 125 } 126 127 if (tldPanel.writeToTLD()) { 129 FileObject tldFo = tldPanel.getTLDFile(); 130 if (tldFo!=null) { 131 if (!tldFo.canWrite()) { 132 String mes = MessageFormat.format ( 133 NbBundle.getMessage (TagHandlerIterator.class, "MSG_tldRO"), 134 new Object [] {tldFo.getNameExt()}); 135 NotifyDescriptor desc = new NotifyDescriptor.Message(mes,NotifyDescriptor.Message.ERROR_MESSAGE); 136 DialogDisplayer.getDefault().notify(desc); 137 } else { 138 TLDDataObject tldDO = (TLDDataObject)DataObject.find(tldFo); 139 Taglib taglib=null; 140 try { 141 taglib = tldDO.getTaglib(); 142 } catch (IOException ex) { 143 String mes = MessageFormat.format ( 144 NbBundle.getMessage (TagHandlerIterator.class, "MSG_tldCorrupted"), 145 new Object [] {tldFo.getNameExt()}); 146 NotifyDescriptor desc = new NotifyDescriptor.Message(mes,NotifyDescriptor.Message.ERROR_MESSAGE); 147 DialogDisplayer.getDefault().notify(desc); 148 } 149 if (taglib!=null) { 150 TagType tag = new TagType(); 151 tag.setName(tldPanel.getTagName()); 152 tag.setTagClass(tldPanel.getClassName()); 153 if (tldPanel.isEmpty()) { 154 tag.setBodyContent("empty"); } else if (tldPanel.isScriptless()) { 156 tag.setBodyContent(isBodyTag?"JSP":"scriptless"); } else if (tldPanel.isTegdependent()) { 158 tag.setBodyContent("tagdependent"); } 160 for (int i=0;i<attrs.length;i++) { 162 TldAttributeType attr = new TldAttributeType(); 163 attr.setName((String )attrs[i][0]); 164 attr.setType((String )attrs[i][1]); 165 boolean required = ((Boolean )attrs[i][2]).booleanValue(); 166 if (required) attr.setRequired("true"); boolean rtexpr = ((Boolean )attrs[i][3]).booleanValue(); 168 if (rtexpr) attr.setRtexprvalue("true"); tag.addAttribute(attr); 170 } 171 taglib.addTag(tag); 172 SaveCookie save = (SaveCookie)tldDO.getCookie(SaveCookie.class); 173 if (save!=null) save.save(); 174 try { 175 tldDO.write(taglib); 176 } catch (IOException ex) { 177 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION,ex); 178 } 179 } 180 } 181 } 182 } 183 184 return Collections.singleton(dobj); 185 } 186 187 189 private transient int index; 190 private transient WizardDescriptor.Panel[] panels; 191 private transient TemplateWizard wiz; 192 193 private static final long serialVersionUID = -7586964579556513549L; 194 195 public void initialize (TemplateWizard wiz) { 200 this.wiz = wiz; 201 index = 0; 202 Project project = Templates.getProject( wiz ); 203 panels = createPanels (project,wiz); 204 205 Object prop = wiz.getProperty ("WizardPanel_contentData"); String [] beforeSteps = null; 208 if (prop != null && prop instanceof String []) { 209 beforeSteps = (String [])prop; 210 } 211 String [] steps = Utilities.createSteps (beforeSteps, panels); 212 213 for (int i = 0; i < panels.length; i++) { 214 Component c = panels[i].getComponent (); 215 if (steps[i] == null) { 216 steps[i] = c.getName (); 220 } 221 if (c instanceof JComponent ) { JComponent jc = (JComponent ) c; 223 jc.putClientProperty ("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty ("WizardPanel_contentData", steps); } 228 } 229 } 230 public void uninitialize (TemplateWizard wiz) { 231 this.wiz = null; 232 panels = null; 233 } 234 235 240 public String name () { 241 return NbBundle.getMessage(TagHandlerIterator.class, "TITLE_x_of_y", 242 new Integer (index + 1), new Integer (panels.length)); 243 } 244 245 public boolean hasNext () { 246 return index < panels.length - 1; 247 } 248 public boolean hasPrevious () { 249 return index > 0; 250 } 251 public void nextPanel () { 252 if (! hasNext ()) throw new NoSuchElementException (); 253 index++; 254 } 255 public void previousPanel () { 256 if (! hasPrevious ()) throw new NoSuchElementException (); 257 index--; 258 } 259 public WizardDescriptor.Panel current () { 260 return panels[index]; 261 } 262 263 public final void addChangeListener (ChangeListener l) {} 265 public final void removeChangeListener (ChangeListener l) {} 266 } 271 | Popular Tags |