Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1af96dc6ba8e00437579208ecc76a217bab1cf5a (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*****************************************************************
 * Copyright (c) 2010 Texas Instruments and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Patrick Chuong (Texas Instruments) - Pin and Clone Supports (331781)
 *     Patrick Chuong (Texas Instruments) - Add support for icon overlay in the debug view (Bug 334566)
 *****************************************************************/
package org.eclipse.cdt.debug.internal.ui.actions;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.cdt.debug.internal.ui.pinclone.DebugContextPinProvider;
import org.eclipse.cdt.debug.internal.ui.pinclone.DebugEventFilterService;
import org.eclipse.cdt.debug.internal.ui.pinclone.PinCloneUtils;
import org.eclipse.cdt.debug.ui.IPinProvider;
import org.eclipse.cdt.debug.ui.IPinProvider.IPinElementColorDescriptor;
import org.eclipse.cdt.debug.ui.IPinProvider.IPinElementHandle;
import org.eclipse.cdt.ui.CDTSharedImages;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.contexts.DebugContextEvent;
import org.eclipse.debug.ui.contexts.IDebugContextListener;
import org.eclipse.debug.ui.contexts.IDebugContextService;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.IActionDelegate2;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IPropertyListener;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartConstants;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.WorkbenchPart;

/**
 * Pin the selected debug context for the view. 
 */
public class PinDebugContextActionDelegate implements IViewActionDelegate, IActionDelegate2, IDebugContextListener {
	private IViewPart fPart;
	private String fPinnedContextLabel = ""; //$NON-NLS-1$
	private String fLastKnownDescription = ""; //$NON-NLS-1$
	private IAction fAction;
	private IPartListener2 fPartListener;
	private DebugContextPinProvider fProvider;
	
	public PinDebugContextActionDelegate() {
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.IActionDelegate2#runWithEvent(org.eclipse.jface.action.IAction, org.eclipse.swt.widgets.Event)
	 */
	public void runWithEvent(IAction action, Event event) {
		run(action);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	public void run(IAction action) {
		if (action.isChecked()) {
			fProvider = DebugEventFilterService.getInstance().addDebugEventFilter(fPart, getActiveDebugContext());
			if (fProvider != null) {
				fLastKnownDescription = ((WorkbenchPart) fPart).getContentDescription();
				fPinnedContextLabel = getPinContextLabel(fProvider);
				PinCloneUtils.setPartContentDescription(fPart, fPinnedContextLabel);
				updatePinContextColor(fProvider); 
			}
		} else {
			fProvider = null;
			DebugEventFilterService.getInstance().removeDebugEventFilter(fPart);
			updatePinContextColor(fProvider);
			PinCloneUtils.setPartContentDescription(fPart, fLastKnownDescription);			
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IActionDelegate2#init(org.eclipse.jface.action.IAction)
	 */
	public void init(IAction action) {
		fAction = action;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
	 */
	public void init(IViewPart view) {
		fPart = view;

		if (fAction != null && !fAction.isChecked()) {
			IDebugContextService service = DebugUITools.getDebugContextManager().getContextService(fPart.getViewSite().getWorkbenchWindow());
			boolean pinnable = PinCloneUtils.isPinnable(fPart, service.getActiveContext());		
			fAction.setEnabled(pinnable);
		}
		
		fPart.addPropertyListener(new IPropertyListener() {			
			public void propertyChanged(Object source, int propId) {
				if (IWorkbenchPartConstants.PROP_CONTENT_DESCRIPTION == propId) {
					// if the content description is not the pinned context label,
					// then cache it so that we can set it back when the action is unchecked.
					String desc = ((WorkbenchPart) fPart).getContentDescription();
					if (!fPinnedContextLabel.equals(desc)) {
						fLastKnownDescription = desc;
					}
					
					// if action is checked, than set it back to the pinned context label.
					if (fAction != null && fAction.isChecked()) {
						PinCloneUtils.setPartContentDescription(fPart, fPinnedContextLabel);
					}
				} else if (IWorkbenchPartConstants.PROP_PART_NAME == propId) {
					PinCloneUtils.setPartTitle(fPart);
				}
			}
		});		
		
		DebugUITools.addPartDebugContextListener(fPart.getSite(), this);
		
		// Platform AbstractDebugView saves action check state,
		// in our case, we don't want this behavior.
		// Listens to part close and set the check state off.
		fPartListener = new IPartListener2() {					
			public void partBroughtToTop(IWorkbenchPartReference partRef) {}
			public void partClosed(IWorkbenchPartReference partRef) {
				IWorkbenchPart part = partRef.getPart(false);
				if (part.equals(fPart)) {
					if (fAction.isChecked()) {
						DebugEventFilterService.getInstance().removeDebugEventFilter(fPart);
						fAction.setChecked(false);
					}					
				}
			}
			public void partDeactivated(IWorkbenchPartReference partRef) {}
			public void partOpened(IWorkbenchPartReference partRef) {}
			public void partHidden(IWorkbenchPartReference partRef) {}
			public void partVisible(IWorkbenchPartReference partRef) {}
			public void partInputChanged(IWorkbenchPartReference partRef) {}
			public void partActivated(IWorkbenchPartReference partRef) {}
		};
		fPart.getSite().getWorkbenchWindow().getPartService().addPartListener(fPartListener);		
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.IActionDelegate2#dispose()
	 */
	public void dispose() {
		DebugUITools.removePartDebugContextListener(fPart.getSite(), this);
		fPart.getSite().getWorkbenchWindow().getPartService().removePartListener(fPartListener);
	}
	
	protected ISelection getActiveDebugContext() {
		IDebugContextService contextService = 
			DebugUITools.getDebugContextManager().getContextService(fPart.getSite().getWorkbenchWindow());
		return contextService.getActiveContext();		
	}
	
	private String getPinContextLabel(DebugContextPinProvider provider) {
		String description = ""; //$NON-NLS-1$
		
		if (provider != null) {
			Set<String> labels = new HashSet<String>();
			for (IPinElementHandle handle : provider.getPinHandles()) {
				String tmp = getLabel(handle);
				if (tmp != null && tmp.trim().length() != 0)
					labels.add(tmp);
			}
			
			for (String label : labels) {
				if (label != null) {
					if (description.length() > 0) {
						description += ", " + label; //$NON-NLS-1$
					} else {
						description = label;
					}
				}
			}
		}
		return description;
	}
	
	private String getLabel(IPinElementHandle handle) {
		String label = ""; //$NON-NLS-1$

		if (handle != null)
			label = handle.getLabel();
		
		return label;
	}

	private boolean useMultiPinImage(Set<IPinElementHandle> handles) {
		if (handles.size() <= 1) return false;
		
		int overlayColor = IPinElementColorDescriptor.UNDEFINED;
		ImageDescriptor imageDesc = null;
		for (IPinElementHandle handle : handles) {
			IPinElementColorDescriptor colorDesc = handle.getPinElementColorDescriptor();
			if (colorDesc != null) {
				ImageDescriptor descImageDesc = colorDesc.getToolbarIconDescriptor();
				if (imageDesc != null && !imageDesc.equals(descImageDesc))
					return true;
				imageDesc = descImageDesc;
				
				int descOverlayColor = colorDesc.getOverlayColor();
				if (overlayColor != IPinElementColorDescriptor.UNDEFINED && descOverlayColor != overlayColor)
					return true;
				overlayColor = descOverlayColor;
			}
		}
		
		return false;
	}
	
	private void updatePinContextColor(DebugContextPinProvider provider) {
		ImageDescriptor imageDesc = null;
		if (provider != null) {
			Set<IPinElementHandle> handles = provider.getPinHandles();
			
			// if handles have different toolbar icon descriptor or different pin color, than use a 
			// multi-pin toolbar icon
			if (useMultiPinImage(handles))
				imageDesc =  CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_VIEW_PIN_ACTION_MULTI);
			
			if (imageDesc == null) {
				Iterator<IPinElementHandle> itr = handles.iterator();
				if (itr.hasNext()) {
					IPinElementHandle handle = itr.next();
					IPinElementColorDescriptor desc = handle.getPinElementColorDescriptor();
					if (desc != null)
						imageDesc = desc.getToolbarIconDescriptor();
					
					if (imageDesc == null && desc != null) {
						int overlayColor = desc.getOverlayColor() % IPinElementColorDescriptor.DEFAULT_COLOR_COUNT;
						
						switch (overlayColor) {
						case IPinProvider.IPinElementColorDescriptor.GREEN:
							imageDesc = CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_VIEW_PIN_ACTION_G);
							break;
						case IPinProvider.IPinElementColorDescriptor.RED:
							imageDesc = CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_VIEW_PIN_ACTION_R);
							break;
						case IPinProvider.IPinElementColorDescriptor.BLUE:
							imageDesc = CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_VIEW_PIN_ACTION_B);
							break;
						}
					}
				}
			}
		}
		
		if (imageDesc == null)
			imageDesc = CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_VIEW_PIN_ACTION);
		fAction.setImageDescriptor(imageDesc);
	}	
	
	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.contexts.IDebugContextListener#debugContextChanged(org.eclipse.debug.ui.contexts.DebugContextEvent)
	 */
	public void debugContextChanged(DebugContextEvent event) {
		if (fAction != null && !fAction.isChecked()) {
			final boolean pinnable = PinCloneUtils.isPinnable(fPart, event.getContext());
			if (pinnable != fAction.isEnabled()) {			
				PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {				
					public void run() {
						fAction.setEnabled(pinnable);
					}
				});
			}
		}
	}
}

Back to the top