KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > ui > wizard > JavaPackageIterator


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 package org.netbeans.modules.java.ui.wizard;
20
21 import java.io.IOException JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25 import javax.swing.event.ChangeListener JavaDoc;
26
27 import org.openide.ErrorManager;
28 import org.openide.WizardDescriptor;
29
30 import org.openide.filesystems.FileUtil;
31 import org.openide.filesystems.FileObject;
32 import org.openide.loaders.DataObject;
33 import org.openide.loaders.DataFolder;
34 import org.openide.loaders.TemplateWizard;
35 import org.netbeans.api.java.classpath.ClassPath;
36
37 /**
38  *
39  * @author builder
40  */

41 class JavaPackageIterator implements TemplateWizard.Iterator {
42     WizardDescriptor.Panel targetChooser;
43     TemplateWizard wizardInstance;
44     
45     static TemplateWizard.Iterator create() {
46         return new JavaPackageIterator();
47     }
48     
49     public boolean hasNext() {
50         return false;
51     }
52     
53     public void previousPanel() {
54         throw new NoSuchElementException JavaDoc();
55     }
56     
57     public void removeChangeListener(ChangeListener JavaDoc l) {
58     }
59     
60     public void addChangeListener(ChangeListener JavaDoc l) {
61     }
62     
63     public String JavaDoc name() {
64         return ""; //NOI18N
65
}
66     
67     public void nextPanel() {
68         throw new NoSuchElementException JavaDoc();
69     }
70     
71     public boolean hasPrevious() {
72         return false;
73     }
74     
75     public WizardDescriptor.Panel current() {
76         return wizardInstance.targetChooser();
77     }
78     
79     public void initialize(TemplateWizard wiz) {
80         wizardInstance = wiz;
81     }
82     
83     public void uninitialize(TemplateWizard wiz) {
84     }
85     
86     private void throwIllegalName(String JavaDoc key, String JavaDoc offending) throws IllegalStateException JavaDoc {
87         String JavaDoc msg;
88         if (offending == null) {
89             msg = Util.getString(key);
90         }
91         else {
92             msg = MessageFormat.format(Util.getString(key),
93                 new Object JavaDoc[] {
94                 offending});
95         }
96
97         IllegalStateException JavaDoc x =
98             (IllegalStateException JavaDoc)ErrorManager.getDefault().annotate(
99         new IllegalStateException JavaDoc(msg),
100         ErrorManager.USER, null, msg,
101         null, null
102         );
103         throw x;
104     }
105     
106     public java.util.Set JavaDoc instantiate(TemplateWizard wiz) throws IOException JavaDoc {
107         DataFolder fld = wiz.getTargetFolder();
108         FileObject fldFO = fld.getPrimaryFile();
109         ClassPath cp = ClassPath.getClassPath(fldFO,ClassPath.SOURCE);
110         if (cp == null) {
111             throwIllegalName("ERR_NotInSourcePath",null); // NOI18N
112
}
113         String JavaDoc s = cp.getResourceName(fldFO,'.',false);
114         if (!Util.isValidPackageName(s)) {
115             throwIllegalName("FMTERR_InvalidPackage", s); // NOI18N
116
}
117         String JavaDoc tn = wiz.getTargetName();
118         if (tn == null) {
119             // special handling for "package"
120
tn = Util.getString("TXT_DefaultPackageName"); // NOI18N
121
} else if (!Util.isValidPackageName(tn)) {
122             throwIllegalName("FMTERR_InvalidPackage", tn); // NOI18N
123
}
124
125         s = s + '.' + tn;
126         String JavaDoc pkgPath = s.replace('.', '/');
127         
128         // #37137: check if the package exists. TemplateWizard.targetChooser is not able to do that for
129
// a package hierarchy (a.b.c).
130
FileObject target = cp.findResource(pkgPath);
131         if (target != null && target.isFolder()) {
132             throwIllegalName("FMTERR_pkg_already_exist", tn); // NOI18N
133
}
134
135         DataObject d = DataObject.find(
136             FileUtil.createFolder(fldFO, tn.replace('.','/')));
137
138         // get name of deepest subpackage
139
s = d.getName();
140         fld = d.getFolder();
141
142         // magic to select package in explorer
143
d = d.createFromTemplate(fld, s);
144
145         return Collections.singleton(d);
146     }
147 }
148
Popular Tags