Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 44dcd36968e3eed8773b3b0c627dafcbc2feca33 (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
86
87
88
89
90
91
/*******************************************************************************
 * Copyright (c) 2000, 2008 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.team.internal.ui.synchronize;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
import org.eclipse.ui.*;

/**
 * An Action that wraps IActionDelegates so they can be used programmatically
 * in toolbars, etc.
 */
public class ActionDelegateWrapper extends Action implements ISelectionChangedListener {

	private IActionDelegate delegate;

	public ActionDelegateWrapper(IActionDelegate delegate, ISynchronizePageConfiguration configuration) {
		this.delegate = delegate;
		IWorkbenchPart part = configuration.getSite().getPart();
		if(part != null) {
			if (delegate instanceof IObjectActionDelegate) {
				((IObjectActionDelegate)delegate).setActivePart(this, part);
			}
			if (part instanceof IViewPart
					&& delegate instanceof IViewActionDelegate) {
				((IViewActionDelegate)delegate).init((IViewPart)part);
			}
			if (part instanceof IEditorPart
					&& delegate instanceof IEditorActionDelegate) {
				((IEditorActionDelegate)delegate).setActiveEditor(this, (IEditorPart)part);
			}
		}
		initialize(configuration);
	}

	public ActionDelegateWrapper(IActionDelegate delegate, ISynchronizePageConfiguration configuration, String id) {
		this(delegate, configuration);
		setId(id);
		setActionDefinitionId(id);
	}

	/**
	 * Method invoked from the constructor when a configuration is provided.
	 * The default implementation registers the action as a selection change
	 * listener. Subclass may override.
	 * @param configuration the synchronize page configuration
	 */
	protected void initialize(final ISynchronizePageConfiguration configuration) {
		configuration.getSite().getSelectionProvider().addSelectionChangedListener(this);
		configuration.getPage().getViewer().getControl().addDisposeListener(e -> configuration.getSite()
				.getSelectionProvider().removeSelectionChangedListener(ActionDelegateWrapper.this));
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
	 */
	@Override
	public void selectionChanged(SelectionChangedEvent event) {
		getDelegate().selectionChanged(this, event.getSelection());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.action.IAction#run()
	 */
	@Override
	public void run() {
		getDelegate().run(this);
	}

	/**
	 * Return the delegate associated with this action.
	 * @return the delegate associated with this action
	 */
	public IActionDelegate getDelegate() {
		return delegate;
	}

}

Back to the top