~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to valgrind/org.eclipse.linuxtools.valgrind.ui/src/org/eclipse/linuxtools/internal/valgrind/ui/CoreMessagesViewer.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2009 Red Hat, Inc.
 
3
 * All rights reserved. This program and the accompanying materials
 
4
 * are made available under the terms of the Eclipse Public License v1.0
 
5
 * which accompanies this distribution, and is available at
 
6
 * http://www.eclipse.org/legal/epl-v10.html
 
7
 *
 
8
 * Contributors:
 
9
 *    Elliott Baron <ebaron@redhat.com> - initial API and implementation
 
10
 *******************************************************************************/ 
 
11
package org.eclipse.linuxtools.internal.valgrind.ui;
 
12
 
 
13
import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
 
14
import org.eclipse.core.runtime.CoreException;
 
15
import org.eclipse.debug.core.DebugPlugin;
 
16
import org.eclipse.debug.core.ILaunch;
 
17
import org.eclipse.debug.core.ILaunchConfiguration;
 
18
import org.eclipse.debug.core.model.IPersistableSourceLocator;
 
19
import org.eclipse.debug.core.model.ISourceLocator;
 
20
import org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector;
 
21
import org.eclipse.debug.core.sourcelookup.ISourceLookupParticipant;
 
22
import org.eclipse.debug.ui.DebugUITools;
 
23
import org.eclipse.debug.ui.IDebugUIConstants;
 
24
import org.eclipse.debug.ui.sourcelookup.ISourceLookupResult;
 
25
import org.eclipse.jface.action.IAction;
 
26
import org.eclipse.jface.action.IMenuListener;
 
27
import org.eclipse.jface.action.IMenuManager;
 
28
import org.eclipse.jface.action.MenuManager;
 
29
import org.eclipse.jface.resource.ImageDescriptor;
 
30
import org.eclipse.jface.resource.ImageRegistry;
 
31
import org.eclipse.jface.text.BadLocationException;
 
32
import org.eclipse.jface.viewers.AbstractTreeViewer;
 
33
import org.eclipse.jface.viewers.DoubleClickEvent;
 
34
import org.eclipse.jface.viewers.IDoubleClickListener;
 
35
import org.eclipse.jface.viewers.ITreeContentProvider;
 
36
import org.eclipse.jface.viewers.ITreeSelection;
 
37
import org.eclipse.jface.viewers.LabelProvider;
 
38
import org.eclipse.jface.viewers.TreeSelection;
 
39
import org.eclipse.jface.viewers.TreeViewer;
 
40
import org.eclipse.jface.viewers.Viewer;
 
41
import org.eclipse.linuxtools.internal.valgrind.core.ValgrindError;
 
42
import org.eclipse.linuxtools.internal.valgrind.core.ValgrindStackFrame;
 
43
import org.eclipse.linuxtools.profiling.ui.ProfileUIUtils;
 
44
import org.eclipse.linuxtools.valgrind.core.IValgrindMessage;
 
45
import org.eclipse.swt.SWT;
 
46
import org.eclipse.swt.graphics.Image;
 
47
import org.eclipse.swt.layout.GridData;
 
48
import org.eclipse.swt.widgets.Composite;
 
49
import org.eclipse.swt.widgets.Menu;
 
50
import org.eclipse.ui.PartInitException;
 
51
 
 
52
public class CoreMessagesViewer {
 
53
 
 
54
        static ImageRegistry imageRegistry = new ImageRegistry();
 
55
 
 
56
        public static final String VALGRIND_ERROR = "Valgrind_Error"; //$NON-NLS-1$
 
57
        /**
 
58
         * @since 0.10
 
59
         */
 
60
        public static final String VALGRIND_INFO = "Valgrind_Info"; //$NON-NLS-1$
 
61
        public static final String VALGRIND_ERROR_IMAGE = "icons/valgrind-error.png"; //$NON-NLS-1$
 
62
        /**
 
63
         * @since 0.10
 
64
         */
 
65
        public static final String VALGRIND_INFO_IMAGE = "icons/valgrind-info.png"; //$NON-NLS-1$
 
66
        public IDoubleClickListener doubleClickListener;
 
67
        public ITreeContentProvider contentProvider;
 
68
        public IAction expandAction;
 
69
        public IAction collapseAction;
 
70
        
 
71
        private TreeViewer viewer;
 
72
 
 
73
        public CoreMessagesViewer(Composite parent, int style) {
 
74
                viewer = new TreeViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | style);
 
75
                viewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
 
76
                if (imageRegistry.getDescriptor(VALGRIND_ERROR) == null) {
 
77
                        ImageDescriptor d = ValgrindUIPlugin.getImageDescriptor(VALGRIND_ERROR_IMAGE);
 
78
                        if (d != null) {
 
79
                                imageRegistry.put(VALGRIND_ERROR, d);
 
80
                        }
 
81
                }
 
82
                if (imageRegistry.getDescriptor(VALGRIND_INFO) == null) {
 
83
                        ImageDescriptor d = ValgrindUIPlugin.getImageDescriptor(VALGRIND_INFO_IMAGE);
 
84
                        if (d != null) {
 
85
                                imageRegistry.put(VALGRIND_INFO, d);
 
86
                        }
 
87
                }
 
88
                contentProvider = new ITreeContentProvider() {
 
89
 
 
90
                        public Object[] getChildren(Object parentElement) {
 
91
                                if (parentElement instanceof Object[]) {
 
92
                                        return (Object[]) parentElement;
 
93
                                }
 
94
                                return ((IValgrindMessage) parentElement).getChildren();
 
95
                        }
 
96
 
 
97
                        public Object getParent(Object element) {
 
98
                                return ((IValgrindMessage) element).getParent();
 
99
                        }
 
100
 
 
101
                        public boolean hasChildren(Object element) {
 
102
                                return getChildren(element).length > 0;
 
103
                        }
 
104
 
 
105
                        public Object[] getElements(Object inputElement) {
 
106
                                return getChildren(inputElement);
 
107
                        }
 
108
 
 
109
                        public void dispose() {}
 
110
 
 
111
                        public void inputChanged(Viewer viewer, Object oldInput,
 
112
                                        Object newInput) {}
 
113
 
 
114
                };
 
115
                viewer.setContentProvider(contentProvider);
 
116
 
 
117
                viewer.setLabelProvider(new LabelProvider() {
 
118
                        @Override
 
119
                        public String getText(Object element) {
 
120
                                return ((IValgrindMessage) element).getText();
 
121
                        }
 
122
 
 
123
                        @Override
 
124
                        public Image getImage(Object element) {
 
125
                                Image image;
 
126
                                if (element instanceof ValgrindStackFrame) {
 
127
                                        image = DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_STACKFRAME);
 
128
                                }
 
129
                                else if (element instanceof ValgrindError)  {
 
130
                                        image = imageRegistry.get(VALGRIND_ERROR);
 
131
                                }
 
132
                                else {
 
133
                                        image = imageRegistry.get(VALGRIND_INFO);
 
134
                                }
 
135
                                return image;
 
136
                        }
 
137
 
 
138
                });
 
139
 
 
140
                doubleClickListener = new IDoubleClickListener() {
 
141
 
 
142
                        public void doubleClick(DoubleClickEvent event) {
 
143
                                Object element = ((TreeSelection) event.getSelection()).getFirstElement();
 
144
                                if (element instanceof ValgrindStackFrame) {
 
145
                                        ValgrindStackFrame frame = (ValgrindStackFrame) element;
 
146
                                        ILaunch launch = frame.getLaunch();
 
147
                                        ISourceLocator locator = launch.getSourceLocator();             
 
148
                                        if (locator instanceof AbstractSourceLookupDirector) {
 
149
                                                AbstractSourceLookupDirector director = (AbstractSourceLookupDirector) locator;
 
150
                                                ISourceLookupParticipant[] participants = director.getParticipants();
 
151
                                                if (participants.length == 0) {
 
152
                                                        // source locator likely disposed, try recreating it
 
153
                                                        IPersistableSourceLocator sourceLocator;
 
154
                                                        ILaunchConfiguration config = launch.getLaunchConfiguration();
 
155
                                                        if (config != null) {
 
156
                                                                try {
 
157
                                                                        String id = config.getAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID, (String) null);
 
158
                                                                        if (id == null) {
 
159
                                                                                sourceLocator = CDebugUIPlugin.createDefaultSourceLocator();
 
160
                                                                                sourceLocator.initializeDefaults(config);
 
161
                                                                        } else {
 
162
                                                                                sourceLocator = DebugPlugin.getDefault().getLaunchManager().newSourceLocator(id);
 
163
                                                                                String memento = config.getAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_MEMENTO, (String) null);
 
164
                                                                                if (memento == null) {
 
165
                                                                                        sourceLocator.initializeDefaults(config);
 
166
                                                                                } else {
 
167
                                                                                        sourceLocator.initializeFromMemento(memento);
 
168
                                                                                }
 
169
                                                                        }
 
170
                                                                        
 
171
                                                                        // replace old source locator
 
172
                                                                        locator = sourceLocator;
 
173
                                                                        launch.setSourceLocator(sourceLocator);
 
174
                                                                } catch (CoreException e) {
 
175
                                                                        e.printStackTrace();
 
176
                                                                }
 
177
                                                        }
 
178
                                                }
 
179
                                        }
 
180
                                        ISourceLookupResult result = DebugUITools.lookupSource(frame.getFile(), locator);                               
 
181
 
 
182
                                        try {
 
183
                                                ProfileUIUtils.openEditorAndSelect(result, frame.getLine());
 
184
                                        } catch (PartInitException e) {
 
185
                                                e.printStackTrace();
 
186
                                        } catch (BadLocationException e) {
 
187
                                                e.printStackTrace();
 
188
                                        }
 
189
                                }
 
190
                                else {
 
191
                                        if (viewer.getExpandedState(element)) {
 
192
                                                viewer.collapseToLevel(element, AbstractTreeViewer.ALL_LEVELS);
 
193
                                        }
 
194
                                        else {
 
195
                                                viewer.expandToLevel(element, 1);
 
196
                                        }
 
197
                                }
 
198
                        }
 
199
                };
 
200
                viewer.addDoubleClickListener(doubleClickListener);
 
201
 
 
202
                expandAction = new ExpandAction(viewer);
 
203
                collapseAction = new CollapseAction(viewer);
 
204
 
 
205
                MenuManager manager = new MenuManager();
 
206
                manager.addMenuListener(new IMenuListener() {
 
207
                        public void menuAboutToShow(IMenuManager manager) {
 
208
                                ITreeSelection selection = (ITreeSelection) viewer.getSelection();
 
209
                                Object element = selection.getFirstElement();
 
210
                                if (contentProvider.hasChildren(element)) {
 
211
                                        manager.add(expandAction);
 
212
                                        manager.add(collapseAction);
 
213
                                }
 
214
                        }                       
 
215
                });
 
216
 
 
217
                manager.setRemoveAllWhenShown(true);    
 
218
                Menu contextMenu = manager.createContextMenu(viewer.getTree());
 
219
                viewer.getControl().setMenu(contextMenu);
 
220
        }
 
221
 
 
222
        public IDoubleClickListener getDoubleClickListener() {
 
223
                return doubleClickListener;
 
224
        }
 
225
 
 
226
        public TreeViewer getTreeViewer() {
 
227
                return viewer;
 
228
        }
 
229
}
 
 
b'\\ No newline at end of file'