KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > java > hover > JavaEditorTextHoverDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.ui.text.java.hover;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IConfigurationElement;
22 import org.eclipse.core.runtime.IExtensionRegistry;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.core.runtime.Status;
26
27 import org.eclipse.swt.SWT;
28
29
30 import org.eclipse.jdt.ui.PreferenceConstants;
31 import org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover;
32
33 import org.eclipse.jdt.internal.ui.JavaPlugin;
34 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
35
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 {
44
45     private static final String JavaDoc JAVA_EDITOR_TEXT_HOVER_EXTENSION_POINT= "org.eclipse.jdt.ui.javaEditorTextHovers"; //$NON-NLS-1$
46
private static final String JavaDoc HOVER_TAG= "hover"; //$NON-NLS-1$
47
private static final String JavaDoc ID_ATTRIBUTE= "id"; //$NON-NLS-1$
48
private static final String JavaDoc CLASS_ATTRIBUTE= "class"; //$NON-NLS-1$
49
private static final String JavaDoc LABEL_ATTRIBUTE= "label"; //$NON-NLS-1$
50
private static final String JavaDoc ACTIVATE_PLUG_IN_ATTRIBUTE= "activate"; //$NON-NLS-1$
51
private static final String JavaDoc DESCRIPTION_ATTRIBUTE= "description"; //$NON-NLS-1$
52

53     public static final String JavaDoc NO_MODIFIER= "0"; //$NON-NLS-1$
54
public static final String JavaDoc DISABLED_TAG= "!"; //$NON-NLS-1$
55
public static final String JavaDoc VALUE_SEPARATOR= ";"; //$NON-NLS-1$
56

57     private int fStateMask;
58     private String JavaDoc fModifierString;
59     private boolean fIsEnabled;
60
61     private IConfigurationElement fElement;
62
63
64     /**
65      * Returns all Java editor text hovers contributed to the workbench.
66      *
67      * @return an array with the contributed text hovers
68      */

69     public static JavaEditorTextHoverDescriptor[] getContributedHovers() {
70         IExtensionRegistry registry= Platform.getExtensionRegistry();
71         IConfigurationElement[] elements= registry.getConfigurationElementsFor(JAVA_EDITOR_TEXT_HOVER_EXTENSION_POINT);
72         JavaEditorTextHoverDescriptor[] hoverDescs= createDescriptors(elements);
73         initializeFromPreferences(hoverDescs);
74         return hoverDescs;
75     }
76
77     /**
78      * Computes the state mask for the given modifier string.
79      *
80      * @param modifiers the string with the modifiers, separated by '+', '-', ';', ',' or '.'
81      * @return the state mask or -1 if the input is invalid
82      */

83     public static int computeStateMask(String JavaDoc modifiers) {
84         if (modifiers == null)
85             return -1;
86
87         if (modifiers.length() == 0)
88             return SWT.NONE;
89
90         int stateMask= 0;
91         StringTokenizer JavaDoc modifierTokenizer= new StringTokenizer JavaDoc(modifiers, ",;.:+-* "); //$NON-NLS-1$
92
while (modifierTokenizer.hasMoreTokens()) {
93             int modifier= EditorUtility.findLocalizedModifier(modifierTokenizer.nextToken());
94             if (modifier == 0 || (stateMask & modifier) == modifier)
95                 return -1;
96             stateMask= stateMask | modifier;
97         }
98         return stateMask;
99     }
100
101     /**
102      * Creates a new Java Editor text hover descriptor from the given configuration element.
103      *
104      * @param element the configuration element
105      */

106     private JavaEditorTextHoverDescriptor(IConfigurationElement element) {
107         Assert.isNotNull(element);
108         fElement= element;
109     }
110
111     /**
112      * Creates the Java editor text hover.
113      *
114      * @return the text hover
115      */

116     public IJavaEditorTextHover createTextHover() {
117         String JavaDoc pluginId = fElement.getContributor().getName();
118         boolean isHoversPlugInActivated= Platform.getBundle(pluginId).getState() == Bundle.ACTIVE;
119         if (isHoversPlugInActivated || canActivatePlugIn()) {
120             try {
121                 return (IJavaEditorTextHover)fElement.createExecutableExtension(CLASS_ATTRIBUTE);
122             } catch (CoreException x) {
123                 JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, JavaHoverMessages.JavaTextHover_createTextHover, null));
124             }
125         }
126
127         return null;
128     }
129
130     //---- XML Attribute accessors ---------------------------------------------
131

132     /**
133      * Returns the hover's id.
134      *
135      * @return the id
136      */

137     public String JavaDoc getId() {
138             return fElement.getAttribute(ID_ATTRIBUTE);
139     }
140
141     /**
142      * Returns the hover's class name.
143      *
144      * @return the class name
145      */

146     public String JavaDoc getHoverClassName() {
147         return fElement.getAttribute(CLASS_ATTRIBUTE);
148     }
149
150     /**
151      * Returns the hover's label.
152      *
153      * @return the label
154      */

155     public String JavaDoc getLabel() {
156         String JavaDoc label= fElement.getAttribute(LABEL_ATTRIBUTE);
157         if (label != null)
158             return label;
159
160         // Return simple class name
161
label= getHoverClassName();
162         int lastDot= label.lastIndexOf('.');
163         if (lastDot >= 0 && lastDot < label.length() - 1)
164             return label.substring(lastDot + 1);
165         else
166             return label;
167     }
168
169     /**
170      * Returns the hover's description.
171      *
172      * @return the hover's description or <code>null</code> if not provided
173      */

174     public String JavaDoc getDescription() {
175         return fElement.getAttribute(DESCRIPTION_ATTRIBUTE);
176     }
177
178
179     public boolean canActivatePlugIn() {
180         return Boolean.valueOf(fElement.getAttribute(ACTIVATE_PLUG_IN_ATTRIBUTE)).booleanValue();
181     }
182
183     public boolean equals(Object JavaDoc obj) {
184         if (obj == null || !obj.getClass().equals(this.getClass()) || getId() == null)
185             return false;
186         return getId().equals(((JavaEditorTextHoverDescriptor)obj).getId());
187     }
188
189     public int hashCode() {
190         return getId().hashCode();
191     }
192
193     private static JavaEditorTextHoverDescriptor[] createDescriptors(IConfigurationElement[] elements) {
194         List JavaDoc result= new ArrayList JavaDoc(elements.length);
195         for (int i= 0; i < elements.length; i++) {
196             IConfigurationElement element= elements[i];
197             if (HOVER_TAG.equals(element.getName())) {
198                 JavaEditorTextHoverDescriptor desc= new JavaEditorTextHoverDescriptor(element);
199                 result.add(desc);
200             }
201         }
202         return (JavaEditorTextHoverDescriptor[])result.toArray(new JavaEditorTextHoverDescriptor[result.size()]);
203     }
204
205     private static void initializeFromPreferences(JavaEditorTextHoverDescriptor[] hovers) {
206         String JavaDoc compiledTextHoverModifiers= JavaPlugin.getDefault().getPreferenceStore().getString(PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIERS);
207
208         StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(compiledTextHoverModifiers, VALUE_SEPARATOR);
209         HashMap JavaDoc idToModifier= new HashMap JavaDoc(tokenizer.countTokens() / 2);
210
211         while (tokenizer.hasMoreTokens()) {
212             String JavaDoc id= tokenizer.nextToken();
213             if (tokenizer.hasMoreTokens())
214                 idToModifier.put(id, tokenizer.nextToken());
215         }
216
217         String JavaDoc compiledTextHoverModifierMasks= JavaPlugin.getDefault().getPreferenceStore().getString(PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIER_MASKS);
218
219         tokenizer= new StringTokenizer JavaDoc(compiledTextHoverModifierMasks, VALUE_SEPARATOR);
220         HashMap JavaDoc idToModifierMask= new HashMap JavaDoc(tokenizer.countTokens() / 2);
221
222         while (tokenizer.hasMoreTokens()) {
223             String JavaDoc id= tokenizer.nextToken();
224             if (tokenizer.hasMoreTokens())
225                 idToModifierMask.put(id, tokenizer.nextToken());
226         }
227
228         for (int i= 0; i < hovers.length; i++) {
229             String JavaDoc modifierString= (String JavaDoc)idToModifier.get(hovers[i].getId());
230             boolean enabled= true;
231             if (modifierString == null)
232                 modifierString= DISABLED_TAG;
233
234             if (modifierString.startsWith(DISABLED_TAG)) {
235                 enabled= false;
236                 modifierString= modifierString.substring(1);
237             }
238
239             if (modifierString.equals(NO_MODIFIER))
240                 modifierString= ""; //$NON-NLS-1$
241

242             hovers[i].fModifierString= modifierString;
243             hovers[i].fIsEnabled= enabled;
244             hovers[i].fStateMask= computeStateMask(modifierString);
245             if (hovers[i].fStateMask == -1) {
246                 // Fallback: use stored modifier masks
247
try {
248                     hovers[i].fStateMask= Integer.parseInt((String JavaDoc)idToModifierMask.get(hovers[i].getId()));
249                 } catch (NumberFormatException JavaDoc ex) {
250                     hovers[i].fStateMask= -1;
251                 }
252                 // Fix modifier string
253
int stateMask= hovers[i].fStateMask;
254                 if (stateMask == -1)
255                     hovers[i].fModifierString= ""; //$NON-NLS-1$
256
else
257                     hovers[i].fModifierString= EditorUtility.getModifierString(stateMask);
258             }
259         }
260     }
261
262     /**
263      * Returns the configured modifier getStateMask for this hover.
264      *
265      * @return the hover modifier stateMask or -1 if no hover is configured
266      */

267     public int getStateMask() {
268         return fStateMask;
269     }
270
271     /**
272      * Returns the modifier String as set in the preference store.
273      *
274      * @return the modifier string
275      */

276     public String JavaDoc getModifierString() {
277         return fModifierString;
278     }
279
280     /**
281      * Returns whether this hover is enabled or not.
282      *
283      * @return <code>true</code> if enabled
284      */

285     public boolean isEnabled() {
286         return fIsEnabled;
287     }
288
289     /**
290      * Returns this hover descriptors configuration element.
291      *
292      * @return the configuration element
293      * @since 3.0
294      */

295     public IConfigurationElement getConfigurationElement() {
296         return fElement;
297     }
298 }
299
Popular Tags