FileDocCategorySizeDatePackage
ProfileView.javaAPI DocAndroid 1.5 API12444Wed May 06 22:41:10 BST 2009com.android.traceview

ProfileView

public class ProfileView extends org.eclipse.swt.widgets.Composite implements Observer

Fields Summary
private org.eclipse.jface.viewers.TreeViewer
mTreeViewer
private org.eclipse.swt.widgets.Text
mSearchBox
private SelectionController
mSelectionController
private ProfileProvider
mProfileProvider
private org.eclipse.swt.graphics.Color
mColorNoMatch
private org.eclipse.swt.graphics.Color
mColorMatch
private MethodData
mCurrentHighlightedMethod
Constructors Summary
public ProfileView(org.eclipse.swt.widgets.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() {
            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() {
            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() {
                    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() {
            public void treeExpanded(TreeExpansionEvent event) {
                Object element = event.getElement();
                if (element instanceof MethodData) {
                    MethodData md = (MethodData) element;
                    expandNode(md);
                }
            }
            public void treeCollapsed(TreeExpansionEvent event) {
            }
        });

        tree.addListener(SWT.MouseDown, new Listener() {
            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");
            }
        });
    
Methods Summary
private voidexpandNode(MethodData md)

        ProfileNode[] nodes = md.getProfileNodes();
        mTreeViewer.setExpandedState(md, true);
        // Also expand the "Parents" and "Children" nodes.
        for (ProfileNode node : nodes) {
            if (node.isRecursive() == false)
                mTreeViewer.setExpandedState(node, true);
        }
    
private voidfindName(java.lang.String query)

        MethodData md = mProfileProvider.findMatchingName(query);
        selectMethod(md);
    
private voidfindNextName(java.lang.String query)

        MethodData md = mProfileProvider.findNextMatchingName(query);
        selectMethod(md);
    
private voidhighlightMethod(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();
        tree.setTopItem(items[0]);
        // workaround a Mac bug by adding showItem().
        tree.showItem(items[0]);
    
private voidselectMethod(MethodData md)

        if (md == null) {
            mSearchBox.setBackground(mColorNoMatch);
            return;
        }
        mSearchBox.setBackground(mColorMatch);
        highlightMethod(md, false);
    
public voidupdate(java.util.Observable objservable, java.lang.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.mMethodData;
                highlightMethod(md, true);
                return;
            }
        }