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