KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > NNCompletionQuery


1 ///*
2
// * The contents of this file are subject to the terms of the Common Development
3
// * and Distribution License (the License). You may not use this file except in
4
// * compliance with the License.
5
// *
6
// * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7
// * or http://www.netbeans.org/cddl.txt.
8
// *
9
// * When distributing Covered Code, include this CDDL Header Notice in each file
10
// * and include the License file at http://www.netbeans.org/cddl.txt.
11
// * If applicable, add the following below the CDDL Header, with the fields
12
// * enclosed by brackets [] replaced by your own identifying information:
13
// * "Portions Copyrighted [year] [name of copyright owner]"
14
// *
15
// * The Original Software is NetBeans. The Initial Developer of the Original
16
// * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17
// * Microsystems, Inc. All Rights Reserved.
18
// */
19
//
20
//package org.netbeans.modules.j2ee.persistence.editor.completion;
21
//
22
//import java.io.IOException;
23
//import java.util.ArrayList;
24
//import java.util.Iterator;
25
//import java.util.List;
26
//import javax.swing.text.BadLocationException;
27
//import javax.swing.text.JTextComponent;
28
//import org.netbeans.editor.TokenItem;
29
//import org.netbeans.editor.ext.CompletionQuery;
30
//import org.netbeans.editor.ext.Completion;
31
//import org.netbeans.editor.ext.ExtEditorUI;
32
//import org.netbeans.editor.BaseDocument;
33
//import org.netbeans.editor.Utilities;
34
//import org.netbeans.editor.ext.java.JCExpression;
35
//import org.netbeans.editor.ext.java.JCFinder;
36
//import org.netbeans.editor.ext.java.JavaCompletionQuery;
37
//import org.netbeans.editor.ext.java.JavaSyntaxSupport;
38
//import org.netbeans.editor.ext.java.JavaTokenContext;
39
//import org.netbeans.jmi.javamodel.*;
40
//import org.netbeans.modules.editor.NbEditorUtilities;
41
//import org.netbeans.modules.editor.java.JCFinderFactory;
42
//import org.netbeans.modules.editor.java.NbJavaJMISyntaxSupport;
43
//import org.netbeans.modules.j2ee.persistence.dd.PersistenceUtils;
44
//import org.netbeans.modules.j2ee.persistence.dd.orm.model_1_0.EntityMappings;
45
//import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
46
//import org.netbeans.modules.j2ee.persistence.editor.completion.db.DBCompletionContextResolver;
47
//import org.netbeans.modules.javacore.JMManager;
48
//import org.openide.ErrorManager;
49
//import org.openide.filesystems.FileObject;
50
//import org.openide.loaders.DataObject;
51
//
52
///**
53
// * Completion query for Java EE 5 aNNotations.
54
// *
55
// * @author Dusan Balek, Andrei Badea, Marek Fukala
56
// */
57
//public class NNCompletionQuery extends JavaCompletionQuery {
58
//
59
// private List resolvers;
60
//
61
// public NNCompletionQuery(boolean isJava15) {
62
// setJava15(isJava15);
63
// initResolvers();
64
// }
65
//
66
// private void initResolvers() {
67
// //XXX temporary - should be registered somehow better
68
// resolvers = new ArrayList();
69
// resolvers.add(new DBCompletionContextResolver());
70
// }
71
//
72
// protected JCFinder getFinder() {
73
// FileObject fo = getFileObject();
74
// return JCFinderFactory.getDefault().getFinder(fo);
75
// }
76
//
77
// protected CompletionQuery.Result getResult(JTextComponent component, JavaSyntaxSupport sup, boolean openingSource, int offset, JCExpression exp) {
78
// Completion completion = ((ExtEditorUI)Utilities.getEditorUI(component)).getCompletion();
79
// boolean autoPopup = completion != null ? completion.provokedByAutoPopup : false;
80
// ArrayList results = new ArrayList();
81
//
82
// //query all available CompletionContextResolver-s
83
// Iterator resolversItr = resolvers.iterator();
84
// while(resolversItr.hasNext()) {
85
// CompletionContextResolver resolver = (CompletionContextResolver)resolversItr.next();
86
// Context ctx = new Context(component, (NbJavaJMISyntaxSupport)sup.get(NbJavaJMISyntaxSupport.class), openingSource, offset, autoPopup);
87
// if(ctx.getEntityMappings() == null) {
88
// ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "No EnitityMappings defined.");
89
// break;
90
// } else {
91
// results.addAll(resolver.resolve(exp, ctx));
92
// }
93
// }
94
// return new CompletionQuery.DefaultResult(component, "*", results, exp.getTokenCount() == 0 ? offset : exp.getTokenOffset(0), 0); // NOI18N
95
// }
96
//
97
// private FileObject getFileObject() {
98
// BaseDocument bDoc = getBaseDocument();
99
// DataObject dobj = NbEditorUtilities.getDataObject(bDoc);
100
// return dobj.getPrimaryFile();
101
// }
102
//
103
// public final class Context {
104
//
105
// /** Text component */
106
// private JTextComponent component;
107
//
108
// /** Syntax support for the given document */
109
// private NbJavaJMISyntaxSupport sup;
110
//
111
// /** Whether the query is performed to open the source file. It has slightly
112
// * different handling in some situations.
113
// */
114
// private boolean openingSource;
115
//
116
// /** End position of the scanning - usually the caret position */
117
// private int endOffset;
118
//
119
// private JavaClass curCls;
120
//
121
// private PersistenceUnit[] pus;
122
// private EntityMappings emaps;
123
//
124
// /** True when code completion is invoked by auto popup. In such case, code completion returns no result
125
// * after "new ". To get a result, code completion has to be invoked manually (using Ctrl-Space). */ // NOI18N
126
// private boolean autoPopup;
127
//
128
// private String completedMemberName, completedMemberJavaClassName;
129
//
130
// private NNParser nnparser;
131
// private NNParser.NN parsednn = null;
132
//
133
// public Context(JTextComponent component, NbJavaJMISyntaxSupport sup, boolean openingSource, int endOffset, boolean autoPopup) {
134
// this.component = component;
135
// this.sup = sup;
136
// this.openingSource = openingSource;
137
// this.endOffset = endOffset;
138
// this.autoPopup = autoPopup;
139
//
140
// FileObject documentFO = getFileObject();
141
// if(documentFO != null) {
142
// try {
143
// this.pus = PersistenceUtils.getPersistenceUnits(documentFO);
144
// } catch (IOException e) {
145
// ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
146
// }
147
// this.emaps = PersistenceUtils.getEntityMappings(documentFO);
148
// }
149
//
150
// this.nnparser = new NNParser(getBaseDocument());
151
// }
152
//
153
// /** Must be run under MDR transaction! */
154
// public JavaClass getJavaClass() {
155
// assert Thread.currentThread() == JMManager.getTransactionMutex().getThread();
156
//
157
// curCls = sup.getJavaClass(endOffset);
158
// if (curCls == null) {
159
// curCls = sup.getTopJavaClass();
160
// }
161
// return curCls;
162
// }
163
//
164
// public BaseDocument getBaseDocument() {
165
// return sup.getDocument();
166
// }
167
//
168
// public FileObject getFileObject() {
169
// return NbEditorUtilities.getFileObject(getBaseDocument());
170
// }
171
//
172
// /** @return an arrat of PUs which this sourcefile belongs to. */
173
// public PersistenceUnit[] getPersistenceUnits() {
174
// return this.pus;
175
// }
176
//
177
// public EntityMappings getEntityMappings() {
178
// return this.emaps;
179
// }
180
//
181
// public int getCompletionOffset() {
182
// return endOffset;
183
// }
184
//
185
// public NbJavaJMISyntaxSupport getSyntaxSupport() {
186
// return sup;
187
// }
188
//
189
// public NNParser.NN getParsedAnnotation() {
190
// synchronized (nnparser) {
191
// if(parsednn == null) {
192
// parsednn = nnparser.parseAnnotation(getCompletionOffset());
193
// }
194
// return parsednn;
195
// }
196
// }
197
//
198
// public String getCompletedMemberClassName() {
199
// if(completedMemberJavaClassName == null) initCompletedMemberContext();
200
// return completedMemberJavaClassName;
201
// }
202
//
203
// public String getCompletedMemberName() {
204
// if(completedMemberName== null) initCompletedMemberContext();
205
// return completedMemberName;
206
// }
207
//
208
//
209
// private void initCompletedMemberContext() {
210
// //parse the text behind the cursor and try to find identifiers.
211
// //it seems to be impossible to use JMI model for this since it havily
212
// //relies on the state of the source (whether it contains errors, which types etc.)
213
// String type = null;
214
// String genericType = null;
215
// String propertyName = null;
216
// NNParser nnp = new NNParser(getBaseDocument()); //helper parser
217
// try {
218
// TokenItem ti = sup.getTokenChain(getCompletionOffset(), getCompletionOffset() + 1);
219
// while(ti != null && propertyName == null) {
220
//
221
// //skip all annotations between the CC offset and the completed member
222
// if(ti.getTokenID() == JavaTokenContext.ANNOTATION) {
223
// //parse to find NN end
224
// NNParser.NN parsed = nnp.parseAnnotation(ti.getOffset() + 1);
225
// if(parsed != null) {
226
// //parse after the NN end (skip)
227
// ti = sup.getTokenChain(parsed.getEndOffset(), parsed.getEndOffset() + 1);
228
// continue;
229
// }
230
// }
231
//
232
// //test whether we have just found a type and '<' character after
233
// if(genericType != null && ti.getTokenID() == JavaTokenContext.LT) {
234
// //maybe a start of generic
235
// TokenItem ti2 = ti.getNext();
236
// if(ti2.getTokenID() == JavaTokenContext.IDENTIFIER) {
237
// //found generic
238
// genericType = ti2.getImage();
239
// ti = ti.getNext(); //skip the next IDENTIFIER token so it is not considered as property name
240
// } else {
241
// //false alarm
242
// genericType = null;
243
// }
244
// } else if(ti.getTokenID() == JavaTokenContext.IDENTIFIER) {
245
// if(type == null) {
246
// type = ti.getImage();
247
// genericType = type;
248
// } else {
249
// propertyName = ti.getImage();
250
// }
251
// }
252
// ti = ti.getNext();
253
// }
254
// } catch (BadLocationException ex) {
255
// ex.printStackTrace();
256
// }
257
//
258
// completedMemberName = propertyName;
259
// completedMemberJavaClassName = genericType == null ? type : genericType;
260
// }
261
//
262
// }
263
//}
264
Popular Tags