1 11 12 package org.eclipse.jdt.internal.ui.text.java.hover; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.List ; 17 import java.util.StringTokenizer ; 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 43 public class JavaEditorTextHoverDescriptor { 44 45 private static final String JAVA_EDITOR_TEXT_HOVER_EXTENSION_POINT= "org.eclipse.jdt.ui.javaEditorTextHovers"; private static final String HOVER_TAG= "hover"; private static final String ID_ATTRIBUTE= "id"; private static final String CLASS_ATTRIBUTE= "class"; private static final String LABEL_ATTRIBUTE= "label"; private static final String ACTIVATE_PLUG_IN_ATTRIBUTE= "activate"; private static final String DESCRIPTION_ATTRIBUTE= "description"; 53 public static final String NO_MODIFIER= "0"; public static final String DISABLED_TAG= "!"; public static final String VALUE_SEPARATOR= ";"; 57 private int fStateMask; 58 private String fModifierString; 59 private boolean fIsEnabled; 60 61 private IConfigurationElement fElement; 62 63 64 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 83 public static int computeStateMask(String 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 modifierTokenizer= new StringTokenizer (modifiers, ",;.:+-* "); 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 106 private JavaEditorTextHoverDescriptor(IConfigurationElement element) { 107 Assert.isNotNull(element); 108 fElement= element; 109 } 110 111 116 public IJavaEditorTextHover createTextHover() { 117 String 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 132 137 public String getId() { 138 return fElement.getAttribute(ID_ATTRIBUTE); 139 } 140 141 146 public String getHoverClassName() { 147 return fElement.getAttribute(CLASS_ATTRIBUTE); 148 } 149 150 155 public String getLabel() { 156 String label= fElement.getAttribute(LABEL_ATTRIBUTE); 157 if (label != null) 158 return label; 159 160 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 174 public String 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 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 result= new ArrayList (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 compiledTextHoverModifiers= JavaPlugin.getDefault().getPreferenceStore().getString(PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIERS); 207 208 StringTokenizer tokenizer= new StringTokenizer (compiledTextHoverModifiers, VALUE_SEPARATOR); 209 HashMap idToModifier= new HashMap (tokenizer.countTokens() / 2); 210 211 while (tokenizer.hasMoreTokens()) { 212 String id= tokenizer.nextToken(); 213 if (tokenizer.hasMoreTokens()) 214 idToModifier.put(id, tokenizer.nextToken()); 215 } 216 217 String compiledTextHoverModifierMasks= JavaPlugin.getDefault().getPreferenceStore().getString(PreferenceConstants.EDITOR_TEXT_HOVER_MODIFIER_MASKS); 218 219 tokenizer= new StringTokenizer (compiledTextHoverModifierMasks, VALUE_SEPARATOR); 220 HashMap idToModifierMask= new HashMap (tokenizer.countTokens() / 2); 221 222 while (tokenizer.hasMoreTokens()) { 223 String 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 modifierString= (String )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= ""; 242 hovers[i].fModifierString= modifierString; 243 hovers[i].fIsEnabled= enabled; 244 hovers[i].fStateMask= computeStateMask(modifierString); 245 if (hovers[i].fStateMask == -1) { 246 try { 248 hovers[i].fStateMask= Integer.parseInt((String )idToModifierMask.get(hovers[i].getId())); 249 } catch (NumberFormatException ex) { 250 hovers[i].fStateMask= -1; 251 } 252 int stateMask= hovers[i].fStateMask; 254 if (stateMask == -1) 255 hovers[i].fModifierString= ""; else 257 hovers[i].fModifierString= EditorUtility.getModifierString(stateMask); 258 } 259 } 260 } 261 262 267 public int getStateMask() { 268 return fStateMask; 269 } 270 271 276 public String getModifierString() { 277 return fModifierString; 278 } 279 280 285 public boolean isEnabled() { 286 return fIsEnabled; 287 } 288 289 295 public IConfigurationElement getConfigurationElement() { 296 return fElement; 297 } 298 } 299 | Popular Tags |