KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > mapper > editors > HBMXMLHyperlinkDetector


1 package org.hibernate.eclipse.mapper.editors;
2
3 import org.eclipse.jdt.core.IJavaElement;
4 import org.eclipse.jdt.core.IJavaProject;
5 import org.eclipse.jface.text.Assert;
6 import org.eclipse.jface.text.IDocument;
7 import org.eclipse.jface.text.IRegion;
8 import org.eclipse.jface.text.ITextViewer;
9 import org.eclipse.jface.text.Region;
10 import org.eclipse.jface.text.hyperlink.IHyperlink;
11 import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
12 import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
13 import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
14 import org.eclipse.wst.sse.core.internal.provisional.StructuredModelManager;
15 import org.eclipse.wst.sse.core.internal.util.StringUtils;
16 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr;
17 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
18 import org.hibernate.eclipse.mapper.extractor.HBMInfoExtractor;
19 import org.hibernate.eclipse.mapper.extractor.HBMInfoHandler;
20 import org.w3c.dom.Attr JavaDoc;
21 import org.w3c.dom.NamedNodeMap JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23
24
25 /**
26  * hyper link detector for hbm.xml
27  */

28 public class HBMXMLHyperlinkDetector implements IHyperlinkDetector {
29
30     HBMInfoExtractor infoExtractor = new HBMInfoExtractor();
31     
32     private final IJavaProject project;
33
34     /**
35      * Creates a new hbm.xml element hyperlink detector.
36      * @param project
37      *
38      * @param editor the editor in which to detect the hyperlink
39      */

40     public HBMXMLHyperlinkDetector(IJavaProject project) {
41         Assert.isNotNull(project);
42         this.project = project;
43     }
44
45     /*
46      * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion, boolean)
47      */

48     public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
49         if (region == null || textViewer == null) {
50             return null;
51         }
52         
53         IDocument document = textViewer.getDocument();
54         Node currentNode = getCurrentNode(document, region.getOffset());
55         if (currentNode != null) {
56         
57             short nodeType = currentNode.getNodeType();
58             if(nodeType == Node.ATTRIBUTE_NODE) {
59                                     
60             } else if (nodeType == Node.ELEMENT_NODE){
61
62                 Attr currentAttrNode = getCurrentAttrNode(currentNode, region.getOffset());
63                 
64                 if(currentAttrNode!=null) {
65                     String JavaDoc path = currentNode.getNodeName() + ">" + currentAttrNode.getName();
66                     HBMInfoHandler handler = infoExtractor.getAttributeHandler(path);
67                     if(handler!=null) {
68                         IJavaElement element = handler.getJavaElement(project, currentNode, currentAttrNode);
69                         if(element!=null) {
70                             return new IHyperlink[] {new HBMXMLHyperlink(getHyperlinkRegion(currentAttrNode), element)};
71                         } else {
72                             return null;
73                         }
74                         
75                     }
76                 }
77             }
78         }
79         
80         return null;
81         
82     }
83     
84     private IRegion getHyperlinkRegion(Node node) {
85         IRegion hyperRegion = null;
86
87         if (node != null) {
88             short nodeType = node.getNodeType();
89             if (nodeType == Node.DOCUMENT_TYPE_NODE) {
90                 // handle doc type node
91
IDOMNode docNode = (IDOMNode) node;
92                 hyperRegion = new Region(docNode.getStartOffset(), docNode.getEndOffset() - docNode.getStartOffset());
93             }
94             else if (nodeType == Node.ATTRIBUTE_NODE) {
95                 // handle attribute nodes
96
IDOMAttr att = (IDOMAttr) node;
97                 // do not include quotes in attribute value region
98
int regOffset = att.getValueRegionStartOffset();
99                 int regLength = att.getValueRegion().getTextLength();
100                 String JavaDoc attValue = att.getValueRegionText();
101                 if (StringUtils.isQuoted(attValue)) {
102                     regOffset = ++regOffset;
103                     regLength = regLength - 2;
104                 }
105                 hyperRegion = new Region(regOffset, regLength);
106             }
107         }
108         return hyperRegion;
109     }
110     
111     private Attr getCurrentAttrNode(Node node, int offset) {
112         if ((node instanceof IndexedRegion) && ((IndexedRegion) node).contains(offset) && (node.hasAttributes())) {
113             NamedNodeMap JavaDoc attrs = node.getAttributes();
114             // go through each attribute in node and if attribute contains
115
// offset, return that attribute
116
for (int i = 0; i < attrs.getLength(); ++i) {
117                 // assumption that if parent node is of type IndexedRegion,
118
// then its attributes will also be of type IndexedRegion
119
IndexedRegion attRegion = (IndexedRegion) attrs.item(i);
120                 if (attRegion.contains(offset)) {
121                     return (Attr) attrs.item(i);
122                 }
123             }
124         }
125         return null;
126     }
127     
128         
129     /**
130      * Returns the node the cursor is currently on in the document. null if no
131      * node is selected
132      *
133      * @param offset
134      * @return Node either element, doctype, text, or null
135      */

136     private Node getCurrentNode(IDocument document, int offset) {
137         // get the current node at the offset (returns either: element,
138
// doctype, text)
139
IndexedRegion inode = null;
140         IStructuredModel sModel = null;
141         try {
142             sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document);
143             inode = sModel.getIndexedRegion(offset);
144             if (inode == null)
145                 inode = sModel.getIndexedRegion(offset - 1);
146         }
147         finally {
148             if (sModel != null)
149                 sModel.releaseFromRead();
150         }
151
152         if (inode instanceof Node) {
153             return (Node) inode;
154         }
155         return null;
156     }
157 }
158
Popular Tags