KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > eclipse > wizards > NewXDoc


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation or its licensors,
3  * as applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.forrest.eclipse.wizards;
18
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20
21 import org.apache.forrest.eclipse.ForrestPlugin;
22 import org.eclipse.ant.core.AntRunner;
23 import org.eclipse.core.resources.IContainer;
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.IProject;
26 import org.eclipse.core.resources.IResource;
27 import org.eclipse.core.resources.IWorkspaceRoot;
28 import org.eclipse.core.resources.ResourcesPlugin;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.core.runtime.IProgressMonitor;
31 import org.eclipse.core.runtime.IStatus;
32 import org.eclipse.core.runtime.Path;
33 import org.eclipse.core.runtime.Status;
34 import org.eclipse.jface.dialogs.MessageDialog;
35 import org.eclipse.jface.operation.IRunnableWithProgress;
36 import org.eclipse.jface.viewers.ISelection;
37 import org.eclipse.jface.viewers.IStructuredSelection;
38 import org.eclipse.jface.wizard.Wizard;
39 import org.eclipse.ui.INewWizard;
40 import org.eclipse.ui.IWorkbench;
41 import org.eclipse.ui.IWorkbenchPage;
42 import org.eclipse.ui.IWorkbenchWizard;
43 import org.eclipse.ui.PartInitException;
44 import org.eclipse.ui.PlatformUI;
45 import org.eclipse.ui.ide.IDE;
46
47 /**
48  * Create a new file XDoc
49  * resource in the provided container. If the container resource
50  * (a folder or a project) is selected in the workspace
51  * when the wizard is opened, it will accept it as the target
52  * container. The wizard creates one file with the extension
53  * "xml". When created the file is opened in the default editor.
54  */

55
56 public class NewXDoc extends Wizard implements INewWizard {
57     protected NewXdocPage page;
58     protected ISelection selection;
59     protected String JavaDoc resourceAntScript = "/src/org/apache/forrest/template/template_build.xml";
60
61     /**
62      * Constructor for NewXDoc.
63      */

64     public NewXDoc() {
65         super();
66         setNeedsProgressMonitor(true);
67     }
68     
69     /**
70      * Adding the page to the wizard.
71      */

72     public void addPages() {
73         page = new NewXdocPage(selection);
74         addPage(page);
75     }
76
77     /**
78      * This method is called when 'Finish' button is pressed in
79      * the wizard. We will create an operation and run it
80      * using wizard as execution context.
81      */

82     public boolean performFinish() {
83         final String JavaDoc containerName = page.getContainerName();
84         final String JavaDoc fileName = page.getFileName();
85         IRunnableWithProgress op = new IRunnableWithProgress() {
86             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
87                 try {
88                     doFinish(containerName, fileName, monitor);
89                 } catch (CoreException e) {
90                     throw new InvocationTargetException JavaDoc(e);
91                 } finally {
92                     monitor.done();
93                 }
94             }
95         };
96         try {
97             getContainer().run(true, false, op);
98         } catch (InterruptedException JavaDoc e) {
99             return false;
100         } catch (InvocationTargetException JavaDoc e) {
101             Throwable JavaDoc realException = e.getTargetException();
102             MessageDialog.openError(getShell(), "Error", realException.getMessage());
103             return false;
104         }
105         return true;
106     }
107     
108     /**
109      * The worker method. It will find the container, create the
110      * file if missing or just replace its contents, and open
111      * the editor on the newly created file.
112      */

113
114     private void doFinish(
115         String JavaDoc containerName,
116         String JavaDoc fileName,
117         IProgressMonitor monitor)
118         throws CoreException {
119         // create a sample file
120
monitor.beginTask("Creating " + fileName, 2);
121         IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
122         IResource resource = root.findMember(new Path(containerName));
123         if (!resource.exists() || !(resource instanceof IContainer)) {
124             throwCoreException("Container \"" + containerName + "\" does not exist.");
125         }
126         
127         createFile(resource, fileName, monitor);
128         
129         IContainer container = (IContainer) resource;
130         final IFile file = container.getFile(new Path(fileName));
131         
132         monitor.worked(1);
133         monitor.setTaskName("Opening file for editing...");
134         getShell().getDisplay().asyncExec(new Runnable JavaDoc() {
135             public void run() {
136                 IWorkbenchPage page =
137                     PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
138                 try {
139                     IDE.openEditor(page, file, true);
140                 } catch (PartInitException e) {
141                 }
142             }
143         });
144         monitor.worked(1);
145     }
146     
147     /*
148      * Create the file and put the template data into it.
149      */

150     protected void createFile(
151             IResource resource,
152             String JavaDoc fileName,
153             IProgressMonitor monitor)
154     throws CoreException {
155         AntRunner runner = new AntRunner();
156         ForrestPlugin plugin = ForrestPlugin.getDefault();
157         String JavaDoc strPluginDir = plugin.getBundle().getLocation();
158         if (strPluginDir.startsWith("update@")) {
159             strPluginDir = strPluginDir.substring(8);
160         }
161         runner.setBuildFileLocation(strPluginDir + resourceAntScript);
162         String JavaDoc strPath = resource.getLocation().toOSString();
163         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("-Dresource.dir=");
164         sb.append(strPath);
165         sb.append(" ");
166         sb.append("-Dresource.name=");
167         sb.append(fileName);
168         runner.setArguments(sb.toString());
169         runner.run(monitor);
170         resource.refreshLocal(IProject.DEPTH_INFINITE, monitor);
171     }
172
173     private void throwCoreException(String JavaDoc message) throws CoreException {
174         IStatus status =
175             new Status(IStatus.ERROR, "org.apache.forrest.eclipse", IStatus.OK, message, null);
176         throw new CoreException(status);
177     }
178
179     /**
180      * We will accept the selection in the workbench to see if
181      * we can initialize from it.
182      * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
183      */

184     public void init(IWorkbench workbench, IStructuredSelection selection) {
185         this.selection = selection;
186     }
187 }
Popular Tags