1 19 20 package org.netbeans.modules.web.wizards; 21 22 import java.awt.Component ; 23 import java.io.File ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.regex.Pattern ; 28 import javax.swing.event.ChangeEvent ; 29 import javax.swing.event.ChangeListener ; 30 import org.netbeans.api.project.Project; 31 import org.netbeans.api.project.SourceGroup; 32 import org.netbeans.spi.project.ui.templates.support.Templates; 33 import org.openide.WizardDescriptor; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.loaders.TemplateWizard; 37 import org.openide.util.HelpCtx; 38 import org.netbeans.modules.web.api.webmodule.WebModule; 39 import org.openide.util.NbBundle; 40 41 45 final class TargetChooserPanel implements WizardDescriptor.Panel { 46 47 private final List listeners = new ArrayList (); 48 private TargetChooserPanelGUI gui; 49 50 private Project project; 51 private SourceGroup[] folders; 52 private FileType fileType; 53 private TemplateWizard templateWizard; 54 private String j2eeVersion; 55 56 private static final Pattern INVALID_FILENAME_CHARACTERS = Pattern.compile("[`~!@#$%^&*()=+\\|{};:'\",<>/?]"); 59 TargetChooserPanel(Project project, SourceGroup[] folders, FileType fileType) { 60 this.folders = folders; 61 this.project = project; 62 this.fileType=fileType; 63 64 if (FileType.TAG.equals(fileType)) { 65 j2eeVersion = WebModule.J2EE_14_LEVEL; 66 if (folders!=null && folders.length>0) { 67 WebModule wm = WebModule.getWebModule(folders[0].getRootFolder()); 68 if (wm!=null) j2eeVersion=wm.getJ2eePlatformVersion(); 69 } 70 } 71 } 72 73 TemplateWizard getTemplateWizard() { 74 return templateWizard; 75 } 76 77 public Component getComponent() { 78 if (gui == null) { 79 gui = new TargetChooserPanelGUI(this, project, folders, fileType ); 80 } 81 return gui; 82 } 83 84 public HelpCtx getHelp() { 85 return null; 86 } 88 89 public boolean isValid() { 90 if (FileType.TAG.equals(fileType) && WebModule.J2EE_13_LEVEL.equals(j2eeVersion)) { 92 templateWizard.putProperty ("WizardPanel_errorMessage", NbBundle.getMessage(TargetChooserPanel.class, "MSG_13notSupported")); 94 return false; 95 } 96 97 boolean ok = ( gui != null && gui.getTargetName() != null); 98 99 if (!ok) { 100 templateWizard.putProperty ("WizardPanel_errorMessage", null); return false; 102 } 103 104 if (FileType.TAG.equals(fileType) && gui.isTldCheckBoxSelected()) { 106 String mes=null; 107 FileObject tldFo = gui.getTldFileObject(); 108 String tagName = gui.getTagName(); 109 if (tldFo==null) { 110 mes = NbBundle.getMessage(TargetChooserPanel.class,"MSG_noTldSelectedForTagFile"); 111 } else if (!gui.isValidTagName(tagName)) { 112 mes = NbBundle.getMessage(TargetChooserPanel.class,"TXT_wrongTagName",tagName); 113 } else if (gui.tagNameExists(tagName)) { 114 mes = NbBundle.getMessage(TargetChooserPanel.class,"TXT_tagNameExists",tagName); 115 } 116 if (mes!=null) { 117 templateWizard.putProperty ("WizardPanel_errorMessage", mes); return false; 119 } 120 } 121 122 String filename = gui.getTargetName(); 123 if (INVALID_FILENAME_CHARACTERS.matcher(filename).find()) { 124 templateWizard.putProperty ("WizardPanel_errorMessage", NbBundle.getMessage(TargetChooserPanel.class, "MSG_invalid_filename", filename)); return false; 126 } 127 128 if (FileType.TAGLIBRARY.equals(fileType)) { 130 String tldName = gui.getTargetName(); 134 if (tldName.indexOf(' ') >= 0 || 135 tldName.indexOf(',') >= 0) { 136 templateWizard.putProperty ("WizardPanel_errorMessage", NbBundle.getMessage(TargetChooserPanel.class,"TXT_wrongTagLibName",tldName)); return false; 138 } 139 } 140 String targetName=gui.getTargetName(); 142 java.io.File file = gui.getTargetFile(); 143 FileObject template = Templates.getTemplate( templateWizard ); 144 String ext = template.getExt (); 145 if (FileType.JSP.equals(fileType) || FileType.TAG.equals(fileType)) { 146 if (isSegment()) ext+="f"; else if (isXml()) ext+="x"; } 149 150 String errorMessage = Utilities.canUseFileName (file, gui.getRelativeTargetFolder(), targetName, ext); 151 if (errorMessage!=null) 152 templateWizard.putProperty ("WizardPanel_errorMessage", errorMessage); else 154 templateWizard.putProperty("WizardPanel_errorMessage", gui.getErrorMessage()); 156 boolean valid = gui.isPanelValid() && errorMessage == null; 157 158 if (valid && targetName.indexOf(".")>=0) { 159 templateWizard.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(TargetChooserPanel.class, "MSG_dotsInName",targetName+"."+ext)); 162 } 163 return valid; 164 } 165 166 public void addChangeListener(ChangeListener l) { 167 listeners.add(l); 168 } 169 170 public void removeChangeListener(ChangeListener l) { 171 listeners.remove(l); 172 } 173 174 protected void fireChange() { 175 ChangeEvent e = new ChangeEvent (this); 176 Iterator it = listeners.iterator(); 177 while (it.hasNext()) { 178 ((ChangeListener )it.next()).stateChanged(e); 179 } 180 } 181 182 public void readSettings( Object settings ) { 183 184 templateWizard = (TemplateWizard)settings; 185 186 if ( gui != null ) { 187 188 Project project = Templates.getProject( templateWizard ); 189 190 193 FileObject preselectedTarget = Templates.getTargetFolder( templateWizard ); 194 195 gui.initValues( project, Templates.getTemplate( templateWizard ), preselectedTarget ); 197 198 if (FileType.JSP.equals(fileType)) 199 templateWizard.putProperty ("NewFileWizard_Title", NbBundle.getMessage(TargetChooserPanel.class, "TITLE_JspFile")); 201 else if (FileType.TAG.equals(fileType)) 202 templateWizard.putProperty ("NewFileWizard_Title", NbBundle.getMessage(TargetChooserPanel.class, "TITLE_TagFile")); 204 else if (FileType.TAGLIBRARY.equals(fileType)) 205 templateWizard.putProperty ("NewFileWizard_Title", NbBundle.getMessage(TargetChooserPanel.class, "TITLE_TLD")); 207 else if (FileType.HTML.equals(fileType)) 208 templateWizard.putProperty ("NewFileWizard_Title", NbBundle.getMessage(TargetChooserPanel.class, "TITLE_HTML")); 210 else if (FileType.XHTML.equals(fileType)) 211 templateWizard.putProperty ("NewFileWizard_Title", NbBundle.getMessage(TargetChooserPanel.class, "TITLE_XHTML")); 213 } 214 } 215 216 public void storeSettings(Object settings) { 217 if ( WizardDescriptor.PREVIOUS_OPTION.equals( ((WizardDescriptor)settings).getValue() ) ) { 218 return; 219 } 220 if ( WizardDescriptor.CANCEL_OPTION.equals( ((WizardDescriptor)settings).getValue() ) ) { 221 return; 222 } 223 if( isValid() ) { 224 File f = new File (gui.getCreatedFilePath()); 225 File ff = new File (f.getParentFile().getPath()); 226 if ( !ff.exists() ) { 227 ff.mkdir(); 228 } 229 FileObject folder = FileUtil.toFileObject(ff); 230 231 Templates.setTargetFolder( (WizardDescriptor)settings, folder ); 232 Templates.setTargetName( (WizardDescriptor)settings, gui.getTargetName() ); 233 } 234 ((WizardDescriptor)settings).putProperty ("NewFileWizard_Title", null); } 236 237 boolean isXml() { 238 return gui.isXml(); 239 } 240 241 boolean isSegment() { 242 return gui.isSegment(); 243 } 244 245 String getUri() { 246 return gui.getUri(); 247 } 248 249 String getPrefix() { 250 return gui.getPrefix(); 251 } 252 253 boolean isTldCheckBoxSelected() { 254 return gui.isTldCheckBoxSelected(); 255 } 256 257 String getTagName() { 258 return gui.getTagName(); 259 } 260 261 FileObject getTldFileObject() { 262 return gui.getTldFileObject(); 263 } 264 } 265 | Popular Tags |