KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > text > PluginXMLTextHover


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.pde.internal.ui.editor.text;
12
13 import java.net.URL JavaDoc;
14
15 import org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.IRegion;
17 import org.eclipse.jface.text.ITextViewer;
18 import org.eclipse.pde.core.plugin.IPluginAttribute;
19 import org.eclipse.pde.core.plugin.IPluginElement;
20 import org.eclipse.pde.core.plugin.IPluginExtension;
21 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
22 import org.eclipse.pde.core.plugin.IPluginObject;
23 import org.eclipse.pde.internal.core.PDECore;
24 import org.eclipse.pde.internal.core.ischema.ISchema;
25 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
26 import org.eclipse.pde.internal.core.ischema.ISchemaElement;
27 import org.eclipse.pde.internal.core.ischema.ISchemaObject;
28 import org.eclipse.pde.internal.core.schema.SchemaAnnotationHandler;
29 import org.eclipse.pde.internal.core.text.IDocumentAttribute;
30 import org.eclipse.pde.internal.core.text.IDocumentNode;
31 import org.eclipse.pde.internal.core.text.IDocumentRange;
32 import org.eclipse.pde.internal.core.text.IDocumentTextNode;
33 import org.eclipse.pde.internal.core.util.SchemaUtil;
34 import org.eclipse.pde.internal.core.util.XMLComponentRegistry;
35 import org.eclipse.pde.internal.ui.editor.PDESourcePage;
36
37 public class PluginXMLTextHover extends PDETextHover {
38     
39     private PDESourcePage fSourcePage;
40     
41     public PluginXMLTextHover(PDESourcePage sourcePage) {
42         fSourcePage = sourcePage;
43     }
44
45     public String JavaDoc getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
46         int offset = hoverRegion.getOffset();
47         IDocumentRange range = fSourcePage.getRangeElement(offset, true);
48         if (range instanceof IDocumentTextNode)
49             return checkTranslatedValue((IDocumentTextNode)range);
50         if (!(range instanceof IPluginObject))
51             return null;
52         
53         ISchema schema = getExtensionSchema((IPluginObject)range);
54         if (schema != null) {
55             ISchemaObject sObj = getSchemaObject(schema, (IPluginObject)range);
56             if (sObj == null) {
57                 return null;
58             } else if (range instanceof IPluginAttribute && sObj instanceof ISchemaElement) {
59                 IDocumentAttribute da = (IDocumentAttribute)range;
60                 if (da.getNameOffset() <= offset &&
61                         offset <= da.getNameOffset() + da.getNameLength() - 1)
62                     // inside name
63
return getAttributeText((IPluginAttribute)range, (ISchemaElement)sObj);
64                 else if (da.getValueOffset() <= offset &&
65                         offset <= da.getValueOffset() + da.getValueLength() - 1)
66                     // inside value
67
return getAttributeValueText((IPluginAttribute)range, (ISchemaElement)sObj);
68             } else if (range instanceof IPluginElement) {
69                 IDocumentNode dn = (IDocumentNode)range;
70                 int dnOff = dn.getOffset();
71                 int dnLen = dn.getLength();
72                 String JavaDoc dnName = dn.getXMLTagName();
73                 if (dnOff + 1 <= offset && offset <= dnOff + dnName.length())
74                     // inside opening tag
75
return getElementText((ISchemaElement)sObj);
76                 try {
77                     String JavaDoc nt = textViewer.getDocument().get(dnOff, dnLen);
78                     if (nt.endsWith("</" + dnName + '>')) { //$NON-NLS-1$
79
offset = offset - dnOff;
80                         if (nt.length() - dnName.length() - 1 <= offset &&
81                                 offset <= nt.length() - 2)
82                             // inside closing tag
83
return getElementText((ISchemaElement)sObj);
84                     }
85                 } catch (BadLocationException e) {
86                 }
87             }
88         } else if (range instanceof IDocumentAttribute &&
89                     ((IDocumentAttribute)range).getEnclosingElement() instanceof IPluginExtensionPoint)
90                 return getExtensionPointHoverInfo((IPluginObject)range, offset);
91         
92         return null;
93     }
94
95     private String JavaDoc getExtensionPointHoverInfo(IPluginObject object, int offset) {
96         IDocumentAttribute da = (IDocumentAttribute)object;
97         if (da.getValueOffset() <= offset &&
98                 offset <= da.getValueOffset() + da.getValueLength() - 1) {
99             String JavaDoc value = da.getAttributeValue();
100             if (da.getAttributeName().equals(IPluginObject.P_NAME) && value.startsWith("%")) //$NON-NLS-1$
101
return object.getResourceString(value);
102         }
103         return null;
104         
105     }
106     
107     private ISchema getExtensionSchema(IPluginObject object) {
108         IPluginObject extension = object;
109         if (object instanceof IDocumentAttribute)
110             extension = (IPluginObject)((IDocumentAttribute)object).getEnclosingElement();
111         while (extension != null && !(extension instanceof IPluginExtension))
112             extension = extension.getParent();
113         
114         if (extension == null)
115             // started off outside of an extension element
116
return null;
117         
118         String JavaDoc point = ((IPluginExtension)extension).getPoint();
119         return PDECore.getDefault().getSchemaRegistry().getSchema(point);
120     }
121     
122     private ISchemaObject getSchemaObject(ISchema schema, IPluginObject object) {
123         if (object instanceof IPluginElement)
124             return schema.findElement(((IPluginElement)object).getName());
125         if (object instanceof IPluginExtension)
126             return schema.findElement("extension"); //$NON-NLS-1$
127
if (object instanceof IDocumentAttribute)
128             return schema.findElement(((IDocumentAttribute)object).getEnclosingElement().getXMLTagName());
129         return null;
130     }
131     
132     private String JavaDoc getAttributeText(IPluginAttribute attrib, ISchemaElement sEle) {
133         ISchemaAttribute sAtt = sEle.getAttribute(attrib.getName());
134         if (sAtt == null)
135             return null;
136         return sAtt.getDescription();
137     }
138     
139     private String JavaDoc getAttributeValueText(IPluginAttribute attrib, ISchemaElement sEle) {
140         if (sEle.getName().equals("extension") && //$NON-NLS-1$
141
attrib.getName().equals(IPluginExtension.P_POINT))
142             return getSchemaDescription(attrib, sEle);
143         ISchemaAttribute sAtt = sEle.getAttribute(attrib.getName());
144         if (sAtt == null)
145             return null;
146         
147         String JavaDoc value = attrib.getValue();
148         if (sAtt.isTranslatable() && value.startsWith("%")) //$NON-NLS-1$
149
return attrib.getResourceString(value);
150         return null;
151     }
152     
153     private String JavaDoc getSchemaDescription(IPluginAttribute attr, ISchemaElement sEle) {
154         String JavaDoc description = XMLComponentRegistry.Instance().getDescription(
155                 attr.getValue(), XMLComponentRegistry.F_SCHEMA_COMPONENT);
156         
157         if (description == null) {
158             URL JavaDoc url = sEle.getSchema().getURL();
159             SchemaAnnotationHandler handler = new SchemaAnnotationHandler();
160             SchemaUtil.parseURL(url, handler);
161             description = handler.getDescription();
162             XMLComponentRegistry.Instance().putDescription(
163                     attr.getValue(), description, XMLComponentRegistry.F_SCHEMA_COMPONENT);
164         }
165         return description;
166     }
167     
168     private String JavaDoc getElementText(ISchemaElement sEle) {
169         if (sEle == null) {
170             return null;
171         }
172         return sEle.getDescription();
173     }
174
175     private String JavaDoc checkTranslatedValue(IDocumentTextNode node) {
176         String JavaDoc value = node.getText();
177         if (value.startsWith("%")) //$NON-NLS-1$
178
return ((IPluginObject)node.getEnclosingElement()).getResourceString(value);
179         
180         return null;
181     }
182 }
183
Popular Tags