aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.pdt/src/com/android/ide/eclipse/pdt/internal/SourceRevealer.java
blob: 51011e1191610b6fa9c912ef8c3a4e1435338e74 (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
/*
 * Copyright (C) 2010 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.ide.eclipse.pdt.internal;

import com.android.ide.eclipse.ddms.ISourceRevealer;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.core.search.SearchParticipant;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.core.search.SearchRequestor;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jdt.ui.actions.OpenJavaPerspectiveAction;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IPerspectiveRegistry;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;

/**
 * Implementation of the com.android.ide.ddms.sourceRevealer extension point.
 * This implementation is a copy of com.android.ide.eclipse.adt.SourceRevealer.
 */
public class SourceRevealer extends DevTreeProjectProvider implements ISourceRevealer {

    @Override
    public boolean reveal(String applicationName, String className, int line) {
        IProject project = getProject();

        if (project != null) {
            // Inner classes are pointless: All we need is the enclosing type to find the file,
            // and the line number.
            // Since the anonymous ones will cause IJavaProject#findType to fail, we remove
            // all of them.
            int pos = className.indexOf('$');
            if (pos != -1) {
                className = className.substring(0, pos);
            }

            // get the java project
            IJavaProject javaProject = JavaCore.create(project);

            try {
                // look for the IType matching the class name.
                IType result = javaProject.findType(className);
                if (result != null && result.exists()) {
                    // before we show the type in an editor window, we make sure the current
                    // workbench page has an editor area (typically the ddms perspective doesn't).
                    IWorkbench workbench = PlatformUI.getWorkbench();
                    IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
                    IWorkbenchPage page = window.getActivePage();
                    if (page.isEditorAreaVisible() == false) {
                        // no editor area? we open the java perspective.
                        new OpenJavaPerspectiveAction().run();
                    }

                    IEditorPart editor = JavaUI.openInEditor(result);
                    if (editor instanceof ITextEditor) {
                        // get the text editor that was just opened.
                        ITextEditor textEditor = (ITextEditor)editor;

                        IEditorInput input = textEditor.getEditorInput();

                        // get the location of the line to show.
                        IDocumentProvider documentProvider = textEditor.getDocumentProvider();
                        IDocument document = documentProvider.getDocument(input);
                        IRegion lineInfo = document.getLineInformation(line - 1);

                        // select and reveal the line.
                        textEditor.selectAndReveal(lineInfo.getOffset(), lineInfo.getLength());
                    }

                    return true;
                }
            } catch (JavaModelException e) {
            } catch (PartInitException e) {
            } catch (BadLocationException e) {
            }
        }

        return false;
    }

    @Override
    public boolean revealMethod(String fqmn, String fileName, int lineNumber, String perspective) {
        SearchEngine se = new SearchEngine();
        SearchPattern searchPattern = SearchPattern.createPattern(
                fqmn,
                IJavaSearchConstants.METHOD,
                IJavaSearchConstants.DECLARATIONS,
                SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
        MethodSearchRequestor requestor = new MethodSearchRequestor(perspective);
        try {
            se.search(searchPattern,
                    new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
                    SearchEngine.createWorkspaceScope(),
                    requestor,
                    new NullProgressMonitor());
        } catch (CoreException e) {
            return false;
        }

        return requestor.didMatch();
    }

    private static class MethodSearchRequestor extends SearchRequestor {
        private boolean mFoundMatch = false;
        private final String mPerspective;

        public MethodSearchRequestor(String perspective) {
            mPerspective = perspective;
        }

        public boolean didMatch() {
            return mFoundMatch;
        }

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object element = match.getElement();
            if (element instanceof IMethod && !mFoundMatch) {
                if (mPerspective != null) {
                    SourceRevealer.switchToPerspective(mPerspective);
                }

                IMethod method = (IMethod) element;
                JavaUI.openInEditor(method);
                mFoundMatch = true;
            }
        }
    }

    public static void switchToPerspective(String perspectiveId) {
        IWorkbench workbench = PlatformUI.getWorkbench();
        IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
        IPerspectiveRegistry perspectiveRegistry = workbench.getPerspectiveRegistry();
        if (perspectiveId != null
                && perspectiveId.length() > 0
                && perspectiveRegistry.findPerspectiveWithId(perspectiveId) != null) {
            try {
                workbench.showPerspective(perspectiveId, window);
            } catch (WorkbenchException e) {
                // ignore exception, perspective won't be switched
            }
        }
    }

}