KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > java > hover > BestMatchHover


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text.java.hover;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jface.text.IInformationControlCreator;
18 import org.eclipse.jface.text.IRegion;
19 import org.eclipse.jface.text.ITextHover;
20 import org.eclipse.jface.text.ITextHoverExtension;
21 import org.eclipse.jface.text.ITextViewer;
22 import org.eclipse.jface.text.information.IInformationProviderExtension2;
23
24 import org.eclipse.ui.IEditorPart;
25
26 import org.eclipse.jdt.ui.PreferenceConstants;
27 import org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover;
28
29 import org.eclipse.jdt.internal.ui.JavaPlugin;
30
31 /**
32  * Caution: this implementation is a layer breaker and contains some "shortcuts"
33  */

34 public class BestMatchHover extends AbstractJavaEditorTextHover implements ITextHoverExtension, IInformationProviderExtension2 {
35
36     private List JavaDoc fTextHoverSpecifications;
37     private List JavaDoc fInstantiatedTextHovers;
38     private ITextHover fBestHover;
39
40     public BestMatchHover() {
41         installTextHovers();
42     }
43
44     public BestMatchHover(IEditorPart editor) {
45         this();
46         setEditor(editor);
47     }
48
49     /**
50      * Installs all text hovers.
51      */

52     private void installTextHovers() {
53
54         // initialize lists - indicates that the initialization happened
55
fTextHoverSpecifications= new ArrayList JavaDoc(2);
56         fInstantiatedTextHovers= new ArrayList JavaDoc(2);
57
58         // populate list
59
JavaEditorTextHoverDescriptor[] hoverDescs= JavaPlugin.getDefault().getJavaEditorTextHoverDescriptors();
60         for (int i= 0; i < hoverDescs.length; i++) {
61             // ensure that we don't add ourselves to the list
62
if (!PreferenceConstants.ID_BESTMATCH_HOVER.equals(hoverDescs[i].getId()))
63                 fTextHoverSpecifications.add(hoverDescs[i]);
64         }
65     }
66
67     private void checkTextHovers() {
68         if (fTextHoverSpecifications.size() == 0)
69             return;
70
71         for (Iterator JavaDoc iterator= new ArrayList JavaDoc(fTextHoverSpecifications).iterator(); iterator.hasNext(); ) {
72             JavaEditorTextHoverDescriptor spec= (JavaEditorTextHoverDescriptor) iterator.next();
73
74             IJavaEditorTextHover hover= spec.createTextHover();
75             if (hover != null) {
76                 hover.setEditor(getEditor());
77                 addTextHover(hover);
78                 fTextHoverSpecifications.remove(spec);
79             }
80         }
81     }
82
83     protected void addTextHover(ITextHover hover) {
84         if (!fInstantiatedTextHovers.contains(hover))
85             fInstantiatedTextHovers.add(hover);
86     }
87
88     /*
89      * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
90      */

91     public String JavaDoc getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
92
93         checkTextHovers();
94         fBestHover= null;
95
96         if (fInstantiatedTextHovers == null)
97             return null;
98
99         for (Iterator JavaDoc iterator= fInstantiatedTextHovers.iterator(); iterator.hasNext(); ) {
100             ITextHover hover= (ITextHover)iterator.next();
101
102             String JavaDoc s= hover.getHoverInfo(textViewer, hoverRegion);
103             if (s != null && s.trim().length() > 0) {
104                 fBestHover= hover;
105                 return s;
106             }
107         }
108
109         return null;
110     }
111
112     /*
113      * @see org.eclipse.jface.text.ITextHoverExtension#getHoverControlCreator()
114      * @since 3.0
115      */

116     public IInformationControlCreator getHoverControlCreator() {
117         if (fBestHover instanceof ITextHoverExtension)
118             return ((ITextHoverExtension)fBestHover).getHoverControlCreator();
119
120         return null;
121     }
122
123     /*
124      * @see org.eclipse.jface.text.information.IInformationProviderExtension2#getInformationPresenterControlCreator()
125      * @since 3.0
126      */

127     public IInformationControlCreator getInformationPresenterControlCreator() {
128         if (fBestHover instanceof IInformationProviderExtension2)
129             return ((IInformationProviderExtension2)fBestHover).getInformationPresenterControlCreator();
130
131         return null;
132     }
133 }
134
Popular Tags