KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > wizards > JUnitWizard


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.junit.wizards;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.ResourcesPlugin;
18
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.Shell;
21
22 import org.eclipse.jface.dialogs.DialogSettings;
23 import org.eclipse.jface.dialogs.IDialogSettings;
24 import org.eclipse.jface.operation.IRunnableWithProgress;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.wizard.Wizard;
27
28 import org.eclipse.ui.INewWizard;
29 import org.eclipse.ui.IWorkbench;
30 import org.eclipse.ui.IWorkbenchPage;
31 import org.eclipse.ui.PartInitException;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation;
34 import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
35
36 import org.eclipse.ui.ide.IDE;
37
38 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin;
39 import org.eclipse.jdt.internal.junit.util.ExceptionHandler;
40
41 /**
42  * The wizard base class for JUnit creation wizards.
43  */

44 public abstract class JUnitWizard extends Wizard implements INewWizard {
45
46     private IWorkbench fWorkbench;
47     protected static final String JavaDoc DIALOG_SETTINGS_KEY= "JUnitWizards"; //$NON-NLS-1$
48
private IStructuredSelection fSelection;
49
50     public JUnitWizard() {
51         setNeedsProgressMonitor(true);
52         initializeDefaultPageImageDescriptor();
53     }
54     
55     /*
56      * @see IWizard#performFinish()
57      */

58     public abstract boolean performFinish();
59
60     /*
61      * Run a runnable
62      */

63     protected boolean finishPage(IRunnableWithProgress runnable) {
64         IRunnableWithProgress op= new WorkspaceModifyDelegatingOperation(runnable);
65         try {
66             PlatformUI.getWorkbench().getProgressService().runInUI(getContainer(), op, ResourcesPlugin.getWorkspace().getRoot());
67             
68         } catch (InvocationTargetException JavaDoc e) {
69             Shell shell= getShell();
70             String JavaDoc title= WizardMessages.NewJUnitWizard_op_error_title;
71             String JavaDoc message= WizardMessages.NewJUnitWizard_op_error_message;
72             ExceptionHandler.handle(e, shell, title, message);
73             return false;
74         } catch (InterruptedException JavaDoc e) {
75             return false;
76         }
77         return true;
78     }
79
80     protected void openResource(final IResource resource) {
81         if (resource.getType() == IResource.FILE) {
82             final IWorkbenchPage activePage= JUnitPlugin.getActivePage();
83             if (activePage != null) {
84                 final Display display= Display.getDefault();
85                 if (display != null) {
86                     display.asyncExec(new Runnable JavaDoc() {
87                         public void run() {
88                             try {
89                                 IDE.openEditor(activePage, (IFile)resource, true);
90                             } catch (PartInitException e) {
91                                 JUnitPlugin.log(e);
92                             }
93                         }
94                     });
95                 }
96             }
97         }
98     }
99
100     /* (non-Javadoc)
101      * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
102      */

103     public void init(IWorkbench workbench, IStructuredSelection currentSelection) {
104         fWorkbench= workbench;
105         fSelection= currentSelection;
106     }
107     
108     public IStructuredSelection getSelection() {
109         return fSelection;
110     }
111
112     protected void selectAndReveal(IResource newResource) {
113         BasicNewResourceWizard.selectAndReveal(newResource, fWorkbench.getActiveWorkbenchWindow());
114     }
115     
116     protected void initDialogSettings() {
117         IDialogSettings pluginSettings= JUnitPlugin.getDefault().getDialogSettings();
118         IDialogSettings wizardSettings= pluginSettings.getSection(DIALOG_SETTINGS_KEY);
119         if (wizardSettings == null) {
120             wizardSettings= new DialogSettings(DIALOG_SETTINGS_KEY);
121             pluginSettings.addSection(wizardSettings);
122         }
123         setDialogSettings(wizardSettings);
124     }
125
126     protected abstract void initializeDefaultPageImageDescriptor();
127 }
128
Popular Tags