01: /*******************************************************************************
02: * Copyright (c) 2005, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.ui.search.dependencies;
11:
12: import java.util.HashMap;
13:
14: import org.eclipse.core.resources.IFile;
15: import org.eclipse.core.resources.IMarker;
16: import org.eclipse.core.runtime.CoreException;
17: import org.eclipse.jdt.core.IJavaElement;
18: import org.eclipse.jdt.core.JavaModelException;
19: import org.eclipse.jdt.ui.JavaUI;
20: import org.eclipse.search.ui.NewSearchUI;
21: import org.eclipse.search.ui.text.Match;
22: import org.eclipse.ui.IEditorPart;
23: import org.eclipse.ui.PartInitException;
24: import org.eclipse.ui.ide.IDE;
25: import org.eclipse.ui.texteditor.ITextEditor;
26:
27: public class JavaEditorOpener {
28:
29: public static IEditorPart open(Match match, int offset, int length,
30: boolean activate) throws PartInitException,
31: JavaModelException {
32: IEditorPart editor = null;
33: Object element = match.getElement();
34: if (element instanceof IJavaElement) {
35: editor = JavaUI.openInEditor((IJavaElement) element);
36: }
37: if (editor != null && activate)
38: editor.getEditorSite().getPage().activate(editor);
39: if (editor instanceof ITextEditor) {
40: ITextEditor textEditor = (ITextEditor) editor;
41: textEditor.selectAndReveal(offset, length);
42: } else if (editor != null) {
43: if (element instanceof IFile) {
44: IFile file = (IFile) element;
45: showWithMarker(editor, file, offset, length);
46: }
47: }
48: return editor;
49: }
50:
51: private static void showWithMarker(IEditorPart editor, IFile file,
52: int offset, int length) throws PartInitException {
53: try {
54: IMarker marker = file
55: .createMarker(NewSearchUI.SEARCH_MARKER);
56: HashMap attributes = new HashMap(4);
57: attributes.put(IMarker.CHAR_START, new Integer(offset));
58: attributes.put(IMarker.CHAR_END, new Integer(offset
59: + length));
60: marker.setAttributes(attributes);
61: IDE.gotoMarker(editor, marker);
62: marker.delete();
63: } catch (CoreException e) {
64: }
65: }
66:
67: }
|