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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.java.hover;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.Comparator;
17 import java.util.Iterator;
18 import java.util.List;
20 import net.sourceforge.phpdt.ui.PreferenceConstants;
21 import net.sourceforge.phpdt.ui.text.java.hover.IJavaEditorTextHover;
22 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24 import org.eclipse.jface.text.IRegion;
25 import org.eclipse.jface.text.ITextHover;
26 import org.eclipse.jface.text.ITextViewer;
27 import org.eclipse.ui.IEditorPart;
30 * Caution: this implementation is a layer breaker and contains some "shortcuts"
32 public class BestMatchHover extends AbstractJavaEditorTextHover {
34 private static class JavaEditorTextHoverDescriptorComparator implements Comparator {
37 * @see Comparator#compare(Object, Object)
39 public int compare(Object object0, Object object1) {
41 JavaEditorTextHoverDescriptor element0= (JavaEditorTextHoverDescriptor)object0;
42 JavaEditorTextHoverDescriptor element1= (JavaEditorTextHoverDescriptor)object1;
44 String id0= element0.getId();
45 String id1= element1.getId();
47 if (id0 != null && id0.equals(id1))
50 if (id0 != null && JavaProblemHover.isJavaProblemHover(id0))
53 if (id1 != null && JavaProblemHover.isJavaProblemHover(id1))
57 // now compare non-problem hovers
58 if (element0.dependsOn(element1))
61 if (element1.dependsOn(element0))
68 protected String fCurrentPerspectiveId;
69 protected List fTextHoverSpecifications;
70 protected List fInstantiatedTextHovers;
73 public BestMatchHover() {
77 public BestMatchHover(IEditorPart editor) {
83 * Installs all text hovers.
85 private void installTextHovers() {
87 // initialize lists - indicates that the initialization happened
88 fTextHoverSpecifications= new ArrayList(2);
89 fInstantiatedTextHovers= new ArrayList(2);
92 JavaEditorTextHoverDescriptor[] hoverDescs= PHPeclipsePlugin.getDefault().getJavaEditorTextHoverDescriptors();
93 for (int i= 0; i < hoverDescs.length; i++) {
94 // ensure that we don't add ourselves to the list
95 if (!PreferenceConstants.ID_BESTMATCH_HOVER.equals(hoverDescs[i].getId()))
96 fTextHoverSpecifications.add(hoverDescs[i]);
98 Collections.sort(fTextHoverSpecifications, new JavaEditorTextHoverDescriptorComparator());
101 private void checkTextHovers() {
102 if (fTextHoverSpecifications.size() == 0)
105 for (Iterator iterator= new ArrayList(fTextHoverSpecifications).iterator(); iterator.hasNext(); ) {
106 JavaEditorTextHoverDescriptor spec= (JavaEditorTextHoverDescriptor) iterator.next();
108 IJavaEditorTextHover hover= spec.createTextHover();
110 hover.setEditor(getEditor());
112 fTextHoverSpecifications.remove(spec);
117 protected void addTextHover(ITextHover hover) {
118 if (!fInstantiatedTextHovers.contains(hover))
119 fInstantiatedTextHovers.add(hover);
123 * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
125 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
129 if (fInstantiatedTextHovers == null)
132 for (Iterator iterator= fInstantiatedTextHovers.iterator(); iterator.hasNext(); ) {
133 ITextHover hover= (ITextHover) iterator.next();
135 String s= hover.getHoverInfo(textViewer, hoverRegion);
136 if (s != null && s.trim().length() > 0)