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.IPluginDescriptor;
29 import org.eclipse.core.runtime.IPluginPrerequisite;
30 import org.eclipse.core.runtime.IPluginRegistry;
31 import org.eclipse.core.runtime.IStatus;
32 import org.eclipse.core.runtime.Platform;
33 import org.eclipse.core.runtime.Status;
34 import org.eclipse.jface.text.Assert;
35 import org.eclipse.swt.SWT;
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$
45 private static final String HOVER_TAG= "hover"; //$NON-NLS-1$
46 private static final String ID_ATTRIBUTE= "id"; //$NON-NLS-1$
47 private static final String CLASS_ATTRIBUTE= "class"; //$NON-NLS-1$
48 private static final String LABEL_ATTRIBUTE= "label"; //$NON-NLS-1$
49 private static final String ACTIVATE_PLUG_IN_ATTRIBUTE= "activate"; //$NON-NLS-1$
50 private static final String DESCRIPTION_ATTRIBUTE= "description"; //$NON-NLS-1$
52 public static final String NO_MODIFIER= "0"; //$NON-NLS-1$
53 public static final String DISABLED_TAG= "!"; //$NON-NLS-1$
54 public static final String VALUE_SEPARATOR= ";"; //$NON-NLS-1$
56 private int fStateMask;
57 private String fModifierString;
58 private boolean fIsEnabled;
60 private IConfigurationElement fElement;
64 * Returns all Java editor text hovers contributed to the workbench.
66 public static JavaEditorTextHoverDescriptor[] getContributedHovers() {
67 IPluginRegistry registry= Platform.getPluginRegistry();
68 IConfigurationElement[] elements= registry.getConfigurationElementsFor(JAVA_EDITOR_TEXT_HOVER_EXTENSION_POINT);
69 JavaEditorTextHoverDescriptor[] hoverDescs= createDescriptors(elements);
70 initializeFromPreferences(hoverDescs);
75 * Computes the state mask for the given modifier string.
77 * @param modifiers the string with the modifiers, separated by '+', '-', ';', ',' or '.'
78 * @return the state mask or -1 if the input is invalid
80 public static int computeStateMask(String modifiers) {
81 if (modifiers == null)
84 if (modifiers.length() == 0)
88 StringTokenizer modifierTokenizer= new StringTokenizer(modifiers, ",;.:+-* "); //$NON-NLS-1$
89 while (modifierTokenizer.hasMoreTokens()) {
90 int modifier= EditorUtility.findLocalizedModifier(modifierTokenizer.nextToken());
91 if (modifier == 0 || (stateMask & modifier) == modifier)
93 stateMask= stateMask | modifier;
99 * Creates a new Java Editor text hover descriptor from the given configuration element.
101 private JavaEditorTextHoverDescriptor(IConfigurationElement element) {
102 Assert.isNotNull(element);
107 * Creates the Java editor text hover.
109 public IJavaEditorTextHover createTextHover() {
110 boolean isHoversPlugInActivated= fElement.getDeclaringExtension().getDeclaringPluginDescriptor().isPluginActivated();
111 if (isHoversPlugInActivated || canActivatePlugIn()) {
113 return (IJavaEditorTextHover)fElement.createExecutableExtension(CLASS_ATTRIBUTE);
114 } catch (CoreException x) {
115 PHPeclipsePlugin.log(new Status(IStatus.ERROR, PHPeclipsePlugin.getPluginId(), 0, JavaHoverMessages.getString("JavaTextHover.createTextHover"), null)); //$NON-NLS-1$
122 //---- XML Attribute accessors ---------------------------------------------
125 * Returns the hover's id.
127 public String getId() {
128 return fElement.getAttribute(ID_ATTRIBUTE);
132 * Returns the hover's class name.
134 public String getHoverClassName() {
135 return fElement.getAttribute(CLASS_ATTRIBUTE);
139 * Returns the hover's label.
141 public String getLabel() {
142 String label= fElement.getAttribute(LABEL_ATTRIBUTE);
146 // Return simple class name
147 label= getHoverClassName();
148 int lastDot= label.lastIndexOf('.');
149 if (lastDot >= 0 && lastDot < label.length() - 1)
150 return label.substring(lastDot + 1);
156 * Returns the hover's description.
158 * @return the hover's description or <code>null</code> if not provided
160 public String getDescription() {
161 return fElement.getAttribute(DESCRIPTION_ATTRIBUTE);
165 public boolean canActivatePlugIn() {
166 return Boolean.valueOf(fElement.getAttribute(ACTIVATE_PLUG_IN_ATTRIBUTE)).booleanValue();
169 public boolean equals(Object obj) {
170 if (obj == null || !obj.getClass().equals(this.getClass()) || getId() == null)
172 return getId().equals(((JavaEditorTextHoverDescriptor)obj).getId());
175 public int hashCode() {
176 return getId().hashCode();
180 * Implements a method from IComparable
182 public int compareTo(Object o) {
183 return Collator.getInstance().compare(getLabel(), ((JavaEditorTextHoverDescriptor)o).getLabel());
187 * @param descriptor a JavaEditorTextHoverDescriptor
188 * @return <code>true</code> if this contributed hover depends on the other one
190 public boolean dependsOn(JavaEditorTextHoverDescriptor descriptor) {
191 if (descriptor == null)
194 IPluginDescriptor thisPluginDescriptor= fElement.getDeclaringExtension().getDeclaringPluginDescriptor();
195 IPluginDescriptor otherPluginDescriptor= descriptor.fElement.getDeclaringExtension().getDeclaringPluginDescriptor();
196 return dependsOn(thisPluginDescriptor, otherPluginDescriptor);
199 private boolean dependsOn(IPluginDescriptor descriptor0, IPluginDescriptor descriptor1) {
201 IPluginRegistry registry= Platform.getPluginRegistry();
202 IPluginPrerequisite[] prerequisites= descriptor0.getPluginPrerequisites();
204 for (int i= 0; i < prerequisites.length; i++) {
205 IPluginPrerequisite prerequisite= prerequisites[i];
206 String id= prerequisite.getUniqueIdentifier();
207 IPluginDescriptor descriptor= registry.getPluginDescriptor(id);
209 if (descriptor != null && (descriptor.equals(descriptor1) || dependsOn(descriptor, descriptor1)))
216 private static JavaEditorTextHoverDescriptor[] createDescriptors(IConfigurationElement[] elements) {
217 List result= new ArrayList(elements.length);
218 for (int i= 0; i < elements.length; i++) {
219 IConfigurationElement element= elements[i];
220 if (HOVER_TAG.equals(element.getName())) {
221 JavaEditorTextHoverDescriptor desc= new JavaEditorTextHoverDescriptor(element);
225 Collections.sort(result);
226 return (JavaEditorTextHoverDescriptor[])result.toArray(new JavaEditorTextHoverDescriptor[result.size()]);
229 private static void initializeFromPreferences(JavaEditorTextHoverDescriptor[] hovers) {
230 String compiledTextHoverModifiers= PHPeclipsePlugin.getDefault().getPreferenceStore().getString(PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIERS);
232 StringTokenizer tokenizer= new StringTokenizer(compiledTextHoverModifiers, VALUE_SEPARATOR);
233 HashMap idToModifier= new HashMap(tokenizer.countTokens() / 2);
235 while (tokenizer.hasMoreTokens()) {
236 String id= tokenizer.nextToken();
237 if (tokenizer.hasMoreTokens())
238 idToModifier.put(id, tokenizer.nextToken());
241 String compiledTextHoverModifierMasks= PHPeclipsePlugin.getDefault().getPreferenceStore().getString(PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIER_MASKS);
243 tokenizer= new StringTokenizer(compiledTextHoverModifierMasks, VALUE_SEPARATOR);
244 HashMap idToModifierMask= new HashMap(tokenizer.countTokens() / 2);
246 while (tokenizer.hasMoreTokens()) {
247 String id= tokenizer.nextToken();
248 if (tokenizer.hasMoreTokens())
249 idToModifierMask.put(id, tokenizer.nextToken());
252 for (int i= 0; i < hovers.length; i++) {
253 String modifierString= (String)idToModifier.get(hovers[i].getId());
254 boolean enabled= true;
255 if (modifierString == null)
256 modifierString= DISABLED_TAG;
258 if (modifierString.startsWith(DISABLED_TAG)) {
260 modifierString= modifierString.substring(1);
263 if (modifierString.equals(NO_MODIFIER))
264 modifierString= ""; //$NON-NLS-1$
266 hovers[i].fModifierString= modifierString;
267 hovers[i].fIsEnabled= enabled;
268 hovers[i].fStateMask= computeStateMask(modifierString);
269 if (hovers[i].fStateMask == -1) {
270 // Fallback: use stored modifier masks
272 hovers[i].fStateMask= Integer.parseInt((String)idToModifierMask.get(hovers[i].getId()));
273 } catch (NumberFormatException ex) {
274 hovers[i].fStateMask= -1;
276 // Fix modifier string
277 int stateMask= hovers[i].fStateMask;
279 hovers[i].fModifierString=""; //$NON-NLS-1$
281 hovers[i].fModifierString= EditorUtility.getModifierString(stateMask);
287 * Returns the configured modifier getStateMask for this hover.
289 * @return the hover modifier stateMask or -1 if no hover is configured
291 public int getStateMask() {
296 * Returns the modifier String as set in the preference store.
298 * @return the modifier string
300 public String getModifierString() {
301 return fModifierString;
305 * Returns whether this hover is enabled or not.
307 * @return <code>true</code> if enabled
309 public boolean isEnabled() {
314 * Returns this hover descriptors configuration element.
316 * @return the configuration element
319 public IConfigurationElement getConfigurationElement() {