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.uml.samples.bankapp;
043:
044: import java.awt.Component;
045: import java.io.File;
046: import java.io.FileNotFoundException;
047: import java.io.FileReader;
048: import java.io.FileWriter;
049: import java.io.IOException;
050: import java.io.InputStream;
051: import java.io.OutputStream;
052: import java.text.MessageFormat;
053: import java.util.Arrays;
054: import java.util.Enumeration;
055: import java.util.LinkedHashSet;
056: import java.util.NoSuchElementException;
057: import java.util.Set;
058: import java.util.zip.ZipEntry;
059: import java.util.zip.ZipInputStream;
060: import javax.swing.JComponent;
061: import javax.swing.event.ChangeListener;
062: import org.netbeans.api.project.Project;
063: import org.netbeans.api.project.ProjectManager;
064: import org.netbeans.modules.uml.project.UMLProjectGenerator;
065: import org.netbeans.modules.uml.project.UMLProjectHelper;
066: import org.netbeans.modules.uml.project.ui.common.JavaSourceRootsUI;
067: import org.netbeans.modules.uml.project.ui.wizards.NewUMLProjectWizardIterator;
068: import org.netbeans.spi.project.ui.support.ProjectChooser;
069: import org.netbeans.spi.project.ui.templates.support.Templates;
070: import org.openide.WizardDescriptor;
071: import org.openide.filesystems.FileLock;
072: import org.openide.filesystems.FileObject;
073: import org.openide.filesystems.FileUtil;
074: import org.openide.util.NbBundle;
075:
076: public class UMLBankAppSampleWizardIterator implements
077: WizardDescriptor.InstantiatingIterator {
078:
079: private int index;
080: private WizardDescriptor.Panel[] panels;
081: private WizardDescriptor wiz;
082:
083: private FileObject umlProjectZipFO;
084: private FileObject readmeFO;
085:
086: public UMLBankAppSampleWizardIterator() {
087: }
088:
089: public static UMLBankAppSampleWizardIterator createIterator() {
090: return new UMLBankAppSampleWizardIterator();
091: }
092:
093: private WizardDescriptor.Panel[] createPanels() {
094: return new WizardDescriptor.Panel[] { new UMLBankAppSampleWizardPanel(), };
095: }
096:
097: private String[] createSteps() {
098: return new String[] { NbBundle.getMessage(
099: UMLBankAppSampleWizardIterator.class,
100: "LBL_CreateProjectStep") // NOI18N
101: };
102: }
103:
104: public Set/*<FileObject>*/instantiate() throws IOException {
105: Set resultSet = new LinkedHashSet();
106:
107: // Create Java Project
108: //////////////////////
109:
110: File javaPrjDir = FileUtil.normalizeFile((File) wiz
111: .getProperty("projdir")); // NOI18N
112:
113: //javaPrjDir.mkdirs();
114: FileObject template = Templates.getTemplate(wiz);
115: FileObject javaPrjFO = FileUtil.createFolder(javaPrjDir);
116: unZipFile(template.getInputStream(), javaPrjFO, false);
117:
118: renameJavaProjectTokens(javaPrjDir, wiz.getProperty("name")
119: .toString()); // NOI18N
120:
121: // Always open top dir as a project:
122: resultSet.add(javaPrjFO);
123:
124: // Look for nested projects to open as well:
125: Enumeration e = javaPrjFO.getFolders(true);
126: while (e.hasMoreElements()) {
127: FileObject subfolder = (FileObject) e.nextElement();
128: if (ProjectManager.getDefault().isProject(subfolder))
129: resultSet.add(subfolder);
130: }
131:
132: File parent = javaPrjDir.getParentFile();
133: if (parent != null && parent.exists())
134: ProjectChooser.setProjectsFolder(parent);
135:
136: if (readmeFO != null)
137: resultSet.add(readmeFO);
138:
139: // Create UML Project
140: /////////////////////
141:
142: File umlPrjDir = FileUtil.normalizeFile((File) wiz
143: .getProperty("umlprojdir")); // NOI18N
144:
145: Project javaProject = ProjectManager.getDefault().findProject(
146: javaPrjFO);
147:
148: UMLProjectGenerator.createRevEngProject(
149: umlPrjDir,
150: wiz.getProperty("umlname").toString(), // NOI18N
151: javaProject,
152: JavaSourceRootsUI.createModel(javaProject), null,
153: NewUMLProjectWizardIterator.TYPE_REVERSE_ENGINEER);
154:
155: // Always open top dir as a project:
156: resultSet.add(FileUtil.toFileObject(umlPrjDir));
157:
158: return resultSet;
159: }
160:
161: public void initialize(WizardDescriptor wiz) {
162: this .wiz = wiz;
163: index = 0;
164: panels = createPanels();
165: // Make sure list of steps is accurate.
166: String[] steps = createSteps();
167: for (int i = 0; i < panels.length; i++) {
168: Component c = panels[i].getComponent();
169: if (steps[i] == null) {
170: // Default step name to component name of panel.
171: // Mainly useful for getting the name of the target
172: // chooser to appear in the list of steps.
173: steps[i] = c.getName();
174: }
175: if (c instanceof JComponent) { // assume Swing components
176: JComponent jc = (JComponent) c;
177: // Step #.
178: jc.putClientProperty(
179: "WizardPanel_contentSelectedIndex",
180: new Integer(i)); // NOI18N
181:
182: // Step name (actually the whole list for reference).
183: jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
184: }
185: }
186: }
187:
188: public void uninitialize(WizardDescriptor wiz) {
189: this .wiz.putProperty("projdir", null); // NOI18N
190: this .wiz.putProperty("name", null); // NOI18N
191: this .wiz = null;
192: panels = null;
193: }
194:
195: public String name() {
196: return MessageFormat.format("{0} of {1}", // NOI18N
197: new Object[] { new Integer(index + 1),
198: new Integer(panels.length) });
199: }
200:
201: public boolean hasNext() {
202: return index < panels.length - 1;
203: }
204:
205: public boolean hasPrevious() {
206: return index > 0;
207: }
208:
209: public void nextPanel() {
210: if (!hasNext()) {
211: throw new NoSuchElementException();
212: }
213: index++;
214: }
215:
216: public void previousPanel() {
217: if (!hasPrevious()) {
218: throw new NoSuchElementException();
219: }
220: index--;
221: }
222:
223: public WizardDescriptor.Panel current() {
224: return panels[index];
225: }
226:
227: // If nothing unusual changes in the middle of the wizard, simply:
228: public final void addChangeListener(ChangeListener l) {
229: }
230:
231: public final void removeChangeListener(ChangeListener l) {
232: }
233:
234: private void unZipFile(InputStream source, FileObject projectRoot,
235: boolean isUmlPrj) throws IOException {
236: try {
237: ZipInputStream str = new ZipInputStream(source);
238: ZipEntry entry;
239:
240: while ((entry = str.getNextEntry()) != null) {
241: if (entry.isDirectory())
242: FileUtil.createFolder(projectRoot, entry.getName());
243:
244: else {
245: FileObject fo = FileUtil.createData(projectRoot,
246: entry.getName());
247:
248: FileLock lock = fo.lock();
249:
250: try {
251: OutputStream out = fo.getOutputStream(lock);
252: try {
253: if (!isUmlPrj) {
254: if (fo.getNameExt().equals(
255: "umlProject.zip")) // NOI18N
256: umlProjectZipFO = fo;
257:
258: else if (fo.getName().equals("README")) // NOI18N
259: readmeFO = fo;
260: }
261:
262: FileUtil.copy(str, out);
263: }
264:
265: finally {
266: out.close();
267: }
268: }
269:
270: finally {
271: lock.releaseLock();
272: }
273: }
274: }
275: }
276:
277: finally {
278: source.close();
279: }
280: }
281:
282: private final static String FILE_CONTENTS_JAVA_PROJECT_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
283: + System.getProperty("line.separator")
284: + // NOI18N
285: "<project xmlns=\"http://www.netbeans.org/ns/project/1\">"
286: + System.getProperty("line.separator")
287: + // NOI18N
288: " <type>org.netbeans.modules.java.j2seproject</type>"
289: + System.getProperty("line.separator")
290: + // NOI18N
291: " <configuration>"
292: + System.getProperty("line.separator")
293: + // NOI18N
294: " <data xmlns=\"http://www.netbeans.org/ns/j2se-project/2\">"
295: + System.getProperty("line.separator")
296: + // NOI18N
297: " <name>{0}</name>"
298: + System.getProperty("line.separator")
299: + // NOI18N
300: " <minimum-ant-version>1.6</minimum-ant-version>"
301: + System.getProperty("line.separator")
302: + // NOI18N
303: " <source-roots>"
304: + System.getProperty("line.separator")
305: + // NOI18N
306: " <root id=\"src.dir\"/>"
307: + System.getProperty("line.separator")
308: + // NOI18N
309: " </source-roots>"
310: + System.getProperty("line.separator")
311: + // NOI18N
312: " <test-roots>"
313: + System.getProperty("line.separator")
314: + // NOI18N
315: " <root id=\"test.src.dir\"/>"
316: + System.getProperty("line.separator")
317: + // NOI18N
318: " </test-roots>"
319: + System.getProperty("line.separator")
320: + // NOI18N
321: " </data>"
322: + System.getProperty("line.separator")
323: + // NOI18N
324: " </configuration>"
325: + System.getProperty("line.separator") + // NOI18N
326: "</project>" + System.getProperty("line.separator"); // NOI18N
327:
328: private void renameJavaProjectTokens(File javaPrjDir,
329: String javaPrjName) {
330: File[] files = javaPrjDir.listFiles();
331:
332: String[] args = { javaPrjName };
333:
334: for (int i = 0; i < files.length; i++) {
335: if (files[i].isDirectory()
336: && files[i].getName().equals("nbproject")) // NOI18N
337: {
338: FileWriter xml = null;
339:
340: try {
341: xml = new FileWriter(files[i].getAbsolutePath()
342: + File.separatorChar + "project.xml"); // NOI18N
343:
344: xml.flush();
345:
346: xml.write(MessageFormat.format(
347: FILE_CONTENTS_JAVA_PROJECT_XML, args));
348: }
349:
350: catch (IOException ex) {
351: ex.printStackTrace();
352: }
353:
354: finally {
355: try {
356: if (xml != null)
357: xml.close();
358: }
359:
360: catch (IOException ex) {
361: ex.printStackTrace();
362: }
363: }
364: }
365: }
366: }
367:
368: }
|