KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > metainfservices > ExportAction


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.apisupport.metainfservices;
20
21 import com.sun.source.tree.Tree;
22 import com.sun.source.util.TreePath;
23 import java.awt.Dialog JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.TreeSet JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33 import javax.lang.model.element.Element;
34 import javax.lang.model.element.TypeElement;
35 import javax.lang.model.type.TypeMirror;
36 import org.netbeans.api.java.project.JavaProjectConstants;
37 import org.netbeans.api.java.source.CancellableTask;
38 import org.netbeans.api.java.source.CompilationController;
39 import org.netbeans.api.java.source.JavaSource;
40 import org.netbeans.api.project.FileOwnerQuery;
41 import org.netbeans.api.project.Project;
42 import org.netbeans.api.project.ProjectUtils;
43 import org.netbeans.api.project.SourceGroup;
44 import org.netbeans.api.project.Sources;
45 import org.openide.DialogDisplayer;
46 import org.openide.ErrorManager;
47 import org.openide.NotifyDescriptor;
48 import org.openide.WizardDescriptor;
49 import org.openide.filesystems.FileLock;
50 import org.openide.filesystems.FileObject;
51 import org.openide.filesystems.FileUtil;
52 import org.openide.loaders.DataObject;
53 import org.openide.nodes.Node;
54 import org.openide.util.HelpCtx;
55 import org.openide.util.NbBundle;
56 import org.openide.util.actions.CookieAction;
57
58 public final class ExportAction extends CookieAction {
59     private static final Logger JavaDoc LOG = Logger.getLogger(ExportAction.class.getName());
60     
61     protected void performAction(Node[] activatedNodes) {
62         MyTask task = new ExportAction.MyTask();
63         FileObject fo = activatedNodes[0].getLookup().lookup(org.openide.filesystems.FileObject.class);
64         if (fo == null) {
65             return;
66         }
67         try {
68             JavaSource source = JavaSource.forFileObject(fo);
69             source.runUserActionTask(task, true);
70         } catch (IOException JavaDoc ex) {
71             LOG.log(Level.SEVERE, ex.getMessage(), ex);
72         }
73
74         task.postProcess(fo);
75     }
76
77     protected int mode() {
78         return CookieAction.MODE_EXACTLY_ONE;
79     }
80     
81     public String JavaDoc getName() {
82         return NbBundle.getMessage(ExportAction.class, "CTL_ExportAction");
83     }
84     
85     protected Class JavaDoc[] cookieClasses() {
86         return new Class JavaDoc[] {
87             DataObject.class
88         };
89     }
90     
91     protected void initialize() {
92         super.initialize();
93         // see org.openide.util.actions.SystemAction.iconResource() javadoc for more details
94
putValue("noIconInMenu", Boolean.TRUE);
95     }
96     
97     public HelpCtx getHelpCtx() {
98         return HelpCtx.DEFAULT_HELP;
99     }
100     
101     protected boolean asynchronous() {
102         return false;
103     }
104
105     private static final class MyTask implements CancellableTask<CompilationController> {
106         Collection JavaDoc<String JavaDoc> allInterfaces = new TreeSet JavaDoc<String JavaDoc>();
107         String JavaDoc clazzName;
108         
109         public void cancel() {
110         }
111
112         public void run(CompilationController cont) throws Exception JavaDoc {
113             cont.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
114             for (Tree t : cont.getCompilationUnit().getTypeDecls()) {
115                 if (t.getKind() == Tree.Kind.CLASS) {
116                     TreePath path = cont.getTrees().getPath(cont.getCompilationUnit(), t);
117                     Element e = cont.getTrees().getElement(path);
118                     if (e instanceof TypeElement) {
119                         TypeElement te = (TypeElement)e;
120                         clazzName = te.getQualifiedName().toString();
121                     }
122                     findInterfaces(cont, e);
123                 }
124             }
125         }
126         
127         private void findInterfaces(CompilationController cont, Element e) {
128             if (e == null) {
129                 return;
130             }
131             if (!e.getKind().isClass() && !e.getKind().isInterface()) {
132                 return;
133             }
134             TypeElement type = (TypeElement)e;
135             allInterfaces.add(type.getQualifiedName().toString());
136             
137             findInterfaces(cont, type.getSuperclass());
138             for (TypeMirror m : type.getInterfaces()) {
139                 findInterfaces(cont, m);
140             }
141         }
142         
143         private void findInterfaces(CompilationController cont, TypeMirror m) {
144             findInterfaces(cont, cont.getTypes().asElement(m));
145         }
146
147         public void postProcess(FileObject fo) {
148             FileObject target = null;
149             Project p = FileOwnerQuery.getOwner(fo);
150
151             if (p != null) {
152                 Sources s = ProjectUtils.getSources(p);
153                 SourceGroup[] arr = s.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
154                 if (arr != null && arr.length > 0) {
155                     target = arr[0].getRootFolder();
156                 }
157             }
158
159
160             if (allInterfaces.isEmpty() || clazzName == null) {
161                 NotifyDescriptor d = new NotifyDescriptor.Message(
162                     NbBundle.getMessage(ExportAction.class, "MSG_CannotFindClass", fo),
163                     NotifyDescriptor.WARNING_MESSAGE
164                 );
165                 DialogDisplayer.getDefault().notify(d);
166                 return;
167             }
168
169             WizardDescriptor wd = new WizardDescriptor(new ExportWizardIterator());
170
171             wd.putProperty("implName", clazzName); // NOI18N
172
wd.putProperty("interfaceNames", allInterfaces); // NOI18N
173
wd.putProperty("target", target);
174
175             Dialog JavaDoc d = DialogDisplayer.getDefault().createDialog(wd);
176             d.setVisible(true);
177
178             if (wd.FINISH_OPTION == wd.getValue()) {
179                 try {
180                     createFiles(clazzName, wd, target);
181                 } catch (IOException JavaDoc ex) {
182                     ErrorManager.getDefault().notify(ex);
183                 }
184             }
185         }
186     }
187     
188     @SuppressWarnings JavaDoc("unchecked")
189     private static void createFiles(String JavaDoc implName, WizardDescriptor wd, FileObject target)
190     throws IOException JavaDoc, FileNotFoundException JavaDoc {
191         List JavaDoc<String JavaDoc> files = (List JavaDoc<String JavaDoc>)wd.getProperty("files"); // NOI18N
192
createFiles(implName, files, target);
193     }
194
195     static void createFiles(String JavaDoc implName, List JavaDoc<String JavaDoc> files, FileObject target)
196     throws IOException JavaDoc, FileNotFoundException JavaDoc {
197         // lets apply the files
198
for (String JavaDoc s : files) {
199             FileObject f = FileUtil.createData(target, s);
200             byte[] exist = new byte[(int)f.getSize()];
201             InputStream JavaDoc is = f.getInputStream();
202             int len = is.read(exist);
203             is.close();
204             //assert len == exist.length;
205

206             String JavaDoc content = new String JavaDoc(exist);
207             if (content.length() > 0 && !content.endsWith("\n")) { // NOI18N
208
content = content + "\n"; // NOI18N
209
}
210
211             content = content + implName + "\n"; // NOI18N
212

213             FileLock lock = f.lock();
214             OutputStream JavaDoc os = f.getOutputStream(lock);
215             os.write(content.getBytes());
216             os.close();
217             lock.releaseLock();
218         }
219     }
220 }
221
222
Popular Tags