Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 609570c7a4c1eb9a2f363103ef4577aa19e6c8ec (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*******************************************************************************
 * Copyright (c) 2006, 2013 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
 *     Wind River - Anton Leherbauer - Fix selection provider (Bug 254442)
 *******************************************************************************/
package org.eclipse.debug.internal.ui.views.variables.details;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.debug.ui.IDetailPane;
import org.eclipse.jface.action.IAction;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.texteditor.IUpdate;

/**
 * Abstract class that holds common methods used by implementors of IDetailPane.
 *
 * @see DefaultDetailPane
 * @since 3.3
 */
public abstract class AbstractDetailPane implements IDetailPane {

	/**
	 * The <code>IWorkbenchPartSite</code> that the details area (and the
	 * variables view) belongs to.
	 */
	private IWorkbenchPartSite fWorkbenchPartSite;

	/**
	 * Map of actions. Keys are strings, values
	 * are <code>IAction</code>.
	 */
	private Map<String, IAction> fActionMap = new HashMap<>();

	/**
	 * Collection to track actions that should be updated when selection occurs.
	 */
	private List<String> fSelectionActions = new ArrayList<>();

	@Override
	public void init(IWorkbenchPartSite workbench) {
		fWorkbenchPartSite = workbench;

	}

	@Override
	public void dispose() {
		fActionMap.clear();
		fSelectionActions.clear();
	}

	/**
	 * Adds an action to the Map storing actions.  Removes it if action is null.
	 *
	 * @param actionID The ID of the action, used as the key in the Map
	 * @param action The action associated with the ID
	 */
	protected void setAction(String actionID, IAction action) {
		if (action == null) {
			fActionMap.remove(actionID);
		} else {
			fActionMap.put(actionID, action);
		}
	}

	/**
	 * Adds the given action to the global action handler for the ViewSite.
	 * A call to <code>updateActionBars()</code> must be called after changes
	 * to propagate changes through the workbench.
	 *
	 * @param actionID The ID of the action
	 * @param action The action to be set globally
	 */
	protected void setGlobalAction(String actionID, IAction action){
		getViewSite().getActionBars().setGlobalActionHandler(actionID, action);
	}

	/**
	 * Adds the given action to the list of actions that will be updated when
	 * <code>updateSelectionDependentActions()</code> is called.  If the string
	 * is null it will not be added to the list.
	 *
	 * @param actionID The ID of the action which should be updated
	 */
	protected void setSelectionDependantAction(String actionID){
		if (actionID != null) {
			fSelectionActions.add(actionID);
		}
	}

	/**
	 * Gets the action out of the map, casts it to an <code>IAction</code>
	 *
	 * @param actionID  The ID of the action to find
	 * @return The action associated with the ID or null if none is found.
	 */
	protected IAction getAction(String actionID) {
		return fActionMap.get(actionID);
	}

	/**
	 * Calls the update method of the action with the given action ID.
	 * The action must exist in the action map and must be an instance of
	 * </code>IUpdate</code>
	 *
	 * @param actionId The ID of the action to update
	 */
	protected void updateAction(String actionId) {
		IAction action= getAction(actionId);
		if (action instanceof IUpdate) {
			((IUpdate) action).update();
		}
	}

	/**
	 * Iterates through the list of selection dependent actions and
	 * updates them.  Use <code>setSelectionDependentAction(String actionID)</code>
	 * to add an action to the list.  The action must have been added to the known
	 * actions map by calling <code>setAction(String actionID, IAction action)</code>
	 * before it can be updated by this method.
	 */
	protected void updateSelectionDependentActions() {
		for (String string : fSelectionActions) {
			updateAction(string);
		}
	}

	/**
	 * Gets the view site for this view.  May be null if this detail pane
	 * is not part of a view.
	 *
	 * @return The site for this view or <code>null</code>
	 */
	protected  IViewSite getViewSite(){
		if (fWorkbenchPartSite == null){
			return null;
		} else {
			return (IViewSite) fWorkbenchPartSite.getPart().getSite();
		}
	}

	/**
	 * Gets the workbench part site for this view.  May be null if this detail pane
	 * is not part of a view.
	 *
	 * @return The workbench part site or <code>null</code>
	 */
	protected IWorkbenchPartSite getWorkbenchPartSite() {
		return fWorkbenchPartSite;
	}

	/**
	 * Returns whether this detail pane is being displayed in a view with a workbench part site.
	 *
	 * @return whether this detail pane is being displayed in a view with a workbench part site.
	 */
	protected boolean isInView(){
		return fWorkbenchPartSite != null;
	}

}

Back to the top