cb927fb2503df92281197c707b3325f0dc721d72
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / java / hover / BestMatchHover.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.java.hover;
12
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16
17 import net.sourceforge.phpdt.ui.PreferenceConstants;
18 import net.sourceforge.phpdt.ui.text.java.hover.IJavaEditorTextHover;
19 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
20
21 import org.eclipse.jface.text.IInformationControlCreator;
22 import org.eclipse.jface.text.IRegion;
23 import org.eclipse.jface.text.ITextHover;
24 import org.eclipse.jface.text.ITextHoverExtension;
25 import org.eclipse.jface.text.ITextViewer;
26 import org.eclipse.jface.text.information.IInformationProviderExtension2;
27 import org.eclipse.ui.IEditorPart;
28
29
30 /**
31  * Caution: this implementation is a layer breaker and contains some "shortcuts"
32  */
33 public class BestMatchHover extends AbstractJavaEditorTextHover implements ITextHoverExtension, IInformationProviderExtension2 {
34
35         private List fTextHoverSpecifications;
36         private List fInstantiatedTextHovers;
37         private ITextHover fBestHover;
38
39         public BestMatchHover() {
40                 installTextHovers();
41         }
42
43         public BestMatchHover(IEditorPart editor) {
44                 this();
45                 setEditor(editor);
46         }
47         
48         /**
49          * Installs all text hovers.
50          */
51         private void installTextHovers() {
52                 
53                 // initialize lists - indicates that the initialization happened
54                 fTextHoverSpecifications= new ArrayList(2);
55                 fInstantiatedTextHovers= new ArrayList(2);
56
57                 // populate list
58                 JavaEditorTextHoverDescriptor[] hoverDescs= PHPeclipsePlugin.getDefault().getJavaEditorTextHoverDescriptors();
59                 for (int i= 0; i < hoverDescs.length; i++) {
60                         // ensure that we don't add ourselves to the list
61                         if (!PreferenceConstants.ID_BESTMATCH_HOVER.equals(hoverDescs[i].getId()))
62                                 fTextHoverSpecifications.add(hoverDescs[i]);
63                 }
64         }       
65
66         private void checkTextHovers() {
67                 if (fTextHoverSpecifications.size() == 0)
68                         return;
69
70                 for (Iterator iterator= new ArrayList(fTextHoverSpecifications).iterator(); iterator.hasNext(); ) {
71                         JavaEditorTextHoverDescriptor spec= (JavaEditorTextHoverDescriptor) iterator.next();
72
73                         IJavaEditorTextHover hover= spec.createTextHover();
74                         if (hover != null) {
75                                 hover.setEditor(getEditor());
76                                 addTextHover(hover);
77                                 fTextHoverSpecifications.remove(spec);
78                         }
79                 }
80         }
81
82         protected void addTextHover(ITextHover hover) {
83                 if (!fInstantiatedTextHovers.contains(hover))
84                         fInstantiatedTextHovers.add(hover);
85         }
86
87         /*
88          * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
89          */
90         public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
91
92                 checkTextHovers();
93                 fBestHover= null;
94
95                 if (fInstantiatedTextHovers == null)
96                         return null;
97
98                 for (Iterator iterator= fInstantiatedTextHovers.iterator(); iterator.hasNext(); ) {
99                         ITextHover hover= (ITextHover)iterator.next();
100
101                         String s= hover.getHoverInfo(textViewer, hoverRegion);
102                         if (s != null && s.trim().length() > 0) {
103                                 fBestHover= hover;
104                                 return s;
105                         }
106                 }
107
108                 return null;
109         }
110
111         /*
112          * @see org.eclipse.jface.text.ITextHoverExtension#getInformationControlCreator()
113          * @since 3.0
114          */
115         public IInformationControlCreator getInformationControlCreator() {
116                 if (fBestHover instanceof ITextHoverExtension)
117                         return ((ITextHoverExtension)fBestHover).getInformationControlCreator();
118
119                 return null;
120         }
121
122         /*
123          * @see org.eclipse.jface.text.information.IInformationProviderExtension2#getInformationPresenterControlCreator()
124          * @since 3.0
125          */
126         public IInformationControlCreator getInformationPresenterControlCreator() {
127                 if (fBestHover instanceof IInformationProviderExtension2)
128                         return ((IInformationProviderExtension2)fBestHover).getInformationPresenterControlCreator();
129
130                 return null;
131         }
132 }