KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > search > dependencies > JavaEditorOpener


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.search.dependencies;
12
13 import java.util.HashMap JavaDoc;
14
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IMarker;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.jdt.core.IJavaElement;
19 import org.eclipse.jdt.core.JavaModelException;
20 import org.eclipse.jdt.ui.JavaUI;
21 import org.eclipse.search.ui.NewSearchUI;
22 import org.eclipse.search.ui.text.Match;
23 import org.eclipse.ui.IEditorPart;
24 import org.eclipse.ui.PartInitException;
25 import org.eclipse.ui.ide.IDE;
26 import org.eclipse.ui.texteditor.ITextEditor;
27
28
29 public class JavaEditorOpener {
30     
31     public static IEditorPart open(Match match, int offset, int length, boolean activate) throws PartInitException, JavaModelException {
32         IEditorPart editor = null;
33         Object JavaDoc 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, int offset, int length) throws PartInitException {
52         try {
53             IMarker marker= file.createMarker(NewSearchUI.SEARCH_MARKER);
54             HashMap JavaDoc attributes= new HashMap JavaDoc(4);
55             attributes.put(IMarker.CHAR_START, new Integer JavaDoc(offset));
56             attributes.put(IMarker.CHAR_END, new Integer JavaDoc(offset + length));
57             marker.setAttributes(attributes);
58             IDE.gotoMarker(editor, marker);
59             marker.delete();
60         } catch (CoreException e) {
61         }
62     }
63
64 }
65
Popular Tags