KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > omg > lifl > eclipse > plugin > project > OpenCCM > ProjectCreationWizard


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2003 USTL - LIFL - GOAL
5 Contact: openccm-team@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): offroy ________________________.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26 /*
27  * Created on 23 mai 2003 by jerome OFFROY (offroy@lifl.fr)
28  *
29  */

30
31 package org.omg.lifl.eclipse.plugin.project.OpenCCM;
32
33 import java.lang.reflect.InvocationTargetException JavaDoc;
34
35 import org.eclipse.core.resources.IFile;
36 import org.eclipse.core.resources.IResource;
37 import org.eclipse.core.runtime.CoreException;
38 import org.eclipse.core.runtime.IConfigurationElement;
39 import org.eclipse.core.runtime.IExecutableExtension;
40 import org.eclipse.core.runtime.IStatus;
41 import org.eclipse.jface.dialogs.ErrorDialog;
42 import org.eclipse.jface.dialogs.IDialogConstants;
43 import org.eclipse.jface.dialogs.MessageDialog;
44 import org.eclipse.jface.operation.IRunnableWithProgress;
45 import org.eclipse.jface.resource.ImageDescriptor;
46 import org.eclipse.swt.widgets.Display;
47 import org.eclipse.ui.INewWizard;
48 import org.eclipse.ui.IWorkbenchPage;
49 import org.eclipse.ui.IWorkbenchWindow;
50 import org.eclipse.ui.PartInitException;
51 import org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation;
52 import org.eclipse.ui.dialogs.IOverwriteQuery;
53 import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;
54 import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
55 import org.omg.lifl.eclipse.plugin.project.OpenCCM.utils.ProjectMessages;
56
57 /**
58  * @author LIFL
59  *
60  */

61 public class ProjectCreationWizard
62     extends BasicNewResourceWizard
63     implements INewWizard, IExecutableExtension {
64
65     private IConfigurationElement fConfigElement;
66
67     private ProjectCreationWizardPage[] fPages;
68
69     public ProjectCreationWizard() {
70         super();
71         setDialogSettings(MainPlugin.getDefault().getDialogSettings());
72         setWindowTitle(
73             ProjectMessages.getString("ProjectCreationWizard.title"));
74         setNeedsProgressMonitor(true);
75     }
76
77     /*
78      * @see BasicNewResourceWizard#initializeDefaultPageImageDescriptor
79      */

80     protected void initializeDefaultPageImageDescriptor() {
81         if (fConfigElement != null) {
82             String JavaDoc banner = fConfigElement.getAttribute("banner");
83             if (banner != null) {
84                 ImageDescriptor desc =
85                     MainPlugin.getDefault().getImageDescriptor(banner);
86                 setDefaultPageImageDescriptor(desc);
87             }
88         }
89     }
90
91     /*
92      * @see Wizard#addPages
93      */

94     public void addPages() {
95         super.addPages();
96
97         IConfigurationElement[] children =
98             fConfigElement.getChildren("projectsetup");
99         if (children == null || children.length == 0) {
100             MainPlugin.log(
101                 "descriptor must contain one ore more projectsetup tags");
102             return;
103         }
104
105         fPages = new ProjectCreationWizardPage[children.length];
106
107         for (int i = 0; i < children.length; i++) {
108             IConfigurationElement curr = children[i];
109             fPages[i] = new ProjectCreationWizardPage(i, children[i]);
110             addPage(fPages[i]);
111         }
112     }
113
114     /* (non-Javadoc)
115      * @see org.eclipse.jface.wizard.IWizard#performFinish()
116      */

117     public boolean performFinish() {
118         ProjectCreationOperation runnable =
119             new ProjectCreationOperation(fPages, new ImportOverwriteQuery());
120
121         IRunnableWithProgress op =
122             new WorkspaceModifyDelegatingOperation(runnable);
123         try {
124             getContainer().run(false, true, op);
125         } catch (InvocationTargetException JavaDoc e) {
126             handleException(e.getTargetException());
127             return false;
128         } catch (InterruptedException JavaDoc e) {
129             return false;
130         }
131         BasicNewProjectResourceWizard.updatePerspective(fConfigElement);
132         IResource res = runnable.getElementToOpen();
133         if (res != null) {
134             openResource(res);
135         }
136         return true;
137     }
138
139     /**
140      * @param res
141      */

142     private void openResource(final IResource resource) {
143         if (resource.getType() != IResource.FILE) {
144             return;
145         }
146         IWorkbenchWindow window =
147             MainPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow();
148         if (window == null) {
149             return;
150         }
151         final IWorkbenchPage activePage = window.getActivePage();
152         if (activePage != null) {
153             final Display display = getShell().getDisplay();
154             display.asyncExec(new Runnable JavaDoc() {
155                 public void run() {
156                     try {
157                         activePage.openEditor((IFile) resource);
158                     } catch (PartInitException e) {
159                         MainPlugin.log(e);
160                     }
161                 }
162             });
163             selectAndReveal(resource);
164         }
165     }
166
167     /**
168      * @param throwable
169      */

170     private void handleException(Throwable JavaDoc target) {
171         String JavaDoc title =
172             ProjectMessages.getString("ProjectCreationWizard.op_error.title");
173         String JavaDoc message =
174             ProjectMessages.getString("ProjectCreationWizard.op_error.message");
175         if (target instanceof CoreException) {
176             IStatus status = ((CoreException) target).getStatus();
177             ErrorDialog.openError(getShell(), title, message, status);
178             MainPlugin.log(status);
179         } else {
180             MessageDialog.openError(getShell(), title, target.getMessage());
181             MainPlugin.log(target);
182         }
183     }
184
185     /* (non-Javadoc)
186      * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement, java.lang.String, java.lang.Object)
187      */

188     public void setInitializationData(
189         IConfigurationElement cfig,
190         String JavaDoc propertyName,
191         Object JavaDoc data) {
192         fConfigElement = cfig;
193
194     }
195
196     // overwrite dialog
197

198     private class ImportOverwriteQuery implements IOverwriteQuery {
199         public String JavaDoc queryOverwrite(String JavaDoc file) {
200             String JavaDoc[] returnCodes = { YES, NO, ALL, CANCEL };
201             int returnVal = openDialog(file);
202             return returnVal < 0 ? CANCEL : returnCodes[returnVal];
203         }
204
205         private int openDialog(final String JavaDoc file) {
206             final int[] result = { IDialogConstants.CANCEL_ID };
207             getShell().getDisplay().syncExec(new Runnable JavaDoc() {
208                 public void run() {
209                     String JavaDoc title =
210                         ProjectMessages.getString(
211                             "ProjectCreationWizard.overwritequery.title");
212                     String JavaDoc msg =
213                         ProjectMessages.getFormattedString(
214                             "ProjectCreationWizard.overwritequery.message",
215                             file);
216                     String JavaDoc[] options =
217                         {
218                             IDialogConstants.YES_LABEL,
219                             IDialogConstants.NO_LABEL,
220                             IDialogConstants.YES_TO_ALL_LABEL,
221                             IDialogConstants.CANCEL_LABEL };
222                     MessageDialog dialog =
223                         new MessageDialog(
224                             getShell(),
225                             title,
226                             null,
227                             msg,
228                             MessageDialog.QUESTION,
229                             options,
230                             0);
231                     result[0] = dialog.open();
232                 }
233             });
234             return result[0];
235         }
236     }
237
238 }
239
Popular Tags