001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.handlers;
011:
012: import java.util.Map;
013:
014: import org.eclipse.core.commands.AbstractHandler;
015: import org.eclipse.core.commands.ExecutionEvent;
016: import org.eclipse.core.commands.ExecutionException;
017: import org.eclipse.jface.window.Window;
018: import org.eclipse.ui.IViewPart;
019: import org.eclipse.ui.IViewReference;
020: import org.eclipse.ui.IWorkbenchPage;
021: import org.eclipse.ui.IWorkbenchWindow;
022: import org.eclipse.ui.PartInitException;
023: import org.eclipse.ui.internal.FastViewBar;
024: import org.eclipse.ui.internal.Perspective;
025: import org.eclipse.ui.internal.WorkbenchMessages;
026: import org.eclipse.ui.internal.WorkbenchPage;
027: import org.eclipse.ui.internal.WorkbenchPlugin;
028: import org.eclipse.ui.internal.dialogs.ShowViewDialog;
029: import org.eclipse.ui.internal.misc.StatusUtil;
030: import org.eclipse.ui.statushandlers.StatusManager;
031: import org.eclipse.ui.views.IViewDescriptor;
032:
033: /**
034: * Shows the given view. If no view is specified in the parameters, then this
035: * opens the view selection dialog.
036: *
037: * @since 3.1
038: */
039: public final class ShowViewHandler extends AbstractHandler {
040:
041: /**
042: * The name of the parameter providing the view identifier.
043: */
044: private static final String PARAMETER_NAME_VIEW_ID = "org.eclipse.ui.views.showView.viewId"; //$NON-NLS-1$
045: private boolean makeFast = false;
046: private static final String PARAMETER_MAKE_FAST = "org.eclipse.ui.views.showView.makeFast"; //$NON-NLS-1$
047:
048: /**
049: * Creates a new ShowViewHandler that will open the view in its default location.
050: */
051: public ShowViewHandler() {
052: }
053:
054: /**
055: * Creates a new ShowViewHandler that will optionally force the view to become
056: * a fast view.
057: *
058: * @param makeFast if true, the view will be moved to the fast view bar (even if it already
059: * exists elsewhere). If false, the view will be shown in its default location. Calling with
060: * false is equivalent to using the default constructor.
061: */
062: public ShowViewHandler(boolean makeFast) {
063: this .makeFast = makeFast;
064: }
065:
066: public final Object execute(final ExecutionEvent event)
067: throws ExecutionException {
068: IWorkbenchWindow window = HandlerUtil
069: .getActiveWorkbenchWindowChecked(event);
070: // Get the view identifier, if any.
071: final Map parameters = event.getParameters();
072: final Object value = parameters.get(PARAMETER_NAME_VIEW_ID);
073: makeFast = "true".equals(parameters.get(PARAMETER_MAKE_FAST)); //$NON-NLS-1$
074:
075: if (value == null) {
076: openOther(window);
077: } else {
078: try {
079: openView((String) value, window);
080: } catch (PartInitException e) {
081: throw new ExecutionException(
082: "Part could not be initialized", e); //$NON-NLS-1$
083: }
084: }
085:
086: return null;
087: }
088:
089: /**
090: * Opens a view selection dialog, allowing the user to chose a view.
091: */
092: private final void openOther(final IWorkbenchWindow window) {
093: final IWorkbenchPage page = window.getActivePage();
094: if (page == null) {
095: return;
096: }
097:
098: final ShowViewDialog dialog = new ShowViewDialog(window,
099: WorkbenchPlugin.getDefault().getViewRegistry());
100: dialog.open();
101:
102: if (dialog.getReturnCode() == Window.CANCEL) {
103: return;
104: }
105:
106: final IViewDescriptor[] descriptors = dialog.getSelection();
107: for (int i = 0; i < descriptors.length; ++i) {
108: try {
109: openView(descriptors[i].getId(), window);
110: } catch (PartInitException e) {
111: StatusUtil.handleStatus(e.getStatus(),
112: WorkbenchMessages.ShowView_errorTitle
113: + ": " + e.getMessage(), //$NON-NLS-1$
114: StatusManager.SHOW);
115: }
116: }
117: }
118:
119: /**
120: * Opens the view with the given identifier.
121: *
122: * @param viewId
123: * The view to open; must not be <code>null</code>
124: * @throws PartInitException
125: * If the part could not be initialized.
126: */
127: private final void openView(final String viewId,
128: final IWorkbenchWindow activeWorkbenchWindow)
129: throws PartInitException {
130:
131: final IWorkbenchPage activePage = activeWorkbenchWindow
132: .getActivePage();
133: if (activePage == null) {
134: return;
135: }
136:
137: if (makeFast) {
138: WorkbenchPage wp = (WorkbenchPage) activePage;
139: Perspective persp = wp.getActivePerspective();
140:
141: // If we're making a fast view then use the new mechanism directly
142: boolean useNewMinMax = Perspective.useNewMinMax(persp);
143: if (useNewMinMax) {
144: IViewReference ref = persp.getViewReference(viewId,
145: null);
146: if (ref == null)
147: return;
148:
149: persp.getFastViewManager().addViewReference(
150: FastViewBar.FASTVIEWBAR_ID, -1, ref, true);
151: wp.activate(ref.getPart(true));
152:
153: return;
154: }
155:
156: IViewReference ref = wp.findViewReference(viewId);
157:
158: if (ref == null) {
159: IViewPart part = wp.showView(viewId, null,
160: IWorkbenchPage.VIEW_CREATE);
161: ref = (IViewReference) wp.getReference(part);
162: }
163:
164: if (!wp.isFastView(ref)) {
165: wp.addFastView(ref);
166: }
167: wp.activate(ref.getPart(true));
168: } else {
169: activePage.showView(viewId);
170: }
171:
172: }
173: }
|