Adding two small options for code formatter (zhil's patch). These features are being...
[phpeclipse.git] / net.sourceforge.phpeclipse / 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
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.resources.IWorkspaceRunnable;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.OperationCanceledException;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.wizard.Wizard;
27 import org.eclipse.swt.widgets.Display;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.ui.INewWizard;
30 import org.eclipse.ui.IWorkbench;
31 import org.eclipse.ui.IWorkbenchPage;
32 import org.eclipse.ui.PartInitException;
33 import org.eclipse.ui.ide.IDE;
34 import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
35
36 public abstract class NewElementWizard extends Wizard implements INewWizard {
37
38         private IWorkbench fWorkbench;
39
40         private IStructuredSelection fSelection;
41
42         public NewElementWizard() {
43                 setNeedsProgressMonitor(true);
44         }
45
46         protected void openResource(final IFile resource) {
47                 final IWorkbenchPage activePage = PHPeclipsePlugin.getActivePage();
48                 if (activePage != null) {
49                         final Display display = getShell().getDisplay();
50                         if (display != null) {
51                                 display.asyncExec(new Runnable() {
52                                         public void run() {
53                                                 try {
54                                                         IDE.openEditor(activePage, resource, true);
55                                                 } catch (PartInitException e) {
56                                                         PHPeclipsePlugin.log(e);
57                                                 }
58                                         }
59                                 });
60                         }
61                 }
62         }
63
64         /**
65          * Subclasses should override to perform the actions of the wizard. This
66          * method is run in the wizard container's context as a workspace runnable.
67          */
68         protected void finishPage(IProgressMonitor monitor)
69                         throws InterruptedException, CoreException {
70         }
71
72         protected void handleFinishException(Shell shell,
73                         InvocationTargetException e) {
74                 String title = NewWizardMessages
75                                 .getString("NewElementWizard.op_error.title"); //$NON-NLS-1$
76                 String message = NewWizardMessages
77                                 .getString("NewElementWizard.op_error.message"); //$NON-NLS-1$
78                 ExceptionHandler.handle(e, shell, title, message);
79         }
80
81         /*
82          * @see Wizard#performFinish
83          */
84         public boolean performFinish() {
85                 IWorkspaceRunnable op = new IWorkspaceRunnable() {
86                         public void run(IProgressMonitor monitor) throws CoreException,
87                                         OperationCanceledException {
88                                 try {
89                                         finishPage(monitor);
90                                 } catch (InterruptedException e) {
91                                         throw new OperationCanceledException(e.getMessage());
92                                 }
93                         }
94                 };
95                 try {
96                         getContainer().run(false, true, new WorkbenchRunnableAdapter(op));
97                 } catch (InvocationTargetException e) {
98                         handleFinishException(getShell(), e);
99                         return false;
100                 } catch (InterruptedException e) {
101                         return false;
102                 }
103                 return true;
104         }
105
106         // protected void warnAboutTypeCommentDeprecation() {
107         // String key= IUIConstants.DIALOGSTORE_TYPECOMMENT_DEPRECATED;
108         // if (OptionalMessageDialog.isDialogEnabled(key)) {
109         // Templates templates= Templates.getInstance();
110         // boolean isOldWorkspace= templates.getTemplates("filecomment").length > 0
111         // && templates.getTemplates("typecomment").length > 0;
112         // //$NON-NLS-1$ //$NON-NLS-2$
113         // if (!isOldWorkspace) {
114         // OptionalMessageDialog.setDialogEnabled(key, false);
115         // }
116         // String title=
117         // NewWizardMessages.getString("NewElementWizard.typecomment.deprecated.title");
118         // //$NON-NLS-1$
119         // String message=
120         // NewWizardMessages.getString("NewElementWizard.typecomment.deprecated.message");
121         // //$NON-NLS-1$
122         // OptionalMessageDialog.open(key, getShell(), title,
123         // OptionalMessageDialog.getDefaultImage(), message,
124         // OptionalMessageDialog.INFORMATION, new String[] {
125         // IDialogConstants.OK_LABEL }, 0);
126         // }
127         // }
128
129         /*
130          * (non-Javadoc)
131          * 
132          * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench,
133          *      org.eclipse.jface.viewers.IStructuredSelection)
134          */
135         public void init(IWorkbench workbench, IStructuredSelection currentSelection) {
136                 fWorkbench = workbench;
137                 fSelection = currentSelection;
138         }
139
140         public IStructuredSelection getSelection() {
141                 return fSelection;
142         }
143
144         public IWorkbench getWorkbench() {
145                 return fWorkbench;
146         }
147
148         protected void selectAndReveal(IResource newResource) {
149                 BasicNewResourceWizard.selectAndReveal(newResource, fWorkbench
150                                 .getActiveWorkbenchWindow());
151         }
152
153 }