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.internal;
11:
12: import org.eclipse.core.runtime.IConfigurationElement;
13: import org.eclipse.ui.IActionDelegate;
14: import org.eclipse.ui.IViewActionDelegate;
15: import org.eclipse.ui.IViewPart;
16: import org.eclipse.ui.WorkbenchException;
17:
18: /**
19: * This class extends regular plugin action with the
20: * additional requirement that the delegate has
21: * to implement interface IViewActionDeelgate.
22: * This interface has one additional method (init)
23: * whose purpose is to initialize the delegate with
24: * the view part in which the action is intended to run.
25: */
26: public final class ViewPluginAction extends PartPluginAction {
27: private IViewPart viewPart;
28:
29: /**
30: * This class adds the requirement that action delegates
31: * loaded on demand implement IViewActionDelegate
32: */
33: public ViewPluginAction(IConfigurationElement actionElement,
34: IViewPart viewPart, String id, int style) {
35: super (actionElement, id, style);
36: this .viewPart = viewPart;
37: registerSelectionListener(viewPart);
38: }
39:
40: /* (non-Javadoc)
41: * Method declared on PluginAction.
42: */
43: protected IActionDelegate validateDelegate(Object obj)
44: throws WorkbenchException {
45: if (obj instanceof IViewActionDelegate) {
46: return (IViewActionDelegate) obj;
47: } else {
48: throw new WorkbenchException(
49: "Action must implement IViewActionDelegate"); //$NON-NLS-1$
50: }
51: }
52:
53: /* (non-Javadoc)
54: * Method declared on PluginAction.
55: */
56: protected void initDelegate() {
57: super .initDelegate();
58: ((IViewActionDelegate) getDelegate()).init(viewPart);
59: }
60:
61: /**
62: * Returns true if the view has been set
63: * The view may be null after the constructor is called and
64: * before the view is stored. We cannot create the delegate
65: * at that time.
66: */
67: public boolean isOkToCreateDelegate() {
68: return super .isOkToCreateDelegate() && viewPart != null;
69: }
70:
71: /* (non-Javadoc)
72: * @see org.eclipse.ui.internal.PluginAction#dispose()
73: */
74: public void dispose() {
75: unregisterSelectionListener(viewPart);
76: super.dispose();
77: }
78: }
|