1. Suppress ArrayIndexOutOfBoundsException occurring in a certain case of formatting...
[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 import net.sourceforge.phpdt.core.IJavaElement;
14 import net.sourceforge.phpdt.core.JavaModelException;
15 import net.sourceforge.phpdt.internal.ui.actions.SelectionConverter;
16 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
17
18 import org.eclipse.jface.text.IRegion;
19 import org.eclipse.jface.text.ITextViewer;
20 import org.eclipse.jface.text.Region;
21 import org.eclipse.jface.text.information.IInformationProvider;
22 import org.eclipse.jface.text.information.IInformationProviderExtension;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.ui.IEditorPart;
25
26 /**
27  * Provides a Java element to be displayed in by an information presenter.
28  */
29 public class JavaElementProvider implements IInformationProvider,
30                 IInformationProviderExtension {
31
32         private PHPEditor fEditor;
33
34         private boolean fUseCodeResolve;
35
36         public JavaElementProvider(IEditorPart editor) {
37                 fUseCodeResolve = false;
38                 if (editor instanceof PHPEditor)
39                         fEditor = (PHPEditor) editor;
40         }
41
42         public JavaElementProvider(IEditorPart editor, boolean useCodeResolve) {
43                 this(editor);
44                 fUseCodeResolve = useCodeResolve;
45         }
46
47         /*
48          * @see IInformationProvider#getSubject(ITextViewer, int)
49          */
50         public IRegion getSubject(ITextViewer textViewer, int offset) {
51                 if (textViewer != null && fEditor != null) {
52                         IRegion region = JavaWordFinder.findWord(textViewer.getDocument(),
53                                         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
79                                                 .getStructuredSelection(fEditor);
80                                 if (!sel.isEmpty())
81                                         return sel.getFirstElement();
82                         }
83                         IJavaElement element = SelectionConverter
84                                         .getElementAtOffset(fEditor);
85                         if (element != null)
86                                 return element;
87                         return SelectionConverter.getInput(fEditor);
88                 } catch (JavaModelException e) {
89                         return null;
90                 }
91         }
92 }