1 11 package org.eclipse.ui.texteditor; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.osgi.framework.Bundle; 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IConfigurationElement; 21 import org.eclipse.core.runtime.IExtensionRegistry; 22 import org.eclipse.core.runtime.ISafeRunnable; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.Platform; 25 import org.eclipse.core.runtime.SafeRunner; 26 import org.eclipse.core.runtime.Status; 27 28 import org.eclipse.jface.util.SafeRunnable; 29 30 import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector; 31 import org.eclipse.jface.text.hyperlink.IHyperlinkDetector; 32 33 import org.eclipse.ui.internal.texteditor.NLSUtility; 34 import org.eclipse.ui.internal.texteditor.TextEditorPlugin; 35 36 37 43 public final class HyperlinkDetectorDescriptor { 44 45 public static final String STATE_MASK_POSTFIX= "_stateMask"; 47 private static final String HYPERLINK_DETECTORS_EXTENSION_POINT= "org.eclipse.ui.workbench.texteditor.hyperlinkDetectors"; private static final String HYPERLINK_DETECTOR_ELEMENT= "hyperlinkDetector"; private static final String ID_ATTRIBUTE= "id"; private static final String NAME_ATTRIBUTE= "name"; private static final String DESCRIPTION_ATTRIBUTE= "description"; private static final String TARGET_ID_ATTRIBUTE= "targetId"; private static final String CLASS_ATTRIBUTE= "class"; private static final String ACTIVATE_PLUG_IN_ATTRIBUTE= "activate"; private static final String MODIFIER_KEYS= "modifierKeys"; 57 private IConfigurationElement fElement; 58 private HyperlinkDetectorTargetDescriptor fTarget; 59 60 61 66 public static HyperlinkDetectorDescriptor[] getContributedHyperlinkDetectors() { 67 IExtensionRegistry registry= Platform.getExtensionRegistry(); 68 IConfigurationElement[] elements= registry.getConfigurationElementsFor(HYPERLINK_DETECTORS_EXTENSION_POINT); 69 HyperlinkDetectorDescriptor[] hyperlinkDetectorDescs= createDescriptors(elements); 70 return hyperlinkDetectorDescs; 71 } 72 73 78 private HyperlinkDetectorDescriptor(IConfigurationElement element) { 79 Assert.isNotNull(element); 80 fElement= element; 81 } 82 83 89 public AbstractHyperlinkDetector createHyperlinkDetector() throws CoreException { 90 final Throwable [] exception= new Throwable [1]; 91 final AbstractHyperlinkDetector[] result= new AbstractHyperlinkDetector[1]; 92 String message= NLSUtility.format(EditorMessages.Editor_error_HyperlinkDetector_couldNotCreate_message, new String [] { getId(), fElement.getContributor().getName() }); 93 ISafeRunnable code= new SafeRunnable(message) { 94 97 public void run() throws Exception { 98 String pluginId = fElement.getContributor().getName(); 99 boolean isPlugInActivated= Platform.getBundle(pluginId).getState() == Bundle.ACTIVE; 100 if (isPlugInActivated || canActivatePlugIn()) 101 result[0]= (AbstractHyperlinkDetector)fElement.createExecutableExtension(CLASS_ATTRIBUTE); 102 } 103 106 public void handleException(Throwable ex) { 107 super.handleException(ex); 108 exception[0]= ex; 109 } 110 111 }; 112 113 SafeRunner.run(code); 114 115 if (exception[0] == null) 116 return result[0]; 117 throw new CoreException(new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, IStatus.OK, message, exception[0])); 118 119 } 120 121 private boolean isValid(HyperlinkDetectorTargetDescriptor[] targets) { 122 if (getId() == null || getName() == null || getTargetId() == null) 123 return false; 124 125 String targetId= getTargetId(); 126 for (int i= 0; i < targets.length; i++) { 127 if (targetId.equals(targets[i].getId())) { 128 fTarget= targets[i]; 129 return true; 130 } 131 } 132 return false; 133 134 } 135 136 138 143 public String getId() { 144 return fElement.getAttribute(ID_ATTRIBUTE); 145 } 146 147 152 public String getName() { 153 return fElement.getAttribute(NAME_ATTRIBUTE); 154 } 155 156 161 public HyperlinkDetectorTargetDescriptor getTarget() { 162 return fTarget; 163 } 164 165 170 public String getTargetId() { 171 return fElement.getAttribute(TARGET_ID_ATTRIBUTE); 172 } 173 174 179 public String getDescription() { 180 return fElement.getAttribute(DESCRIPTION_ATTRIBUTE); 181 } 182 183 189 public String getModifierKeys() { 190 return fElement.getAttribute(MODIFIER_KEYS); 191 } 192 193 public boolean canActivatePlugIn() { 194 String value= fElement.getAttribute(ACTIVATE_PLUG_IN_ATTRIBUTE); 195 if (value == null) 196 return true; 197 return Boolean.valueOf(value).booleanValue(); 198 } 199 200 public boolean equals(Object obj) { 201 if (obj == null || !obj.getClass().equals(this.getClass()) || getId() == null) 202 return false; 203 return getId().equals(((HyperlinkDetectorDescriptor)obj).getId()); 204 } 205 206 public int hashCode() { 207 return getId().hashCode(); 208 } 209 210 private static HyperlinkDetectorDescriptor[] createDescriptors(IConfigurationElement[] elements) { 211 HyperlinkDetectorTargetDescriptor[] targets= HyperlinkDetectorTargetDescriptor.getContributedHyperlinkDetectorTargets(); 212 List result= new ArrayList (elements.length); 213 for (int i= 0; i < elements.length; i++) { 214 IConfigurationElement element= elements[i]; 215 if (HYPERLINK_DETECTOR_ELEMENT.equals(element.getName())) { 216 HyperlinkDetectorDescriptor desc= new HyperlinkDetectorDescriptor(element); 217 if (desc.isValid(targets)) 218 result.add(desc); 219 else { 220 String message= NLSUtility.format(EditorMessages.Editor_error_HyperlinkDetector_invalidExtension_message, new String [] {desc.getId(), element.getContributor().getName()}); 221 TextEditorPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, IStatus.OK, message, null)); 222 } 223 } else { 224 String message= NLSUtility.format(EditorMessages.Editor_error_HyperlinkDetector_invalidElementName_message, new String [] { element.getContributor().getName(), element.getName() }); 225 TextEditorPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, IStatus.OK, message, null)); 226 } 227 } 228 return (HyperlinkDetectorDescriptor[])result.toArray(new HyperlinkDetectorDescriptor[result.size()]); 229 } 230 231 } 232 | Popular Tags |