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 *******************************************************************************/
12 package net.sourceforge.phpdt.internal.ui.text.java.hover;
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;
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;
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;
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;
39 * Describes a Java editor text hover.
43 public class JavaEditorTextHoverDescriptor implements Comparable {
45 private static final String JAVA_EDITOR_TEXT_HOVER_EXTENSION_POINT = "net.sourceforge.phpeclipse.phpEditorTextHovers"; //$NON-NLS-1$
47 private static final String HOVER_TAG = "hover"; //$NON-NLS-1$
49 private static final String ID_ATTRIBUTE = "id"; //$NON-NLS-1$
51 private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$
53 private static final String LABEL_ATTRIBUTE = "label"; //$NON-NLS-1$
55 private static final String ACTIVATE_PLUG_IN_ATTRIBUTE = "activate"; //$NON-NLS-1$
57 private static final String DESCRIPTION_ATTRIBUTE = "description"; //$NON-NLS-1$
59 public static final String NO_MODIFIER = "0"; //$NON-NLS-1$
61 public static final String DISABLED_TAG = "!"; //$NON-NLS-1$
63 public static final String VALUE_SEPARATOR = ";"; //$NON-NLS-1$
65 private int fStateMask;
67 private String fModifierString;
69 private boolean fIsEnabled;
71 private IConfigurationElement fElement;
74 * Returns all Java editor text hovers contributed to the workbench.
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);
86 * Computes the state mask for the given modifier string.
89 * the string with the modifiers, separated by '+', '-', ';', ','
91 * @return the state mask or -1 if the input is invalid
93 public static int computeStateMask(String modifiers) {
94 if (modifiers == null)
97 if (modifiers.length() == 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)
108 stateMask = stateMask | modifier;
114 * Creates a new Java Editor text hover descriptor from the given
115 * configuration element.
117 private JavaEditorTextHoverDescriptor(IConfigurationElement element) {
118 Assert.isNotNull(element);
123 * Creates the Java editor text hover.
125 public IJavaEditorTextHover createTextHover() {
126 String pluginId = fElement.getDeclaringExtension().getNamespace();
127 boolean isHoversPlugInActivated = Platform.getBundle(pluginId)
128 .getState() == Bundle.ACTIVE;
129 if (isHoversPlugInActivated || canActivatePlugIn()) {
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$
143 // ---- XML Attribute accessors
144 // ---------------------------------------------
147 * Returns the hover's id.
149 public String getId() {
150 return fElement.getAttribute(ID_ATTRIBUTE);
154 * Returns the hover's class name.
156 public String getHoverClassName() {
157 return fElement.getAttribute(CLASS_ATTRIBUTE);
161 * Returns the hover's label.
163 public String getLabel() {
164 String label = fElement.getAttribute(LABEL_ATTRIBUTE);
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);
178 * Returns the hover's description.
180 * @return the hover's description or <code>null</code> if not provided
182 public String getDescription() {
183 return fElement.getAttribute(DESCRIPTION_ATTRIBUTE);
186 public boolean canActivatePlugIn() {
187 return Boolean.valueOf(
188 fElement.getAttribute(ACTIVATE_PLUG_IN_ATTRIBUTE))
192 public boolean equals(Object obj) {
193 if (obj == null || !obj.getClass().equals(this.getClass())
196 return getId().equals(((JavaEditorTextHoverDescriptor) obj).getId());
199 public int hashCode() {
200 return getId().hashCode();
204 * Implements a method from IComparable
206 public int compareTo(Object o) {
207 return Collator.getInstance().compare(getLabel(),
208 ((JavaEditorTextHoverDescriptor) o).getLabel());
212 // * @param descriptor a JavaEditorTextHoverDescriptor
213 // * @return <code>true</code> if this contributed hover depends on the
216 // public boolean dependsOn(JavaEditorTextHoverDescriptor descriptor) {
217 // if (descriptor == null)
220 // IPluginDescriptor thisPluginDescriptor=
221 // fElement.getDeclaringExtension().getDeclaringPluginDescriptor();
222 // IPluginDescriptor otherPluginDescriptor=
223 // descriptor.fElement.getDeclaringExtension().getDeclaringPluginDescriptor();
224 // return dependsOn(thisPluginDescriptor, otherPluginDescriptor);
227 // private boolean dependsOn(IPluginDescriptor descriptor0,
228 // IPluginDescriptor descriptor1) {
230 // IPluginRegistry registry= Platform.getPluginRegistry();
231 // IPluginPrerequisite[] prerequisites=
232 // descriptor0.getPluginPrerequisites();
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);
239 // if (descriptor != null && (descriptor.equals(descriptor1) ||
240 // dependsOn(descriptor, descriptor1)))
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(
258 Collections.sort(result);
259 return (JavaEditorTextHoverDescriptor[]) result
260 .toArray(new JavaEditorTextHoverDescriptor[result.size()]);
263 private static void initializeFromPreferences(
264 JavaEditorTextHoverDescriptor[] hovers) {
265 String compiledTextHoverModifiers = PHPeclipsePlugin.getDefault()
266 .getPreferenceStore().getString(
267 PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIERS);
269 StringTokenizer tokenizer = new StringTokenizer(
270 compiledTextHoverModifiers, VALUE_SEPARATOR);
271 HashMap idToModifier = new HashMap(tokenizer.countTokens() / 2);
273 while (tokenizer.hasMoreTokens()) {
274 String id = tokenizer.nextToken();
275 if (tokenizer.hasMoreTokens())
276 idToModifier.put(id, tokenizer.nextToken());
279 String compiledTextHoverModifierMasks = PHPeclipsePlugin.getDefault()
280 .getPreferenceStore().getString(
281 PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIER_MASKS);
283 tokenizer = new StringTokenizer(compiledTextHoverModifierMasks,
285 HashMap idToModifierMask = new HashMap(tokenizer.countTokens() / 2);
287 while (tokenizer.hasMoreTokens()) {
288 String id = tokenizer.nextToken();
289 if (tokenizer.hasMoreTokens())
290 idToModifierMask.put(id, tokenizer.nextToken());
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;
300 if (modifierString.startsWith(DISABLED_TAG)) {
302 modifierString = modifierString.substring(1);
305 if (modifierString.equals(NO_MODIFIER))
306 modifierString = ""; //$NON-NLS-1$
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
314 hovers[i].fStateMask = Integer
315 .parseInt((String) idToModifierMask.get(hovers[i]
317 } catch (NumberFormatException ex) {
318 hovers[i].fStateMask = -1;
320 // Fix modifier string
321 int stateMask = hovers[i].fStateMask;
323 hovers[i].fModifierString = ""; //$NON-NLS-1$
325 hovers[i].fModifierString = EditorUtility
326 .getModifierString(stateMask);
332 * Returns the configured modifier getStateMask for this hover.
334 * @return the hover modifier stateMask or -1 if no hover is configured
336 public int getStateMask() {
341 * Returns the modifier String as set in the preference store.
343 * @return the modifier string
345 public String getModifierString() {
346 return fModifierString;
350 * Returns whether this hover is enabled or not.
352 * @return <code>true</code> if enabled
354 public boolean isEnabled() {
359 * Returns this hover descriptors configuration element.
361 * @return the configuration element
364 public IConfigurationElement getConfigurationElement() {