KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > JavaElementHyperlinkDetector


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.internal.ui.javaeditor;
12
13 import org.eclipse.jface.action.IAction;
14
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.IRegion;
17 import org.eclipse.jface.text.ITextViewer;
18 import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector;
19 import org.eclipse.jface.text.hyperlink.IHyperlink;
20
21 import org.eclipse.ui.texteditor.ITextEditor;
22
23 import org.eclipse.jdt.core.ICodeAssist;
24 import org.eclipse.jdt.core.IJavaElement;
25 import org.eclipse.jdt.core.JavaModelException;
26
27 import org.eclipse.jdt.internal.ui.text.JavaWordFinder;
28
29
30 /**
31  * Java element hyperlink detector.
32  *
33  * @since 3.1
34  */

35 public class JavaElementHyperlinkDetector extends AbstractHyperlinkDetector {
36
37     /*
38      * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion, boolean)
39      */

40     public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
41         ITextEditor textEditor= (ITextEditor)getAdapter(ITextEditor.class);
42         if (region == null || canShowMultipleHyperlinks || !(textEditor instanceof JavaEditor))
43             return null;
44
45         IAction openAction= textEditor.getAction("OpenEditor"); //$NON-NLS-1$
46
if (openAction == null)
47             return null;
48
49         int offset= region.getOffset();
50
51         IJavaElement input= EditorUtility.getEditorInputJavaElement(textEditor, false);
52         if (input == null)
53             return null;
54
55         try {
56             IDocument document= textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
57             IRegion wordRegion= JavaWordFinder.findWord(document, offset);
58             if (wordRegion == null)
59                 return null;
60             
61             IJavaElement[] elements= null;
62             elements= ((ICodeAssist) input).codeSelect(wordRegion.getOffset(), wordRegion.getLength());
63             if (elements != null && elements.length > 0)
64                 return new IHyperlink[] {new JavaElementHyperlink(wordRegion, openAction)};
65         } catch (JavaModelException e) {
66             return null;
67         }
68
69         return null;
70     }
71
72 }
73
Popular Tags