KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > wizards > newresource > BasicNewResourceWizard


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.wizards.newresource;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.jface.wizard.Wizard;
23 import org.eclipse.ui.INewWizard;
24 import org.eclipse.ui.IWorkbench;
25 import org.eclipse.ui.IWorkbenchPage;
26 import org.eclipse.ui.IWorkbenchPart;
27 import org.eclipse.ui.IWorkbenchPartReference;
28 import org.eclipse.ui.IWorkbenchWindow;
29 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
30 import org.eclipse.ui.part.ISetSelectionTarget;
31
32 /**
33  * Abstract base implementation of the standard workbench wizards
34  * that create new resources in the workspace.
35  * <p>
36  * This class is not intended to be subclassed outside this package.
37  * </p>
38  */

39 public abstract class BasicNewResourceWizard extends Wizard implements
40         INewWizard {
41
42     /**
43      * The workbench.
44      */

45     private IWorkbench workbench;
46
47     /**
48      * The current selection.
49      */

50     protected IStructuredSelection selection;
51
52     /**
53      * Creates an empty wizard for creating a new resource in the workspace.
54      */

55     protected BasicNewResourceWizard() {
56         super();
57     }
58
59     /**
60      * Returns the selection which was passed to <code>init</code>.
61      *
62      * @return the selection
63      */

64     public IStructuredSelection getSelection() {
65         return selection;
66     }
67
68     /**
69      * Returns the workbench which was passed to <code>init</code>.
70      *
71      * @return the workbench
72      */

73     public IWorkbench getWorkbench() {
74         return workbench;
75     }
76
77     /**
78      * The <code>BasicNewResourceWizard</code> implementation of this
79      * <code>IWorkbenchWizard</code> method records the given workbench and
80      * selection, and initializes the default banner image for the pages
81      * by calling <code>initializeDefaultPageImageDescriptor</code>.
82      * Subclasses may extend.
83      */

84     public void init(IWorkbench workbench, IStructuredSelection currentSelection) {
85         this.workbench = workbench;
86         this.selection = currentSelection;
87
88         initializeDefaultPageImageDescriptor();
89     }
90
91     /**
92      * Initializes the default page image descriptor to an appropriate banner.
93      * By calling <code>setDefaultPageImageDescriptor</code>.
94      * The default implementation of this method uses a generic new wizard image.
95      * <p>
96      * Subclasses may reimplement.
97      * </p>
98      */

99     protected void initializeDefaultPageImageDescriptor() {
100         ImageDescriptor desc = IDEWorkbenchPlugin.getIDEImageDescriptor("wizban/new_wiz.png");//$NON-NLS-1$
101
setDefaultPageImageDescriptor(desc);
102     }
103
104     /**
105      * Selects and reveals the newly added resource in all parts
106      * of the active workbench window's active page.
107      *
108      * @see ISetSelectionTarget
109      */

110     protected void selectAndReveal(IResource newResource) {
111         selectAndReveal(newResource, getWorkbench().getActiveWorkbenchWindow());
112     }
113
114     /**
115      * Attempts to select and reveal the specified resource in all
116      * parts within the supplied workbench window's active page.
117      * <p>
118      * Checks all parts in the active page to see if they implement <code>ISetSelectionTarget</code>,
119      * either directly or as an adapter. If so, tells the part to select and reveal the
120      * specified resource.
121      * </p>
122      *
123      * @param resource the resource to be selected and revealed
124      * @param window the workbench window to select and reveal the resource
125      *
126      * @see ISetSelectionTarget
127      */

128     public static void selectAndReveal(IResource resource,
129             IWorkbenchWindow window) {
130         // validate the input
131
if (window == null || resource == null) {
132             return;
133         }
134         IWorkbenchPage page = window.getActivePage();
135         if (page == null) {
136             return;
137         }
138
139         // get all the view and editor parts
140
List JavaDoc parts = new ArrayList JavaDoc();
141         IWorkbenchPartReference refs[] = page.getViewReferences();
142         for (int i = 0; i < refs.length; i++) {
143             IWorkbenchPart part = refs[i].getPart(false);
144             if (part != null) {
145                 parts.add(part);
146             }
147         }
148         refs = page.getEditorReferences();
149         for (int i = 0; i < refs.length; i++) {
150             if (refs[i].getPart(false) != null) {
151                 parts.add(refs[i].getPart(false));
152             }
153         }
154
155         final ISelection selection = new StructuredSelection(resource);
156         Iterator JavaDoc itr = parts.iterator();
157         while (itr.hasNext()) {
158             IWorkbenchPart part = (IWorkbenchPart) itr.next();
159
160             // get the part's ISetSelectionTarget implementation
161
ISetSelectionTarget target = null;
162             if (part instanceof ISetSelectionTarget) {
163                 target = (ISetSelectionTarget) part;
164             } else {
165                 target = (ISetSelectionTarget) part
166                         .getAdapter(ISetSelectionTarget.class);
167             }
168
169             if (target != null) {
170                 // select and reveal resource
171
final ISetSelectionTarget finalTarget = target;
172                 window.getShell().getDisplay().asyncExec(new Runnable JavaDoc() {
173                     public void run() {
174                         finalTarget.selectReveal(selection);
175                     }
176                 });
177             }
178         }
179     }
180 }
181
Popular Tags