KickJava   Java API By Example, From Geeks To Geeks.

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


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
27 import javax.swing.JComponent JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
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 /** A template wizard iterator for new struts action
58  *
59  * @author Petr Pisl
60  *
61  */

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"); //NOI18N
73
index = 0;
74         // obtaining target folder
75
Project project = Templates.getProject( wizard );
76         DataFolder targetFolder=null;
77         try {
78             targetFolder = wizard.getTargetFolder();
79         } catch (IOException JavaDoc 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); //NOI18N
87
log ("\ttargetFolder: " + targetFolder); //NOI18N
88
log ("\tsourceGroups.length: " + sourceGroups.length); //NOI18N
89
}
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         // Creating steps.
105
Object JavaDoc prop = wizard.getProperty ("WizardPanel_contentData"); // NOI18N
106
String JavaDoc[] beforeSteps = null;
107         if (prop != null && prop instanceof String JavaDoc[]) {
108             beforeSteps = (String JavaDoc[])prop;
109         }
110         String JavaDoc[] steps = createSteps (beforeSteps, panels);
111         
112         for (int i = 0; i < panels.length; i++) {
113             JComponent JavaDoc jc = (JComponent JavaDoc)panels[i].getComponent ();
114             if (steps[i] == null) {
115                 steps[i] = jc.getName ();
116             }
117         jc.putClientProperty ("WizardPanel_contentSelectedIndex", new Integer JavaDoc (i)); // NOI18N
118
jc.putClientProperty ("WizardPanel_contentData", steps); // NOI18N
119
}
120     }
121     
122     public void uninitialize (TemplateWizard wizard) {
123         panels = null;
124     }
125     
126     public Set JavaDoc instantiate(TemplateWizard wizard) throws IOException JavaDoc {
127 //how to get dynamic form bean properties
128
//String formBeanClassName = (String) wizard.getProperty(WizardProperties.FORMBEAN_CLASS); //NOI18N
129

130         if (debug)
131             log("instantiate"); //NOI18N
132

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 JavaDoc doc = editorCookie.openDocument();
143             replaceInDocument(doc, "__SUPERCLASS__", (String JavaDoc) wizard.getProperty(WizardProperties.FORMBEAN_SUPERCLASS)); //NOI18N
144
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 JavaDoc configFile = (String JavaDoc) wizard.getProperty(WizardProperties.FORMBEAN_CONFIG_FILE);
152
153         if (wm != null && configFile != null && !"".equals(configFile)){ //NOI18N
154
// write to a struts configuration file, only when it's inside wm.
155
dir = wm.getDocumentBase();
156
157             String JavaDoc targetName = Templates.getTargetName(wizard);
158             Sources sources = ProjectUtils.getSources(project);
159             SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
160             String JavaDoc 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=""; //NOI18N
168
String JavaDoc className=null;
169             if (packageName.length()>0)
170                 className=packageName+"."+targetName;//NOI18N
171
else
172                 className=targetName;
173
174             
175             FileObject fo = dir.getFileObject(configFile); //NOI18N
176
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); //NOI18N
182
formBean.setAttributeValue("type", className); //NOI18N
183
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"); //NOI18N
194
configDO.getEditorSupport().saveDocument();
195             }
196         }
197         return Collections.singleton(dobj);
198     }
199     
200     public void previousPanel () {
201         if (! hasPrevious ()) throw new NoSuchElementException JavaDoc ();
202         index--;
203     }
204     
205     public void nextPanel () {
206         if (! hasNext ()) throw new NoSuchElementException JavaDoc ();
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 JavaDoc name () {
219         return NbBundle.getMessage (ActionIterator.class, "TITLE_x_of_y", //NOI18N
220
new Integer JavaDoc (index + 1), new Integer JavaDoc (panels.length));
221     }
222     
223     public WizardDescriptor.Panel current () {
224         return panels[index];
225     }
226     // If nothing unusual changes in the middle of the wizard, simply:
227
public final void addChangeListener (ChangeListener JavaDoc l) {}
228     public final void removeChangeListener (ChangeListener JavaDoc l) {}
229     
230     
231     private void log (String JavaDoc message){
232         System.out.println("ActionIterator:: \t" + message); //NOI18N
233
}
234     
235     private String JavaDoc[] createSteps(String JavaDoc[] before, WizardDescriptor.Panel[] panels) {
236         int diff = 0;
237         if (before == null) {
238             before = new String JavaDoc[0];
239         } else if (before.length > 0) {
240             diff = ("...".equals (before[before.length - 1])) ? 1 : 0; // NOI18N
241
}
242         String JavaDoc[] res = new String JavaDoc[ (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 JavaDoc document, String JavaDoc replaceFrom, String JavaDoc replaceTo) {
254         javax.swing.text.AbstractDocument JavaDoc doc = (javax.swing.text.AbstractDocument JavaDoc)document;
255         int len = replaceFrom.length();
256         try {
257             String JavaDoc 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 JavaDoc ex){}
265     }
266
267 }
268
Popular Tags