Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f60a2e16fb86fdd83cd7447543383abd29d3921a (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
/*******************************************************************************
 * Copyright (c) 2009 Red Hat, Inc.
 * 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:
 *    Elliott Baron <ebaron@redhat.com> - initial API and implementation
 *******************************************************************************/ 
package org.eclipse.linuxtools.internal.valgrind.ui;

import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.model.IPersistableSourceLocator;
import org.eclipse.debug.core.model.ISourceLocator;
import org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector;
import org.eclipse.debug.core.sourcelookup.ISourceLookupParticipant;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.debug.ui.sourcelookup.ISourceLookupResult;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.linuxtools.internal.valgrind.core.ValgrindError;
import org.eclipse.linuxtools.internal.valgrind.core.ValgrindStackFrame;
import org.eclipse.linuxtools.profiling.ui.ProfileUIUtils;
import org.eclipse.linuxtools.valgrind.core.IValgrindMessage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.PartInitException;

public class CoreMessagesViewer {

	static ImageRegistry imageRegistry = new ImageRegistry();

	public static final String VALGRIND_ERROR = "Valgrind_Error"; //$NON-NLS-1$
	/**
	 * @since 0.10
	 */
	public static final String VALGRIND_INFO = "Valgrind_Info"; //$NON-NLS-1$
	public static final String VALGRIND_ERROR_IMAGE = "icons/valgrind-error.png"; //$NON-NLS-1$
	/**
	 * @since 0.10
	 */
	public static final String VALGRIND_INFO_IMAGE = "icons/valgrind-info.png"; //$NON-NLS-1$
	public IDoubleClickListener doubleClickListener;
	public ITreeContentProvider contentProvider;
	public IAction expandAction;
	public IAction collapseAction;
	
	private TreeViewer viewer;

	public CoreMessagesViewer(Composite parent, int style) {
		viewer = new TreeViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | style);
		viewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
		if (imageRegistry.getDescriptor(VALGRIND_ERROR) == null) {
			ImageDescriptor d = ValgrindUIPlugin.getImageDescriptor(VALGRIND_ERROR_IMAGE);
			if (d != null) {
				imageRegistry.put(VALGRIND_ERROR, d);
			}
		}
		if (imageRegistry.getDescriptor(VALGRIND_INFO) == null) {
			ImageDescriptor d = ValgrindUIPlugin.getImageDescriptor(VALGRIND_INFO_IMAGE);
			if (d != null) {
				imageRegistry.put(VALGRIND_INFO, d);
			}
		}
		contentProvider = new ITreeContentProvider() {

			public Object[] getChildren(Object parentElement) {
				if (parentElement instanceof Object[]) {
					return (Object[]) parentElement;
				}
				return ((IValgrindMessage) parentElement).getChildren();
			}

			public Object getParent(Object element) {
				return ((IValgrindMessage) element).getParent();
			}

			public boolean hasChildren(Object element) {
				return getChildren(element).length > 0;
			}

			public Object[] getElements(Object inputElement) {
				return getChildren(inputElement);
			}

			public void dispose() {}

			public void inputChanged(Viewer viewer, Object oldInput,
					Object newInput) {}

		};
		viewer.setContentProvider(contentProvider);

		viewer.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element) {
				return ((IValgrindMessage) element).getText();
			}

			@Override
			public Image getImage(Object element) {
				Image image;
				if (element instanceof ValgrindStackFrame) {
					image = DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_STACKFRAME);
				}
				else if (element instanceof ValgrindError)  {
					image = imageRegistry.get(VALGRIND_ERROR);
				}
				else {
					image = imageRegistry.get(VALGRIND_INFO);
				}
				return image;
			}

		});

		doubleClickListener = new IDoubleClickListener() {

			public void doubleClick(DoubleClickEvent event) {
				Object element = ((TreeSelection) event.getSelection()).getFirstElement();
				if (element instanceof ValgrindStackFrame) {
					ValgrindStackFrame frame = (ValgrindStackFrame) element;
					ILaunch launch = frame.getLaunch();
					ISourceLocator locator = launch.getSourceLocator();		
					if (locator instanceof AbstractSourceLookupDirector) {
						AbstractSourceLookupDirector director = (AbstractSourceLookupDirector) locator;
						ISourceLookupParticipant[] participants = director.getParticipants();
						if (participants.length == 0) {
							// source locator likely disposed, try recreating it
							IPersistableSourceLocator sourceLocator;
							ILaunchConfiguration config = launch.getLaunchConfiguration();
							if (config != null) {
								try {
									String id = config.getAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID, (String) null);
									if (id == null) {
										sourceLocator = CDebugUIPlugin.createDefaultSourceLocator();
										sourceLocator.initializeDefaults(config);
									} else {
										sourceLocator = DebugPlugin.getDefault().getLaunchManager().newSourceLocator(id);
										String memento = config.getAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_MEMENTO, (String) null);
										if (memento == null) {
											sourceLocator.initializeDefaults(config);
										} else {
											sourceLocator.initializeFromMemento(memento);
										}
									}
									
									// replace old source locator
									locator = sourceLocator;
									launch.setSourceLocator(sourceLocator);
								} catch (CoreException e) {
									e.printStackTrace();
								}
							}
						}
					}
					ISourceLookupResult result = DebugUITools.lookupSource(frame.getFile(), locator);				

					try {
						ProfileUIUtils.openEditorAndSelect(result, frame.getLine());
					} catch (PartInitException e) {
						e.printStackTrace();
					} catch (BadLocationException e) {
						e.printStackTrace();
					}
				}
				else {
					if (viewer.getExpandedState(element)) {
						viewer.collapseToLevel(element, AbstractTreeViewer.ALL_LEVELS);
					}
					else {
						viewer.expandToLevel(element, 1);
					}
				}
			}
		};
		viewer.addDoubleClickListener(doubleClickListener);

		expandAction = new ExpandAction(viewer);
		collapseAction = new CollapseAction(viewer);

		MenuManager manager = new MenuManager();
		manager.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				ITreeSelection selection = (ITreeSelection) viewer.getSelection();
				Object element = selection.getFirstElement();
				if (contentProvider.hasChildren(element)) {
					manager.add(expandAction);
					manager.add(collapseAction);
				}
			}			
		});

		manager.setRemoveAllWhenShown(true);	
		Menu contextMenu = manager.createContextMenu(viewer.getTree());
		viewer.getControl().setMenu(contextMenu);
	}

	public IDoubleClickListener getDoubleClickListener() {
		return doubleClickListener;
	}

	public TreeViewer getTreeViewer() {
		return viewer;
	}
}

Back to the top