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 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.netbeans.modules.web.struts.StrutsConfigDataObject; 33 import org.netbeans.modules.web.struts.config.model.*; 34 import org.netbeans.modules.web.struts.editor.StrutsEditorUtilities; 35 import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates; 36 import org.openide.WizardDescriptor; 37 import org.openide.loaders.DataFolder; 38 import org.openide.loaders.DataObject; 39 import org.openide.loaders.TemplateWizard; 40 import org.openide.util.NbBundle; 41 import org.openide.filesystems.FileObject; 42 43 import org.netbeans.api.project.Project; 44 45 import org.netbeans.spi.project.ui.templates.support.Templates; 46 import org.netbeans.api.project.SourceGroup; 47 import org.netbeans.editor.BaseDocument; 48 import org.openide.cookies.EditorCookie; 49 import org.openide.cookies.OpenCookie; 50 import org.openide.cookies.SaveCookie; 51 52 57 58 public class ActionIterator implements TemplateWizard.Iterator { 59 60 private int index; 61 62 private transient WizardDescriptor.Panel[] panels; 63 64 private transient boolean debug = false; 65 66 public void initialize (TemplateWizard wizard) { 67 if (debug) log ("initialize"); index = 0; 69 Project project = Templates.getProject( wizard ); 71 DataFolder targetFolder=null; 72 try { 73 targetFolder = wizard.getTargetFolder(); 74 } catch (IOException ex) { 75 targetFolder = DataFolder.findFolder(project.getProjectDirectory()); 76 } 77 78 SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups( 79 JavaProjectConstants.SOURCES_TYPE_JAVA); 80 if (debug) { 81 log ("\tproject: " + project); log ("\ttargetFolder: " + targetFolder); log ("\tsourceGroups.length: " + sourceGroups.length); } 85 86 WizardDescriptor.Panel secondPanel = new ActionPanel(project, wizard); 87 WizardDescriptor.Panel thirdPanel = new ActionPanel1(project); 88 89 WizardDescriptor.Panel javaPanel; 90 if (sourceGroups.length == 0) 91 javaPanel = new FinishableProxyWizardPanel(Templates.createSimpleTargetChooser(project, sourceGroups, secondPanel)); 92 else 93 javaPanel = new FinishableProxyWizardPanel(JavaTemplates.createPackageChooser(project, sourceGroups, secondPanel)); 94 95 panels = new WizardDescriptor.Panel[] { javaPanel, thirdPanel }; 96 97 Object prop = wizard.getProperty ("WizardPanel_contentData"); String [] beforeSteps = null; 100 if (prop != null && prop instanceof String []) { 101 beforeSteps = (String [])prop; 102 } 103 String [] steps = createSteps (beforeSteps, panels); 104 105 for (int i = 0; i < panels.length; i++) { 106 JComponent jc = (JComponent )panels[i].getComponent (); 107 if (steps[i] == null) { 108 steps[i] = jc.getName (); 109 } 110 jc.putClientProperty ("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty ("WizardPanel_contentData", steps); } 113 } 114 115 public void uninitialize (TemplateWizard wizard) { 116 panels = null; 117 } 118 119 public Set instantiate(TemplateWizard wizard) throws IOException { 120 if (debug) 121 log("instantiate"); 123 FileObject dir = Templates.getTargetFolder( wizard ); 124 DataFolder df = DataFolder.findFolder( dir ); 125 FileObject template = Templates.getTemplate( wizard ); 126 127 String superclass=(String )wizard.getProperty(WizardProperties.ACTION_SUPERCLASS); 128 if (debug) 129 log("superclass="+superclass); boolean replaceSuperClass = false; 131 if (ActionPanelVisual.DEFAULT_ACTION.equals(superclass)){ 132 superclass = "Action"; 133 replaceSuperClass = true; 134 } else if (ActionPanelVisual.DISPATCH_ACTION.equals(superclass)) { 135 FileObject templateParent = template.getParent(); 136 template = templateParent.getFileObject("DispatchAction","java"); } else if (ActionPanelVisual.MAPPING_DISPATCH_ACTION.equals(superclass)) { 138 FileObject templateParent = template.getParent(); 139 template = templateParent.getFileObject("MappingDispatchAction","java"); } else if (ActionPanelVisual.LOOKUP_DISPATCH_ACTION.equals(superclass)) { 141 FileObject templateParent = template.getParent(); 142 template = templateParent.getFileObject("LookupDispatchAction","java"); } 144 else { 145 replaceSuperClass = true; 146 } 147 148 149 String targetName = Templates.getTargetName(wizard); 150 DataObject dTemplate = DataObject.find( template ); 151 DataObject dobj = dTemplate.createFromTemplate( df, targetName ); 152 if (replaceSuperClass){ 153 EditorCookie editorCookie = (EditorCookie) dobj.getCookie(EditorCookie.class); 154 if (editorCookie != null) { 155 javax.swing.text.Document doc = editorCookie.openDocument(); 156 replaceInDocument(doc, "__SUPERCLASS__", superclass); SaveCookie save = (SaveCookie) dobj.getCookie(SaveCookie.class); 158 if (save != null) 159 save.save(); 160 } 161 } 162 163 Project project = Templates.getProject( wizard ); 164 WebModule wm = WebModule.getWebModule(project.getProjectDirectory()); 165 String configFile = (String ) wizard.getProperty(WizardProperties.ACTION_CONFIG_FILE); 166 if (wm != null && configFile != null && !"".equals(configFile)) { dir = wm.getDocumentBase(); 169 170 FileObject fo = dir.getFileObject(configFile); 171 StrutsConfigDataObject configDO = (StrutsConfigDataObject)DataObject.find(fo); 172 StrutsConfig config= configDO.getStrutsConfig(); 173 Action action = new Action(); 174 175 Sources sources = ProjectUtils.getSources(project); 176 SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 177 String packageName = null; 178 org.openide.filesystems.FileObject targetFolder = Templates.getTargetFolder(wizard); 179 for (int i = 0; i < groups.length && packageName == null; i++) { 180 packageName = org.openide.filesystems.FileUtil.getRelativePath (groups [i].getRootFolder (), targetFolder); 181 if (packageName!=null) break; 182 } 183 if (packageName!=null) packageName = packageName.replace('/','.'); 184 else packageName=""; String className=null; 186 if (packageName.length()>0) 187 className=packageName+"."+targetName; else 189 className=targetName; 190 action.setAttributeValue("type", className); 192 String path = (String ) wizard.getProperty(WizardProperties.ACTION_PATH); 193 action.setAttributeValue("path", path.startsWith("/") ? path : "/" + path); 195 String formName = (String ) wizard.getProperty(WizardProperties.ACTION_FORM_NAME); 196 if (formName!=null) { 197 action.setAttributeValue("name", formName); action.setAttributeValue("scope",(String ) wizard.getProperty(WizardProperties.ACTION_SCOPE)); action.setAttributeValue("input",(String ) wizard.getProperty(WizardProperties.ACTION_INPUT)); action.setAttributeValue("attribute",(String ) wizard.getProperty(WizardProperties.ACTION_ATTRIBUTE)); Boolean validate = (Boolean ) wizard.getProperty(WizardProperties.ACTION_VALIDATE); 202 if (Boolean.FALSE.equals(validate)) action.setAttributeValue("validate","false"); action.setAttributeValue("attribute",(String ) wizard.getProperty(WizardProperties.ACTION_ATTRIBUTE)); } 205 action.setAttributeValue("parameter",(String ) wizard.getProperty(WizardProperties.ACTION_PARAMETER)); 207 if (config != null && config.getActionMappings() == null) 208 config.setActionMappings(new ActionMappings()); 209 config.getActionMappings().addAction(action); 210 BaseDocument doc = (BaseDocument)configDO.getEditorSupport().getDocument(); 211 if (doc == null){ 212 ((OpenCookie)configDO.getCookie(OpenCookie.class)).open(); 213 doc = (BaseDocument)configDO.getEditorSupport().getDocument(); 214 } 215 StrutsEditorUtilities.writeBean(doc, action, "action", "action-mappings"); configDO.getEditorSupport().saveDocument(); 217 } 218 return Collections.singleton(dobj); 219 } 220 221 public void previousPanel () { 222 if (! hasPrevious ()) throw new NoSuchElementException (); 223 index--; 224 } 225 226 public void nextPanel () { 227 if (! hasNext ()) throw new NoSuchElementException (); 228 index++; 229 } 230 231 public boolean hasPrevious () { 232 return index > 0; 233 } 234 235 public boolean hasNext () { 236 return index < panels.length - 1; 237 } 238 239 public String name () { 240 return NbBundle.getMessage (ActionIterator.class, "TITLE_x_of_y", new Integer (index + 1), new Integer (panels.length)); 242 } 243 244 public WizardDescriptor.Panel current () { 245 return panels[index]; 246 } 247 public final void addChangeListener (ChangeListener l) {} 249 public final void removeChangeListener (ChangeListener l) {} 250 251 252 private void log (String message){ 253 System.out.println("ActionIterator:: \t" + message); } 255 256 private String [] createSteps(String [] before, WizardDescriptor.Panel[] panels) { 257 int diff = 0; 258 if (before == null) { 259 before = new String [0]; 260 } else if (before.length > 0) { 261 diff = ("...".equals (before[before.length - 1])) ? 1 : 0; } 263 String [] res = new String [ (before.length - diff) + panels.length]; 264 for (int i = 0; i < res.length; i++) { 265 if (i < (before.length - diff)) { 266 res[i] = before[i]; 267 } else { 268 res[i] = panels[i - before.length + diff].getComponent ().getName (); 269 } 270 } 271 return res; 272 } 273 274 private void replaceInDocument(javax.swing.text.Document document, String replaceFrom, String replaceTo) { 275 javax.swing.text.AbstractDocument doc = (javax.swing.text.AbstractDocument )document; 276 int len = replaceFrom.length(); 277 try { 278 String content = doc.getText(0,doc.getLength()); 279 int index = content.lastIndexOf(replaceFrom); 280 while (index>=0) { 281 doc.replace(index,len,replaceTo,null); 282 content=content.substring(0,index); 283 index = content.lastIndexOf(replaceFrom); 284 } 285 } catch (javax.swing.text.BadLocationException ex){} 286 } 287 288 } 289 | Popular Tags |