1 11 package org.eclipse.pde.internal.ui.editor.text; 12 13 import java.net.URL ; 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 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 return getAttributeText((IPluginAttribute)range, (ISchemaElement)sObj); 64 else if (da.getValueOffset() <= offset && 65 offset <= da.getValueOffset() + da.getValueLength() - 1) 66 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 dnName = dn.getXMLTagName(); 73 if (dnOff + 1 <= offset && offset <= dnOff + dnName.length()) 74 return getElementText((ISchemaElement)sObj); 76 try { 77 String nt = textViewer.getDocument().get(dnOff, dnLen); 78 if (nt.endsWith("</" + dnName + '>')) { offset = offset - dnOff; 80 if (nt.length() - dnName.length() - 1 <= offset && 81 offset <= nt.length() - 2) 82 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 getExtensionPointHoverInfo(IPluginObject object, int offset) { 96 IDocumentAttribute da = (IDocumentAttribute)object; 97 if (da.getValueOffset() <= offset && 98 offset <= da.getValueOffset() + da.getValueLength() - 1) { 99 String value = da.getAttributeValue(); 100 if (da.getAttributeName().equals(IPluginObject.P_NAME) && value.startsWith("%")) 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 return null; 117 118 String 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"); if (object instanceof IDocumentAttribute) 128 return schema.findElement(((IDocumentAttribute)object).getEnclosingElement().getXMLTagName()); 129 return null; 130 } 131 132 private String 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 getAttributeValueText(IPluginAttribute attrib, ISchemaElement sEle) { 140 if (sEle.getName().equals("extension") && 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 value = attrib.getValue(); 148 if (sAtt.isTranslatable() && value.startsWith("%")) return attrib.getResourceString(value); 150 return null; 151 } 152 153 private String getSchemaDescription(IPluginAttribute attr, ISchemaElement sEle) { 154 String description = XMLComponentRegistry.Instance().getDescription( 155 attr.getValue(), XMLComponentRegistry.F_SCHEMA_COMPONENT); 156 157 if (description == null) { 158 URL 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 getElementText(ISchemaElement sEle) { 169 if (sEle == null) { 170 return null; 171 } 172 return sEle.getDescription(); 173 } 174 175 private String checkTranslatedValue(IDocumentTextNode node) { 176 String value = node.getText(); 177 if (value.startsWith("%")) return ((IPluginObject)node.getEnclosingElement()).getResourceString(value); 179 180 return null; 181 } 182 } 183 | Popular Tags |