KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > JavaElementProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.internal.ui.text;
12
13
14 import org.eclipse.jface.viewers.IStructuredSelection;
15
16 import org.eclipse.jface.text.IRegion;
17 import org.eclipse.jface.text.ITextViewer;
18 import org.eclipse.jface.text.Region;
19 import org.eclipse.jface.text.information.IInformationProvider;
20 import org.eclipse.jface.text.information.IInformationProviderExtension;
21
22 import org.eclipse.ui.IEditorPart;
23
24 import org.eclipse.jdt.core.IJavaElement;
25 import org.eclipse.jdt.core.JavaModelException;
26
27 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
28 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
29 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
30
31 /**
32  * Provides a Java element to be displayed in by an information presenter.
33  */

34 public class JavaElementProvider implements IInformationProvider, IInformationProviderExtension {
35
36     private JavaEditor fEditor;
37     private boolean fUseCodeResolve;
38
39     public JavaElementProvider(IEditorPart editor) {
40         fUseCodeResolve= false;
41         if (editor instanceof JavaEditor)
42             fEditor= (JavaEditor)editor;
43     }
44
45     public JavaElementProvider(IEditorPart editor, boolean useCodeResolve) {
46         this(editor);
47         fUseCodeResolve= useCodeResolve;
48     }
49
50     /*
51      * @see IInformationProvider#getSubject(ITextViewer, int)
52      */

53     public IRegion getSubject(ITextViewer textViewer, int offset) {
54         if (textViewer != null && fEditor != null) {
55             IRegion region= JavaWordFinder.findWord(textViewer.getDocument(), offset);
56             if (region != null)
57                 return region;
58             else
59                 return new Region(offset, 0);
60         }
61         return null;
62     }
63
64     /*
65      * @see IInformationProvider#getInformation(ITextViewer, IRegion)
66      */

67     public String JavaDoc getInformation(ITextViewer textViewer, IRegion subject) {
68         return getInformation2(textViewer, subject).toString();
69     }
70
71     /*
72      * @see IInformationProviderExtension#getElement(ITextViewer, IRegion)
73      */

74     public Object JavaDoc getInformation2(ITextViewer textViewer, IRegion subject) {
75         if (fEditor == null)
76             return null;
77
78         try {
79             if (fUseCodeResolve) {
80                 IStructuredSelection sel= SelectionConverter.getStructuredSelection(fEditor);
81                 if (!sel.isEmpty())
82                     return sel.getFirstElement();
83             }
84             IJavaElement element= SelectionConverter.getElementAtOffset(fEditor);
85             if (element != null)
86                 return element;
87             
88             return EditorUtility.getEditorInputJavaElement(fEditor, false);
89         } catch (JavaModelException e) {
90             return null;
91         }
92     }
93 }
94
Popular Tags