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;
32 import org.eclipse.jface.text.Assert;
33 import org.eclipse.swt.SWT;
34 import org.osgi.framework.Bundle;
37 * Describes a Java editor text hover.
41 public class JavaEditorTextHoverDescriptor implements Comparable {
43 private static final String JAVA_EDITOR_TEXT_HOVER_EXTENSION_POINT= "net.sourceforge.phpeclipse.phpEditorTextHovers"; //$NON-NLS-1$
44 private static final String HOVER_TAG= "hover"; //$NON-NLS-1$
45 private static final String ID_ATTRIBUTE= "id"; //$NON-NLS-1$
46 private static final String CLASS_ATTRIBUTE= "class"; //$NON-NLS-1$
47 private static final String LABEL_ATTRIBUTE= "label"; //$NON-NLS-1$
48 private static final String ACTIVATE_PLUG_IN_ATTRIBUTE= "activate"; //$NON-NLS-1$
49 private static final String DESCRIPTION_ATTRIBUTE= "description"; //$NON-NLS-1$
51 public static final String NO_MODIFIER= "0"; //$NON-NLS-1$
52 public static final String DISABLED_TAG= "!"; //$NON-NLS-1$
53 public static final String VALUE_SEPARATOR= ";"; //$NON-NLS-1$
55 private int fStateMask;
56 private String fModifierString;
57 private boolean fIsEnabled;
59 private IConfigurationElement fElement;
62 * Returns all Java editor text hovers contributed to the workbench.
64 public static JavaEditorTextHoverDescriptor[] getContributedHovers() {
65 IExtensionRegistry registry= Platform.getExtensionRegistry();
66 IConfigurationElement[] elements= registry.getConfigurationElementsFor(JAVA_EDITOR_TEXT_HOVER_EXTENSION_POINT);
67 JavaEditorTextHoverDescriptor[] hoverDescs= createDescriptors(elements);
68 initializeFromPreferences(hoverDescs);
72 * Computes the state mask for the given modifier string.
74 * @param modifiers the string with the modifiers, separated by '+', '-', ';', ',' or '.'
75 * @return the state mask or -1 if the input is invalid
77 public static int computeStateMask(String modifiers) {
78 if (modifiers == null)
81 if (modifiers.length() == 0)
85 StringTokenizer modifierTokenizer= new StringTokenizer(modifiers, ",;.:+-* "); //$NON-NLS-1$
86 while (modifierTokenizer.hasMoreTokens()) {
87 int modifier= EditorUtility.findLocalizedModifier(modifierTokenizer.nextToken());
88 if (modifier == 0 || (stateMask & modifier) == modifier)
90 stateMask= stateMask | modifier;
96 * Creates a new Java Editor text hover descriptor from the given configuration element.
98 private JavaEditorTextHoverDescriptor(IConfigurationElement element) {
99 Assert.isNotNull(element);
104 * Creates the Java editor text hover.
106 public IJavaEditorTextHover createTextHover() {
107 String pluginId = fElement.getDeclaringExtension().getNamespace();
108 boolean isHoversPlugInActivated= Platform.getBundle(pluginId).getState() == Bundle.ACTIVE;
109 if (isHoversPlugInActivated || canActivatePlugIn()) {
111 return (IJavaEditorTextHover)fElement.createExecutableExtension(CLASS_ATTRIBUTE);
112 } catch (CoreException x) {
113 PHPeclipsePlugin.log(new Status(IStatus.ERROR, PHPeclipsePlugin.getPluginId(), 0, JavaHoverMessages.getString("JavaTextHover.createTextHover"), null)); //$NON-NLS-1$
119 //---- XML Attribute accessors ---------------------------------------------
122 * Returns the hover's id.
124 public String getId() {
125 return fElement.getAttribute(ID_ATTRIBUTE);
129 * Returns the hover's class name.
131 public String getHoverClassName() {
132 return fElement.getAttribute(CLASS_ATTRIBUTE);
136 * Returns the hover's label.
138 public String getLabel() {
139 String label= fElement.getAttribute(LABEL_ATTRIBUTE);
143 // Return simple class name
144 label= getHoverClassName();
145 int lastDot= label.lastIndexOf('.');
146 if (lastDot >= 0 && lastDot < label.length() - 1)
147 return label.substring(lastDot + 1);
153 * Returns the hover's description.
155 * @return the hover's description or <code>null</code> if not provided
157 public String getDescription() {
158 return fElement.getAttribute(DESCRIPTION_ATTRIBUTE);
162 public boolean canActivatePlugIn() {
163 return Boolean.valueOf(fElement.getAttribute(ACTIVATE_PLUG_IN_ATTRIBUTE)).booleanValue();
166 public boolean equals(Object obj) {
167 if (obj == null || !obj.getClass().equals(this.getClass()) || getId() == null)
169 return getId().equals(((JavaEditorTextHoverDescriptor)obj).getId());
172 public int hashCode() {
173 return getId().hashCode();
177 * Implements a method from IComparable
179 public int compareTo(Object o) {
180 return Collator.getInstance().compare(getLabel(), ((JavaEditorTextHoverDescriptor)o).getLabel());
184 // * @param descriptor a JavaEditorTextHoverDescriptor
185 // * @return <code>true</code> if this contributed hover depends on the other one
187 // public boolean dependsOn(JavaEditorTextHoverDescriptor descriptor) {
188 // if (descriptor == null)
191 // IPluginDescriptor thisPluginDescriptor= fElement.getDeclaringExtension().getDeclaringPluginDescriptor();
192 // IPluginDescriptor otherPluginDescriptor= descriptor.fElement.getDeclaringExtension().getDeclaringPluginDescriptor();
193 // return dependsOn(thisPluginDescriptor, otherPluginDescriptor);
196 // private boolean dependsOn(IPluginDescriptor descriptor0, IPluginDescriptor descriptor1) {
198 // IPluginRegistry registry= Platform.getPluginRegistry();
199 // IPluginPrerequisite[] prerequisites= descriptor0.getPluginPrerequisites();
201 // for (int i= 0; i < prerequisites.length; i++) {
202 // IPluginPrerequisite prerequisite= prerequisites[i];
203 // String id= prerequisite.getUniqueIdentifier();
204 // IPluginDescriptor descriptor= registry.getPluginDescriptor(id);
206 // if (descriptor != null && (descriptor.equals(descriptor1) || dependsOn(descriptor, descriptor1)))
213 private static JavaEditorTextHoverDescriptor[] createDescriptors(IConfigurationElement[] elements) {
214 List result= new ArrayList(elements.length);
215 for (int i= 0; i < elements.length; i++) {
216 IConfigurationElement element= elements[i];
217 if (HOVER_TAG.equals(element.getName())) {
218 JavaEditorTextHoverDescriptor desc= new JavaEditorTextHoverDescriptor(element);
222 Collections.sort(result);
223 return (JavaEditorTextHoverDescriptor[])result.toArray(new JavaEditorTextHoverDescriptor[result.size()]);
226 private static void initializeFromPreferences(JavaEditorTextHoverDescriptor[] hovers) {
227 String compiledTextHoverModifiers= PHPeclipsePlugin.getDefault().getPreferenceStore().getString(PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIERS);
229 StringTokenizer tokenizer= new StringTokenizer(compiledTextHoverModifiers, VALUE_SEPARATOR);
230 HashMap idToModifier= new HashMap(tokenizer.countTokens() / 2);
232 while (tokenizer.hasMoreTokens()) {
233 String id= tokenizer.nextToken();
234 if (tokenizer.hasMoreTokens())
235 idToModifier.put(id, tokenizer.nextToken());
238 String compiledTextHoverModifierMasks= PHPeclipsePlugin.getDefault().getPreferenceStore().getString(PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIER_MASKS);
240 tokenizer= new StringTokenizer(compiledTextHoverModifierMasks, VALUE_SEPARATOR);
241 HashMap idToModifierMask= new HashMap(tokenizer.countTokens() / 2);
243 while (tokenizer.hasMoreTokens()) {
244 String id= tokenizer.nextToken();
245 if (tokenizer.hasMoreTokens())
246 idToModifierMask.put(id, tokenizer.nextToken());
249 for (int i= 0; i < hovers.length; i++) {
250 String modifierString= (String)idToModifier.get(hovers[i].getId());
251 boolean enabled= true;
252 if (modifierString == null)
253 modifierString= DISABLED_TAG;
255 if (modifierString.startsWith(DISABLED_TAG)) {
257 modifierString= modifierString.substring(1);
260 if (modifierString.equals(NO_MODIFIER))
261 modifierString= ""; //$NON-NLS-1$
263 hovers[i].fModifierString= modifierString;
264 hovers[i].fIsEnabled= enabled;
265 hovers[i].fStateMask= computeStateMask(modifierString);
266 if (hovers[i].fStateMask == -1) {
267 // Fallback: use stored modifier masks
269 hovers[i].fStateMask= Integer.parseInt((String)idToModifierMask.get(hovers[i].getId()));
270 } catch (NumberFormatException ex) {
271 hovers[i].fStateMask= -1;
273 // Fix modifier string
274 int stateMask= hovers[i].fStateMask;
276 hovers[i].fModifierString=""; //$NON-NLS-1$
278 hovers[i].fModifierString= EditorUtility.getModifierString(stateMask);
284 * Returns the configured modifier getStateMask for this hover.
286 * @return the hover modifier stateMask or -1 if no hover is configured
288 public int getStateMask() {
293 * Returns the modifier String as set in the preference store.
295 * @return the modifier string
297 public String getModifierString() {
298 return fModifierString;
302 * Returns whether this hover is enabled or not.
304 * @return <code>true</code> if enabled
306 public boolean isEnabled() {
311 * Returns this hover descriptors configuration element.
313 * @return the configuration element
316 public IConfigurationElement getConfigurationElement() {