KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > plugin > ManifestHyperlinkDetector


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.pde.internal.ui.editor.plugin;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.jface.text.IRegion;
15 import org.eclipse.jface.text.ITextViewer;
16 import org.eclipse.jface.text.Region;
17 import org.eclipse.jface.text.hyperlink.IHyperlink;
18 import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
19 import org.eclipse.pde.core.plugin.IPluginExtension;
20 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
21 import org.eclipse.pde.core.plugin.IPluginModelBase;
22 import org.eclipse.pde.core.plugin.IPluginObject;
23 import org.eclipse.pde.internal.core.ischema.IMetaAttribute;
24 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
25 import org.eclipse.pde.internal.core.schema.SchemaRootElement;
26 import org.eclipse.pde.internal.core.text.IDocumentAttribute;
27 import org.eclipse.pde.internal.core.text.IDocumentNode;
28 import org.eclipse.pde.internal.core.text.IDocumentRange;
29 import org.eclipse.pde.internal.core.text.IDocumentTextNode;
30 import org.eclipse.pde.internal.ui.editor.PDESourcePage;
31 import org.eclipse.pde.internal.ui.editor.text.JavaHyperlink;
32 import org.eclipse.pde.internal.ui.editor.text.ResourceHyperlink;
33 import org.eclipse.pde.internal.ui.editor.text.SchemaHyperlink;
34 import org.eclipse.pde.internal.ui.editor.text.TranslationHyperlink;
35 import org.eclipse.pde.internal.ui.editor.text.XMLUtil;
36
37 public class ManifestHyperlinkDetector implements IHyperlinkDetector {
38
39     private PDESourcePage fSourcePage;
40     
41     /**
42      * @param editor the editor in which to detect the hyperlink
43      */

44     public ManifestHyperlinkDetector(PDESourcePage page) {
45         fSourcePage = page;
46     }
47     
48     public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
49         if (region == null || canShowMultipleHyperlinks)
50             return null;
51
52         IDocumentRange element = fSourcePage.getRangeElement(region.getOffset(), true);
53         if (!XMLUtil.withinRange(element, region.getOffset()))
54             return null;
55         
56         if (element instanceof IDocumentAttribute)
57             return detectAttributeHyperlink((IDocumentAttribute)element);
58         if (element instanceof IDocumentNode)
59             return detectNodeHyperlink((IDocumentNode)element);
60         if (element instanceof IDocumentTextNode)
61             return detectTextNodeHyperlink((IDocumentTextNode)element);
62         return null;
63     }
64
65     private IHyperlink[] detectAttributeHyperlink(IDocumentAttribute attr) {
66         String JavaDoc attrValue = attr.getAttributeValue();
67         if (attrValue.length() == 0)
68             return null;
69         
70         IPluginObject node = XMLUtil.getTopLevelParent((IDocumentNode)attr);
71         if (node == null || !node.getModel().isEditable())
72             return null;
73         
74         IPluginModelBase base = node.getPluginModel();
75         IResource res = base.getUnderlyingResource();
76         IRegion linkRegion = new Region(attr.getValueOffset(), attr.getValueLength());
77
78         IHyperlink[] link = new IHyperlink[1];
79         if (node instanceof IPluginExtensionPoint) {
80             if (attr.getAttributeName().equals(IPluginExtensionPoint.P_SCHEMA))
81                 link[0] = new SchemaHyperlink(linkRegion, attrValue, res);
82             else if (attr.getAttributeName().equals(IPluginObject.P_NAME))
83                 if (attrValue.charAt(0) == '%')
84                     link[0] = new TranslationHyperlink(linkRegion, attrValue, base);
85             
86         } else if (node instanceof IPluginExtension) {
87             ISchemaAttribute sAttr = XMLUtil.getSchemaAttribute(attr, ((IPluginExtension)node).getPoint());
88             if (sAttr == null)
89                 return null;
90             
91             if (sAttr.getKind() == IMetaAttribute.JAVA) {
92                 link[0] = new JavaHyperlink(linkRegion, attrValue, res);
93             } else if (sAttr.getKind() == IMetaAttribute.RESOURCE) {
94                 link[0] = new ResourceHyperlink(linkRegion, attrValue, res);
95             } else if (sAttr.getParent() instanceof SchemaRootElement) {
96                 if (attr.getAttributeName().equals(IPluginExtension.P_POINT))
97                     link[0] = new ExtensionHyperLink(linkRegion, attrValue);
98             } else if (sAttr.isTranslatable()) {
99                 if (attrValue.charAt(0) == '%')
100                     link[0] = new TranslationHyperlink(linkRegion, attrValue, base);
101             }
102         }
103         
104         if (link[0] != null)
105             return link;
106         return null;
107     }
108     
109     private IHyperlink[] detectNodeHyperlink(IDocumentNode node) {
110         // TODO what can we do here?
111
// suggestions:
112
// - use SchemaEditor.openToElement(IPath path, ISchemaElement element)
113
// to directly highlight this particular element in a schema editor
114
// ? too fancy ?
115
/*
116         IPluginObject parent = XMLUtil.getTopLevelParent(node);
117         if (parent == null || !parent.getModel().isEditable())
118             return null;
119         
120         if (parent instanceof IPluginExtension) {
121             ISchemaElement sElement = XMLUtil.getSchemaElement(node, ((IPluginExtension)parent).getPoint());
122             if (sElement == null)
123                 return null;
124             URL url = sElement.getSchema().getURL();
125             // only have access to URL now - extend SchemaEditor?
126             SchemaEditor.openToElement(url, sElement);
127         }
128 */

129         return null;
130     }
131     
132     private IHyperlink[] detectTextNodeHyperlink(IDocumentTextNode node) {
133         IDocumentNode enclosing = node.getEnclosingElement();
134         if (!(enclosing instanceof IPluginObject))
135             return null;
136         IPluginModelBase base = ((IPluginObject)enclosing).getPluginModel();
137         if (node.getText().charAt(0) == '%')
138             return new IHyperlink[] {
139                 new TranslationHyperlink(
140                         new Region(node.getOffset(), node.getLength()), node.getText(), base)};
141         return null;
142     }
143 }
144
Popular Tags