001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.text.java.hover;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.jface.text.IInformationControlCreator;
017: import org.eclipse.jface.text.IRegion;
018: import org.eclipse.jface.text.ITextHover;
019: import org.eclipse.jface.text.ITextHoverExtension;
020: import org.eclipse.jface.text.ITextViewer;
021: import org.eclipse.jface.text.information.IInformationProviderExtension2;
022:
023: import org.eclipse.ui.IEditorPart;
024:
025: import org.eclipse.jdt.ui.PreferenceConstants;
026: import org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover;
027:
028: import org.eclipse.jdt.internal.ui.JavaPlugin;
029:
030: /**
031: * Caution: this implementation is a layer breaker and contains some "shortcuts"
032: */
033: public class BestMatchHover extends AbstractJavaEditorTextHover
034: implements ITextHoverExtension, IInformationProviderExtension2 {
035:
036: private List fTextHoverSpecifications;
037: private List fInstantiatedTextHovers;
038: private ITextHover fBestHover;
039:
040: public BestMatchHover() {
041: installTextHovers();
042: }
043:
044: public BestMatchHover(IEditorPart editor) {
045: this ();
046: setEditor(editor);
047: }
048:
049: /**
050: * Installs all text hovers.
051: */
052: private void installTextHovers() {
053:
054: // initialize lists - indicates that the initialization happened
055: fTextHoverSpecifications = new ArrayList(2);
056: fInstantiatedTextHovers = new ArrayList(2);
057:
058: // populate list
059: JavaEditorTextHoverDescriptor[] hoverDescs = JavaPlugin
060: .getDefault().getJavaEditorTextHoverDescriptors();
061: for (int i = 0; i < hoverDescs.length; i++) {
062: // ensure that we don't add ourselves to the list
063: if (!PreferenceConstants.ID_BESTMATCH_HOVER
064: .equals(hoverDescs[i].getId()))
065: fTextHoverSpecifications.add(hoverDescs[i]);
066: }
067: }
068:
069: private void checkTextHovers() {
070: if (fTextHoverSpecifications.size() == 0)
071: return;
072:
073: for (Iterator iterator = new ArrayList(fTextHoverSpecifications)
074: .iterator(); iterator.hasNext();) {
075: JavaEditorTextHoverDescriptor spec = (JavaEditorTextHoverDescriptor) iterator
076: .next();
077:
078: IJavaEditorTextHover hover = spec.createTextHover();
079: if (hover != null) {
080: hover.setEditor(getEditor());
081: addTextHover(hover);
082: fTextHoverSpecifications.remove(spec);
083: }
084: }
085: }
086:
087: protected void addTextHover(ITextHover hover) {
088: if (!fInstantiatedTextHovers.contains(hover))
089: fInstantiatedTextHovers.add(hover);
090: }
091:
092: /*
093: * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
094: */
095: public String getHoverInfo(ITextViewer textViewer,
096: IRegion hoverRegion) {
097:
098: checkTextHovers();
099: 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)
125: .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: }
|