1 19 20 package org.netbeans.modules.java.project; 21 22 import java.awt.Component ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.Enumeration ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.StringTokenizer ; 30 import javax.swing.event.ChangeEvent ; 31 import javax.swing.event.ChangeListener ; 32 import org.netbeans.api.java.queries.SourceLevelQuery; 33 import org.netbeans.api.project.Project; 34 import org.netbeans.api.project.SourceGroup; 35 import org.netbeans.spi.project.ui.templates.support.Templates; 36 import org.openide.ErrorManager; 37 import org.openide.WizardDescriptor; 38 import org.openide.filesystems.FileObject; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.modules.SpecificationVersion; 41 import org.openide.util.HelpCtx; 42 import org.openide.util.NbBundle; 43 import org.openide.util.Utilities; 44 45 49 public final class JavaTargetChooserPanel implements WizardDescriptor.Panel, ChangeListener { 50 51 private static final String FOLDER_TO_DELETE = "folderToDelete"; 53 private final SpecificationVersion JDK_14 = new SpecificationVersion ("1.4"); private final List <ChangeListener > listeners = new ArrayList <ChangeListener >(); 55 private JavaTargetChooserPanelGUI gui; 56 private WizardDescriptor.Panel bottomPanel; 57 private WizardDescriptor wizard; 58 59 private Project project; 60 private SourceGroup folders[]; 61 private int type; 62 private boolean isValidPackageRequired; 63 64 public JavaTargetChooserPanel( Project project, SourceGroup folders[], WizardDescriptor.Panel bottomPanel, int type ) { 65 this(project, folders, bottomPanel, type, false); 66 } 67 68 public JavaTargetChooserPanel( Project project, SourceGroup folders[], WizardDescriptor.Panel bottomPanel, int type, boolean isValidPackageRequired ) { 69 this.project = project; 70 this.folders = folders; 71 this.bottomPanel = bottomPanel; 72 this.type = type; 73 if ( bottomPanel != null ) { 74 bottomPanel.addChangeListener( this ); 75 } 76 this.isValidPackageRequired = isValidPackageRequired; 77 } 78 79 public Component getComponent() { 80 if (gui == null) { 81 gui = new JavaTargetChooserPanelGUI( project, folders, bottomPanel == null ? null : bottomPanel.getComponent(), type ); 82 gui.addChangeListener(this); 83 } 84 return gui; 85 } 86 87 public HelpCtx getHelp() { 88 if ( bottomPanel != null ) { 89 HelpCtx bottomHelp = bottomPanel.getHelp(); 90 if ( bottomHelp != null ) { 91 return bottomHelp; 92 } 93 } 94 95 return null; 97 98 } 99 100 public boolean isValid() { 101 if (gui == null || gui.getTargetName() == null) { 102 setErrorMessage( null ); 103 return false; 104 } 105 if ( type == NewJavaFileWizardIterator.TYPE_PACKAGE) { 106 if ( !isValidPackageName( gui.getTargetName() ) ) { 107 setErrorMessage( "ERR_JavaTargetChooser_InvalidPackage" ); 108 return false; 109 } 110 } 111 else if (type == NewJavaFileWizardIterator.TYPE_PKG_INFO) { 112 assert "package-info".equals( gui.getTargetName() ); if ( !isValidPackageName( gui.getPackageName() ) ) { 114 setErrorMessage( "ERR_JavaTargetChooser_InvalidPackage" ); 115 return false; 116 } 117 } 118 else { 119 if ( !isValidTypeIdentifier( gui.getTargetName() ) ) { 120 setErrorMessage( "ERR_JavaTargetChooser_InvalidClass" ); 121 return false; 122 } 123 else if ( !isValidPackageName( gui.getPackageName() ) ) { 124 setErrorMessage( "ERR_JavaTargetChooser_InvalidPackage" ); 125 return false; 126 } 127 } 128 129 FileObject template = Templates.getTemplate( wizard ); 131 132 boolean returnValue=true; 133 FileObject rootFolder = gui.getRootFolder(); 134 SpecificationVersion specVersion = null; 135 if (type != NewJavaFileWizardIterator.TYPE_PACKAGE) { 136 String sl = SourceLevelQuery.getSourceLevel(rootFolder); 137 specVersion = sl != null? new SpecificationVersion(sl): null; 138 } 139 String errorMessage = canUseFileName (rootFolder, gui.getPackageFileName(), gui.getTargetName(), template.getExt ()); 140 if (gui != null) { 141 setLocalizedErrorMessage (errorMessage); 142 } 143 if (errorMessage!=null) returnValue=false; 144 145 if (type != NewJavaFileWizardIterator.TYPE_PACKAGE && returnValue && gui.getPackageName().length() == 0 && specVersion != null && JDK_14.compareTo(specVersion)<=0) { 146 if(isValidPackageRequired){ 147 setErrorMessage( "ERR_JavaTargetChooser_CantUseDefaultPackage" ); 148 return false; 149 } 150 setErrorMessage( "ERR_JavaTargetChooser_DefaultPackage" ); 152 } 153 String templateSrcLev = (String ) template.getAttribute("javac.source"); if (specVersion != null && templateSrcLev != null && specVersion.compareTo(new SpecificationVersion(templateSrcLev)) < 0) { 156 setErrorMessage("ERR_JavaTargetChooser_WrongPlatform"); } 158 159 if (returnValue && bottomPanel != null) { 162 if (!bottomPanel.isValid()) 163 return false; 164 } 165 166 return returnValue; 167 } 168 169 public void addChangeListener(ChangeListener l) { 170 listeners.add(l); 171 } 172 173 public void removeChangeListener(ChangeListener l) { 174 listeners.remove(l); 175 } 176 177 private void fireChange() { 178 ChangeEvent e = new ChangeEvent (this); 179 Iterator it = listeners.iterator(); 180 while (it.hasNext()) { 181 ((ChangeListener )it.next()).stateChanged(e); 182 } 183 } 184 185 public void readSettings( Object settings ) { 186 187 wizard = (WizardDescriptor)settings; 188 189 if ( gui != null ) { 190 FileObject preselectedFolder = Templates.getTargetFolder( wizard ); 192 gui.initValues( Templates.getTemplate( wizard ), preselectedFolder ); 194 } 195 196 if ( bottomPanel != null ) { 197 bottomPanel.readSettings( settings ); 198 } 199 200 if (gui != null) { 203 Object substitute = gui.getClientProperty ("NewFileWizard_Title"); if (substitute != null) { 205 wizard.putProperty ("NewFileWizard_Title", substitute); } 207 } 208 } 209 210 public void storeSettings(Object settings) { 211 Object value = ((WizardDescriptor)settings).getValue(); 212 if (WizardDescriptor.PREVIOUS_OPTION.equals(value) || WizardDescriptor.CANCEL_OPTION.equals(value) || 213 WizardDescriptor.CLOSED_OPTION.equals(value)) { 214 return; 215 } 216 if( isValid() ) { 217 if ( bottomPanel != null ) { 218 bottomPanel.storeSettings( settings ); 219 } 220 Templates.setTargetFolder( (WizardDescriptor)settings, getTargetFolderFromGUI ((WizardDescriptor)settings)); 221 Templates.setTargetName( (WizardDescriptor)settings, gui.getTargetName() ); 222 } 223 ((WizardDescriptor)settings).putProperty ("NewFileWizard_Title", null); 225 if (WizardDescriptor.FINISH_OPTION.equals(value)) { 226 wizard.putProperty(FOLDER_TO_DELETE, null); 227 } 228 } 229 230 public void stateChanged(ChangeEvent e) { 231 fireChange(); 232 } 233 234 236 private void setErrorMessage( String key ) { 237 if ( key == null ) { 238 setLocalizedErrorMessage ( "" ); } 240 else { 241 setLocalizedErrorMessage ( NbBundle.getMessage( JavaTargetChooserPanelGUI.class, key) ); } 243 } 244 245 private void setLocalizedErrorMessage (String message) { 246 wizard.putProperty ("WizardPanel_errorMessage", message); } 248 249 private FileObject getTargetFolderFromGUI (WizardDescriptor wd) { 250 assert gui != null; 251 FileObject rootFolder = gui.getRootFolder(); 252 FileObject folder = null; 253 if ( type != NewJavaFileWizardIterator.TYPE_PACKAGE ) { 254 String packageFileName = gui.getPackageFileName(); 255 folder = rootFolder.getFileObject( packageFileName ); 256 if ( folder == null ) { 257 try { 258 folder = rootFolder; 259 StringTokenizer tk = new StringTokenizer (packageFileName,"/"); String name = null; 261 while (tk.hasMoreTokens()) { 262 name = tk.nextToken(); 263 FileObject fo = folder.getFileObject (name,""); if (fo == null) { 265 break; 266 } 267 folder = fo; 268 } 269 folder = folder.createFolder(name); 270 FileObject toDelete = (FileObject) wd.getProperty(FOLDER_TO_DELETE); 271 if (toDelete == null) { 272 wd.putProperty(FOLDER_TO_DELETE,folder); 273 } 274 else if (!toDelete.equals(folder)) { 275 toDelete.delete(); 276 wd.putProperty(FOLDER_TO_DELETE,folder); 277 } 278 while (tk.hasMoreTokens()) { 279 name = tk.nextToken(); 280 folder = folder.createFolder(name); 281 } 282 283 } 284 catch( IOException e ) { 285 ErrorManager.getDefault().notify( ErrorManager.INFORMATIONAL, e ); 286 } 287 } 288 } 289 else { 290 folder = rootFolder; 291 } 292 return folder; 293 } 294 295 297 static boolean isValidPackageName(String str) { 298 if (str.length() > 0 && str.charAt(0) == '.') { 299 return false; 300 } 301 StringTokenizer tukac = new StringTokenizer (str, "."); 302 while (tukac.hasMoreTokens()) { 303 String token = tukac.nextToken(); 304 if ("".equals(token)) 305 return false; 306 if (!Utilities.isJavaIdentifier(token)) 307 return false; 308 } 309 return true; 310 } 311 312 static boolean isValidTypeIdentifier(String ident) { 313 if (ident == null || "".equals(ident) || !Utilities.isJavaIdentifier( ident ) ) { 314 return false; 315 } 316 else { 317 return true; 318 } 319 } 320 321 330 final public static String canUseFileName (FileObject targetFolder, String folderName, String newObjectName, String extension) { 331 String newObjectNameToDisplay = newObjectName; 332 if (newObjectName != null) { 333 newObjectName = newObjectName.replace ('.', '/'); } 335 if (extension != null && extension.length () > 0) { 336 StringBuffer sb = new StringBuffer (); 337 sb.append (newObjectName); 338 sb.append ('.'); sb.append (extension); 340 newObjectName = sb.toString (); 341 } 342 343 if (extension != null && extension.length () > 0) { 344 StringBuffer sb = new StringBuffer (); 345 sb.append (newObjectNameToDisplay); 346 sb.append ('.'); sb.append (extension); 348 newObjectNameToDisplay = sb.toString (); 349 } 350 351 String relFileName = folderName + "/" + newObjectName; 353 if (targetFolder == null) { 355 return NbBundle.getMessage (JavaTargetChooserPanel.class, "MSG_fs_or_folder_does_not_exist"); } 357 358 if (!targetFolder.canWrite ()) { 360 return NbBundle.getMessage (JavaTargetChooserPanel.class, "MSG_fs_is_readonly"); } 362 363 364 if (existFileName(targetFolder, relFileName)) { 365 return NbBundle.getMessage (JavaTargetChooserPanel.class, "MSG_file_already_exist", newObjectNameToDisplay); } 367 368 return null; 370 } 371 372 private static boolean existFileName(FileObject targetFolder, String relFileName) { 373 boolean result = false; 374 File fileForTargetFolder = FileUtil.toFile(targetFolder); 375 if (fileForTargetFolder.exists()) { 376 result = new File (fileForTargetFolder, relFileName).exists(); 377 } else { 378 result = targetFolder.getFileObject (relFileName) != null; 379 } 380 381 return result; 382 } 383 } 384 | Popular Tags |