001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.wizards.newresource;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.jface.resource.ImageDescriptor;
018: import org.eclipse.jface.viewers.ISelection;
019: import org.eclipse.jface.viewers.IStructuredSelection;
020: import org.eclipse.jface.viewers.StructuredSelection;
021: import org.eclipse.jface.wizard.Wizard;
022: import org.eclipse.ui.INewWizard;
023: import org.eclipse.ui.IWorkbench;
024: import org.eclipse.ui.IWorkbenchPage;
025: import org.eclipse.ui.IWorkbenchPart;
026: import org.eclipse.ui.IWorkbenchPartReference;
027: import org.eclipse.ui.IWorkbenchWindow;
028: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
029: import org.eclipse.ui.part.ISetSelectionTarget;
030:
031: /**
032: * Abstract base implementation of the standard workbench wizards
033: * that create new resources in the workspace.
034: * <p>
035: * This class is not intended to be subclassed outside this package.
036: * </p>
037: */
038: public abstract class BasicNewResourceWizard extends Wizard implements
039: INewWizard {
040:
041: /**
042: * The workbench.
043: */
044: private IWorkbench workbench;
045:
046: /**
047: * The current selection.
048: */
049: protected IStructuredSelection selection;
050:
051: /**
052: * Creates an empty wizard for creating a new resource in the workspace.
053: */
054: protected BasicNewResourceWizard() {
055: super ();
056: }
057:
058: /**
059: * Returns the selection which was passed to <code>init</code>.
060: *
061: * @return the selection
062: */
063: public IStructuredSelection getSelection() {
064: return selection;
065: }
066:
067: /**
068: * Returns the workbench which was passed to <code>init</code>.
069: *
070: * @return the workbench
071: */
072: public IWorkbench getWorkbench() {
073: return workbench;
074: }
075:
076: /**
077: * The <code>BasicNewResourceWizard</code> implementation of this
078: * <code>IWorkbenchWizard</code> method records the given workbench and
079: * selection, and initializes the default banner image for the pages
080: * by calling <code>initializeDefaultPageImageDescriptor</code>.
081: * Subclasses may extend.
082: */
083: public void init(IWorkbench workbench,
084: IStructuredSelection currentSelection) {
085: this .workbench = workbench;
086: this .selection = currentSelection;
087:
088: initializeDefaultPageImageDescriptor();
089: }
090:
091: /**
092: * Initializes the default page image descriptor to an appropriate banner.
093: * By calling <code>setDefaultPageImageDescriptor</code>.
094: * The default implementation of this method uses a generic new wizard image.
095: * <p>
096: * Subclasses may reimplement.
097: * </p>
098: */
099: protected void initializeDefaultPageImageDescriptor() {
100: ImageDescriptor desc = IDEWorkbenchPlugin
101: .getIDEImageDescriptor("wizban/new_wiz.png");//$NON-NLS-1$
102: setDefaultPageImageDescriptor(desc);
103: }
104:
105: /**
106: * Selects and reveals the newly added resource in all parts
107: * of the active workbench window's active page.
108: *
109: * @see ISetSelectionTarget
110: */
111: protected void selectAndReveal(IResource newResource) {
112: selectAndReveal(newResource, getWorkbench()
113: .getActiveWorkbenchWindow());
114: }
115:
116: /**
117: * Attempts to select and reveal the specified resource in all
118: * parts within the supplied workbench window's active page.
119: * <p>
120: * Checks all parts in the active page to see if they implement <code>ISetSelectionTarget</code>,
121: * either directly or as an adapter. If so, tells the part to select and reveal the
122: * specified resource.
123: * </p>
124: *
125: * @param resource the resource to be selected and revealed
126: * @param window the workbench window to select and reveal the resource
127: *
128: * @see ISetSelectionTarget
129: */
130: public static void selectAndReveal(IResource resource,
131: IWorkbenchWindow window) {
132: // validate the input
133: if (window == null || resource == null) {
134: return;
135: }
136: IWorkbenchPage page = window.getActivePage();
137: if (page == null) {
138: return;
139: }
140:
141: // get all the view and editor parts
142: List parts = new ArrayList();
143: IWorkbenchPartReference refs[] = page.getViewReferences();
144: for (int i = 0; i < refs.length; i++) {
145: IWorkbenchPart part = refs[i].getPart(false);
146: if (part != null) {
147: parts.add(part);
148: }
149: }
150: refs = page.getEditorReferences();
151: for (int i = 0; i < refs.length; i++) {
152: if (refs[i].getPart(false) != null) {
153: parts.add(refs[i].getPart(false));
154: }
155: }
156:
157: final ISelection selection = new StructuredSelection(resource);
158: Iterator itr = parts.iterator();
159: while (itr.hasNext()) {
160: IWorkbenchPart part = (IWorkbenchPart) itr.next();
161:
162: // get the part's ISetSelectionTarget implementation
163: ISetSelectionTarget target = null;
164: if (part instanceof ISetSelectionTarget) {
165: target = (ISetSelectionTarget) part;
166: } else {
167: target = (ISetSelectionTarget) part
168: .getAdapter(ISetSelectionTarget.class);
169: }
170:
171: if (target != null) {
172: // select and reveal resource
173: final ISetSelectionTarget finalTarget = target;
174: window.getShell().getDisplay().asyncExec(
175: new Runnable() {
176: public void run() {
177: finalTarget.selectReveal(selection);
178: }
179: });
180: }
181: }
182: }
183: }
|