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