new version with WorkingCopy Management
[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
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;
19
20 import net.sourceforge.phpdt.ui.PreferenceConstants;
21 import net.sourceforge.phpdt.ui.text.java.hover.IJavaEditorTextHover;
22 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
23
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;
28
29 /**
30  * Caution: this implementation is a layer breaker and contains some "shortcuts"
31  */
32 public class BestMatchHover extends AbstractJavaEditorTextHover {
33
34         private static class JavaEditorTextHoverDescriptorComparator implements Comparator {
35                 
36                 /*
37                  * @see Comparator#compare(Object, Object)
38                  */
39                 public int compare(Object object0, Object object1) {
40
41                         JavaEditorTextHoverDescriptor element0= (JavaEditorTextHoverDescriptor)object0;
42                         JavaEditorTextHoverDescriptor element1= (JavaEditorTextHoverDescriptor)object1; 
43
44                         String id0=     element0.getId();
45                         String id1= element1.getId();
46                         
47                         if (id0 != null && id0.equals(id1))
48                                 return 0;
49                         
50                         if (id0 != null && JavaProblemHover.isJavaProblemHover(id0))
51                                 return -1;
52
53                         if (id1 != null && JavaProblemHover.isJavaProblemHover(id1))
54                                 return +1;
55
56
57                         // now compare non-problem hovers
58                         if (element0.dependsOn(element1))
59                                 return -1;
60
61                         if (element1.dependsOn(element0))
62                                 return +1;
63                         
64                         return 0;
65                 }
66         }
67         
68         protected String fCurrentPerspectiveId;
69         protected List fTextHoverSpecifications;
70         protected List fInstantiatedTextHovers;
71
72
73         public BestMatchHover() {
74                 installTextHovers();
75         }
76
77         public BestMatchHover(IEditorPart editor) {
78                 this();
79                 setEditor(editor);
80         }
81         
82         /**
83          * Installs all text hovers.
84          */
85         private void installTextHovers() {
86                 
87                 // initialize lists - indicates that the initialization happened
88                 fTextHoverSpecifications= new ArrayList(2);
89                 fInstantiatedTextHovers= new ArrayList(2);
90
91                 // populate list
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]);
97                 }
98                 Collections.sort(fTextHoverSpecifications, new JavaEditorTextHoverDescriptorComparator());
99         }       
100
101         private void checkTextHovers() {
102                 if (fTextHoverSpecifications.size() == 0)
103                         return;
104
105                 for (Iterator iterator= new ArrayList(fTextHoverSpecifications).iterator(); iterator.hasNext(); ) {
106                         JavaEditorTextHoverDescriptor spec= (JavaEditorTextHoverDescriptor) iterator.next();
107
108                         IJavaEditorTextHover hover= spec.createTextHover();
109                         if (hover != null) {
110                                 hover.setEditor(getEditor());
111                                 addTextHover(hover);
112                                 fTextHoverSpecifications.remove(spec);
113                         }
114                 }
115         }
116
117         protected void addTextHover(ITextHover hover) {
118                 if (!fInstantiatedTextHovers.contains(hover))
119                         fInstantiatedTextHovers.add(hover);
120         }
121
122         /*
123          * @see ITextHover#getHoverInfo(ITextViewer, IRegion)
124          */
125         public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
126
127                 checkTextHovers();
128
129                 if (fInstantiatedTextHovers == null)
130                         return null;
131
132                 for (Iterator iterator= fInstantiatedTextHovers.iterator(); iterator.hasNext(); ) {
133                         ITextHover hover= (ITextHover) iterator.next();
134
135                         String s= hover.getHoverInfo(textViewer, hoverRegion);
136                         if (s != null && s.trim().length() > 0)
137                                 return s;
138                 }
139
140                 return null;
141         }
142 }