1) Moved net.sourceforge.phpeclipse.ui\src\net\sourceforge\phpdt back to net.sourcefo...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / java / hover / JavaEditorTextHoverDescriptor.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
12 package net.sourceforge.phpdt.internal.ui.text.java.hover;
13
14 import java.text.Collator;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.StringTokenizer;
20
21 import net.sourceforge.phpdt.ui.PreferenceConstants;
22 import net.sourceforge.phpdt.ui.text.java.hover.IJavaEditorTextHover;
23 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24 import net.sourceforge.phpeclipse.phpeditor.EditorUtility;
25
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IConfigurationElement;
28 import org.eclipse.core.runtime.IExtensionRegistry;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.Platform;
31 import org.eclipse.core.runtime.Status;
32 //incastrix
33 //import org.eclipse.jface.text.Assert;
34 import org.eclipse.core.runtime.Assert;
35 import org.eclipse.swt.SWT;
36 import org.osgi.framework.Bundle;
37
38 /**
39  * Describes a Java editor text hover.
40  * 
41  * @since 2.1
42  */
43 public class JavaEditorTextHoverDescriptor implements Comparable {
44
45         private static final String JAVA_EDITOR_TEXT_HOVER_EXTENSION_POINT = "net.sourceforge.phpeclipse.phpEditorTextHovers"; //$NON-NLS-1$
46
47         private static final String HOVER_TAG = "hover"; //$NON-NLS-1$
48
49         private static final String ID_ATTRIBUTE = "id"; //$NON-NLS-1$
50
51         private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$
52
53         private static final String LABEL_ATTRIBUTE = "label"; //$NON-NLS-1$
54
55         private static final String ACTIVATE_PLUG_IN_ATTRIBUTE = "activate"; //$NON-NLS-1$
56
57         private static final String DESCRIPTION_ATTRIBUTE = "description"; //$NON-NLS-1$
58
59         public static final String NO_MODIFIER = "0"; //$NON-NLS-1$
60
61         public static final String DISABLED_TAG = "!"; //$NON-NLS-1$
62
63         public static final String VALUE_SEPARATOR = ";"; //$NON-NLS-1$
64
65         private int fStateMask;
66
67         private String fModifierString;
68
69         private boolean fIsEnabled;
70
71         private IConfigurationElement fElement;
72
73         /**
74          * Returns all Java editor text hovers contributed to the workbench.
75          */
76         public static JavaEditorTextHoverDescriptor[] getContributedHovers() {
77                 IExtensionRegistry registry = Platform.getExtensionRegistry();
78                 IConfigurationElement[] elements = registry
79                                 .getConfigurationElementsFor(JAVA_EDITOR_TEXT_HOVER_EXTENSION_POINT);
80                 JavaEditorTextHoverDescriptor[] hoverDescs = createDescriptors(elements);
81                 initializeFromPreferences(hoverDescs);
82                 return hoverDescs;
83         }
84
85         /**
86          * Computes the state mask for the given modifier string.
87          * 
88          * @param modifiers
89          *            the string with the modifiers, separated by '+', '-', ';', ','
90          *            or '.'
91          * @return the state mask or -1 if the input is invalid
92          */
93         public static int computeStateMask(String modifiers) {
94                 if (modifiers == null)
95                         return -1;
96
97                 if (modifiers.length() == 0)
98                         return SWT.NONE;
99
100                 int stateMask = 0;
101                 StringTokenizer modifierTokenizer = new StringTokenizer(modifiers,
102                                 ",;.:+-* "); //$NON-NLS-1$
103                 while (modifierTokenizer.hasMoreTokens()) {
104                         int modifier = EditorUtility
105                                         .findLocalizedModifier(modifierTokenizer.nextToken());
106                         if (modifier == 0 || (stateMask & modifier) == modifier)
107                                 return -1;
108                         stateMask = stateMask | modifier;
109                 }
110                 return stateMask;
111         }
112
113         /**
114          * Creates a new Java Editor text hover descriptor from the given
115          * configuration element.
116          */
117         private JavaEditorTextHoverDescriptor(IConfigurationElement element) {
118                 Assert.isNotNull(element);
119                 fElement = element;
120         }
121
122         /**
123          * Creates the Java editor text hover.
124          */
125         public IJavaEditorTextHover createTextHover() {
126                 String pluginId = fElement.getDeclaringExtension().getNamespace();
127                 boolean isHoversPlugInActivated = Platform.getBundle(pluginId)
128                                 .getState() == Bundle.ACTIVE;
129                 if (isHoversPlugInActivated || canActivatePlugIn()) {
130                         try {
131                                 return (IJavaEditorTextHover) fElement
132                                                 .createExecutableExtension(CLASS_ATTRIBUTE);
133                         } catch (CoreException x) {
134                                 PHPeclipsePlugin.log(new Status(IStatus.ERROR, PHPeclipsePlugin
135                                                 .getPluginId(), 0, JavaHoverMessages
136                                                 .getString("JavaTextHover.createTextHover"), null)); //$NON-NLS-1$
137                         }
138                 }
139
140                 return null;
141         }
142
143         // ---- XML Attribute accessors
144         // ---------------------------------------------
145
146         /**
147          * Returns the hover's id.
148          */
149         public String getId() {
150                 return fElement.getAttribute(ID_ATTRIBUTE);
151         }
152
153         /**
154          * Returns the hover's class name.
155          */
156         public String getHoverClassName() {
157                 return fElement.getAttribute(CLASS_ATTRIBUTE);
158         }
159
160         /**
161          * Returns the hover's label.
162          */
163         public String getLabel() {
164                 String label = fElement.getAttribute(LABEL_ATTRIBUTE);
165                 if (label != null)
166                         return label;
167
168                 // Return simple class name
169                 label = getHoverClassName();
170                 int lastDot = label.lastIndexOf('.');
171                 if (lastDot >= 0 && lastDot < label.length() - 1)
172                         return label.substring(lastDot + 1);
173                 else
174                         return label;
175         }
176
177         /**
178          * Returns the hover's description.
179          * 
180          * @return the hover's description or <code>null</code> if not provided
181          */
182         public String getDescription() {
183                 return fElement.getAttribute(DESCRIPTION_ATTRIBUTE);
184         }
185
186         public boolean canActivatePlugIn() {
187                 return Boolean.valueOf(
188                                 fElement.getAttribute(ACTIVATE_PLUG_IN_ATTRIBUTE))
189                                 .booleanValue();
190         }
191
192         public boolean equals(Object obj) {
193                 if (obj == null || !obj.getClass().equals(this.getClass())
194                                 || getId() == null)
195                         return false;
196                 return getId().equals(((JavaEditorTextHoverDescriptor) obj).getId());
197         }
198
199         public int hashCode() {
200                 return getId().hashCode();
201         }
202
203         /*
204          * Implements a method from IComparable
205          */
206         public int compareTo(Object o) {
207                 return Collator.getInstance().compare(getLabel(),
208                                 ((JavaEditorTextHoverDescriptor) o).getLabel());
209         }
210
211         // /**
212         // * @param descriptor a JavaEditorTextHoverDescriptor
213         // * @return <code>true</code> if this contributed hover depends on the
214         // other one
215         // */
216         // public boolean dependsOn(JavaEditorTextHoverDescriptor descriptor) {
217         // if (descriptor == null)
218         // return false;
219         //              
220         // IPluginDescriptor thisPluginDescriptor=
221         // fElement.getDeclaringExtension().getDeclaringPluginDescriptor();
222         // IPluginDescriptor otherPluginDescriptor=
223         // descriptor.fElement.getDeclaringExtension().getDeclaringPluginDescriptor();
224         // return dependsOn(thisPluginDescriptor, otherPluginDescriptor);
225         // }
226
227         // private boolean dependsOn(IPluginDescriptor descriptor0,
228         // IPluginDescriptor descriptor1) {
229         //
230         // IPluginRegistry registry= Platform.getPluginRegistry();
231         // IPluginPrerequisite[] prerequisites=
232         // descriptor0.getPluginPrerequisites();
233         //
234         // for (int i= 0; i < prerequisites.length; i++) {
235         // IPluginPrerequisite prerequisite= prerequisites[i];
236         // String id= prerequisite.getUniqueIdentifier();
237         // IPluginDescriptor descriptor= registry.getPluginDescriptor(id);
238         //                      
239         // if (descriptor != null && (descriptor.equals(descriptor1) ||
240         // dependsOn(descriptor, descriptor1)))
241         // return true;
242         // }
243         //              
244         // return false;
245         // }
246
247         private static JavaEditorTextHoverDescriptor[] createDescriptors(
248                         IConfigurationElement[] elements) {
249                 List result = new ArrayList(elements.length);
250                 for (int i = 0; i < elements.length; i++) {
251                         IConfigurationElement element = elements[i];
252                         if (HOVER_TAG.equals(element.getName())) {
253                                 JavaEditorTextHoverDescriptor desc = new JavaEditorTextHoverDescriptor(
254                                                 element);
255                                 result.add(desc);
256                         }
257                 }
258                 Collections.sort(result);
259                 return (JavaEditorTextHoverDescriptor[]) result
260                                 .toArray(new JavaEditorTextHoverDescriptor[result.size()]);
261         }
262
263         private static void initializeFromPreferences(
264                         JavaEditorTextHoverDescriptor[] hovers) {
265                 String compiledTextHoverModifiers = PHPeclipsePlugin.getDefault()
266                                 .getPreferenceStore().getString(
267                                                 PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIERS);
268
269                 StringTokenizer tokenizer = new StringTokenizer(
270                                 compiledTextHoverModifiers, VALUE_SEPARATOR);
271                 HashMap idToModifier = new HashMap(tokenizer.countTokens() / 2);
272
273                 while (tokenizer.hasMoreTokens()) {
274                         String id = tokenizer.nextToken();
275                         if (tokenizer.hasMoreTokens())
276                                 idToModifier.put(id, tokenizer.nextToken());
277                 }
278
279                 String compiledTextHoverModifierMasks = PHPeclipsePlugin.getDefault()
280                                 .getPreferenceStore().getString(
281                                                 PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIER_MASKS);
282
283                 tokenizer = new StringTokenizer(compiledTextHoverModifierMasks,
284                                 VALUE_SEPARATOR);
285                 HashMap idToModifierMask = new HashMap(tokenizer.countTokens() / 2);
286
287                 while (tokenizer.hasMoreTokens()) {
288                         String id = tokenizer.nextToken();
289                         if (tokenizer.hasMoreTokens())
290                                 idToModifierMask.put(id, tokenizer.nextToken());
291                 }
292
293                 for (int i = 0; i < hovers.length; i++) {
294                         String modifierString = (String) idToModifier
295                                         .get(hovers[i].getId());
296                         boolean enabled = true;
297                         if (modifierString == null)
298                                 modifierString = DISABLED_TAG;
299
300                         if (modifierString.startsWith(DISABLED_TAG)) {
301                                 enabled = false;
302                                 modifierString = modifierString.substring(1);
303                         }
304
305                         if (modifierString.equals(NO_MODIFIER))
306                                 modifierString = ""; //$NON-NLS-1$
307
308                         hovers[i].fModifierString = modifierString;
309                         hovers[i].fIsEnabled = enabled;
310                         hovers[i].fStateMask = computeStateMask(modifierString);
311                         if (hovers[i].fStateMask == -1) {
312                                 // Fallback: use stored modifier masks
313                                 try {
314                                         hovers[i].fStateMask = Integer
315                                                         .parseInt((String) idToModifierMask.get(hovers[i]
316                                                                         .getId()));
317                                 } catch (NumberFormatException ex) {
318                                         hovers[i].fStateMask = -1;
319                                 }
320                                 // Fix modifier string
321                                 int stateMask = hovers[i].fStateMask;
322                                 if (stateMask == -1)
323                                         hovers[i].fModifierString = ""; //$NON-NLS-1$
324                                 else
325                                         hovers[i].fModifierString = EditorUtility
326                                                         .getModifierString(stateMask);
327                         }
328                 }
329         }
330
331         /**
332          * Returns the configured modifier getStateMask for this hover.
333          * 
334          * @return the hover modifier stateMask or -1 if no hover is configured
335          */
336         public int getStateMask() {
337                 return fStateMask;
338         }
339
340         /**
341          * Returns the modifier String as set in the preference store.
342          * 
343          * @return the modifier string
344          */
345         public String getModifierString() {
346                 return fModifierString;
347         }
348
349         /**
350          * Returns whether this hover is enabled or not.
351          * 
352          * @return <code>true</code> if enabled
353          */
354         public boolean isEnabled() {
355                 return fIsEnabled;
356         }
357
358         /**
359          * Returns this hover descriptors configuration element.
360          * 
361          * @return the configuration element
362          * @since 3.0
363          */
364         public IConfigurationElement getConfigurationElement() {
365                 return fElement;
366         }
367 }