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.IEditorActionDelegate;
15: import org.eclipse.ui.IEditorPart;
16: import org.eclipse.ui.WorkbenchException;
17:
18: /**
19: * Extends PartPluginAction for usage in editor parts. Objects
20: * of this class are created by reading the registry (extension point "editorActions").
21: */
22: public final class EditorPluginAction extends PartPluginAction {
23: private IEditorPart currentEditor;
24:
25: /**
26: * This class adds the requirement that action delegates
27: * loaded on demand implement IViewActionDelegate
28: */
29: public EditorPluginAction(IConfigurationElement actionElement,
30: IEditorPart part, String id, int style) {
31: super (actionElement, id, style);
32: if (part != null) {
33: editorChanged(part);
34: }
35: }
36:
37: /* (non-Javadoc)
38: * Method declared on PluginAction.
39: */
40: protected IActionDelegate validateDelegate(Object obj)
41: throws WorkbenchException {
42: if (obj instanceof IEditorActionDelegate) {
43: return (IEditorActionDelegate) obj;
44: } else {
45: throw new WorkbenchException(
46: "Action must implement IEditorActionDelegate"); //$NON-NLS-1$
47: }
48: }
49:
50: /* (non-Javadoc)
51: * Method declared on PluginAction.
52: */
53: protected void initDelegate() {
54: super .initDelegate();
55: ((IEditorActionDelegate) getDelegate()).setActiveEditor(this ,
56: currentEditor);
57: }
58:
59: /**
60: * Handles editor change by re-registering for selection
61: * changes and updating IEditorActionDelegate.
62: */
63: public void editorChanged(IEditorPart part) {
64: if (currentEditor != null) {
65: unregisterSelectionListener(currentEditor);
66: }
67:
68: currentEditor = part;
69:
70: if (getDelegate() == null && isOkToCreateDelegate()) {
71: createDelegate();
72: }
73: if (getDelegate() != null) {
74: ((IEditorActionDelegate) getDelegate()).setActiveEditor(
75: this , part);
76: }
77:
78: if (part != null) {
79: registerSelectionListener(part);
80: }
81: }
82:
83: /* (non-Javadoc)
84: * @see org.eclipse.ui.internal.PluginAction#dispose()
85: */
86: public void dispose() {
87: if (currentEditor != null) {
88: unregisterSelectionListener(currentEditor);
89: currentEditor = null;
90: }
91: super.dispose();
92: }
93: }
|