KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > project > JavaTargetChooserPanel


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.java.project;
21
22 import java.awt.Component JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30 import javax.swing.event.ChangeEvent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
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 /**
46  *
47  * @author Petr Hrebejk
48  */

49 public final class JavaTargetChooserPanel implements WizardDescriptor.Panel, ChangeListener JavaDoc {
50
51     private static final String JavaDoc FOLDER_TO_DELETE = "folderToDelete"; //NOI18N
52

53     private final SpecificationVersion JDK_14 = new SpecificationVersion ("1.4"); //NOI18N
54
private final List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
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 JavaDoc 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         //XXX
96
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() ); //NOI18N
113
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         // check if the file name can be created
130
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 JavaDoc sl = SourceLevelQuery.getSourceLevel(rootFolder);
137             specVersion = sl != null? new SpecificationVersion(sl): null;
138         }
139         String JavaDoc 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             //Only warning, display it only if everything else is OK.
151
setErrorMessage( "ERR_JavaTargetChooser_DefaultPackage" );
152         }
153         String JavaDoc templateSrcLev = (String JavaDoc) template.getAttribute("javac.source"); // NOI18N
154
//Only warning, display it only if everything else id OK.
155
if (specVersion != null && templateSrcLev != null && specVersion.compareTo(new SpecificationVersion(templateSrcLev)) < 0) {
156             setErrorMessage("ERR_JavaTargetChooser_WrongPlatform"); // NOI18N
157
}
158         
159         // this enables to display error messages from the bottom panel
160
// Nevertheless, the previous error messages have bigger priorities
161
if (returnValue && bottomPanel != null) {
162            if (!bottomPanel.isValid())
163                return false;
164         }
165         
166         return returnValue;
167     }
168
169     public void addChangeListener(ChangeListener JavaDoc l) {
170         listeners.add(l);
171     }
172
173     public void removeChangeListener(ChangeListener JavaDoc l) {
174         listeners.remove(l);
175     }
176
177     private void fireChange() {
178         ChangeEvent JavaDoc e = new ChangeEvent JavaDoc(this);
179         Iterator JavaDoc it = listeners.iterator();
180         while (it.hasNext()) {
181             ((ChangeListener JavaDoc)it.next()).stateChanged(e);
182         }
183     }
184
185     public void readSettings( Object JavaDoc settings ) {
186         
187         wizard = (WizardDescriptor)settings;
188         
189         if ( gui != null ) {
190             // Try to preselect a folder
191
FileObject preselectedFolder = Templates.getTargetFolder( wizard );
192             // Init values
193
gui.initValues( Templates.getTemplate( wizard ), preselectedFolder );
194         }
195         
196         if ( bottomPanel != null ) {
197             bottomPanel.readSettings( settings );
198         }
199         
200         // XXX hack, TemplateWizard in final setTemplateImpl() forces new wizard's title
201
// this name is used in NewFileWizard to modify the title
202
if (gui != null) {
203             Object JavaDoc substitute = gui.getClientProperty ("NewFileWizard_Title"); // NOI18N
204
if (substitute != null) {
205                 wizard.putProperty ("NewFileWizard_Title", substitute); // NOI18N
206
}
207         }
208     }
209
210     public void storeSettings(Object JavaDoc settings) {
211         Object JavaDoc 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); // NOI18N
224

225         if (WizardDescriptor.FINISH_OPTION.equals(value)) {
226             wizard.putProperty(FOLDER_TO_DELETE, null);
227         }
228     }
229
230     public void stateChanged(ChangeEvent JavaDoc e) {
231         fireChange();
232     }
233     
234     // Private methods ---------------------------------------------------------
235

236     private void setErrorMessage( String JavaDoc key ) {
237         if ( key == null ) {
238             setLocalizedErrorMessage ( "" ); // NOI18N
239
}
240         else {
241             setLocalizedErrorMessage ( NbBundle.getMessage( JavaTargetChooserPanelGUI.class, key) ); // NOI18N
242
}
243     }
244     
245     private void setLocalizedErrorMessage (String JavaDoc message) {
246         wizard.putProperty ("WizardPanel_errorMessage", message); // NOI18N
247
}
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 JavaDoc packageFileName = gui.getPackageFileName();
255             folder = rootFolder.getFileObject( packageFileName );
256             if ( folder == null ) {
257                 try {
258                     folder = rootFolder;
259                     StringTokenizer JavaDoc tk = new StringTokenizer JavaDoc (packageFileName,"/"); //NOI18N
260
String JavaDoc name = null;
261                     while (tk.hasMoreTokens()) {
262                         name = tk.nextToken();
263                         FileObject fo = folder.getFileObject (name,""); //NOI8N
264
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 JavaDoc e ) {
285                     ErrorManager.getDefault().notify( ErrorManager.INFORMATIONAL, e );
286                 }
287             }
288         }
289         else {
290             folder = rootFolder;
291         }
292         return folder;
293     }
294     
295     // Nice copy of useful methods (Taken from JavaModule)
296

297     static boolean isValidPackageName(String JavaDoc str) {
298         if (str.length() > 0 && str.charAt(0) == '.') {
299             return false;
300         }
301         StringTokenizer JavaDoc tukac = new StringTokenizer JavaDoc(str, ".");
302         while (tukac.hasMoreTokens()) {
303             String JavaDoc 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 JavaDoc ident) {
313         if (ident == null || "".equals(ident) || !Utilities.isJavaIdentifier( ident ) ) {
314             return false;
315         }
316         else {
317             return true;
318         }
319     }
320     
321     // helper methods copied from project/ui/ProjectUtilities
322
/** Checks if the given file name can be created in the target folder.
323      *
324      * @param targetFolder target folder (e.g. source group)
325      * @param folderName name of the folder relative to target folder
326      * @param newObjectName name of created file
327      * @param extension extension of created file
328      * @return localized error message or null if all right
329      */

330     final public static String JavaDoc canUseFileName (FileObject targetFolder, String JavaDoc folderName, String JavaDoc newObjectName, String JavaDoc extension) {
331         String JavaDoc newObjectNameToDisplay = newObjectName;
332         if (newObjectName != null) {
333             newObjectName = newObjectName.replace ('.', '/'); // NOI8N
334
}
335         if (extension != null && extension.length () > 0) {
336             StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
337             sb.append (newObjectName);
338             sb.append ('.'); // NOI18N
339
sb.append (extension);
340             newObjectName = sb.toString ();
341         }
342         
343         if (extension != null && extension.length () > 0) {
344             StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
345             sb.append (newObjectNameToDisplay);
346             sb.append ('.'); // NOI18N
347
sb.append (extension);
348             newObjectNameToDisplay = sb.toString ();
349         }
350         
351         String JavaDoc relFileName = folderName + "/" + newObjectName; // NOI18N
352

353         // test whether the selected folder on selected filesystem already exists
354
if (targetFolder == null) {
355             return NbBundle.getMessage (JavaTargetChooserPanel.class, "MSG_fs_or_folder_does_not_exist"); // NOI18N
356
}
357         
358         // target filesystem should be writable
359
if (!targetFolder.canWrite ()) {
360             return NbBundle.getMessage (JavaTargetChooserPanel.class, "MSG_fs_is_readonly"); // NOI18N
361
}
362         
363         
364         if (existFileName(targetFolder, relFileName)) {
365             return NbBundle.getMessage (JavaTargetChooserPanel.class, "MSG_file_already_exist", newObjectNameToDisplay); // NOI18N
366
}
367         
368         // all ok
369
return null;
370     }
371
372     private static boolean existFileName(FileObject targetFolder, String JavaDoc relFileName) {
373         boolean result = false;
374         File JavaDoc fileForTargetFolder = FileUtil.toFile(targetFolder);
375         if (fileForTargetFolder.exists()) {
376             result = new File JavaDoc (fileForTargetFolder, relFileName).exists();
377         } else {
378             result = targetFolder.getFileObject (relFileName) != null;
379         }
380         
381         return result;
382     }
383 }
384
Popular Tags