Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ed3c769466bac3df451dcc628937578a04525a0b (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*******************************************************************************
 * Copyright (c) 2010, 2015 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.debug.internal.ui.commands.actions;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler2;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.debug.internal.ui.views.launch.LaunchView;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * An action handler that delegates to an action in the debug view.
 */
public abstract class DebugActionHandler implements IHandler2 {

	private String fActionId;

	/**
	 * Constructs a new handler for the given action identifier.
	 *
	 * @param actionId action identifier
	 */
	public DebugActionHandler(String actionId) {
		fActionId = actionId;
	}

	/**
	 * Returns the delegate handler or <code>null</code> if none.
	 *
	 * @return handler or <code>null</code>
	 */
	protected IHandler2 getDelegate() {
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (window != null && window.getActivePage() != null) {
			IViewReference reference = window.getActivePage().findViewReference(IDebugUIConstants.ID_DEBUG_VIEW);
			if (reference != null) {
				IViewPart view = reference.getView(false);
				if (view instanceof LaunchView) {
					return ((LaunchView)view).getHandler(fActionId);
				}
			}
		}
		return null;
	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IHandler2 delegate = getDelegate();
		if (delegate != null) {
			return delegate.execute(event);
		}
		return null;
	}

	@Override
	public void addHandlerListener(IHandlerListener handlerListener) {
		IHandler2 delegate = getDelegate();
		if (delegate != null) {
			delegate.addHandlerListener(handlerListener);
		}
	}

	@Override
	public void dispose() {
	}

	@Override
	public boolean isEnabled() {
		IHandler2 delegate = getDelegate();
		if (delegate != null) {
			return delegate.isEnabled();
		}
		return false;
	}

	@Override
	public boolean isHandled() {
		IHandler2 delegate = getDelegate();
		if (delegate != null) {
			return delegate.isHandled();
		}
		return false;
	}

	@Override
	public void removeHandlerListener(IHandlerListener handlerListener) {
		IHandler2 delegate = getDelegate();
		if (delegate != null) {
			delegate.removeHandlerListener(handlerListener);
		}
	}

	@Override
	public void setEnabled(Object evaluationContext) {
		IHandler2 delegate = getDelegate();
		if (delegate != null) {
			delegate.setEnabled(evaluationContext);
		}
	}
}

Back to the top