KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > handlers > ShowViewHandler


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.handlers;
12
13 import java.util.Map JavaDoc;
14
15 import org.eclipse.core.commands.AbstractHandler;
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.jface.window.Window;
19 import org.eclipse.ui.IViewPart;
20 import org.eclipse.ui.IViewReference;
21 import org.eclipse.ui.IWorkbenchPage;
22 import org.eclipse.ui.IWorkbenchWindow;
23 import org.eclipse.ui.PartInitException;
24 import org.eclipse.ui.internal.FastViewBar;
25 import org.eclipse.ui.internal.Perspective;
26 import org.eclipse.ui.internal.WorkbenchMessages;
27 import org.eclipse.ui.internal.WorkbenchPage;
28 import org.eclipse.ui.internal.WorkbenchPlugin;
29 import org.eclipse.ui.internal.dialogs.ShowViewDialog;
30 import org.eclipse.ui.internal.misc.StatusUtil;
31 import org.eclipse.ui.statushandlers.StatusManager;
32 import org.eclipse.ui.views.IViewDescriptor;
33
34 /**
35  * Shows the given view. If no view is specified in the parameters, then this
36  * opens the view selection dialog.
37  *
38  * @since 3.1
39  */

40 public final class ShowViewHandler extends AbstractHandler {
41
42     /**
43      * The name of the parameter providing the view identifier.
44      */

45     private static final String JavaDoc PARAMETER_NAME_VIEW_ID = "org.eclipse.ui.views.showView.viewId"; //$NON-NLS-1$
46
private boolean makeFast = false;
47     private static final String JavaDoc PARAMETER_MAKE_FAST = "org.eclipse.ui.views.showView.makeFast"; //$NON-NLS-1$
48

49     /**
50      * Creates a new ShowViewHandler that will open the view in its default location.
51      */

52     public ShowViewHandler() {
53     }
54     
55     /**
56      * Creates a new ShowViewHandler that will optionally force the view to become
57      * a fast view.
58      *
59      * @param makeFast if true, the view will be moved to the fast view bar (even if it already
60      * exists elsewhere). If false, the view will be shown in its default location. Calling with
61      * false is equivalent to using the default constructor.
62      */

63     public ShowViewHandler(boolean makeFast) {
64         this.makeFast = makeFast;
65     }
66     
67     public final Object JavaDoc execute(final ExecutionEvent event)
68             throws ExecutionException {
69         IWorkbenchWindow window = HandlerUtil
70                 .getActiveWorkbenchWindowChecked(event);
71         // Get the view identifier, if any.
72
final Map JavaDoc parameters = event.getParameters();
73         final Object JavaDoc value = parameters.get(PARAMETER_NAME_VIEW_ID);
74         makeFast = "true".equals(parameters.get(PARAMETER_MAKE_FAST)); //$NON-NLS-1$
75

76         if (value == null) {
77             openOther(window);
78         } else {
79             try {
80                 openView((String JavaDoc) value, window);
81             } catch (PartInitException e) {
82                 throw new ExecutionException("Part could not be initialized", e); //$NON-NLS-1$
83
}
84         }
85
86         return null;
87     }
88
89     /**
90      * Opens a view selection dialog, allowing the user to chose a view.
91      */

92     private final void openOther(final IWorkbenchWindow window) {
93         final IWorkbenchPage page = window.getActivePage();
94         if (page == null) {
95             return;
96         }
97         
98         final ShowViewDialog dialog = new ShowViewDialog(window,
99                 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 JavaDoc viewId,
128             final IWorkbenchWindow activeWorkbenchWindow)
129             throws PartInitException {
130
131         final IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
132         if (activePage == null) {
133             return;
134         }
135
136         if (makeFast) {
137             WorkbenchPage wp = (WorkbenchPage) activePage;
138             Perspective persp = wp.getActivePerspective();
139
140             // If we're making a fast view then use the new mechanism directly
141
boolean useNewMinMax = Perspective.useNewMinMax(persp);
142             if (useNewMinMax) {
143                 IViewReference ref = persp.getViewReference(viewId, null);
144                 if (ref == null)
145                     return;
146
147                 persp.getFastViewManager().addViewReference(FastViewBar.FASTVIEWBAR_ID, -1, ref, true);
148                 wp.activate(ref.getPart(true));
149                 
150                 return;
151             }
152             
153             IViewReference ref = wp.findViewReference(viewId);
154             
155             if (ref == null) {
156                 IViewPart part = wp.showView(viewId, null, IWorkbenchPage.VIEW_CREATE);
157                 ref = (IViewReference)wp.getReference(part);
158             }
159             
160             if (!wp.isFastView(ref)) {
161                 wp.addFastView(ref);
162             }
163             wp.activate(ref.getPart(true));
164         } else {
165             activePage.showView(viewId);
166         }
167         
168     }
169 }
170
Popular Tags