1 19 20 package org.netbeans.modules.web.struts.wizards; 21 22 import java.io.IOException ; 23 import java.util.Collections ; 24 import java.util.NoSuchElementException ; 25 import java.util.Set ; 26 27 import javax.swing.JComponent ; 28 import javax.swing.event.ChangeListener ; 29 import org.netbeans.api.project.Sources; 30 import org.netbeans.modules.web.struts.config.model.FormBeans; 31 32 import org.openide.WizardDescriptor; 33 import org.openide.filesystems.FileObject; 34 import org.openide.loaders.DataObject; 35 import org.openide.loaders.DataFolder; 36 import org.openide.loaders.TemplateWizard; 37 import org.openide.util.NbBundle; 38 39 import org.netbeans.api.java.project.JavaProjectConstants; 40 import org.netbeans.api.project.Project; 41 import org.netbeans.api.project.ProjectUtils; 42 43 import org.netbeans.api.project.SourceGroup; 44 import org.netbeans.editor.BaseDocument; 45 import org.netbeans.modules.web.api.webmodule.WebModule; 46 import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates; 47 import org.netbeans.spi.project.ui.templates.support.Templates; 48 import org.netbeans.modules.web.struts.StrutsConfigDataObject; 49 import org.netbeans.modules.web.struts.config.model.FormBean; 50 import org.netbeans.modules.web.struts.config.model.StrutsConfig; 51 import org.netbeans.modules.web.struts.editor.StrutsEditorUtilities; 52 import org.openide.cookies.EditorCookie; 53 import org.openide.cookies.OpenCookie; 54 import org.openide.cookies.SaveCookie; 55 56 57 62 63 public class FormBeanIterator implements TemplateWizard.Iterator { 64 65 private int index; 66 67 private transient WizardDescriptor.Panel[] panels; 68 69 private transient boolean debug = false; 70 71 public void initialize (TemplateWizard wizard) { 72 if (debug) log ("initialize"); index = 0; 74 Project project = Templates.getProject( wizard ); 76 DataFolder targetFolder=null; 77 try { 78 targetFolder = wizard.getTargetFolder(); 79 } catch (IOException ex) { 80 targetFolder = DataFolder.findFolder(project.getProjectDirectory()); 81 } 82 83 SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups( 84 JavaProjectConstants.SOURCES_TYPE_JAVA); 85 if (debug) { 86 log ("\tproject: " + project); log ("\ttargetFolder: " + targetFolder); log ("\tsourceGroups.length: " + sourceGroups.length); } 90 91 WizardDescriptor.Panel secondPanel = new FormBeanNewPanel(project, wizard); 92 93 WizardDescriptor.Panel javaPanel; 94 if (sourceGroups.length == 0) 95 javaPanel = Templates.createSimpleTargetChooser(project, sourceGroups, secondPanel); 96 else 97 javaPanel = JavaTemplates.createPackageChooser(project, sourceGroups, secondPanel); 98 99 100 panels = new WizardDescriptor.Panel[] { 101 javaPanel 102 }; 103 104 Object prop = wizard.getProperty ("WizardPanel_contentData"); String [] beforeSteps = null; 107 if (prop != null && prop instanceof String []) { 108 beforeSteps = (String [])prop; 109 } 110 String [] steps = createSteps (beforeSteps, panels); 111 112 for (int i = 0; i < panels.length; i++) { 113 JComponent jc = (JComponent )panels[i].getComponent (); 114 if (steps[i] == null) { 115 steps[i] = jc.getName (); 116 } 117 jc.putClientProperty ("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty ("WizardPanel_contentData", steps); } 120 } 121 122 public void uninitialize (TemplateWizard wizard) { 123 panels = null; 124 } 125 126 public Set instantiate(TemplateWizard wizard) throws IOException { 127 130 if (debug) 131 log("instantiate"); 133 FileObject dir = Templates.getTargetFolder( wizard ); 134 DataFolder df = DataFolder.findFolder( dir ); 135 FileObject template = Templates.getTemplate( wizard ); 136 137 DataObject dTemplate = DataObject.find( template ); 138 DataObject dobj = dTemplate.createFromTemplate( df, Templates.getTargetName( wizard ) ); 139 140 EditorCookie editorCookie = (EditorCookie) dobj.getCookie(EditorCookie.class); 141 if (editorCookie != null) { 142 javax.swing.text.Document doc = editorCookie.openDocument(); 143 replaceInDocument(doc, "__SUPERCLASS__", (String ) wizard.getProperty(WizardProperties.FORMBEAN_SUPERCLASS)); SaveCookie save = (SaveCookie) dobj.getCookie(SaveCookie.class); 145 if (save != null) 146 save.save(); 147 } 148 149 Project project = Templates.getProject( wizard ); 150 WebModule wm = WebModule.getWebModule(project.getProjectDirectory()); 151 String configFile = (String ) wizard.getProperty(WizardProperties.FORMBEAN_CONFIG_FILE); 152 153 if (wm != null && configFile != null && !"".equals(configFile)){ dir = wm.getDocumentBase(); 156 157 String targetName = Templates.getTargetName(wizard); 158 Sources sources = ProjectUtils.getSources(project); 159 SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 160 String packageName = null; 161 org.openide.filesystems.FileObject targetFolder = Templates.getTargetFolder(wizard); 162 for (int i = 0; i < groups.length && packageName == null; i++) { 163 packageName = org.openide.filesystems.FileUtil.getRelativePath (groups [i].getRootFolder (), targetFolder); 164 if (packageName!=null) break; 165 } 166 if (packageName!=null) packageName = packageName.replace('/','.'); 167 else packageName=""; String className=null; 169 if (packageName.length()>0) 170 className=packageName+"."+targetName; else 172 className=targetName; 173 174 175 FileObject fo = dir.getFileObject(configFile); if (fo != null){ 177 StrutsConfigDataObject configDO = (StrutsConfigDataObject)DataObject.find(fo); 178 StrutsConfig config= configDO.getStrutsConfig(); 179 180 FormBean formBean = new FormBean(); 181 formBean.setAttributeValue("name", targetName); formBean.setAttributeValue("type", className); if (config != null && config.getFormBeans()==null){ 184 config.setFormBeans(new FormBeans()); 185 } 186 187 config.getFormBeans().addFormBean(formBean); 188 BaseDocument doc = (BaseDocument)configDO.getEditorSupport().getDocument(); 189 if (doc == null){ 190 ((OpenCookie)configDO.getCookie(OpenCookie.class)).open(); 191 doc = (BaseDocument)configDO.getEditorSupport().getDocument(); 192 } 193 StrutsEditorUtilities.writeBean(doc, formBean, "form-bean", "form-beans"); configDO.getEditorSupport().saveDocument(); 195 } 196 } 197 return Collections.singleton(dobj); 198 } 199 200 public void previousPanel () { 201 if (! hasPrevious ()) throw new NoSuchElementException (); 202 index--; 203 } 204 205 public void nextPanel () { 206 if (! hasNext ()) throw new NoSuchElementException (); 207 index++; 208 } 209 210 public boolean hasPrevious () { 211 return index > 0; 212 } 213 214 public boolean hasNext () { 215 return index < panels.length - 1; 216 } 217 218 public String name () { 219 return NbBundle.getMessage (ActionIterator.class, "TITLE_x_of_y", new Integer (index + 1), new Integer (panels.length)); 221 } 222 223 public WizardDescriptor.Panel current () { 224 return panels[index]; 225 } 226 public final void addChangeListener (ChangeListener l) {} 228 public final void removeChangeListener (ChangeListener l) {} 229 230 231 private void log (String message){ 232 System.out.println("ActionIterator:: \t" + message); } 234 235 private String [] createSteps(String [] before, WizardDescriptor.Panel[] panels) { 236 int diff = 0; 237 if (before == null) { 238 before = new String [0]; 239 } else if (before.length > 0) { 240 diff = ("...".equals (before[before.length - 1])) ? 1 : 0; } 242 String [] res = new String [ (before.length - diff) + panels.length]; 243 for (int i = 0; i < res.length; i++) { 244 if (i < (before.length - diff)) { 245 res[i] = before[i]; 246 } else { 247 res[i] = panels[i - before.length + diff].getComponent ().getName (); 248 } 249 } 250 return res; 251 } 252 253 private void replaceInDocument(javax.swing.text.Document document, String replaceFrom, String replaceTo) { 254 javax.swing.text.AbstractDocument doc = (javax.swing.text.AbstractDocument )document; 255 int len = replaceFrom.length(); 256 try { 257 String content = doc.getText(0,doc.getLength()); 258 int index = content.lastIndexOf(replaceFrom); 259 while (index>=0) { 260 doc.replace(index,len,replaceTo,null); 261 content=content.substring(0,index); 262 index = content.lastIndexOf(replaceFrom); 263 } 264 } catch (javax.swing.text.BadLocationException ex){} 265 } 266 267 } 268 | Popular Tags |