Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 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.ui.internal.texteditor; |
13 |
|
14 |
import java.util.ArrayList; |
15 |
import java.util.List; |
16 |
|
17 |
import org.osgi.framework.Bundle; |
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.ISafeRunnable; |
24 |
import org.eclipse.core.runtime.IStatus; |
25 |
import org.eclipse.core.runtime.Platform; |
26 |
import org.eclipse.core.runtime.SafeRunner; |
27 |
import org.eclipse.core.runtime.Status; |
28 |
|
29 |
import org.eclipse.jface.util.SafeRunnable; |
30 |
|
31 |
import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector; |
32 |
import org.eclipse.jface.text.hyperlink.IHyperlinkDetector; |
33 |
|
34 |
/** |
35 |
* Describes a contribution to the 'org.eclipse.ui.workbench.texteditor.hyperlinkDetectors' |
36 |
* extension point. |
37 |
* |
38 |
* @since 3.3 |
39 |
*/ |
40 |
public class HyperlinkDetectorDescriptor { |
41 |
|
42 |
private static final String HYPERLINK_DETECTORS_EXTENSION_POINT= "org.eclipse.ui.workbench.texteditor.hyperlinkDetectors"; //$NON-NLS-1$ |
43 |
private static final String HYPERLINK_DETECTOR_ELEMENT= "hyperlinkDetector"; //$NON-NLS-1$ |
44 |
private static final String ID_ATTRIBUTE= "id"; //$NON-NLS-1$ |
45 |
private static final String NAME_ATTRIBUTE= "name"; //$NON-NLS-1$ |
46 |
private static final String DESCRIPTION_ATTRIBUTE= "description"; //$NON-NLS-1$ |
47 |
private static final String TARGET_ID_ATTRIBUTE= "targetId"; //$NON-NLS-1$ |
48 |
private static final String CLASS_ATTRIBUTE= "class"; //$NON-NLS-1$ |
49 |
private static final String ACTIVATE_PLUG_IN_ATTRIBUTE= "activate"; //$NON-NLS-1$ |
50 |
|
51 |
private IConfigurationElement fElement; |
52 |
|
53 |
|
54 |
/** |
55 |
* Returns descriptors for all hyperlink detector extensions. |
56 |
* |
57 |
* @return an array with the contributed hyperlink detectors |
58 |
*/ |
59 |
public static HyperlinkDetectorDescriptor[] getContributedHyperlinkDetectors() { |
60 |
IExtensionRegistry registry= Platform.getExtensionRegistry(); |
61 |
IConfigurationElement[] elements= registry.getConfigurationElementsFor(HYPERLINK_DETECTORS_EXTENSION_POINT); |
62 |
HyperlinkDetectorDescriptor[] hyperlinkDetectorDescs= createDescriptors(elements); |
63 |
return hyperlinkDetectorDescs; |
64 |
} |
65 |
|
66 |
/** |
67 |
* Creates a new descriptor from the given configuration element. |
68 |
* |
69 |
* @param element the configuration element |
70 |
*/ |
71 |
private HyperlinkDetectorDescriptor(IConfigurationElement element) { |
72 |
Assert.isNotNull(element); |
73 |
fElement= element; |
74 |
} |
75 |
|
76 |
/** |
77 |
* Creates a new {@link IHyperlinkDetector}. |
78 |
* |
79 |
* @return the hyperlink detector or <code>null</code> if the plug-in isn't loaded yet |
80 |
* @throws CoreException if a failure occurred during creation |
81 |
*/ |
82 |
public AbstractHyperlinkDetector createHyperlinkDetector() throws CoreException { |
83 |
final Throwable[] exception= new Throwable[1]; |
84 |
final AbstractHyperlinkDetector[] result= new AbstractHyperlinkDetector[1]; |
85 |
String message= NLSUtility.format(EditorMessages.Editor_error_HyperlinkDetector_couldNotCreate_message, new String[] { getId(), fElement.getContributor().getName() }); |
86 |
ISafeRunnable code= new SafeRunnable(message) { |
87 |
/* |
88 |
* @see org.eclipse.core.runtime.ISafeRunnable#run() |
89 |
*/ |
90 |
public void run() throws Exception { |
91 |
String pluginId = fElement.getContributor().getName(); |
92 |
boolean isPlugInActivated= Platform.getBundle(pluginId).getState() == Bundle.ACTIVE; |
93 |
if (isPlugInActivated || canActivatePlugIn()) |
94 |
result[0]= (AbstractHyperlinkDetector)fElement.createExecutableExtension(CLASS_ATTRIBUTE); |
95 |
} |
96 |
/* |
97 |
* @see org.eclipse.jface.util.SafeRunnable#handleException(java.lang.Throwable) |
98 |
*/ |
99 |
public void handleException(Throwable ex) { |
100 |
super.handleException(ex); |
101 |
exception[0]= ex; |
102 |
} |
103 |
|
104 |
}; |
105 |
|
106 |
SafeRunner.run(code); |
107 |
|
108 |
if (exception[0] == null) |
109 |
return result[0]; |
110 |
throw new CoreException(new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, IStatus.OK, message, exception[0])); |
111 |
|
112 |
} |
113 |
|
114 |
private boolean isValid(HyperlinkDetectorTargetDescriptor[] targets) { |
115 |
if (getId() == null || getName() == null || getTargetId() == null) |
116 |
return false; |
117 |
|
118 |
String targetId= getTargetId(); |
119 |
for (int i= 0; i < targets.length; i++) { |
120 |
if (targetId.equals(targets[i].getId())) |
121 |
return true; |
122 |
} |
123 |
return false; |
124 |
|
125 |
} |
126 |
|
127 |
//---- XML Attribute accessors --------------------------------------------- |
128 |
|
129 |
/** |
130 |
* Returns the hyperlink detector's id. |
131 |
* |
132 |
* @return the hyperlink detector's id |
133 |
*/ |
134 |
public String getId() { |
135 |
return fElement.getAttribute(ID_ATTRIBUTE); |
136 |
} |
137 |
|
138 |
/** |
139 |
* Returns the hyperlink detector's name. |
140 |
* |
141 |
* @return the hyperlink detector's name |
142 |
*/ |
143 |
public String getName() { |
144 |
return fElement.getAttribute(NAME_ATTRIBUTE); |
145 |
} |
146 |
|
147 |
/** |
148 |
* Returns the hyperlink detector's target id. |
149 |
* |
150 |
* @return the hyperlink detector's target id |
151 |
*/ |
152 |
public String getTargetId() { |
153 |
return fElement.getAttribute(TARGET_ID_ATTRIBUTE); |
154 |
} |
155 |
|
156 |
/** |
157 |
* Returns the hyperlink detector's description. |
158 |
* |
159 |
* @return the hyperlink detector's description or <code>null</code> if not provided |
160 |
*/ |
161 |
public String getDescription() { |
162 |
return fElement.getAttribute(DESCRIPTION_ATTRIBUTE); |
163 |
} |
164 |
|
165 |
public boolean canActivatePlugIn() { |
166 |
String value= fElement.getAttribute(ACTIVATE_PLUG_IN_ATTRIBUTE); |
167 |
if (value == null) |
168 |
return true; |
169 |
return Boolean.valueOf(value).booleanValue(); |
170 |
} |
171 |
|
172 |
public boolean equals(Object obj) { |
173 |
if (obj == null || !obj.getClass().equals(this.getClass()) || getId() == null) |
174 |
return false; |
175 |
return getId().equals(((HyperlinkDetectorDescriptor)obj).getId()); |
176 |
} |
177 |
|
178 |
public int hashCode() { |
179 |
return getId().hashCode(); |
180 |
} |
181 |
|
182 |
private static HyperlinkDetectorDescriptor[] createDescriptors(IConfigurationElement[] elements) { |
183 |
HyperlinkDetectorTargetDescriptor[] targets= HyperlinkDetectorTargetDescriptor.getContributedHyperlinkDetectorTargets(); |
184 |
List result= new ArrayList(elements.length); |
185 |
for (int i= 0; i < elements.length; i++) { |
186 |
IConfigurationElement element= elements[i]; |
187 |
if (HYPERLINK_DETECTOR_ELEMENT.equals(element.getName())) { |
188 |
HyperlinkDetectorDescriptor desc= new HyperlinkDetectorDescriptor(element); |
189 |
if (desc.isValid(targets)) |
190 |
result.add(desc); |
191 |
else { |
192 |
String message= NLSUtility.format(EditorMessages.Editor_error_HyperlinkDetector_invalidExtension_message, new String[] {desc.getId(), element.getContributor().getName()}); |
193 |
TextEditorPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, IStatus.OK, message, null)); |
194 |
} |
195 |
} else { |
196 |
String message= NLSUtility.format(EditorMessages.Editor_error_HyperlinkDetector_invalidElementName_message, new String[] { element.getContributor().getName(), element.getName() }); |
197 |
TextEditorPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, IStatus.OK, message, null)); |
198 |
} |
199 |
} |
200 |
return (HyperlinkDetectorDescriptor[])result.toArray(new HyperlinkDetectorDescriptor[result.size()]); |
201 |
} |
202 |
|
203 |
} |