]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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; | |
14 | ||
15 | import org.eclipse.swt.widgets.Display; | |
16 | import org.eclipse.swt.widgets.Shell; | |
17 | ||
18 | import org.eclipse.core.runtime.CoreException; | |
19 | import org.eclipse.core.runtime.IProgressMonitor; | |
20 | import org.eclipse.core.runtime.OperationCanceledException; | |
21 | import org.eclipse.core.runtime.jobs.ISchedulingRule; | |
22 | import org.eclipse.core.runtime.jobs.Job; | |
23 | ||
24 | import org.eclipse.core.resources.IFile; | |
25 | import org.eclipse.core.resources.IResource; | |
26 | import org.eclipse.core.resources.IWorkspaceRunnable; | |
27 | import org.eclipse.core.resources.ResourcesPlugin; | |
28 | ||
29 | import org.eclipse.jface.dialogs.IDialogConstants; | |
30 | import org.eclipse.jface.dialogs.MessageDialog; | |
31 | import org.eclipse.jface.layout.PixelConverter; | |
32 | import org.eclipse.jface.operation.IRunnableWithProgress; | |
33 | import org.eclipse.jface.resource.JFaceResources; | |
34 | import org.eclipse.jface.viewers.IStructuredSelection; | |
35 | import org.eclipse.jface.viewers.StructuredSelection; | |
36 | import org.eclipse.jface.window.Window; | |
37 | import org.eclipse.jface.wizard.Wizard; | |
38 | import org.eclipse.jface.wizard.WizardDialog; | |
39 | ||
40 | import org.eclipse.jface.text.templates.persistence.TemplateStore; | |
41 | ||
42 | import org.eclipse.ui.INewWizard; | |
43 | import org.eclipse.ui.IWorkbench; | |
44 | import org.eclipse.ui.IWorkbenchPage; | |
45 | import org.eclipse.ui.PartInitException; | |
46 | import org.eclipse.ui.ide.IDE; | |
47 | import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard; | |
48 | ||
49 | import org.eclipse.jdt.core.ICompilationUnit; | |
50 | import org.eclipse.jdt.core.IJavaElement; | |
51 | import org.eclipse.jdt.core.IType; | |
52 | import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; | |
53 | ||
54 | import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; | |
55 | import org.eclipse.jdt.internal.corext.util.JavaModelUtil; | |
56 | ||
57 | import org.eclipse.jdt.ui.wizards.NewTypeWizardPage; | |
58 | ||
59 | import org.eclipse.jdt.internal.ui.IUIConstants; | |
60 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
61 | import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter; | |
62 | import org.eclipse.jdt.internal.ui.dialogs.OptionalMessageDialog; | |
63 | import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages; | |
64 | import org.eclipse.jdt.internal.ui.text.correction.proposals.NewCUUsingWizardProposal; | |
65 | import org.eclipse.jdt.internal.ui.util.ExceptionHandler; | |
66 | ||
67 | public abstract class NewElementWizard extends Wizard implements INewWizard { | |
68 | ||
69 | private IWorkbench fWorkbench; | |
70 | private IStructuredSelection fSelection; | |
71 | ||
72 | public NewElementWizard() { | |
73 | setNeedsProgressMonitor(true); | |
74 | } | |
75 | ||
76 | protected void openResource(final IFile resource) { | |
77 | final IWorkbenchPage activePage= JavaPlugin.getActivePage(); | |
78 | if (activePage != null) { | |
79 | final Display display= getShell().getDisplay(); | |
80 | if (display != null) { | |
81 | display.asyncExec(new Runnable() { | |
82 | public void run() { | |
83 | try { | |
84 | IDE.openEditor(activePage, resource, true); | |
85 | } catch (PartInitException e) { | |
86 | JavaPlugin.log(e); | |
87 | } | |
88 | } | |
89 | }); | |
90 | } | |
91 | } | |
92 | } | |
93 | ||
94 | /** | |
95 | * Subclasses should override to perform the actions of the wizard. This method is run in the | |
96 | * wizard container's context as a workspace runnable. | |
97 | * | |
98 | * @param monitor the progress monitor | |
99 | * @throws InterruptedException when the operation is cancelled | |
100 | * @throws CoreException if the element cannot be created | |
101 | */ | |
102 | protected abstract void finishPage(IProgressMonitor monitor) throws InterruptedException, CoreException; | |
103 | ||
104 | /** | |
105 | * Returns the scheduling rule for creating the element. | |
106 | * @return returns the scheduling rule | |
107 | */ | |
108 | protected ISchedulingRule getSchedulingRule() { | |
109 | return ResourcesPlugin.getWorkspace().getRoot(); // look all by default | |
110 | } | |
111 | ||
112 | ||
113 | protected boolean canRunForked() { | |
114 | return true; | |
115 | } | |
116 | ||
117 | public abstract IJavaElement getCreatedElement(); | |
118 | ||
119 | protected void handleFinishException(Shell shell, InvocationTargetException e) { | |
120 | String title= NewWizardMessages.NewElementWizard_op_error_title; | |
121 | String message= NewWizardMessages.NewElementWizard_op_error_message; | |
122 | ExceptionHandler.handle(e, shell, title, message); | |
123 | } | |
124 | ||
125 | /* | |
126 | * @see Wizard#performFinish | |
127 | */ | |
128 | @Override | |
129 | public boolean performFinish() { | |
130 | IWorkspaceRunnable op= new IWorkspaceRunnable() { | |
131 | public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException { | |
132 | try { | |
133 | finishPage(monitor); | |
134 | } catch (InterruptedException e) { | |
135 | throw new OperationCanceledException(e.getMessage()); | |
136 | } | |
137 | } | |
138 | }; | |
139 | try { | |
140 | ISchedulingRule rule= null; | |
141 | Job job= Job.getJobManager().currentJob(); | |
142 | if (job != null) | |
143 | rule= job.getRule(); | |
144 | IRunnableWithProgress runnable= null; | |
145 | if (rule != null) | |
146 | runnable= new WorkbenchRunnableAdapter(op, rule, true); | |
147 | else | |
148 | runnable= new WorkbenchRunnableAdapter(op, getSchedulingRule()); | |
149 | getContainer().run(canRunForked(), true, runnable); | |
150 | } catch (InvocationTargetException e) { | |
151 | handleFinishException(getShell(), e); | |
152 | return false; | |
153 | } catch (InterruptedException e) { | |
154 | return false; | |
155 | } | |
156 | return true; | |
157 | } | |
158 | ||
159 | protected void warnAboutTypeCommentDeprecation() { | |
160 | String key= IUIConstants.DIALOGSTORE_TYPECOMMENT_DEPRECATED; | |
161 | if (OptionalMessageDialog.isDialogEnabled(key)) { | |
162 | TemplateStore templates= JavaPlugin.getDefault().getTemplateStore(); | |
163 | boolean isOldWorkspace= templates.findTemplate("filecomment") != null && templates.findTemplate("typecomment") != null; //$NON-NLS-1$ //$NON-NLS-2$ | |
164 | if (!isOldWorkspace) { | |
165 | OptionalMessageDialog.setDialogEnabled(key, false); | |
166 | } | |
167 | String title= NewWizardMessages.NewElementWizard_typecomment_deprecated_title; | |
168 | String message= NewWizardMessages.NewElementWizard_typecomment_deprecated_message; | |
169 | OptionalMessageDialog.open(key, getShell(), title, null, message, MessageDialog.INFORMATION, new String[] { IDialogConstants.OK_LABEL }, 0); | |
170 | } | |
171 | } | |
172 | ||
173 | /* (non-Javadoc) | |
174 | * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection) | |
175 | */ | |
176 | public void init(IWorkbench workbench, IStructuredSelection currentSelection) { | |
177 | fWorkbench= workbench; | |
178 | fSelection= currentSelection; | |
179 | } | |
180 | ||
181 | public IStructuredSelection getSelection() { | |
182 | return fSelection; | |
183 | } | |
184 | ||
185 | public IWorkbench getWorkbench() { | |
186 | return fWorkbench; | |
187 | } | |
188 | ||
189 | protected void selectAndReveal(IResource newResource) { | |
190 | BasicNewResourceWizard.selectAndReveal(newResource, fWorkbench.getActiveWorkbenchWindow()); | |
191 | } | |
192 | ||
193 | public void generated_904266101912896755(StructuredSelection selection, NewCUUsingWizardProposal newcuusingwizardproposal) { | |
194 | init(JavaPlugin.getDefault().getWorkbench(), selection); | |
195 | ||
196 | IType createdType= null; | |
197 | ||
198 | if (newcuusingwizardproposal.fShowDialog) { | |
199 | Shell shell= JavaPlugin.getActiveWorkbenchShell(); | |
200 | WizardDialog dialog= new WizardDialog(shell, this); | |
201 | ||
202 | PixelConverter converter= new PixelConverter(JFaceResources.getDialogFont()); | |
203 | dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70), converter.convertHeightInCharsToPixels(20)); | |
204 | dialog.create(); | |
205 | dialog.getShell().setText(CorrectionMessages.NewCUCompletionUsingWizardProposal_dialogtitle); | |
206 | ||
207 | if (dialog.open() == Window.OK) { | |
208 | createdType= (IType) getCreatedElement(); | |
209 | } | |
210 | } else { | |
211 | addPages(); | |
212 | try { | |
213 | NewTypeWizardPage page= newcuusingwizardproposal.getPage(this); | |
214 | page.createType(null); | |
215 | createdType= page.getCreatedType(); | |
216 | } catch (CoreException e) { | |
217 | JavaPlugin.log(e); | |
218 | } catch (InterruptedException e) { | |
219 | } | |
220 | } | |
221 | ||
222 | if (createdType != null) { | |
223 | IJavaElement container= createdType.getParent(); | |
224 | if (container instanceof ICompilationUnit) { | |
225 | container= container.getParent(); | |
226 | } | |
227 | if (!container.equals(newcuusingwizardproposal.fTypeContainer)) { | |
228 | // add import | |
229 | try { | |
230 | ImportRewrite rewrite= StubUtility.createImportRewrite(newcuusingwizardproposal.fCompilationUnit, true); | |
231 | rewrite.addImport(createdType.getFullyQualifiedName('.')); | |
232 | JavaModelUtil.applyEdit(newcuusingwizardproposal.fCompilationUnit, rewrite.rewriteImports(null), false, null); | |
233 | } catch (CoreException e) { | |
234 | } | |
235 | } | |
236 | newcuusingwizardproposal.fCreatedType= createdType; | |
237 | } | |
238 | } | |
239 | ||
240 | } |