bugfix 1412408
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / JavaElementProvider.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
12
13
14 import net.sourceforge.phpdt.core.IJavaElement;
15 import net.sourceforge.phpdt.core.JavaModelException;
16 import net.sourceforge.phpdt.internal.ui.actions.SelectionConverter;
17 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
18
19 import org.eclipse.jface.text.IRegion;
20 import org.eclipse.jface.text.ITextViewer;
21 import org.eclipse.jface.text.Region;
22 import org.eclipse.jface.text.information.IInformationProvider;
23 import org.eclipse.jface.text.information.IInformationProviderExtension;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.ui.IEditorPart;
26
27
28
29 /**
30  * Provides a Java element to be displayed in by an information presenter.
31  */
32 public class JavaElementProvider implements IInformationProvider, IInformationProviderExtension {
33
34         private PHPEditor fEditor;
35         private boolean fUseCodeResolve;
36
37         public JavaElementProvider(IEditorPart editor) {
38                 fUseCodeResolve= false;
39                 if (editor instanceof PHPEditor)
40                         fEditor= (PHPEditor)editor;
41         }
42
43         public JavaElementProvider(IEditorPart editor, boolean useCodeResolve) {
44                 this(editor);
45                 fUseCodeResolve= useCodeResolve;
46         }
47         
48         /*
49          * @see IInformationProvider#getSubject(ITextViewer, int)
50          */
51         public IRegion getSubject(ITextViewer textViewer, int offset) {
52                 if (textViewer != null && fEditor != null) {
53                         IRegion region= JavaWordFinder.findWord(textViewer.getDocument(), offset);
54                         if (region != null)
55                                 return region;
56                         else
57                                 return new Region(offset, 0);
58                 }
59                 return null;    
60         }
61
62         /*
63          * @see IInformationProvider#getInformation(ITextViewer, IRegion)
64          */
65         public String getInformation(ITextViewer textViewer, IRegion subject) {
66                 return getInformation2(textViewer, subject).toString();
67         }
68
69         /*
70          * @see IInformationProviderExtension#getElement(ITextViewer, IRegion)
71          */
72         public Object getInformation2(ITextViewer textViewer, IRegion subject) {
73                 if (fEditor == null)
74                         return null;
75                 
76                 try {
77                         if (fUseCodeResolve) {
78                                 IStructuredSelection sel= SelectionConverter.getStructuredSelection(fEditor);
79                                 if (!sel.isEmpty())
80                                         return sel.getFirstElement();
81                         }
82                         IJavaElement element= SelectionConverter.getElementAtOffset(fEditor);
83                         if (element != null)
84                                 return element;
85                         return SelectionConverter.getInput(fEditor);
86                 } catch (JavaModelException e) {
87                         return null;
88                 }
89         }
90 }