KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.hibernate.eclipse.mapper.editors;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.List JavaDoc;
5
6 import org.eclipse.jdt.core.IJavaProject;
7 import org.eclipse.jface.text.ITextViewer;
8 import org.eclipse.jface.text.contentassist.ICompletionProposal;
9 import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
10 import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;
11 import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList;
12 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
13 import org.eclipse.wst.xml.core.internal.regions.DOMRegionContext;
14 import org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest;
15 import org.eclipse.wst.xml.ui.internal.contentassist.XMLContentAssistProcessor;
16
17 public abstract class HibernateContentAssistProcessor extends XMLContentAssistProcessor {
18
19     private final IJavaProject project;
20
21     public HibernateContentAssistProcessor(IJavaProject project) {
22         this.project = project;
23     }
24
25     protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest) {
26         super.addAttributeValueProposals(contentAssistRequest);
27         
28         IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
29     
30         // Find the attribute region and name for which this position should have a value proposed
31
IStructuredDocumentRegion open = node.getFirstStructuredDocumentRegion();
32         ITextRegionList openRegions = open.getRegions();
33         int i = openRegions.indexOf(contentAssistRequest.getRegion());
34         if (i < 0)
35             return;
36         ITextRegion nameRegion = null;
37         while (i >= 0) {
38             nameRegion = openRegions.get(i--);
39             if (nameRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME)
40                 break;
41         }
42     
43         String JavaDoc matchString = contentAssistRequest.getMatchString();
44         int offset = contentAssistRequest.getReplacementBeginPosition();
45         if (matchString == null) {
46             matchString = ""; //$NON-NLS-1$
47
}
48         if (matchString.length() > 0 && (matchString.startsWith("\"") || matchString.startsWith("'"))) {//$NON-NLS-2$//$NON-NLS-1$
49
matchString = matchString.substring(1);
50             offset = offset+1;
51         }
52     
53         if (nameRegion != null) {
54             String JavaDoc attributeName = open.getText(nameRegion);
55             
56             List JavaDoc attributeValueProposals = getAttributeValueProposals(attributeName, matchString, offset, contentAssistRequest);
57             if(attributeValueProposals!=null) {
58                 for (Iterator JavaDoc iter = attributeValueProposals.iterator(); iter.hasNext();) {
59                     ICompletionProposal element = (ICompletionProposal) iter.next();
60                     contentAssistRequest.addProposal(element);
61                 }
62             }
63             
64         }
65         
66     }
67
68     abstract protected List JavaDoc getAttributeValueProposals(String JavaDoc attributeName, String JavaDoc matchString, int offset, ContentAssistRequest contentAssistRequest);
69         
70     public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
71         return super.computeCompletionProposals(viewer, offset);
72         
73     }
74     
75     protected IJavaProject getJavaProject() {
76         return project;
77     }
78 }
79
Popular Tags