KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > struts > wizards > ActionIterator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.struts.wizards;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import javax.swing.event.ChangeListener JavaDoc;
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 /** A template wizard iterator for new struts action
53  *
54  * @author Petr Pisl
55  *
56  */

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"); //NOI18N
68
index = 0;
69         // obtaining target folder
70
Project project = Templates.getProject( wizard );
71         DataFolder targetFolder=null;
72         try {
73             targetFolder = wizard.getTargetFolder();
74         } catch (IOException JavaDoc 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); //NOI18N
82
log ("\ttargetFolder: " + targetFolder); //NOI18N
83
log ("\tsourceGroups.length: " + sourceGroups.length); //NOI18N
84
}
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         // Creating steps.
98
Object JavaDoc prop = wizard.getProperty ("WizardPanel_contentData"); // NOI18N
99
String JavaDoc[] beforeSteps = null;
100         if (prop != null && prop instanceof String JavaDoc[]) {
101             beforeSteps = (String JavaDoc[])prop;
102         }
103         String JavaDoc[] steps = createSteps (beforeSteps, panels);
104         
105         for (int i = 0; i < panels.length; i++) {
106             JComponent JavaDoc jc = (JComponent JavaDoc)panels[i].getComponent ();
107             if (steps[i] == null) {
108                 steps[i] = jc.getName ();
109             }
110         jc.putClientProperty ("WizardPanel_contentSelectedIndex", new Integer JavaDoc (i)); // NOI18N
111
jc.putClientProperty ("WizardPanel_contentData", steps); // NOI18N
112
}
113     }
114     
115     public void uninitialize (TemplateWizard wizard) {
116         panels = null;
117     }
118     
119     public Set JavaDoc instantiate(TemplateWizard wizard) throws IOException JavaDoc {
120         if (debug)
121             log("instantiate"); //NOI18N
122

123         FileObject dir = Templates.getTargetFolder( wizard );
124         DataFolder df = DataFolder.findFolder( dir );
125         FileObject template = Templates.getTemplate( wizard );
126         
127         String JavaDoc superclass=(String JavaDoc)wizard.getProperty(WizardProperties.ACTION_SUPERCLASS);
128         if (debug)
129             log("superclass="+superclass); //NOI18N
130
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"); //NOI18N
137
} else if (ActionPanelVisual.MAPPING_DISPATCH_ACTION.equals(superclass)) {
138             FileObject templateParent = template.getParent();
139             template = templateParent.getFileObject("MappingDispatchAction","java"); //NOI18N
140
} else if (ActionPanelVisual.LOOKUP_DISPATCH_ACTION.equals(superclass)) {
141             FileObject templateParent = template.getParent();
142             template = templateParent.getFileObject("LookupDispatchAction","java"); //NOI18N
143
}
144         else {
145             replaceSuperClass = true;
146         }
147         
148         
149         String JavaDoc 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 JavaDoc doc = editorCookie.openDocument();
156                 replaceInDocument(doc, "__SUPERCLASS__", superclass); //NOI18N
157
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 JavaDoc configFile = (String JavaDoc) wizard.getProperty(WizardProperties.ACTION_CONFIG_FILE);
166         if (wm != null && configFile != null && !"".equals(configFile)) { //NOI18N
167
// the file is created outside a wm -> we don't need to write the declaration.
168
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 JavaDoc 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=""; //NOI18N
185
String JavaDoc className=null;
186             if (packageName.length()>0)
187                 className=packageName+"."+targetName;//NOI18N
188
else
189                 className=targetName;
190             action.setAttributeValue("type", className); //NOI18N
191

192             String JavaDoc path = (String JavaDoc) wizard.getProperty(WizardProperties.ACTION_PATH);
193             action.setAttributeValue("path", path.startsWith("/") ? path : "/" + path); //NOI18N
194

195             String JavaDoc formName = (String JavaDoc) wizard.getProperty(WizardProperties.ACTION_FORM_NAME);
196             if (formName!=null) {
197                 action.setAttributeValue("name", formName); //NOI18N
198
action.setAttributeValue("scope",(String JavaDoc) wizard.getProperty(WizardProperties.ACTION_SCOPE)); //NOI18N
199
action.setAttributeValue("input",(String JavaDoc) wizard.getProperty(WizardProperties.ACTION_INPUT)); //NOI18N
200
action.setAttributeValue("attribute",(String JavaDoc) wizard.getProperty(WizardProperties.ACTION_ATTRIBUTE)); //NOI18N
201
Boolean JavaDoc validate = (Boolean JavaDoc) wizard.getProperty(WizardProperties.ACTION_VALIDATE);
202                 if (Boolean.FALSE.equals(validate)) action.setAttributeValue("validate","false"); //NOI18N
203
action.setAttributeValue("attribute",(String JavaDoc) wizard.getProperty(WizardProperties.ACTION_ATTRIBUTE)); //NOI18N
204
}
205             action.setAttributeValue("parameter",(String JavaDoc) wizard.getProperty(WizardProperties.ACTION_PARAMETER)); //NOI18N
206

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"); //NOI18N
216
configDO.getEditorSupport().saveDocument();
217         }
218         return Collections.singleton(dobj);
219     }
220     
221     public void previousPanel () {
222         if (! hasPrevious ()) throw new NoSuchElementException JavaDoc ();
223         index--;
224     }
225     
226     public void nextPanel () {
227         if (! hasNext ()) throw new NoSuchElementException JavaDoc ();
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 JavaDoc name () {
240         return NbBundle.getMessage (ActionIterator.class, "TITLE_x_of_y", //NOI18N
241
new Integer JavaDoc (index + 1), new Integer JavaDoc (panels.length));
242     }
243     
244     public WizardDescriptor.Panel current () {
245         return panels[index];
246     }
247     // If nothing unusual changes in the middle of the wizard, simply:
248
public final void addChangeListener (ChangeListener JavaDoc l) {}
249     public final void removeChangeListener (ChangeListener JavaDoc l) {}
250     
251     
252     private void log (String JavaDoc message){
253         System.out.println("ActionIterator:: \t" + message); //NOI18N
254
}
255     
256     private String JavaDoc[] createSteps(String JavaDoc[] before, WizardDescriptor.Panel[] panels) {
257         int diff = 0;
258         if (before == null) {
259             before = new String JavaDoc[0];
260         } else if (before.length > 0) {
261             diff = ("...".equals (before[before.length - 1])) ? 1 : 0; // NOI18N
262
}
263         String JavaDoc[] res = new String JavaDoc[ (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 JavaDoc document, String JavaDoc replaceFrom, String JavaDoc replaceTo) {
275         javax.swing.text.AbstractDocument JavaDoc doc = (javax.swing.text.AbstractDocument JavaDoc)document;
276         int len = replaceFrom.length();
277         try {
278             String JavaDoc 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 JavaDoc ex){}
286     }
287     
288 }
289
Popular Tags