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;
25 import net.sourceforge.phpeclipse.ui.WebUI;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IConfigurationElement;
29 import org.eclipse.core.runtime.IExtensionRegistry;
30 import org.eclipse.core.runtime.IStatus;
31 import org.eclipse.core.runtime.Platform;
32 import org.eclipse.core.runtime.Status;
33 import org.eclipse.jface.text.Assert;
34 import org.eclipse.swt.SWT;
35 import org.osgi.framework.Bundle;
38 * Describes a Java editor text hover.
42 public class JavaEditorTextHoverDescriptor implements Comparable {
44 private static final String JAVA_EDITOR_TEXT_HOVER_EXTENSION_POINT = "net.sourceforge.phpeclipse.phpEditorTextHovers"; //$NON-NLS-1$
46 private static final String HOVER_TAG = "hover"; //$NON-NLS-1$
48 private static final String ID_ATTRIBUTE = "id"; //$NON-NLS-1$
50 private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$
52 private static final String LABEL_ATTRIBUTE = "label"; //$NON-NLS-1$
54 private static final String ACTIVATE_PLUG_IN_ATTRIBUTE = "activate"; //$NON-NLS-1$
56 private static final String DESCRIPTION_ATTRIBUTE = "description"; //$NON-NLS-1$
58 public static final String NO_MODIFIER = "0"; //$NON-NLS-1$
60 public static final String DISABLED_TAG = "!"; //$NON-NLS-1$
62 public static final String VALUE_SEPARATOR = ";"; //$NON-NLS-1$
64 private int fStateMask;
66 private String fModifierString;
68 private boolean fIsEnabled;
70 private IConfigurationElement fElement;
73 * Returns all Java editor text hovers contributed to the workbench.
75 public static JavaEditorTextHoverDescriptor[] getContributedHovers() {
76 IExtensionRegistry registry = Platform.getExtensionRegistry();
77 IConfigurationElement[] elements = registry
78 .getConfigurationElementsFor(JAVA_EDITOR_TEXT_HOVER_EXTENSION_POINT);
79 JavaEditorTextHoverDescriptor[] hoverDescs = createDescriptors(elements);
80 initializeFromPreferences(hoverDescs);
85 * Computes the state mask for the given modifier string.
88 * the string with the modifiers, separated by '+', '-', ';', ','
90 * @return the state mask or -1 if the input is invalid
92 public static int computeStateMask(String modifiers) {
93 if (modifiers == null)
96 if (modifiers.length() == 0)
100 StringTokenizer modifierTokenizer = new StringTokenizer(modifiers,
101 ",;.:+-* "); //$NON-NLS-1$
102 while (modifierTokenizer.hasMoreTokens()) {
103 int modifier = EditorUtility
104 .findLocalizedModifier(modifierTokenizer.nextToken());
105 if (modifier == 0 || (stateMask & modifier) == modifier)
107 stateMask = stateMask | modifier;
113 * Creates a new Java Editor text hover descriptor from the given
114 * configuration element.
116 private JavaEditorTextHoverDescriptor(IConfigurationElement element) {
117 Assert.isNotNull(element);
122 * Creates the Java editor text hover.
124 public IJavaEditorTextHover createTextHover() {
125 String pluginId = fElement.getDeclaringExtension().getNamespace();
126 boolean isHoversPlugInActivated = Platform.getBundle(pluginId)
127 .getState() == Bundle.ACTIVE;
128 if (isHoversPlugInActivated || canActivatePlugIn()) {
130 return (IJavaEditorTextHover) fElement
131 .createExecutableExtension(CLASS_ATTRIBUTE);
132 } catch (CoreException x) {
133 WebUI.log(new Status(IStatus.ERROR, WebUI
134 .getPluginId(), 0, JavaHoverMessages
135 .getString("JavaTextHover.createTextHover"), null)); //$NON-NLS-1$
142 // ---- XML Attribute accessors
143 // ---------------------------------------------
146 * Returns the hover's id.
148 public String getId() {
149 return fElement.getAttribute(ID_ATTRIBUTE);
153 * Returns the hover's class name.
155 public String getHoverClassName() {
156 return fElement.getAttribute(CLASS_ATTRIBUTE);
160 * Returns the hover's label.
162 public String getLabel() {
163 String label = fElement.getAttribute(LABEL_ATTRIBUTE);
167 // Return simple class name
168 label = getHoverClassName();
169 int lastDot = label.lastIndexOf('.');
170 if (lastDot >= 0 && lastDot < label.length() - 1)
171 return label.substring(lastDot + 1);
177 * Returns the hover's description.
179 * @return the hover's description or <code>null</code> if not provided
181 public String getDescription() {
182 return fElement.getAttribute(DESCRIPTION_ATTRIBUTE);
185 public boolean canActivatePlugIn() {
186 return Boolean.valueOf(
187 fElement.getAttribute(ACTIVATE_PLUG_IN_ATTRIBUTE))
191 public boolean equals(Object obj) {
192 if (obj == null || !obj.getClass().equals(this.getClass())
195 return getId().equals(((JavaEditorTextHoverDescriptor) obj).getId());
198 public int hashCode() {
199 return getId().hashCode();
203 * Implements a method from IComparable
205 public int compareTo(Object o) {
206 return Collator.getInstance().compare(getLabel(),
207 ((JavaEditorTextHoverDescriptor) o).getLabel());
211 // * @param descriptor a JavaEditorTextHoverDescriptor
212 // * @return <code>true</code> if this contributed hover depends on the
215 // public boolean dependsOn(JavaEditorTextHoverDescriptor descriptor) {
216 // if (descriptor == null)
219 // IPluginDescriptor thisPluginDescriptor=
220 // fElement.getDeclaringExtension().getDeclaringPluginDescriptor();
221 // IPluginDescriptor otherPluginDescriptor=
222 // descriptor.fElement.getDeclaringExtension().getDeclaringPluginDescriptor();
223 // return dependsOn(thisPluginDescriptor, otherPluginDescriptor);
226 // private boolean dependsOn(IPluginDescriptor descriptor0,
227 // IPluginDescriptor descriptor1) {
229 // IPluginRegistry registry= Platform.getPluginRegistry();
230 // IPluginPrerequisite[] prerequisites=
231 // descriptor0.getPluginPrerequisites();
233 // for (int i= 0; i < prerequisites.length; i++) {
234 // IPluginPrerequisite prerequisite= prerequisites[i];
235 // String id= prerequisite.getUniqueIdentifier();
236 // IPluginDescriptor descriptor= registry.getPluginDescriptor(id);
238 // if (descriptor != null && (descriptor.equals(descriptor1) ||
239 // dependsOn(descriptor, descriptor1)))
246 private static JavaEditorTextHoverDescriptor[] createDescriptors(
247 IConfigurationElement[] elements) {
248 List result = new ArrayList(elements.length);
249 for (int i = 0; i < elements.length; i++) {
250 IConfigurationElement element = elements[i];
251 if (HOVER_TAG.equals(element.getName())) {
252 JavaEditorTextHoverDescriptor desc = new JavaEditorTextHoverDescriptor(
257 Collections.sort(result);
258 return (JavaEditorTextHoverDescriptor[]) result
259 .toArray(new JavaEditorTextHoverDescriptor[result.size()]);
262 private static void initializeFromPreferences(
263 JavaEditorTextHoverDescriptor[] hovers) {
264 String compiledTextHoverModifiers = WebUI.getDefault()
265 .getPreferenceStore().getString(
266 PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIERS);
268 StringTokenizer tokenizer = new StringTokenizer(
269 compiledTextHoverModifiers, VALUE_SEPARATOR);
270 HashMap idToModifier = new HashMap(tokenizer.countTokens() / 2);
272 while (tokenizer.hasMoreTokens()) {
273 String id = tokenizer.nextToken();
274 if (tokenizer.hasMoreTokens())
275 idToModifier.put(id, tokenizer.nextToken());
278 String compiledTextHoverModifierMasks = WebUI.getDefault()
279 .getPreferenceStore().getString(
280 PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIER_MASKS);
282 tokenizer = new StringTokenizer(compiledTextHoverModifierMasks,
284 HashMap idToModifierMask = new HashMap(tokenizer.countTokens() / 2);
286 while (tokenizer.hasMoreTokens()) {
287 String id = tokenizer.nextToken();
288 if (tokenizer.hasMoreTokens())
289 idToModifierMask.put(id, tokenizer.nextToken());
292 for (int i = 0; i < hovers.length; i++) {
293 String modifierString = (String) idToModifier
294 .get(hovers[i].getId());
295 boolean enabled = true;
296 if (modifierString == null)
297 modifierString = DISABLED_TAG;
299 if (modifierString.startsWith(DISABLED_TAG)) {
301 modifierString = modifierString.substring(1);
304 if (modifierString.equals(NO_MODIFIER))
305 modifierString = ""; //$NON-NLS-1$
307 hovers[i].fModifierString = modifierString;
308 hovers[i].fIsEnabled = enabled;
309 hovers[i].fStateMask = computeStateMask(modifierString);
310 if (hovers[i].fStateMask == -1) {
311 // Fallback: use stored modifier masks
313 hovers[i].fStateMask = Integer
314 .parseInt((String) idToModifierMask.get(hovers[i]
316 } catch (NumberFormatException ex) {
317 hovers[i].fStateMask = -1;
319 // Fix modifier string
320 int stateMask = hovers[i].fStateMask;
322 hovers[i].fModifierString = ""; //$NON-NLS-1$
324 hovers[i].fModifierString = EditorUtility
325 .getModifierString(stateMask);
331 * Returns the configured modifier getStateMask for this hover.
333 * @return the hover modifier stateMask or -1 if no hover is configured
335 public int getStateMask() {
340 * Returns the modifier String as set in the preference store.
342 * @return the modifier string
344 public String getModifierString() {
345 return fModifierString;
349 * Returns whether this hover is enabled or not.
351 * @return <code>true</code> if enabled
353 public boolean isEnabled() {
358 * Returns this hover descriptors configuration element.
360 * @return the configuration element
363 public IConfigurationElement getConfigurationElement() {