1 19 20 package org.netbeans.modules.web.jsf.wizards; 21 22 import java.io.IOException ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.NoSuchElementException ; 27 import java.util.Set ; 28 import javax.swing.JComponent ; 29 import javax.swing.event.ChangeListener ; 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 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 Project project = Templates.getProject( wizard ); 75 DataFolder targetFolder=null; 76 try { 77 targetFolder = wizard.getTargetFolder(); 78 } catch (IOException 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 Object prop = wizard.getProperty ("WizardPanel_contentData"); String [] beforeSteps = null; 102 if (prop != null && prop instanceof String []) { 103 beforeSteps = (String [])prop; 104 } 105 String [] steps = createSteps (beforeSteps, panels); 106 107 for (int i = 0; i < panels.length; i++) { 108 JComponent jc = (JComponent )panels[i].getComponent (); 109 if (steps[i] == null) { 110 steps[i] = jc.getName (); 111 } 112 jc.putClientProperty ("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty ("WizardPanel_contentData", steps); } 115 } 116 117 public void uninitialize (TemplateWizard wizard) { 118 panels = null; 119 } 120 121 public Set instantiate(TemplateWizard wizard) throws IOException { 122 125 if (debug) 126 log("instantiate"); 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 configFile = (String ) 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); FacesConfig facesConfig = ConfigurationUtils.getConfigModel(fo, true).getRootComponent(); 141 142 ManagedBean bean = facesConfig.getModel().getFactory().createManagedBean(); 143 String targetName = Templates.getTargetName(wizard); 144 Sources sources = ProjectUtils.getSources(project); 145 SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 146 String 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 className=null; 155 if (packageName.length()>0) 156 className=packageName+"."+targetName; else 158 className=targetName; 159 160 bean.setManagedBeanName(getUniqueName(targetName, facesConfig)); 161 bean.setManagedBeanClass(className); 162 bean.setManagedBeanScope((String ) wizard.getProperty(WizardProperties.SCOPE)); 163 164 String description = (String ) wizard.getProperty(WizardProperties.DESCRIPTION); 165 if (description != null && description.length() > 0){ 166 String 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 (); 179 index--; 180 } 181 182 public void nextPanel () { 183 if (! hasNext ()) throw new NoSuchElementException (); 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 name () { 196 return NbBundle.getMessage (ManagedBeanIterator.class, "TITLE_x_of_y", 197 new Integer (index + 1), new Integer (panels.length)); 198 } 199 200 public WizardDescriptor.Panel current () { 201 return panels[index]; 202 } 203 public final void addChangeListener (ChangeListener l) {} 205 public final void removeChangeListener (ChangeListener l) {} 206 207 208 private void log (String message){ 209 System.out.println("ActionIterator:: \t" + message); 210 } 211 212 private String [] createSteps(String [] before, WizardDescriptor.Panel[] panels) { 213 int diff = 0; 214 if (before == null) { 215 before = new String [0]; 216 } else if (before.length > 0) { 217 diff = ("...".equals (before[before.length - 1])) ? 1 : 0; } 219 String [] res = new String [ (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 document, String replaceFrom, String replaceTo) { 231 javax.swing.text.AbstractDocument doc = (javax.swing.text.AbstractDocument )document; 232 int len = replaceFrom.length(); 233 try { 234 String 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 ex){} 242 } 243 244 private String getUniqueName(String original, FacesConfig facesConfig){ 245 String value = original; 246 Collection <ManagedBean> beans = facesConfig.getManagedBeans(); 247 int count = 0; 248 for (Iterator <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 |