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