KickJava   Java API By Example, From Geeks To Geeks.

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


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.texteditor;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
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 /**
28  * Describes a contribution to the 'org.eclipse.ui.workbench.texteditor.hyperlinkDetectorTargets'
29  * extension point.
30  *
31  * @since 3.3
32  */

33 public final class HyperlinkDetectorTargetDescriptor {
34
35     private static final String JavaDoc HYPERLINK_DETECTOR_TARGETS_EXTENSION_POINT= "org.eclipse.ui.workbench.texteditor.hyperlinkDetectorTargets"; //$NON-NLS-1$
36
private static final String JavaDoc TARGET_ELEMENT= "target"; //$NON-NLS-1$
37
private static final String JavaDoc ID_ATTRIBUTE= "id"; //$NON-NLS-1$
38
private static final String JavaDoc NAME_ATTRIBUTE= "name"; //$NON-NLS-1$
39
private static final String JavaDoc DESCRIPTION_ATTRIBUTE= "description"; //$NON-NLS-1$
40
private static final String JavaDoc CONTEXT_ELEMENT= "context"; //$NON-NLS-1$
41
private static final String JavaDoc TYPE_ATTRIBUTE= "type"; //$NON-NLS-1$
42

43     private IConfigurationElement fElement;
44
45
46     /**
47      * Returns descriptors for all hyperlink detector extensions.
48      *
49      * @return an array with the contributed hyperlink detectors
50      */

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     /**
59      * Creates a new descriptor from the given configuration element.
60      *
61      * @param element the configuration element
62      */

63     private HyperlinkDetectorTargetDescriptor(IConfigurationElement element) {
64         Assert.isNotNull(element);
65         fElement= element;
66     }
67
68     //---- XML Attribute accessors ---------------------------------------------
69

70     /**
71      * Returns the hyperlink detector target's id.
72      *
73      * @return the hyperlink detector target's id
74      */

75     public String JavaDoc getId() {
76         return fElement.getAttribute(ID_ATTRIBUTE);
77     }
78
79     /**
80      * Returns the hyperlink detector target's name.
81      *
82      * @return the hyperlink detector target's name
83      */

84     public String JavaDoc getName() {
85         return fElement.getAttribute(NAME_ATTRIBUTE);
86     }
87     
88     /**
89      * Returns the types that the context of this
90      * hyperlink detector target supports.
91      *
92      * @return an array with type names that this target's context supports
93      */

94     public String JavaDoc[] getTypes() {
95         IConfigurationElement[] contexts= fElement.getChildren(CONTEXT_ELEMENT);
96         String JavaDoc[] types= new String JavaDoc[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     /**
103      * Returns the hyperlink detector target's description.
104      *
105      * @return the hyperlink detector target's description or <code>null</code> if not provided
106      */

107     public String JavaDoc getDescription() {
108         return fElement.getAttribute(DESCRIPTION_ATTRIBUTE);
109     }
110
111
112     public boolean equals(Object JavaDoc 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 JavaDoc result= new ArrayList JavaDoc(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 JavaDoc message= NLSUtility.format(EditorMessages.Editor_error_HyperlinkDetectorTarget_invalidExtension_message, new String JavaDoc[] {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 JavaDoc message= NLSUtility.format(EditorMessages.Editor_error_HyperlinkDetectorTarget_invalidElementName_message, new String JavaDoc[] { 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