KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > bluej > packagewizard > PackageWizardIterator


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.bluej.packagewizard;
21
22 import java.io.IOException JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.swing.event.ChangeListener JavaDoc;
27 import org.netbeans.api.project.FileOwnerQuery;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.bluej.BluejProject;
30 import org.netbeans.spi.project.ui.templates.support.Templates;
31 import org.openide.WizardDescriptor;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileSystem;
34 import org.openide.util.Lookup;
35
36 /**
37  * a hacky wrapper around the default package wizard iterator, making sure we create the bluej.pkg file in a package so
38  * that it appears in the bluej view.
39  * @author mkleint
40  */

41 public class PackageWizardIterator implements WizardDescriptor.InstantiatingIterator {
42     
43     private WizardDescriptor.InstantiatingIterator delegate;
44
45     private WizardDescriptor wiz;
46
47     private Set JavaDoc set;
48     
49     
50     public static PackageWizardIterator createWizard() {
51         return new PackageWizardIterator();
52     }
53     
54     /** Creates a new instance of PackageWizardIterator */
55     private PackageWizardIterator() {
56         ClassLoader JavaDoc ldr = (ClassLoader JavaDoc) Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
57         Class JavaDoc clazz;
58         Method JavaDoc method;
59         try {
60             clazz = Class.forName("org.netbeans.modules.java.project.NewJavaFileWizardIterator", true, ldr); // NOI18N
61
method = clazz.getMethod("packageWizard", null); // NOI18N
62
delegate = (WizardDescriptor.InstantiatingIterator)method.invoke(null, null);
63         } catch (SecurityException JavaDoc ex) {
64             ex.printStackTrace();
65         } catch (NoSuchMethodException JavaDoc ex) {
66             ex.printStackTrace();
67         } catch (ClassNotFoundException JavaDoc ex) {
68             ex.printStackTrace();
69         } catch (IllegalArgumentException JavaDoc ex) {
70             ex.printStackTrace();
71         } catch (IllegalAccessException JavaDoc ex) {
72             ex.printStackTrace();
73         } catch (InvocationTargetException JavaDoc ex) {
74             ex.printStackTrace();
75         }
76     }
77
78     public Set JavaDoc instantiate() throws IOException JavaDoc {
79         FileObject dir = Templates.getTargetFolder( wiz );
80         Project project = FileOwnerQuery.getOwner(dir);
81         if (project.getLookup().lookup(BluejProject.class) != null) {
82             FileSystem fs = dir.getFileSystem();
83             
84             fs.runAtomicAction(
85                     new FileSystem.AtomicAction() {
86                 public void run() throws IOException JavaDoc {
87                     set = delegate.instantiate();
88                     FileObject fo = (FileObject)set.iterator().next();
89                     fo.createData("bluej.pkg"); // NOI18N
90
}
91             }
92             );
93             
94         } else {
95             set = delegate.instantiate();
96             
97         }
98         return set;
99     }
100
101     public void initialize(WizardDescriptor wizard) {
102         wiz = wizard;
103         delegate.initialize(wizard);
104     }
105
106     public void uninitialize(WizardDescriptor wizard) {
107         delegate.uninitialize(wizard);
108     }
109
110     public WizardDescriptor.Panel current() {
111         return delegate.current();
112     }
113
114     public String JavaDoc name() {
115         return delegate.name();
116     }
117
118     public boolean hasNext() {
119         return delegate.hasNext();
120     }
121
122     public boolean hasPrevious() {
123         return delegate.hasPrevious();
124     }
125
126     public void nextPanel() {
127         delegate.nextPanel();
128     }
129
130     public void previousPanel() {
131         delegate.previousPanel();
132     }
133
134     public void addChangeListener(ChangeListener JavaDoc l) {
135         delegate.addChangeListener(l);
136     }
137
138     public void removeChangeListener(ChangeListener JavaDoc l) {
139         delegate.removeChangeListener(l);
140     }
141     
142 }
143
Popular Tags