KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > bluej > export > ConvertToJ2SEAction


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.bluej.export;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.Dialog JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.text.MessageFormat JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import javax.swing.AbstractAction JavaDoc;
34 import javax.swing.JComponent JavaDoc;
35 import javax.swing.SwingUtilities JavaDoc;
36 import org.netbeans.api.java.classpath.ClassPath;
37 import org.netbeans.api.progress.ProgressHandle;
38 import org.netbeans.api.progress.ProgressHandleFactory;
39 import org.netbeans.api.project.Project;
40 import org.netbeans.api.project.ProjectInformation;
41 import org.netbeans.api.project.ProjectManager;
42 import org.netbeans.api.project.ui.OpenProjects;
43 import org.netbeans.bluej.BluejProject;
44 import org.netbeans.bluej.UpdateHelper;
45 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender;
46 import org.netbeans.spi.project.support.ant.AntProjectHelper;
47 import org.netbeans.spi.project.support.ant.EditableProperties;
48 import org.openide.DialogDisplayer;
49 import org.openide.WizardDescriptor;
50 import org.openide.filesystems.FileLock;
51 import org.openide.filesystems.FileObject;
52 import org.openide.filesystems.FileUtil;
53 import org.openide.filesystems.URLMapper;
54 import org.openide.util.Lookup;
55 import org.openide.util.NbBundle;
56 import org.openide.util.RequestProcessor;
57 import org.openide.windows.TopComponent;
58 import org.openide.windows.WindowManager;
59
60 public final class ConvertToJ2SEAction extends AbstractAction JavaDoc {
61     
62     private BluejProject project;
63     private WizardDescriptor.Panel[] panels;
64     
65     public ConvertToJ2SEAction(BluejProject project) {
66         putValue(NAME, getName());
67         this.project = project;
68     }
69     
70     public void actionPerformed(ActionEvent JavaDoc e) {
71         
72         final WizardDescriptor wizardDescriptor = new WizardDescriptor(getPanels());
73         // {0} will be replaced by WizardDesriptor.Panel.getComponent().getName()
74
wizardDescriptor.setTitleFormat(new MessageFormat JavaDoc("{0}")); // NOI18N
75
wizardDescriptor.setTitle(org.openide.util.NbBundle.getMessage(ConvertToJ2SEAction.class, "TITLE_Convert"));
76         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(wizardDescriptor);
77         dialog.setVisible(true);
78         dialog.toFront();
79         boolean cancelled = wizardDescriptor.getValue() != WizardDescriptor.FINISH_OPTION;
80         if (!cancelled) {
81                 final ProgressHandle handle = ProgressHandleFactory.createHandle(org.openide.util.NbBundle.getMessage(ConvertToJ2SEAction.class, "Progress_Display"));
82                 handle.start(10);
83                 RequestProcessor.getDefault().post(new Runnable JavaDoc() {
84                     public void run() {
85                         try {
86                             doExport((File JavaDoc)wizardDescriptor.getProperty("NewProjectLocation"), handle); // NOI18N
87
} catch (ClassNotFoundException JavaDoc ex) {
88                             ex.printStackTrace();
89                         } catch (InvocationTargetException JavaDoc ex) {
90                             ex.printStackTrace();
91                         } catch (IOException JavaDoc ex) {
92                             ex.printStackTrace();
93                         } catch (NoSuchMethodException JavaDoc ex) {
94                             ex.printStackTrace();
95                         } catch (IllegalAccessException JavaDoc ex) {
96                             ex.printStackTrace();
97                         }
98                     }
99                 });
100         }
101     }
102     
103     
104     /**
105      * Initialize panels representing individual wizard's steps and sets
106      * various properties for them influencing wizard appearance.
107      */

108     private WizardDescriptor.Panel[] getPanels() {
109         if (panels == null) {
110             panels = new WizardDescriptor.Panel[] {
111                 new ExportWizardPanel1(project.getProjectDirectory())
112             };
113             String JavaDoc[] steps = new String JavaDoc[panels.length];
114             for (int i = 0; i < panels.length; i++) {
115                 Component JavaDoc c = panels[i].getComponent();
116                 // Default step name to component name of panel. Mainly useful
117
// for getting the name of the target chooser to appear in the
118
// list of steps.
119
steps[i] = c.getName();
120                 if (c instanceof JComponent JavaDoc) { // assume Swing components
121
JComponent JavaDoc jc = (JComponent JavaDoc) c;
122                     // Sets step number of a component
123
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
124
// Sets steps names for a panel
125
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
126
// Turn on subtitle creation on each step
127
jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); // NOI18N
128
// Show steps on the left side with the image on the background
129
jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE); // NOI18N
130
// Turn on numbering of all steps
131
jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE); // NOI18N
132
}
133             }
134         }
135         return panels;
136     }
137     
138     public String JavaDoc getName() {
139         return NbBundle.getMessage(ConvertToJ2SEAction.class, "CTL_ConvertToJ2SEAction"); // NOI18N
140
}
141     
142     
143     /**
144      * lets assume the file is directory and it's empty or not existing..
145      */

146     private void doExport(File JavaDoc file, ProgressHandle handle) throws IOException JavaDoc, ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
147         if (!file.exists()) {
148             file.mkdirs();
149         }
150         handle.progress(1);
151         ProjectInformation info = (ProjectInformation)project.getLookup().lookup(ProjectInformation.class);
152         ClassLoader JavaDoc loader = (ClassLoader JavaDoc)Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
153         Class JavaDoc j2seclazz = loader.loadClass("org.netbeans.modules.java.j2seproject.J2SEProjectGenerator"); // NOI18N
154
Method JavaDoc createMethod = j2seclazz.getMethod("createProject", new Class JavaDoc[] { // NOI18N
155
File JavaDoc.class, String JavaDoc.class, String JavaDoc.class, String JavaDoc.class
156         });
157         createMethod.invoke(null, new Object JavaDoc[] {
158             file, info.getName(), null, null
159         });
160         handle.progress(5);
161         
162         
163         FileObject root = FileUtil.toFileObject(file);
164         Project j2seproject = ProjectManager.getDefault().findProject(root);
165 // ProjectManager.getDefault().saveProject(j2seproject);
166
FileObject originRoot = project.getProjectDirectory();
167         FileObject targetTestRoot = root.getFileObject("test"); // NOI18N
168
FileObject targetSrcRoot = root.getFileObject("src"); // NOI18N
169
splitSources(originRoot, targetSrcRoot, targetTestRoot);
170         handle.progress(7);
171         ClassPath path = ClassPath.getClassPath(project.getProjectDirectory(), ClassPath.COMPILE);
172         ProjectClassPathExtender extender = (ProjectClassPathExtender)j2seproject.getLookup().lookup(ProjectClassPathExtender.class);
173         Iterator JavaDoc it = path.entries().iterator();
174         FileObject libsFolder = root.getFileObject("libs"); // NOI18N
175
if (it.hasNext() && libsFolder == null) {
176             libsFolder = root.createFolder("libs"); // NOI18N
177
}
178         while (it.hasNext()) {
179             ClassPath.Entry entry = (ClassPath.Entry) it.next();
180             URL JavaDoc url = FileUtil.getArchiveFile(entry.getURL());
181             FileObject fo = URLMapper.findFileObject(url);
182             if (fo != null) {
183                 if (fo.getName().indexOf("junit") == -1) { // NOI18N
184
// we don't want to copy junit..
185
FileObject createdOne = FileUtil.copyFile(fo, libsFolder, fo.getName());
186                     extender.addArchiveFile(createdOne);
187                 }
188             }
189         }
190         // if main class selected, add the main.class property to prop file.
191
UpdateHelper updateHelper = project.getUpdateHelper();
192         EditableProperties ep = updateHelper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
193         String JavaDoc mainClass = (String JavaDoc)ep.get ("main.class"); // NOI18N
194

195         if (mainClass != null) {
196             FileObject fo = j2seproject.getProjectDirectory().getFileObject(AntProjectHelper.PROJECT_PROPERTIES_PATH);
197             EditableProperties eds = new EditableProperties();
198             InputStream JavaDoc instr = null;
199             OutputStream JavaDoc outstr = null;
200             FileLock lock = null;
201             try {
202                 instr = fo.getInputStream();
203                 eds.load(instr);
204                 instr.close();
205                 instr = null;
206                 eds.setProperty("main.class", mainClass);
207                 if (ep.getProperty("application.args") != null) {
208                     eds.setProperty("application.args", ep.getProperty("application.args"));
209                 }
210                 if (ep.getProperty("work.dir") != null) {
211                     eds.setProperty("work.dir", ep.getProperty("work.dir"));
212                 }
213                 if (ep.getProperty("run.jvmargs") != null) {
214                     eds.setProperty("run.jvmargs", ep.getProperty("run.jvmargs"));
215                 }
216                 lock = fo.lock();
217                 outstr = fo.getOutputStream(lock);
218                 eds.store(outstr);
219             } catch (IOException JavaDoc ex) {
220                 
221             } finally {
222                 if (instr != null) {
223                     instr.close();
224                 }
225                 if (outstr != null) {
226                     outstr.close();
227                 }
228                 if (lock != null) {
229                     lock.releaseLock();
230                 }
231             }
232         }
233         
234         handle.progress(9);
235         OpenProjects.getDefault().open(new Project[] { j2seproject }, false);
236         SwingUtilities.invokeLater(new Runnable JavaDoc() {
237             public void run() {
238                 TopComponent tc = WindowManager.getDefault().findTopComponent("projectTabLogical_tc"); // NOI18N
239
if (tc != null) {
240                     tc.open();
241                     tc.requestActive();
242                 }
243             }
244         });
245         handle.finish();
246         
247     }
248     
249     private void splitSources(FileObject originRoot, FileObject targetSrcRoot, FileObject targetTestRoot) throws IOException JavaDoc {
250         FileObject[] sourceFOs = originRoot.getChildren();
251         for (int i = 0; i < sourceFOs.length; i++) {
252             if (sourceFOs[i].isData()) {
253                 if ("java".equals(sourceFOs[i].getExt())) { // NOI18N
254
boolean test = sourceFOs[i].getName().endsWith("Test"); // NOI18N
255
FileUtil.copyFile(sourceFOs[i], test ? targetTestRoot : targetSrcRoot, sourceFOs[i].getName());
256                 }
257             } else if (sourceFOs[i].getFileObject("bluej.pkg") != null) { // NOI18N
258
//only the bluej package items get copied.
259
FileObject childTargetSrc = targetSrcRoot.createFolder(sourceFOs[i].getName());
260                 FileObject childTargetTest = targetTestRoot.createFolder(sourceFOs[i].getName());
261                 splitSources(sourceFOs[i], childTargetSrc, childTargetTest);
262             }
263         }
264     }
265     
266     
267 }
268
Popular Tags