Source Code Cross Referenced for FileWizardIterator.java in  » IDE-Netbeans » mobility » org » netbeans » modules » mobility » project » ui » wizard » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » mobility » org.netbeans.modules.mobility.project.ui.wizard 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:         *
004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005:         *
006:         * The contents of this file are subject to the terms of either the GNU
007:         * General Public License Version 2 only ("GPL") or the Common
008:         * Development and Distribution License("CDDL") (collectively, the
009:         * "License"). You may not use this file except in compliance with the
010:         * License. You can obtain a copy of the License at
011:         * http://www.netbeans.org/cddl-gplv2.html
012:         * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013:         * specific language governing permissions and limitations under the
014:         * License.  When distributing the software, include this License Header
015:         * Notice in each file and include the License file at
016:         * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
017:         * particular file as subject to the "Classpath" exception as provided
018:         * by Sun in the GPL Version 2 section of the License file that
019:         * accompanied this code. If applicable, add the following below the
020:         * License Header, with the fields enclosed by brackets [] replaced by
021:         * your own identifying information:
022:         * "Portions Copyrighted [year] [name of copyright owner]"
023:         *
024:         * Contributor(s):
025:         *
026:         * The Original Software is NetBeans. The Initial Developer of the Original
027:         * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028:         * Microsystems, Inc. All Rights Reserved.
029:         *
030:         * If you wish your version of this file to be governed by only the CDDL
031:         * or only the GPL Version 2, indicate your decision by adding
032:         * "[Contributor] elects to include this software in this distribution
033:         * under the [CDDL or GPL Version 2] license." If you do not indicate a
034:         * single choice of license, a recipient has the option to distribute
035:         * your version of this file under either the CDDL, the GPL Version 2 or
036:         * to extend the choice of license to its licensees as provided above.
037:         * However, if you add GPL Version 2 code and therefore, elected the GPL
038:         * Version 2 license, then the option applies only if the new code is
039:         * made subject to such option by the copyright holder.
040:         */
041:
042:        package org.netbeans.modules.mobility.project.ui.wizard;
043:
044:        import java.text.MessageFormat;
045:        import java.util.NoSuchElementException;
046:
047:        import javax.swing.event.ChangeListener;
048:
049:        import org.openide.ErrorManager;
050:        import org.openide.filesystems.FileObject;
051:        import org.openide.util.RequestProcessor;
052:        import org.openide.util.Utilities;
053:        import org.openide.util.NbBundle;
054:
055:        import org.netbeans.api.java.classpath.ClassPath;
056:
057:        import java.io.IOException;
058:        import javax.swing.*;
059:
060:        import org.netbeans.api.project.Project;
061:        import org.netbeans.api.project.ProjectManager;
062:        import org.netbeans.modules.mobility.project.J2MEProject;
063:        import org.netbeans.modules.mobility.project.J2MEProjectGenerator;
064:        import org.netbeans.spi.project.support.ant.AntProjectHelper;
065:        import org.netbeans.spi.project.ui.templates.support.Templates;
066:        import org.openide.WizardDescriptor;
067:        import org.openide.loaders.DataFolder;
068:        import org.openide.loaders.DataObject;
069:        import org.openide.loaders.TemplateWizard;
070:        import org.openide.util.Utilities;
071:
072:        public class FileWizardIterator implements  TemplateWizard.Iterator {
073:            private static final long serialVersionUID = -1987345825459L;
074:
075:            protected static final String JAVA_EXTENSION = "java"; // NOI18N
076:
077:            /** Singleton instance of FileWizardIterator, should it be ever needed.
078:             */
079:            protected static FileWizardIterator instance;
080:
081:            /** Index of the current panel. Panels are numbered from 0 to PANEL_COUNT - 1.
082:             */
083:            protected transient int panelIndex = 0;
084:
085:            protected transient WizardDescriptor.Panel[] panels;
086:
087:            protected transient TemplateWizard wizardInstance;
088:
089:            /** Returns FileWizardIterator singleton instance. This method is used
090:             * for constructing the instance from filesystem.attributes.
091:             */
092:            public static synchronized FileWizardIterator singleton() {
093:                if (instance == null) {
094:                    instance = new FileWizardIterator();
095:                }
096:                return instance;
097:            }
098:
099:            // ========================= TemplateWizard.Iterator ============================
100:
101:            /** Instantiates the template using informations provided by
102:             * the wizard.
103:             *
104:             * @param wiz the wizard
105:             * @return set of data objects that has been created (should contain
106:             *  at least one
107:             * @exception IOException if the instantiation fails
108:             */
109:            public java.util.Set<DataObject> instantiate(
110:                    final TemplateWizard wiz) throws IOException,
111:                    IllegalArgumentException {
112:                wizardInstance = wiz;
113:                final DataObject obj = instantiateTemplate(wiz.getTemplate(),
114:                        wiz.getTargetFolder(), wiz.getTargetName());
115:
116:                final Object isMidletObject = Templates.getTemplate(
117:                        wizardInstance).getAttribute(
118:                        MIDPTargetChooserPanelGUI.IS_MIDLET_TEMPLATE_ATTRIBUTE); // NOI18N
119:                boolean isMIDlet = false;
120:                if (isMidletObject instanceof  Boolean)
121:                    isMIDlet = ((Boolean) isMidletObject).booleanValue();
122:
123:                if (isMIDlet) {
124:                    final Project p = Templates.getProject(wizardInstance);
125:                    final AntProjectHelper h = p.getLookup().lookup(
126:                            AntProjectHelper.class);
127:                    if (p instanceof  J2MEProject && h != null) {
128:                        RequestProcessor.getDefault().post(new Runnable() {
129:                            public void run() {
130:                                try {
131:                                    J2MEProjectGenerator
132:                                            .addMIDletProperty(
133:                                                    p,
134:                                                    h,
135:                                                    (String) wiz
136:                                                            .getProperty(MIDPTargetChooserPanel.MIDLET_NAME),
137:                                                    (String) wiz
138:                                                            .getProperty(MIDPTargetChooserPanel.MIDLET_CLASSNAME),
139:                                                    (String) wiz
140:                                                            .getProperty(MIDPTargetChooserPanel.MIDLET_ICON));
141:                                    ProjectManager.getDefault().saveProject(p);
142:                                } catch (IOException e) {
143:                                    e.printStackTrace(); // TODO
144:                                }
145:                            }
146:                        });
147:                    }
148:                }
149:                return java.util.Collections.singleton(obj);
150:            }
151:
152:            public WizardDescriptor.Panel current() {
153:                return panels[panelIndex];
154:            }
155:
156:            public String name() {
157:                return current().getComponent().getName();
158:            }
159:
160:            public boolean hasNext() {
161:                return false;
162:            }
163:
164:            public boolean hasPrevious() {
165:                return false;
166:            }
167:
168:            public void nextPanel() {
169:                throw new NoSuchElementException();
170:            }
171:
172:            public void previousPanel() {
173:                throw new NoSuchElementException();
174:            }
175:
176:            /** Add a listener to changes of the current panel.
177:             * The listener is notified when the possibility to move forward/backward changes.
178:             * @param l the listener to add
179:             */
180:            public void addChangeListener(@SuppressWarnings("unused")
181:            final ChangeListener l) {
182:            }
183:
184:            /** Remove a listener to changes of the current panel.
185:             * @param l the listener to remove
186:             */
187:            public void removeChangeListener(@SuppressWarnings("unused")
188:            final ChangeListener l) {
189:            }
190:
191:            public void initialize(final TemplateWizard wizard) {
192:                wizardInstance = wizard;
193:                if (panels == null) {
194:                    panels = new WizardDescriptor.Panel[] { new MIDPTargetChooserPanel(), };
195:                }
196:                panelIndex = 0;
197:                updateStepsList();
198:            }
199:
200:            public void uninitialize(@SuppressWarnings("unused")
201:            final TemplateWizard wiz) {
202:                wizardInstance = null;
203:                panels = null;
204:                panelIndex = -1;
205:            }
206:
207:            // ========================= IMPLEMENTATION ============================
208:
209:            /** Instantiates the template. Currently it just delegates to the template DataObject's
210:             * createFromTemplate method.
211:             */
212:            private DataObject instantiateTemplate(final DataObject tpl,
213:                    final DataFolder target, String name) throws IOException {
214:                if (name == null) {
215:                    name = getDefaultName(tpl, target);
216:                }
217:
218:                String message;
219:                message = checkValidPackageName(target);
220:                if (message == null)
221:                    message = checkTargetName(target, name);
222:                if (message != null)
223:                    throw (IllegalStateException) ErrorManager.getDefault()
224:                            .annotate(new IllegalStateException(message),
225:                                    ErrorManager.USER, null, message, null,
226:                                    null);
227:
228:                return tpl.createFromTemplate(target, name);
229:            }
230:
231:            private static boolean isValidPackageName(final String s) {
232:                // valid package is an empty one, or well-formed java identifier :-)
233:                if ("".equals(s)) // NOI18N
234:                    return true;
235:                try {
236:                    Utilities.isJavaIdentifier(s);
237:                    return true;
238:                } catch (IllegalArgumentException ex) {
239:                    return false;
240:                }
241:            }
242:
243:            public static String checkValidPackageName(
244:                    final DataFolder targetFolder) throws IllegalStateException {
245:                final FileObject folder = targetFolder.getPrimaryFile();
246:                final ClassPath cp = ClassPath.getClassPath(folder,
247:                        ClassPath.SOURCE);
248:                String msg = null;
249:                if (cp != null) {
250:                    final String fullTarget = cp.getResourceName(folder, '.',
251:                            false);
252:                    if (isValidPackageName(fullTarget)) {
253:                        return null;
254:                    }
255:                    msg = MessageFormat.format(
256:                            getString("ERR_File_IllegalFolderName"), // NOI18N
257:                            new Object[] { folder.getPath(), fullTarget });
258:                } else {
259:                    msg = getString("ERR_File_NotInSourcePath"); // NOI18N
260:                }
261:                return msg;
262:                // checking for java-compatible name - both the folder name and the target name
263:                // must be acceptable.
264:            }
265:
266:            /**
267:             * @param folder target folder for java file
268:             * @param desiredName name to check
269:             * @return true if the desiredName is OK
270:             */
271:            public static String checkTargetName(final DataFolder folder,
272:                    final String desiredName) {
273:                if (!Utilities.isJavaIdentifier(desiredName)) {
274:                    final String msg = MessageFormat.format(
275:                            getString("ERR_File_IllegalTargetName"), // NOI18N
276:                            new Object[] { desiredName });
277:                    return msg;
278:                }
279:
280:                final FileObject f = folder.getPrimaryFile();
281:                // check whether the name already exists:
282:                if (f.getFileObject(desiredName, JAVA_EXTENSION) != null) {
283:                    final String msg = MessageFormat.format(
284:                            getString("ERR_File_TargetExists"), // NOI18N
285:                            new Object[] { desiredName });
286:                    return msg;
287:                }
288:                return null;
289:            }
290:
291:            private String getDefaultName(final DataObject template,
292:                    final DataFolder targetFolder) {
293:                final String desiredName = org.openide.filesystems.FileUtil
294:                        .findFreeFileName(targetFolder.getPrimaryFile(),
295:                                template.getName(), JAVA_EXTENSION);
296:                return desiredName;
297:            }
298:
299:            static String getString(final String key) {
300:                return NbBundle.getMessage(FileWizardIterator.class, key);
301:            }
302:
303:            static char getMnemonic(final String key) {
304:                return getString(key).charAt(0);
305:            }
306:
307:            private void updateStepsList() {
308:                final JComponent component = (JComponent) current()
309:                        .getComponent();
310:                component.putClientProperty("WizardPanel_contentData",
311:                        new String[] {// NOI18N
312:                        NbBundle.getMessage(MIDPTargetChooserPanel.class,
313:                                "TITLE_File"), // NOI18N
314:                        }); // NOI18N
315:                component.putClientProperty("WizardPanel_contentSelectedIndex",
316:                        new Integer(panelIndex)); // NOI18N
317:            }
318:
319:        }
ww___w.___j___av___a_2_s.c___o___m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.