refactory: added UI removed from core plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / wizards / NewElementWizard.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.wizards;
12
13 import java.lang.reflect.InvocationTargetException;
14
15 import net.sourceforge.phpdt.internal.ui.actions.WorkbenchRunnableAdapter;
16 import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18 import net.sourceforge.phpeclipse.ui.WebUI;
19
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.IWorkspaceRunnable;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.core.runtime.OperationCanceledException;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.wizard.Wizard;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.ui.INewWizard;
31 import org.eclipse.ui.IWorkbench;
32 import org.eclipse.ui.IWorkbenchPage;
33 import org.eclipse.ui.PartInitException;
34 import org.eclipse.ui.ide.IDE;
35 import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
36
37 public abstract class NewElementWizard extends Wizard implements INewWizard {
38
39         private IWorkbench fWorkbench;
40
41         private IStructuredSelection fSelection;
42
43         public NewElementWizard() {
44                 setNeedsProgressMonitor(true);
45         }
46
47         protected void openResource(final IFile resource) {
48                 final IWorkbenchPage activePage = WebUI.getActivePage();
49                 if (activePage != null) {
50                         final Display display = getShell().getDisplay();
51                         if (display != null) {
52                                 display.asyncExec(new Runnable() {
53                                         public void run() {
54                                                 try {
55                                                         IDE.openEditor(activePage, resource, true);
56                                                 } catch (PartInitException e) {
57                                                         PHPeclipsePlugin.log(e);
58                                                 }
59                                         }
60                                 });
61                         }
62                 }
63         }
64
65         /**
66          * Subclasses should override to perform the actions of the wizard. This
67          * method is run in the wizard container's context as a workspace runnable.
68          */
69         protected void finishPage(IProgressMonitor monitor)
70                         throws InterruptedException, CoreException {
71         }
72
73         protected void handleFinishException(Shell shell,
74                         InvocationTargetException e) {
75                 String title = NewWizardMessages
76                                 .getString("NewElementWizard.op_error.title"); //$NON-NLS-1$
77                 String message = NewWizardMessages
78                                 .getString("NewElementWizard.op_error.message"); //$NON-NLS-1$
79                 ExceptionHandler.handle(e, shell, title, message);
80         }
81
82         /*
83          * @see Wizard#performFinish
84          */
85         public boolean performFinish() {
86                 IWorkspaceRunnable op = new IWorkspaceRunnable() {
87                         public void run(IProgressMonitor monitor) throws CoreException,
88                                         OperationCanceledException {
89                                 try {
90                                         finishPage(monitor);
91                                 } catch (InterruptedException e) {
92                                         throw new OperationCanceledException(e.getMessage());
93                                 }
94                         }
95                 };
96                 try {
97                         getContainer().run(false, true, new WorkbenchRunnableAdapter(op));
98                 } catch (InvocationTargetException e) {
99                         handleFinishException(getShell(), e);
100                         return false;
101                 } catch (InterruptedException e) {
102                         return false;
103                 }
104                 return true;
105         }
106
107         // protected void warnAboutTypeCommentDeprecation() {
108         // String key= IUIConstants.DIALOGSTORE_TYPECOMMENT_DEPRECATED;
109         // if (OptionalMessageDialog.isDialogEnabled(key)) {
110         // Templates templates= Templates.getInstance();
111         // boolean isOldWorkspace= templates.getTemplates("filecomment").length > 0
112         // && templates.getTemplates("typecomment").length > 0;
113         // //$NON-NLS-1$ //$NON-NLS-2$
114         // if (!isOldWorkspace) {
115         // OptionalMessageDialog.setDialogEnabled(key, false);
116         // }
117         // String title=
118         // NewWizardMessages.getString("NewElementWizard.typecomment.deprecated.title");
119         // //$NON-NLS-1$
120         // String message=
121         // NewWizardMessages.getString("NewElementWizard.typecomment.deprecated.message");
122         // //$NON-NLS-1$
123         // OptionalMessageDialog.open(key, getShell(), title,
124         // OptionalMessageDialog.getDefaultImage(), message,
125         // OptionalMessageDialog.INFORMATION, new String[] {
126         // IDialogConstants.OK_LABEL }, 0);
127         // }
128         // }
129
130         /*
131          * (non-Javadoc)
132          * 
133          * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench,
134          *      org.eclipse.jface.viewers.IStructuredSelection)
135          */
136         public void init(IWorkbench workbench, IStructuredSelection currentSelection) {
137                 fWorkbench = workbench;
138                 fSelection = currentSelection;
139         }
140
141         public IStructuredSelection getSelection() {
142                 return fSelection;
143         }
144
145         public IWorkbench getWorkbench() {
146                 return fWorkbench;
147         }
148
149         protected void selectAndReveal(IResource newResource) {
150                 BasicNewResourceWizard.selectAndReveal(newResource, fWorkbench
151                                 .getActiveWorkbenchWindow());
152         }
153
154 }