KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > JavaProjectWizard


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.wizards;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.IExecutableExtension;
18 import org.eclipse.core.runtime.IProgressMonitor;
19
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.Shell;
22
23 import org.eclipse.jface.viewers.IStructuredSelection;
24
25 import org.eclipse.ui.IWorkbenchPage;
26 import org.eclipse.ui.IWorkingSet;
27 import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;
28
29 import org.eclipse.jdt.core.IJavaElement;
30 import org.eclipse.jdt.core.JavaCore;
31
32 import org.eclipse.jdt.internal.ui.JavaPlugin;
33 import org.eclipse.jdt.internal.ui.JavaPluginImages;
34 import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
35 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
36 import org.eclipse.jdt.internal.ui.workingsets.JavaWorkingSetUpdater;
37 import org.eclipse.jdt.internal.ui.workingsets.ViewActionGroup;
38 import org.eclipse.jdt.internal.ui.workingsets.WorkingSetConfigurationBlock;
39
40 public class JavaProjectWizard extends NewElementWizard implements IExecutableExtension {
41     
42     private JavaProjectWizardFirstPage fFirstPage;
43     private JavaProjectWizardSecondPage fSecondPage;
44     
45     private IConfigurationElement fConfigElement;
46     
47     public JavaProjectWizard() {
48         setDefaultPageImageDescriptor(JavaPluginImages.DESC_WIZBAN_NEWJPRJ);
49         setDialogSettings(JavaPlugin.getDefault().getDialogSettings());
50         setWindowTitle(NewWizardMessages.JavaProjectWizard_title);
51     }
52
53     /*
54      * @see Wizard#addPages
55      */

56     public void addPages() {
57         super.addPages();
58         fFirstPage= new JavaProjectWizardFirstPage();
59         fFirstPage.setWorkingSets(getWorkingSets(getSelection()));
60         addPage(fFirstPage);
61         fSecondPage= new JavaProjectWizardSecondPage(fFirstPage);
62         addPage(fSecondPage);
63     }
64     
65     /* (non-Javadoc)
66      * @see org.eclipse.jdt.internal.ui.wizards.NewElementWizard#finishPage(org.eclipse.core.runtime.IProgressMonitor)
67      */

68     protected void finishPage(IProgressMonitor monitor) throws InterruptedException JavaDoc, CoreException {
69         fSecondPage.performFinish(monitor); // use the full progress monitor
70
}
71        
72     /* (non-Javadoc)
73      * @see org.eclipse.jface.wizard.IWizard#performFinish()
74      */

75     public boolean performFinish() {
76         boolean res= super.performFinish();
77         if (res) {
78             final IJavaElement newElement= getCreatedElement();
79             
80             IWorkingSet[] workingSets= fFirstPage.getWorkingSets();
81             WorkingSetConfigurationBlock.addToWorkingSets(newElement, workingSets);
82             
83             BasicNewProjectResourceWizard.updatePerspective(fConfigElement);
84             selectAndReveal(fSecondPage.getJavaProject().getProject());
85             
86             Display.getDefault().asyncExec(new Runnable JavaDoc() {
87                 public void run() {
88                     PackageExplorerPart activePackageExplorer= getActivePackageExplorer();
89                     if (activePackageExplorer != null) {
90                         activePackageExplorer.tryToReveal(newElement);
91                     }
92                 }
93             });
94         }
95         return res;
96     }
97
98     protected void handleFinishException(Shell shell, InvocationTargetException JavaDoc e) {
99         String JavaDoc title= NewWizardMessages.JavaProjectWizard_op_error_title;
100         String JavaDoc message= NewWizardMessages.JavaProjectWizard_op_error_create_message;
101         ExceptionHandler.handle(e, getShell(), title, message);
102     }
103     
104     /*
105      * Stores the configuration element for the wizard. The config element will be used
106      * in <code>performFinish</code> to set the result perspective.
107      */

108     public void setInitializationData(IConfigurationElement cfig, String JavaDoc propertyName, Object JavaDoc data) {
109         fConfigElement= cfig;
110     }
111     
112     /* (non-Javadoc)
113      * @see IWizard#performCancel()
114      */

115     public boolean performCancel() {
116         fSecondPage.performCancel();
117         return super.performCancel();
118     }
119     
120     /* (non-Javadoc)
121      * @see org.eclipse.jdt.internal.ui.wizards.NewElementWizard#getCreatedElement()
122      */

123     public IJavaElement getCreatedElement() {
124         return JavaCore.create(fFirstPage.getProjectHandle());
125     }
126     
127     private IWorkingSet[] getWorkingSets(IStructuredSelection selection) {
128         IWorkingSet[] selected= WorkingSetConfigurationBlock.getSelectedWorkingSet(selection);
129         if (selected != null && selected.length > 0) {
130             for (int i= 0; i < selected.length; i++) {
131                 if (!isValidWorkingSet(selected[i]))
132                     return null;
133             }
134             return selected;
135         }
136         
137         PackageExplorerPart explorerPart= getActivePackageExplorer();
138         if (explorerPart == null)
139             return null;
140         
141         if (explorerPart.getRootMode() == ViewActionGroup.SHOW_PROJECTS) {
142             //Get active filter
143
IWorkingSet filterWorkingSet= explorerPart.getFilterWorkingSet();
144             if (filterWorkingSet == null)
145                 return null;
146             
147             if (!isValidWorkingSet(filterWorkingSet))
148                 return null;
149             
150             return new IWorkingSet[] {filterWorkingSet};
151         } else if (explorerPart.getRootMode() == ViewActionGroup.SHOW_WORKING_SETS) {
152             //If we have been gone into a working set return the working set
153
Object JavaDoc input= explorerPart.getViewPartInput();
154             if (!(input instanceof IWorkingSet))
155                 return null;
156             
157             IWorkingSet workingSet= (IWorkingSet)input;
158             if (!isValidWorkingSet(workingSet))
159                 return null;
160             
161             return new IWorkingSet[] {workingSet};
162         }
163         
164         return null;
165     }
166
167     private PackageExplorerPart getActivePackageExplorer() {
168         PackageExplorerPart explorerPart= PackageExplorerPart.getFromActivePerspective();
169         if (explorerPart == null)
170             return null;
171         
172         IWorkbenchPage activePage= explorerPart.getViewSite().getWorkbenchWindow().getActivePage();
173         if (activePage == null)
174             return null;
175         
176         if (activePage.getActivePart() != explorerPart)
177             return null;
178         
179         return explorerPart;
180     }
181
182     private boolean isValidWorkingSet(IWorkingSet workingSet) {
183         String JavaDoc id= workingSet.getId();
184         if (!JavaWorkingSetUpdater.ID.equals(id) && !"org.eclipse.ui.resourceWorkingSetPage".equals(id)) //$NON-NLS-1$
185
return false;
186         
187         if (workingSet.isAggregateWorkingSet())
188             return false;
189         
190         return true;
191     }
192     
193 }
194
Popular Tags