1 11 12 package org.eclipse.ui.texteditor; 13 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.core.runtime.IConfigurationElement; 19 import org.eclipse.core.runtime.IExtensionRegistry; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.Platform; 22 import org.eclipse.core.runtime.Status; 23 24 import org.eclipse.ui.internal.texteditor.NLSUtility; 25 import org.eclipse.ui.internal.texteditor.TextEditorPlugin; 26 27 33 public final class HyperlinkDetectorTargetDescriptor { 34 35 private static final String HYPERLINK_DETECTOR_TARGETS_EXTENSION_POINT= "org.eclipse.ui.workbench.texteditor.hyperlinkDetectorTargets"; private static final String TARGET_ELEMENT= "target"; 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 CONTEXT_ELEMENT= "context"; private static final String TYPE_ATTRIBUTE= "type"; 43 private IConfigurationElement fElement; 44 45 46 51 public static HyperlinkDetectorTargetDescriptor[] getContributedHyperlinkDetectorTargets() { 52 IExtensionRegistry registry= Platform.getExtensionRegistry(); 53 IConfigurationElement[] elements= registry.getConfigurationElementsFor(HYPERLINK_DETECTOR_TARGETS_EXTENSION_POINT); 54 HyperlinkDetectorTargetDescriptor[] hyperlinkDetectorDescs= createDescriptors(elements); 55 return hyperlinkDetectorDescs; 56 } 57 58 63 private HyperlinkDetectorTargetDescriptor(IConfigurationElement element) { 64 Assert.isNotNull(element); 65 fElement= element; 66 } 67 68 70 75 public String getId() { 76 return fElement.getAttribute(ID_ATTRIBUTE); 77 } 78 79 84 public String getName() { 85 return fElement.getAttribute(NAME_ATTRIBUTE); 86 } 87 88 94 public String [] getTypes() { 95 IConfigurationElement[] contexts= fElement.getChildren(CONTEXT_ELEMENT); 96 String [] types= new String [contexts.length]; 97 for (int i= 0; i < contexts.length; i++) 98 types[i]= contexts[i].getAttribute(TYPE_ATTRIBUTE); 99 return types; 100 } 101 102 107 public String getDescription() { 108 return fElement.getAttribute(DESCRIPTION_ATTRIBUTE); 109 } 110 111 112 public boolean equals(Object obj) { 113 if (obj == null || !obj.getClass().equals(this.getClass()) || getId() == null) 114 return false; 115 return getId().equals(((HyperlinkDetectorTargetDescriptor)obj).getId()); 116 } 117 118 public int hashCode() { 119 return getId().hashCode(); 120 } 121 122 private static HyperlinkDetectorTargetDescriptor[] createDescriptors(IConfigurationElement[] elements) { 123 List result= new ArrayList (elements.length); 124 for (int i= 0; i < elements.length; i++) { 125 IConfigurationElement element= elements[i]; 126 if (TARGET_ELEMENT.equals(element.getName())) { 127 HyperlinkDetectorTargetDescriptor desc= new HyperlinkDetectorTargetDescriptor(element); 128 if (desc.isValid()) 129 result.add(desc); 130 else { 131 String message= NLSUtility.format(EditorMessages.Editor_error_HyperlinkDetectorTarget_invalidExtension_message, new String [] {desc.getId(), element.getContributor().getName()}); 132 TextEditorPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, IStatus.OK, message, null)); 133 } 134 } else { 135 String message= NLSUtility.format(EditorMessages.Editor_error_HyperlinkDetectorTarget_invalidElementName_message, new String [] { element.getContributor().getName(), element.getName() }); 136 TextEditorPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, IStatus.OK, message, null)); 137 } 138 139 } 140 return (HyperlinkDetectorTargetDescriptor[])result.toArray(new HyperlinkDetectorTargetDescriptor[result.size()]); 141 } 142 143 private boolean isValid() { 144 return getId() != null && getName() != null; 145 } 146 147 } 148 | Popular Tags |