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