aboutsummaryrefslogtreecommitdiffstats
path: root/ddms/libs/ddmuilib/src/com/android/ddmuilib/StackTracePanel.java
blob: 336a5a341e7f3476f087130003bb37d77fb3874d (plain)
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
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ddmuilib;

import com.android.ddmlib.Client;
import com.android.ddmlib.IStackTraceInfo;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;

/**
 * Stack Trace Panel.
 * <p/>This is not a panel in the regular sense. Instead this is just an object around the creation
 * and management of a Stack Trace display.
 * <p/>UI creation is done through
 * {@link #createPanel(Composite, String, String, String, String, String, IPreferenceStore)}.
 *
 */
public final class StackTracePanel {

    private static ISourceRevealer sSourceRevealer;

    private Table mStackTraceTable;
    private TableViewer mStackTraceViewer;

    private Client mCurrentClient;


    /**
     * Content Provider to display the stack trace of a thread.
     * Expected input is a {@link IStackTraceInfo} object.
     */
    private static class StackTraceContentProvider implements IStructuredContentProvider {
        @Override
        public Object[] getElements(Object inputElement) {
            if (inputElement instanceof IStackTraceInfo) {
                // getElement cannot return null, so we return an empty array
                // if there's no stack trace
                StackTraceElement trace[] = ((IStackTraceInfo)inputElement).getStackTrace();
                if (trace != null) {
                    return trace;
                }
            }

            return new Object[0];
        }

        @Override
        public void dispose() {
            // pass
        }

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            // pass
        }
    }


    /**
     * A Label Provider to use with {@link StackTraceContentProvider}. It expects the elements to be
     * of type {@link StackTraceElement}.
     */
    private static class StackTraceLabelProvider implements ITableLabelProvider {

        @Override
        public Image getColumnImage(Object element, int columnIndex) {
            return null;
        }

        @Override
        public String getColumnText(Object element, int columnIndex) {
            if (element instanceof StackTraceElement) {
                StackTraceElement traceElement = (StackTraceElement)element;
                switch (columnIndex) {
                    case 0:
                        return traceElement.getClassName();
                    case 1:
                        return traceElement.getMethodName();
                    case 2:
                        return traceElement.getFileName();
                    case 3:
                        return Integer.toString(traceElement.getLineNumber());
                    case 4:
                        return Boolean.toString(traceElement.isNativeMethod());
                }
            }

            return null;
        }

        @Override
        public void addListener(ILabelProviderListener listener) {
            // pass
        }

        @Override
        public void dispose() {
            // pass
        }

        @Override
        public boolean isLabelProperty(Object element, String property) {
            // pass
            return false;
        }

        @Override
        public void removeListener(ILabelProviderListener listener) {
            // pass
        }
    }

    /**
     * Classes which implement this interface provide a method that is able to reveal a method
     * in a source editor
     */
    public interface ISourceRevealer {
        /**
         * Sent to reveal a particular line in a source editor
         * @param applicationName the name of the application running the source.
         * @param className the fully qualified class name
         * @param line the line to reveal
         */
        public void reveal(String applicationName, String className, int line);
    }


    /**
     * Sets the {@link ISourceRevealer} object able to reveal source code in a source editor.
     * @param revealer
     */
    public static void setSourceRevealer(ISourceRevealer revealer) {
        sSourceRevealer = revealer;
    }

    /**
     * Creates the controls for the StrackTrace display.
     * <p/>This method will set the parent {@link Composite} to use a {@link GridLayout} with
     * 2 columns.
     * @param parent the parent composite.
     * @param prefs_stack_col_class
     * @param prefs_stack_col_method
     * @param prefs_stack_col_file
     * @param prefs_stack_col_line
     * @param prefs_stack_col_native
     * @param store
     */
    public Table createPanel(Composite parent, String prefs_stack_col_class,
            String prefs_stack_col_method, String prefs_stack_col_file, String prefs_stack_col_line,
            String prefs_stack_col_native, IPreferenceStore store) {

        mStackTraceTable = new Table(parent, SWT.MULTI | SWT.FULL_SELECTION);
        mStackTraceTable.setHeaderVisible(true);
        mStackTraceTable.setLinesVisible(true);

        TableHelper.createTableColumn(
                mStackTraceTable,
                "Class",
                SWT.LEFT,
                "SomeLongClassName", //$NON-NLS-1$
                prefs_stack_col_class, store);

        TableHelper.createTableColumn(
                mStackTraceTable,
                "Method",
                SWT.LEFT,
                "someLongMethod", //$NON-NLS-1$
                prefs_stack_col_method, store);

        TableHelper.createTableColumn(
                mStackTraceTable,
                "File",
                SWT.LEFT,
                "android/somepackage/someotherpackage/somefile.class", //$NON-NLS-1$
                prefs_stack_col_file, store);

        TableHelper.createTableColumn(
                mStackTraceTable,
                "Line",
                SWT.RIGHT,
                "99999", //$NON-NLS-1$
                prefs_stack_col_line, store);

        TableHelper.createTableColumn(
                mStackTraceTable,
                "Native",
                SWT.LEFT,
                "Native", //$NON-NLS-1$
                prefs_stack_col_native, store);

        mStackTraceViewer = new TableViewer(mStackTraceTable);
        mStackTraceViewer.setContentProvider(new StackTraceContentProvider());
        mStackTraceViewer.setLabelProvider(new StackTraceLabelProvider());

        mStackTraceViewer.addDoubleClickListener(new IDoubleClickListener() {
            @Override
            public void doubleClick(DoubleClickEvent event) {
                if (sSourceRevealer != null && mCurrentClient != null) {
                    // get the selected stack trace element
                    ISelection selection = mStackTraceViewer.getSelection();

                    if (selection instanceof IStructuredSelection) {
                        IStructuredSelection structuredSelection = (IStructuredSelection)selection;
                        Object object = structuredSelection.getFirstElement();
                        if (object instanceof StackTraceElement) {
                            StackTraceElement traceElement = (StackTraceElement)object;

                            if (traceElement.isNativeMethod() == false) {
                                sSourceRevealer.reveal(
                                        mCurrentClient.getClientData().getClientDescription(),
                                        traceElement.getClassName(),
                                        traceElement.getLineNumber());
                            }
                        }
                    }
                }
            }
        });

        return mStackTraceTable;
    }

    /**
     * Sets the input for the {@link TableViewer}.
     * @param input the {@link IStackTraceInfo} that will provide the viewer with the list of
     * {@link StackTraceElement}
     */
    public void setViewerInput(IStackTraceInfo input) {
        mStackTraceViewer.setInput(input);
        mStackTraceViewer.refresh();
    }

    /**
     * Sets the current client running the stack trace.
     * @param currentClient the {@link Client}.
     */
    public void setCurrentClient(Client currentClient) {
        mCurrentClient = currentClient;
    }
}