KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.IProgressMonitor;
17 import org.eclipse.core.runtime.OperationCanceledException;
18 import org.eclipse.core.runtime.jobs.ISchedulingRule;
19 import org.eclipse.core.runtime.jobs.Job;
20
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.IWorkspaceRunnable;
24 import org.eclipse.core.resources.ResourcesPlugin;
25
26 import org.eclipse.swt.widgets.Display;
27 import org.eclipse.swt.widgets.Shell;
28
29 import org.eclipse.jface.dialogs.IDialogConstants;
30 import org.eclipse.jface.dialogs.MessageDialog;
31 import org.eclipse.jface.operation.IRunnableWithProgress;
32 import org.eclipse.jface.viewers.IStructuredSelection;
33 import org.eclipse.jface.wizard.Wizard;
34
35 import org.eclipse.jface.text.templates.persistence.TemplateStore;
36
37 import org.eclipse.ui.INewWizard;
38 import org.eclipse.ui.IWorkbench;
39 import org.eclipse.ui.IWorkbenchPage;
40 import org.eclipse.ui.PartInitException;
41 import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
42
43 import org.eclipse.ui.ide.IDE;
44
45 import org.eclipse.jdt.core.IJavaElement;
46
47 import org.eclipse.jdt.internal.ui.IUIConstants;
48 import org.eclipse.jdt.internal.ui.JavaPlugin;
49 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
50 import org.eclipse.jdt.internal.ui.dialogs.OptionalMessageDialog;
51 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
52
53 public abstract class NewElementWizard extends Wizard implements INewWizard {
54
55     private IWorkbench fWorkbench;
56     private IStructuredSelection fSelection;
57
58     public NewElementWizard() {
59         setNeedsProgressMonitor(true);
60     }
61             
62     protected void openResource(final IFile resource) {
63         final IWorkbenchPage activePage= JavaPlugin.getActivePage();
64         if (activePage != null) {
65             final Display display= getShell().getDisplay();
66             if (display != null) {
67                 display.asyncExec(new Runnable JavaDoc() {
68                     public void run() {
69                         try {
70                             IDE.openEditor(activePage, resource, true);
71                         } catch (PartInitException e) {
72                             JavaPlugin.log(e);
73                         }
74                     }
75                 });
76             }
77         }
78     }
79     
80     /**
81      * Subclasses should override to perform the actions of the wizard.
82      * This method is run in the wizard container's context as a workspace runnable.
83      * @param monitor
84      * @throws InterruptedException
85      * @throws CoreException
86      */

87     protected abstract void finishPage(IProgressMonitor monitor) throws InterruptedException JavaDoc, CoreException;
88     
89     /**
90      * Returns the scheduling rule for creating the element.
91      */

92     protected ISchedulingRule getSchedulingRule() {
93         return ResourcesPlugin.getWorkspace().getRoot(); // look all by default
94
}
95     
96     
97     protected boolean canRunForked() {
98         return true;
99     }
100     
101     public abstract IJavaElement getCreatedElement();
102     
103     protected void handleFinishException(Shell shell, InvocationTargetException JavaDoc e) {
104         String JavaDoc title= NewWizardMessages.NewElementWizard_op_error_title;
105         String JavaDoc message= NewWizardMessages.NewElementWizard_op_error_message;
106         ExceptionHandler.handle(e, shell, title, message);
107     }
108     
109     /*
110      * @see Wizard#performFinish
111      */

112     public boolean performFinish() {
113         IWorkspaceRunnable op= new IWorkspaceRunnable() {
114             public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
115                 try {
116                     finishPage(monitor);
117                 } catch (InterruptedException JavaDoc e) {
118                     throw new OperationCanceledException(e.getMessage());
119                 }
120             }
121         };
122         try {
123             ISchedulingRule rule= null;
124             Job job= Job.getJobManager().currentJob();
125             if (job != null)
126                 rule= job.getRule();
127             IRunnableWithProgress runnable= null;
128             if (rule != null)
129                 runnable= new WorkbenchRunnableAdapter(op, rule, true);
130             else
131                 runnable= new WorkbenchRunnableAdapter(op, getSchedulingRule());
132             getContainer().run(canRunForked(), true, runnable);
133         } catch (InvocationTargetException JavaDoc e) {
134             handleFinishException(getShell(), e);
135             return false;
136         } catch (InterruptedException JavaDoc e) {
137             return false;
138         }
139         return true;
140     }
141     
142     protected void warnAboutTypeCommentDeprecation() {
143         String JavaDoc key= IUIConstants.DIALOGSTORE_TYPECOMMENT_DEPRECATED;
144         if (OptionalMessageDialog.isDialogEnabled(key)) {
145             TemplateStore templates= JavaPlugin.getDefault().getTemplateStore();
146             boolean isOldWorkspace= templates.findTemplate("filecomment") != null && templates.findTemplate("typecomment") != null; //$NON-NLS-1$ //$NON-NLS-2$
147
if (!isOldWorkspace) {
148                 OptionalMessageDialog.setDialogEnabled(key, false);
149             }
150             String JavaDoc title= NewWizardMessages.NewElementWizard_typecomment_deprecated_title;
151             String JavaDoc message= NewWizardMessages.NewElementWizard_typecomment_deprecated_message;
152             OptionalMessageDialog.open(key, getShell(), title, null, message, MessageDialog.INFORMATION, new String JavaDoc[] { IDialogConstants.OK_LABEL }, 0);
153         }
154     }
155
156     /* (non-Javadoc)
157      * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
158      */

159     public void init(IWorkbench workbench, IStructuredSelection currentSelection) {
160         fWorkbench= workbench;
161         fSelection= currentSelection;
162     }
163     
164     public IStructuredSelection getSelection() {
165         return fSelection;
166     }
167
168     public IWorkbench getWorkbench() {
169         return fWorkbench;
170     }
171
172     protected void selectAndReveal(IResource newResource) {
173         BasicNewResourceWizard.selectAndReveal(newResource, fWorkbench.getActiveWorkbenchWindow());
174     }
175
176 }
177
Popular Tags