aboutsummaryrefslogtreecommitdiffstats
path: root/traceview/src/com/android/traceview/ProfileView.java
blob: 683a2c7a23e3bbeee96add607277ab9cb7250b0f (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
 * Copyright (C) 2006 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.traceview;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeViewerListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeExpansionEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

public class ProfileView extends Composite implements Observer {

    private TreeViewer mTreeViewer;
    private Text mSearchBox;
    private SelectionController mSelectionController;
    private ProfileProvider mProfileProvider;
    private Color mColorNoMatch;
    private Color mColorMatch;
    private MethodData mCurrentHighlightedMethod;
    private MethodHandler mMethodHandler;

    public interface MethodHandler {
        void handleMethod(MethodData method);
    }

    public ProfileView(Composite parent, TraceReader reader,
            SelectionController selectController) {
        super(parent, SWT.NONE);
        setLayout(new GridLayout(1, false));
        this.mSelectionController = selectController;
        mSelectionController.addObserver(this);

        // Add a tree viewer at the top
        mTreeViewer = new TreeViewer(this, SWT.MULTI | SWT.NONE);
        mTreeViewer.setUseHashlookup(true);
        mProfileProvider = reader.getProfileProvider();
        mProfileProvider.setTreeViewer(mTreeViewer);
        SelectionAdapter listener = mProfileProvider.getColumnListener();
        final Tree tree = mTreeViewer.getTree();
        tree.setHeaderVisible(true);
        tree.setLayoutData(new GridData(GridData.FILL_BOTH));

        // Get the column names from the ProfileProvider
        String[] columnNames = mProfileProvider.getColumnNames();
        int[] columnWidths = mProfileProvider.getColumnWidths();
        int[] columnAlignments = mProfileProvider.getColumnAlignments();
        for (int ii = 0; ii < columnWidths.length; ++ii) {
            TreeColumn column = new TreeColumn(tree, SWT.LEFT);
            column.setText(columnNames[ii]);
            column.setWidth(columnWidths[ii]);
            column.setMoveable(true);
            column.addSelectionListener(listener);
            column.setAlignment(columnAlignments[ii]);
        }

        // Add a listener to the tree so that we can make the row
        // height smaller.
        tree.addListener(SWT.MeasureItem, new Listener() {
            @Override
            public void handleEvent(Event event) {
                int fontHeight = event.gc.getFontMetrics().getHeight();
                event.height = fontHeight;
            }
        });

        mTreeViewer.setContentProvider(mProfileProvider);
        mTreeViewer.setLabelProvider(mProfileProvider.getLabelProvider());
        mTreeViewer.setInput(mProfileProvider.getRoot());

        // Create another composite to hold the label and text box
        Composite composite = new Composite(this, SWT.NONE);
        composite.setLayout(new GridLayout(2, false));
        composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        // Add a label for the search box
        Label label = new Label(composite, SWT.NONE);
        label.setText("Find:");

        // Add a text box for searching for method names
        mSearchBox = new Text(composite, SWT.BORDER);
        mSearchBox.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        Display display = getDisplay();
        mColorNoMatch = new Color(display, 255, 200, 200);
        mColorMatch = mSearchBox.getBackground();

        mSearchBox.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent ev) {
                String query = mSearchBox.getText();
                if (query.length() == 0)
                    return;
                findName(query);
            }
        });

        // Add a key listener to the text box so that we can clear
        // the text box if the user presses <ESC>.
        mSearchBox.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent event) {
                if (event.keyCode == SWT.ESC) {
                    mSearchBox.setText("");
                } else if (event.keyCode == SWT.CR) {
                    String query = mSearchBox.getText();
                    if (query.length() == 0)
                        return;
                    findNextName(query);
                }
            }
        });

        // Also add a key listener to the tree viewer so that the
        // user can just start typing anywhere in the tree view.
        tree.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent event) {
                if (event.keyCode == SWT.ESC) {
                    mSearchBox.setText("");
                } else if (event.keyCode == SWT.BS) {
                    // Erase the last character from the search box
                    String text = mSearchBox.getText();
                    int len = text.length();
                    String chopped;
                    if (len <= 1)
                        chopped = "";
                    else
                        chopped = text.substring(0, len - 1);
                    mSearchBox.setText(chopped);
                } else if (event.keyCode == SWT.CR) {
                    String query = mSearchBox.getText();
                    if (query.length() == 0)
                        return;
                    findNextName(query);
                } else {
                    // Append the given character to the search box
                    String str = String.valueOf(event.character);
                    mSearchBox.append(str);
                }
                event.doit = false;
            }
        });

        // Add a selection listener to the tree so that the user can click
        // on a method that is a child or parent and jump to that method.
        mTreeViewer
                .addSelectionChangedListener(new ISelectionChangedListener() {
                    @Override
                    public void selectionChanged(SelectionChangedEvent ev) {
                        ISelection sel = ev.getSelection();
                        if (sel.isEmpty())
                            return;
                        if (sel instanceof IStructuredSelection) {
                            IStructuredSelection selection = (IStructuredSelection) sel;
                            Object element = selection.getFirstElement();
                            if (element == null)
                                return;
                            if (element instanceof MethodData) {
                                MethodData md = (MethodData) element;
                                highlightMethod(md, true);
                            }
                            if (element instanceof ProfileData) {
                                MethodData md = ((ProfileData) element)
                                        .getMethodData();
                                highlightMethod(md, true);
                            }
                        }
                    }
                });

        // Add a tree listener so that we can expand the parents and children
        // of a method when a method is expanded.
        mTreeViewer.addTreeListener(new ITreeViewerListener() {
            @Override
            public void treeExpanded(TreeExpansionEvent event) {
                Object element = event.getElement();
                if (element instanceof MethodData) {
                    MethodData md = (MethodData) element;
                    expandNode(md);
                }
            }
            @Override
            public void treeCollapsed(TreeExpansionEvent event) {
            }
        });

        tree.addListener(SWT.MouseDown, new Listener() {
            @Override
            public void handleEvent(Event event) {
                Point point = new Point(event.x, event.y);
                TreeItem treeItem = tree.getItem(point);
                MethodData md = mProfileProvider.findMatchingTreeItem(treeItem);
                if (md == null)
                    return;
                ArrayList<Selection> selections = new ArrayList<Selection>();
                selections.add(Selection.highlight("MethodData", md));
                mSelectionController.change(selections, "ProfileView");

                if (mMethodHandler != null && (event.stateMask & SWT.MOD1) != 0) {
                    mMethodHandler.handleMethod(md);
                }
            }
        });
    }

    public void setMethodHandler(MethodHandler handler) {
        mMethodHandler = handler;
    }

    private void findName(String query) {
        MethodData md = mProfileProvider.findMatchingName(query);
        selectMethod(md);
    }

    private void findNextName(String query) {
        MethodData md = mProfileProvider.findNextMatchingName(query);
        selectMethod(md);
    }

    private void selectMethod(MethodData md) {
        if (md == null) {
            mSearchBox.setBackground(mColorNoMatch);
            return;
        }
        mSearchBox.setBackground(mColorMatch);
        highlightMethod(md, false);
    }

    @Override
    public void update(Observable objservable, Object arg) {
        // Ignore updates from myself
        if (arg == "ProfileView")
            return;
        // System.out.printf("profileview update from %s\n", arg);
        ArrayList<Selection> selections;
        selections = mSelectionController.getSelections();
        for (Selection selection : selections) {
            Selection.Action action = selection.getAction();
            if (action != Selection.Action.Highlight)
                continue;
            String name = selection.getName();
            if (name == "MethodData") {
                MethodData md = (MethodData) selection.getValue();
                highlightMethod(md, true);
                return;
            }
            if (name == "Call") {
                Call call = (Call) selection.getValue();
                MethodData md = call.getMethodData();
                highlightMethod(md, true);
                return;
            }
        }
    }

    private void highlightMethod(MethodData md, boolean clearSearch) {
        if (md == null)
            return;
        // Avoid an infinite recursion
        if (md == mCurrentHighlightedMethod)
            return;
        if (clearSearch) {
            mSearchBox.setText("");
            mSearchBox.setBackground(mColorMatch);
        }
        mCurrentHighlightedMethod = md;
        mTreeViewer.collapseAll();
        // Expand this node and its children
        expandNode(md);
        StructuredSelection sel = new StructuredSelection(md);
        mTreeViewer.setSelection(sel, true);
        Tree tree = mTreeViewer.getTree();
        TreeItem[] items = tree.getSelection();
        if (items.length != 0) {
            tree.setTopItem(items[0]);
            // workaround a Mac bug by adding showItem().
            tree.showItem(items[0]);
        }
    }

    private void expandNode(MethodData md) {
        ProfileNode[] nodes = md.getProfileNodes();
        mTreeViewer.setExpandedState(md, true);
        // Also expand the "Parents" and "Children" nodes.
        if (nodes != null) {
            for (ProfileNode node : nodes) {
                if (node.isRecursive() == false)
                    mTreeViewer.setExpandedState(node, true);
            }
        }
    }
}