KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > HyperlinkDetectorDescriptor


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 package org.eclipse.ui.texteditor;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
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 /**
38  * Describes a contribution to the 'org.eclipse.ui.workbench.texteditor.hyperlinkDetectors'
39  * extension point.
40  *
41  * @since 3.3
42  */

43 public final class HyperlinkDetectorDescriptor {
44     
45     public static final String JavaDoc STATE_MASK_POSTFIX= "_stateMask"; //$NON-NLS-1$
46

47     private static final String JavaDoc HYPERLINK_DETECTORS_EXTENSION_POINT= "org.eclipse.ui.workbench.texteditor.hyperlinkDetectors"; //$NON-NLS-1$
48
private static final String JavaDoc HYPERLINK_DETECTOR_ELEMENT= "hyperlinkDetector"; //$NON-NLS-1$
49
private static final String JavaDoc ID_ATTRIBUTE= "id"; //$NON-NLS-1$
50
private static final String JavaDoc NAME_ATTRIBUTE= "name"; //$NON-NLS-1$
51
private static final String JavaDoc DESCRIPTION_ATTRIBUTE= "description"; //$NON-NLS-1$
52
private static final String JavaDoc TARGET_ID_ATTRIBUTE= "targetId"; //$NON-NLS-1$
53
private static final String JavaDoc CLASS_ATTRIBUTE= "class"; //$NON-NLS-1$
54
private static final String JavaDoc ACTIVATE_PLUG_IN_ATTRIBUTE= "activate"; //$NON-NLS-1$
55
private static final String JavaDoc MODIFIER_KEYS= "modifierKeys"; //$NON-NLS-1$
56

57     private IConfigurationElement fElement;
58     private HyperlinkDetectorTargetDescriptor fTarget;
59
60
61     /**
62      * Returns descriptors for all hyperlink detector extensions.
63      *
64      * @return an array with the contributed hyperlink detectors
65      */

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     /**
74      * Creates a new descriptor from the given configuration element.
75      *
76      * @param element the configuration element
77      */

78     private HyperlinkDetectorDescriptor(IConfigurationElement element) {
79         Assert.isNotNull(element);
80         fElement= element;
81     }
82
83     /**
84      * Creates a new {@link IHyperlinkDetector}.
85      *
86      * @return the hyperlink detector or <code>null</code> if the plug-in isn't loaded yet
87      * @throws CoreException if a failure occurred during creation
88      */

89     public AbstractHyperlinkDetector createHyperlinkDetector() throws CoreException {
90         final Throwable JavaDoc[] exception= new Throwable JavaDoc[1];
91         final AbstractHyperlinkDetector[] result= new AbstractHyperlinkDetector[1];
92         String JavaDoc message= NLSUtility.format(EditorMessages.Editor_error_HyperlinkDetector_couldNotCreate_message, new String JavaDoc[] { getId(), fElement.getContributor().getName() });
93         ISafeRunnable code= new SafeRunnable(message) {
94             /*
95              * @see org.eclipse.core.runtime.ISafeRunnable#run()
96              */

97             public void run() throws Exception JavaDoc {
98                 String JavaDoc 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             /*
104              * @see org.eclipse.jface.util.SafeRunnable#handleException(java.lang.Throwable)
105              */

106             public void handleException(Throwable JavaDoc 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 JavaDoc 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     //---- XML Attribute accessors ---------------------------------------------
137

138     /**
139      * Returns the hyperlink detector's id.
140      *
141      * @return the hyperlink detector's id
142      */

143     public String JavaDoc getId() {
144         return fElement.getAttribute(ID_ATTRIBUTE);
145     }
146
147     /**
148      * Returns the hyperlink detector's name.
149      *
150      * @return the hyperlink detector's name
151      */

152     public String JavaDoc getName() {
153         return fElement.getAttribute(NAME_ATTRIBUTE);
154     }
155     
156     /**
157      * Returns the hyperlink detector's target descriptor.
158      *
159      * @return the hyperlink detector's target descriptor
160      */

161     public HyperlinkDetectorTargetDescriptor getTarget() {
162         return fTarget;
163     }
164     
165     /**
166      * Returns the hyperlink detector's target id.
167      *
168      * @return the hyperlink detector's target id
169      */

170     public String JavaDoc getTargetId() {
171         return fElement.getAttribute(TARGET_ID_ATTRIBUTE);
172     }
173
174     /**
175      * Returns the hyperlink detector's description.
176      *
177      * @return the hyperlink detector's description or <code>null</code> if not provided
178      */

179     public String JavaDoc getDescription() {
180         return fElement.getAttribute(DESCRIPTION_ATTRIBUTE);
181     }
182
183     /**
184      * Returns the hyperlink detector's modifier keys that
185      * need to be pressed for this hyperlink detector.
186      *
187      * @return the hyperlink detector's description or <code>null</code> if not provided
188      */

189     public String JavaDoc getModifierKeys() {
190         return fElement.getAttribute(MODIFIER_KEYS);
191     }
192
193     public boolean canActivatePlugIn() {
194         String JavaDoc 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 JavaDoc 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 JavaDoc result= new ArrayList JavaDoc(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 JavaDoc message= NLSUtility.format(EditorMessages.Editor_error_HyperlinkDetector_invalidExtension_message, new String JavaDoc[] {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 JavaDoc message= NLSUtility.format(EditorMessages.Editor_error_HyperlinkDetector_invalidElementName_message, new String JavaDoc[] { 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