01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.actions;
11:
12: import org.eclipse.jface.action.Action;
13: import org.eclipse.swt.events.SelectionEvent;
14: import org.eclipse.swt.widgets.Event;
15: import org.eclipse.ui.IPerspectiveDescriptor;
16: import org.eclipse.ui.IPluginContribution;
17: import org.eclipse.ui.IWorkbenchWindow;
18: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
19: import org.eclipse.ui.internal.util.Util;
20:
21: /**
22: * Opens a perspective.
23: *
24: * @since 3.1
25: */
26: public final class OpenPerspectiveAction extends Action implements
27: IPluginContribution {
28:
29: /**
30: * The perspective menu that will handle the execution of this action. This
31: * allows subclasses of <code>PerspectiveMenu</code> to define custom
32: * behaviour for these actions. This value should not be <code>null</code>.
33: */
34: private final PerspectiveMenu callback;
35:
36: /**
37: * The descriptor for the perspective that this action should open. This
38: * value is never <code>null</code>.
39: */
40: private final IPerspectiveDescriptor descriptor;
41:
42: /**
43: * Constructs a new instance of <code>OpenPerspectiveAction</code>
44: *
45: * @param window
46: * The workbench window in which this action is created; should
47: * not be <code>null</code>.
48: * @param descriptor
49: * The descriptor for the perspective that this action should
50: * open; must not be <code>null</code>.
51: * @param callback
52: * The perspective menu who will handle the actual execution of
53: * this action; should not be <code>null</code>.
54: */
55: public OpenPerspectiveAction(final IWorkbenchWindow window,
56: final IPerspectiveDescriptor descriptor,
57: final PerspectiveMenu callback) {
58: super (Util.ZERO_LENGTH_STRING);
59:
60: this .descriptor = descriptor;
61: this .callback = callback;
62:
63: final String label = descriptor.getLabel();
64: setText(label);
65: setToolTipText(label);
66: setImageDescriptor(descriptor.getImageDescriptor());
67:
68: window.getWorkbench().getHelpSystem().setHelp(this ,
69: IWorkbenchHelpContextIds.OPEN_PERSPECTIVE_ACTION);
70: }
71:
72: /* (non-Javadoc)
73: * @see org.eclipse.jface.action.IAction#runWithEvent(org.eclipse.swt.widgets.Event)
74: */
75: public final void runWithEvent(final Event event) {
76: callback.run(descriptor, new SelectionEvent(event));
77: }
78:
79: /*
80: * (non-Javadoc)
81: *
82: * @see org.eclipse.ui.activities.support.IPluginContribution#getLocalId()
83: */
84: public String getLocalId() {
85: return descriptor.getId();
86: }
87:
88: /*
89: * (non-Javadoc)
90: *
91: * @see org.eclipse.ui.activities.support.IPluginContribution#getPluginId()
92: */
93: public String getPluginId() {
94: return descriptor instanceof IPluginContribution ? ((IPluginContribution) descriptor)
95: .getPluginId()
96: : null;
97: }
98: }
|