KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > wizards > ManagedBeanIterator


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.jsf.wizards;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.NoSuchElementException JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.swing.JComponent JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30 import org.netbeans.api.project.Sources;
31 import org.netbeans.editor.BaseDocument;
32 import org.netbeans.modules.schema2beans.BaseBean;
33 import org.netbeans.modules.web.api.webmodule.WebModule;
34 import org.netbeans.modules.web.jsf.JSFConfigDataObject;
35 import org.netbeans.modules.web.jsf.api.ConfigurationUtils;
36 import org.netbeans.modules.web.jsf.editor.JSFEditorUtilities;
37 import org.netbeans.modules.web.jsf.api.facesmodel.FacesConfig;
38 import org.netbeans.modules.web.jsf.api.facesmodel.JSFConfigComponentFactory;
39 import org.netbeans.modules.web.jsf.api.facesmodel.ManagedBean;
40 import org.openide.WizardDescriptor;
41 import org.openide.cookies.OpenCookie;
42 import org.openide.filesystems.FileObject;
43 import org.openide.loaders.DataObject;
44 import org.openide.loaders.DataFolder;
45 import org.openide.loaders.TemplateWizard;
46 import org.openide.util.NbBundle;
47 import org.netbeans.api.java.project.JavaProjectConstants;
48 import org.netbeans.api.project.Project;
49 import org.netbeans.api.project.ProjectUtils;
50 import org.netbeans.api.project.SourceGroup;
51 import org.netbeans.modules.web.jsf.JSFConfigUtilities;
52 import org.netbeans.modules.web.jsf.api.facesmodel.JSFConfigComponentFactory;
53 import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates;
54 import org.netbeans.spi.project.ui.templates.support.Templates;
55
56 /** A template wizard iterator for new struts action
57  *
58  * @author Petr Pisl
59  *
60  */

61
62 public class ManagedBeanIterator implements TemplateWizard.Iterator {
63     
64     private int index;
65     
66     private transient WizardDescriptor.Panel[] panels;
67     
68     private transient boolean debug = false;
69     
70     public void initialize (TemplateWizard wizard) {
71         if (debug) log ("initialize");
72         index = 0;
73         // obtaining target folder
74
Project project = Templates.getProject( wizard );
75         DataFolder targetFolder=null;
76         try {
77             targetFolder = wizard.getTargetFolder();
78         } catch (IOException JavaDoc ex) {
79             targetFolder = DataFolder.findFolder(project.getProjectDirectory());
80         }
81         
82         SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
83         if (debug) {
84             log ("\tproject: " + project);
85             log ("\ttargetFolder: " + targetFolder);
86             log ("\tsourceGroups.length: " + sourceGroups.length);
87         }
88         
89         WizardDescriptor.Panel secondPanel = new ManagedBeanPanel(project, wizard);
90         
91         WizardDescriptor.Panel javaPanel;
92         if (sourceGroups.length == 0)
93             javaPanel = Templates.createSimpleTargetChooser(project, sourceGroups, secondPanel);
94         else
95             javaPanel = JavaTemplates.createPackageChooser(project, sourceGroups, secondPanel);
96
97         panels = new WizardDescriptor.Panel[] { javaPanel };
98         
99         // Creating steps.
100
Object JavaDoc prop = wizard.getProperty ("WizardPanel_contentData"); // NOI18N
101
String JavaDoc[] beforeSteps = null;
102         if (prop != null && prop instanceof String JavaDoc[]) {
103             beforeSteps = (String JavaDoc[])prop;
104         }
105         String JavaDoc[] steps = createSteps (beforeSteps, panels);
106         
107         for (int i = 0; i < panels.length; i++) {
108             JComponent JavaDoc jc = (JComponent JavaDoc)panels[i].getComponent ();
109             if (steps[i] == null) {
110                 steps[i] = jc.getName ();
111             }
112         jc.putClientProperty ("WizardPanel_contentSelectedIndex", new Integer JavaDoc (i)); // NOI18N
113
jc.putClientProperty ("WizardPanel_contentData", steps); // NOI18N
114
}
115     }
116     
117     public void uninitialize (TemplateWizard wizard) {
118         panels = null;
119     }
120     
121     public Set JavaDoc instantiate(TemplateWizard wizard) throws IOException JavaDoc {
122 //how to get dynamic form bean properties
123
//String formBeanClassName = (String) wizard.getProperty(WizardProperties.FORMBEAN_CLASS); //NOI18N
124

125         if (debug)
126             log("instantiate"); //NOI18N
127

128         FileObject dir = Templates.getTargetFolder( wizard );
129         DataFolder df = DataFolder.findFolder( dir );
130         FileObject template = Templates.getTemplate( wizard );
131         
132         DataObject dTemplate = DataObject.find( template );
133         DataObject dobj = dTemplate.createFromTemplate( df, Templates.getTargetName( wizard ) );
134         
135         String JavaDoc configFile = (String JavaDoc) wizard.getProperty(WizardProperties.CONFIG_FILE);
136         Project project = Templates.getProject( wizard );
137         WebModule wm = WebModule.getWebModule(project.getProjectDirectory());
138         dir = wm.getDocumentBase();
139         FileObject fo = dir.getFileObject(configFile); //NOI18N
140
FacesConfig facesConfig = ConfigurationUtils.getConfigModel(fo, true).getRootComponent();
141                 
142         ManagedBean bean = facesConfig.getModel().getFactory().createManagedBean();
143         String JavaDoc targetName = Templates.getTargetName(wizard);
144         Sources sources = ProjectUtils.getSources(project);
145         SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
146         String JavaDoc packageName = null;
147         org.openide.filesystems.FileObject targetFolder = Templates.getTargetFolder(wizard);
148         for (int i = 0; i < groups.length && packageName == null; i++) {
149             packageName = org.openide.filesystems.FileUtil.getRelativePath (groups [i].getRootFolder (), targetFolder);
150             if (packageName!=null) break;
151         }
152         if (packageName!=null) packageName = packageName.replace('/','.');
153             else packageName="";
154         String JavaDoc className=null;
155         if (packageName.length()>0)
156             className=packageName+"."+targetName;//NOI18N
157
else
158             className=targetName;
159         
160         bean.setManagedBeanName(getUniqueName(targetName, facesConfig));
161         bean.setManagedBeanClass(className);
162         bean.setManagedBeanScope((String JavaDoc) wizard.getProperty(WizardProperties.SCOPE));
163         
164         String JavaDoc description = (String JavaDoc) wizard.getProperty(WizardProperties.DESCRIPTION);
165         if (description != null && description.length() > 0){
166             String JavaDoc newLine = System.getProperty("line.separator");
167             bean.setDescription(newLine + description + newLine);
168         }
169         facesConfig.getModel().startTransaction();
170         facesConfig.addManagedBean(bean);
171         facesConfig.getModel().endTransaction();
172         facesConfig.getModel().sync();
173         
174         return Collections.singleton(dobj);
175     }
176     
177     public void previousPanel () {
178         if (! hasPrevious ()) throw new NoSuchElementException JavaDoc ();
179         index--;
180     }
181     
182     public void nextPanel () {
183         if (! hasNext ()) throw new NoSuchElementException JavaDoc ();
184         index++;
185     }
186     
187     public boolean hasPrevious () {
188         return index > 0;
189     }
190     
191     public boolean hasNext () {
192         return index < panels.length - 1;
193     }
194     
195     public String JavaDoc name () {
196         return NbBundle.getMessage (ManagedBeanIterator.class, "TITLE_x_of_y",
197             new Integer JavaDoc (index + 1), new Integer JavaDoc (panels.length));
198     }
199     
200     public WizardDescriptor.Panel current () {
201         return panels[index];
202     }
203     // If nothing unusual changes in the middle of the wizard, simply:
204
public final void addChangeListener (ChangeListener JavaDoc l) {}
205     public final void removeChangeListener (ChangeListener JavaDoc l) {}
206     
207     
208     private void log (String JavaDoc message){
209         System.out.println("ActionIterator:: \t" + message);
210     }
211     
212     private String JavaDoc[] createSteps(String JavaDoc[] before, WizardDescriptor.Panel[] panels) {
213         int diff = 0;
214         if (before == null) {
215             before = new String JavaDoc[0];
216         } else if (before.length > 0) {
217             diff = ("...".equals (before[before.length - 1])) ? 1 : 0; // NOI18N
218
}
219         String JavaDoc[] res = new String JavaDoc[ (before.length - diff) + panels.length];
220         for (int i = 0; i < res.length; i++) {
221             if (i < (before.length - diff)) {
222                 res[i] = before[i];
223             } else {
224                 res[i] = panels[i - before.length + diff].getComponent ().getName ();
225             }
226         }
227         return res;
228     }
229     
230     private void replaceInDocument(javax.swing.text.Document JavaDoc document, String JavaDoc replaceFrom, String JavaDoc replaceTo) {
231         javax.swing.text.AbstractDocument JavaDoc doc = (javax.swing.text.AbstractDocument JavaDoc)document;
232         int len = replaceFrom.length();
233         try {
234             String JavaDoc content = doc.getText(0,doc.getLength());
235             int index = content.lastIndexOf(replaceFrom);
236             while (index>=0) {
237                 doc.replace(index,len,replaceTo,null);
238                 content=content.substring(0,index);
239                 index = content.lastIndexOf(replaceFrom);
240             }
241         } catch (javax.swing.text.BadLocationException JavaDoc ex){}
242     }
243     
244     private String JavaDoc getUniqueName(String JavaDoc original, FacesConfig facesConfig){
245         String JavaDoc value = original;
246         Collection JavaDoc<ManagedBean> beans = facesConfig.getManagedBeans();
247         int count = 0;
248         for (Iterator JavaDoc<ManagedBean> it = beans.iterator(); it.hasNext();) {
249             ManagedBean managedBean = it.next();
250             if (!value.equals(managedBean.getManagedBeanName())){
251                 index++;
252             }
253             else {
254                 index = 0;
255                 count++;
256                 value = original+count;
257             }
258         }
259         return value;
260     }
261
262 }
263
Popular Tags