Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 91171453e478b6bcb34ee61d10fa5c70f2691a60 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.WorkbenchException;

/**
 * Extends PartPluginAction for usage in editor parts. Objects of this class are
 * created by reading the registry (extension point "editorActions").
 */
public final class EditorPluginAction extends PartPluginAction {
	private IEditorPart currentEditor;

	/**
	 * This class adds the requirement that action delegates loaded on demand
	 * implement IViewActionDelegate
	 */
	public EditorPluginAction(IConfigurationElement actionElement, IEditorPart part, String id, int style) {
		super(actionElement, id, style);
		if (part != null) {
			editorChanged(part);
		}
	}

	@Override
	protected IActionDelegate validateDelegate(Object obj) throws WorkbenchException {
		if (obj instanceof IEditorActionDelegate) {
			return (IEditorActionDelegate) obj;
		}
		throw new WorkbenchException("Action must implement IEditorActionDelegate"); //$NON-NLS-1$
	}

	@Override
	protected void initDelegate() {
		super.initDelegate();
		((IEditorActionDelegate) getDelegate()).setActiveEditor(this, currentEditor);
	}

	/**
	 * Handles editor change by re-registering for selection changes and updating
	 * IEditorActionDelegate.
	 */
	public void editorChanged(IEditorPart part) {
		if (currentEditor != null) {
			unregisterSelectionListener(currentEditor);
		}

		currentEditor = part;

		if (getDelegate() == null && isOkToCreateDelegate()) {
			createDelegate();
		}
		if (getDelegate() != null) {
			((IEditorActionDelegate) getDelegate()).setActiveEditor(this, part);
		}

		if (part != null) {
			registerSelectionListener(part);
		}
	}

	@Override
	public void dispose() {
		if (currentEditor != null) {
			unregisterSelectionListener(currentEditor);
			currentEditor = null;
		}
		super.dispose();
	}
}

Back to the top