KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > ide > swing > AccCompletionEngine


1 /*
2   Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.ide.swing;
20
21 import java.io.StringReader JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Vector JavaDoc;
26 import org.apache.log4j.Logger;
27 import org.objectweb.jac.aspects.gui.swing.DefaultCompletionEngine;
28 import org.objectweb.jac.core.AspectComponent;
29 import org.objectweb.jac.core.parsers.acc.NonTerminal;
30 import org.objectweb.jac.core.parsers.acc.SyntaxElement;
31 import org.objectweb.jac.core.parsers.acc.Terminal;
32 import org.objectweb.jac.core.parsers.acc.ToolParserWrapper;
33 import org.objectweb.jac.core.rtti.AbstractMethodItem;
34 import org.objectweb.jac.core.rtti.ClassItem;
35 import org.objectweb.jac.core.rtti.FieldItem;
36 import org.objectweb.jac.core.rtti.MemberItem;
37 import org.objectweb.jac.ide.Class;
38 import org.objectweb.jac.ide.Field;
39 import org.objectweb.jac.ide.Method;
40 import org.objectweb.jac.ide.Package;
41 import org.objectweb.jac.ide.Project;
42 import org.objectweb.jac.ide.RelationRole;
43
44 /**
45  * This class implements a completion engine for method acc
46  * configuration code of the UMLAF IDE.
47  */

48 public class AccCompletionEngine extends DefaultCompletionEngine {
49     static final Logger logger = Logger.getLogger("completion");
50
51     Project project;
52     /**
53      * Creates a new AccCompletionEngine using a given parser.
54      * @param parser the parser to use
55      */

56     public AccCompletionEngine(ToolParserWrapper parser, Project project) {
57         this.project = project;
58         this.parser = parser;
59     }
60
61     SyntaxElement currentSyntaxElement;
62     public SyntaxElement getCurrentSyntaxElement() {
63         return currentSyntaxElement;
64     }
65
66     AspectComponent aspectInstance;
67     /**
68      * Sets the aspect instance associated with the completion engine
69      * @param instance the AspectComponent instance
70      */

71     public void setAspectInstance(AspectComponent instance) {
72         this.aspectInstance = instance;
73     }
74
75     ToolParserWrapper parser;
76     public List JavaDoc getContextualChoices(String JavaDoc text, int position,
77                                      String JavaDoc writtenText) {
78         NonTerminal elements = parser.parse(new StringReader JavaDoc(text),"");
79         if (writtenText.length()>0)
80             position = position+writtenText.length()-1;
81         logger.debug("Syntax elements = "+elements);
82         logger.debug("position = "+position);
83         logger.debug("writtenText = "+writtenText);
84
85         SyntaxElement se = parser.getSyntaxElementAt(position);
86         currentSyntaxElement = se;
87         logger.debug("Syntax element = "+se);
88         if (se!=null && (se.getName().equals("CONF_METHOD") ||
89                          se.getName().equals("EOL") ||
90                          se.getName().equals("class_block"))) {
91             logger.debug("Completing!");
92             NonTerminal block = (NonTerminal)se.findParent("class_block");
93             if (block!=null) {
94                 Terminal keyword = (Terminal)block.getChild("BLOCK_KEYWORD");
95                 if (keyword!=null) {
96                     // Completion inside a block
97
if (keyword.getValue().equals("attribute")) {
98                         logger.debug(" Completing in attribute block");
99                         return aspectInstance.getConfigurationMethodsName(
100                             FieldItem.class);
101                     } else if (keyword.getValue().equals("method")) {
102                         logger.debug(" Completing in method block");
103                         return aspectInstance.getConfigurationMethodsName(
104                             AbstractMethodItem.class);
105                     } else if (keyword.getValue().equals("member")) {
106                         logger.debug(" Completing in member block");
107                         return aspectInstance.getConfigurationMethodsName(
108                             MemberItem.class);
109                     } else if (keyword.getValue().equals("class")) {
110                         logger.debug(" Completing in class block");
111                         Vector JavaDoc result = new Vector JavaDoc();
112                         result.addAll(
113                             aspectInstance.getConfigurationMethodsName(ClassItem.class));
114                         result.addAll(
115                             aspectInstance.getConfigurationMethodsName(MemberItem.class));
116                         result.addAll(Arrays.asList(
117                             new String JavaDoc[] {"attribute","method","member"}));
118                         return result;
119                     } else {
120                         logger.debug(" Unknown block keyword");
121                         return baseWords;
122                     }
123                 } else {
124                     logger.debug(" No keyword in block");
125                     return baseWords;
126                 }
127             } else {
128                 logger.debug(" No block keyword");
129                 return baseWords;
130             }
131         } else {
132             Terminal term = parser.getTerminalAt(position);
133             logger.debug(" SyntaxElement before: "+term);
134             if (term!=null && term.getName().equals("BLOCK_PARAM")) {
135                 return completeBlockParam(term);
136             } else if (se==null) {
137                 logger.debug(" No current syntax element");
138                 return baseWords;
139             } else {
140                 NonTerminal confMethod = (NonTerminal)se.findParent("conf_method");
141                 if (confMethod!=null) {
142                     String JavaDoc methodName = confMethod.getChild(0).getName();
143                     logger.debug(" completing arg of method "+methodName);
144                     String JavaDoc className = getClassName(confMethod);
145                     if (className!=null) {
146                         logger.debug(" className = "+className);
147                     }
148                 }
149             }
150         }
151         return new Vector JavaDoc();
152     }
153
154     protected List JavaDoc completeConfMethodParam() {
155         return null;
156     }
157
158     /**
159      * Gets completion for a block parameter
160      * @param term the Terminal syntax element to complete
161      */

162     protected List JavaDoc completeBlockParam(Terminal term) {
163         logger.debug("complete block param "+term);
164         Vector JavaDoc result = new Vector JavaDoc();
165         NonTerminal block = (NonTerminal)term.findParent("class_block");
166         if (block!=null) {
167             Terminal keyword = (Terminal)block.getChild("BLOCK_KEYWORD");
168             if (keyword!=null) {
169                 if (keyword.getValue().equals("attribute")) {
170                     String JavaDoc className = getClassName(block);
171                     if (className!=null) {
172                         logger.debug("completing attribute for class "+className);
173                         Class JavaDoc cl = project.findClass(className);
174                         if (cl!=null) {
175                             completeAttributeName(term.getValue(),cl,result);
176                         }
177                     }
178                 } else if (keyword.getValue().equals("method")) {
179                     String JavaDoc className = getClassName(block);
180                     if (className!=null) {
181                         logger.debug("completing method for class "+className);
182                         Class JavaDoc cl = project.findClass(className);
183                         if (cl!=null) {
184                             completeMethodName(term.getValue(),cl,result);
185                         }
186                     }
187                 } else if (keyword.getValue().equals("member")) {
188                     String JavaDoc className = getClassName(block);
189                     if (className!=null) {
190                         logger.debug("completing member for class "+className);
191                         Class JavaDoc cl = project.findClass(className);
192                         if (cl!=null) {
193                             completeAttributeName(term.getValue(),cl,result);
194                             completeMethodName(term.getValue(),cl,result);
195                         } else {
196                             logger.warn("No such class in project: "+className);
197                         }
198                     }
199                 } else if (keyword.getValue().equals("class")) {
200                     completeClassName(term.getValue(),result);
201                 } else {
202                     logger.debug("BLOCK_KEYWORD is "+keyword.getValue());
203                 }
204             } else {
205                 logger.debug("No child BLOCK_KEYWORD "+term);
206             }
207         } else {
208             logger.debug("No parent class_block "+term);
209         }
210
211         return result;
212     }
213
214     /**
215      * Gets the className for a member,method or attribute block
216      * @param block Non terminal of the block keyword
217      * @return the class name, or null if it cannot be computed
218      */

219     protected String JavaDoc getClassName(NonTerminal block) {
220         String JavaDoc className = null;
221         block = (NonTerminal)block.getParent().findParent("class_block");
222         if (block!=null) {
223             Terminal keyword = (Terminal)block.getChild("BLOCK_KEYWORD");
224             if (keyword!=null && keyword.getValue().equals("class")) {
225                 NonTerminal blockParams =
226                     (NonTerminal)block.getChild("block_params");
227                 if (blockParams!=null) {
228                     Terminal param = (Terminal)blockParams.getChild(0);
229                     className = param.getValue();
230                 }
231             }
232         }
233         return className;
234     }
235
236     protected String JavaDoc getMemberName(NonTerminal block) {
237         String JavaDoc memberName = null;
238         block = (NonTerminal)block.getParent().findParent("class_block");
239         if (block!=null) {
240             Terminal keyword = (Terminal)block.getChild("BLOCK_KEYWORD");
241             if (keyword!=null &&
242                 (keyword.getValue().equals("method") ||
243                  keyword.getValue().equals("attribute"))) {
244                 NonTerminal blockParams =
245                     (NonTerminal)block.getChild("block_params");
246                 if (blockParams!=null) {
247                     Terminal param = (Terminal)blockParams.getChild(0);
248                     memberName = param.getValue();
249                 }
250             }
251         }
252         return memberName;
253     }
254
255     protected void completeAttributeName(String JavaDoc start, Class JavaDoc cl, List JavaDoc result) {
256         Iterator JavaDoc it = cl.getAllFields().iterator();
257         while(it.hasNext()) {
258             Field field = (Field)it.next();
259             if (field.getName().startsWith(start)) {
260                 result.add(field.getGenerationName());
261             }
262         }
263         it = cl.getAllNavigableRoles().iterator();
264         while(it.hasNext()) {
265             RelationRole role = (RelationRole)it.next();
266             if (role.getGenerationName().startsWith(start)) {
267                 result.add(role.getGenerationName());
268             }
269         }
270     }
271
272     protected void completeMethodName(String JavaDoc start, Class JavaDoc cl, List JavaDoc result) {
273         Iterator JavaDoc it = cl.getMethods().iterator();
274         while(it.hasNext()) {
275             Method method = (Method)it.next();
276             if (method.getName().startsWith(start)) {
277                 result.add(method.getGenerationName());
278             }
279         }
280     }
281
282     protected void completeClassName(String JavaDoc start, List JavaDoc result) {
283         logger.debug("completing class name "+start);
284         int dot = start.lastIndexOf('.');
285         if (dot==-1) {
286             Iterator JavaDoc it = project.getPackages().iterator();
287             while (it.hasNext()) {
288                 Package JavaDoc pkg = (Package JavaDoc)it.next();
289                 if (pkg.getGenerationName().startsWith(start)) {
290                     result.add(pkg.getPPath());
291                 }
292             }
293          
294         } else {
295             String JavaDoc end = start.substring(dot+1);
296             Package JavaDoc pkg = project.findPackage(start.substring(0,dot));
297
298             if (pkg!=null) {
299                 logger.debug("Looking for "+end+" in package "+pkg.getPPath());
300                 Iterator JavaDoc it = pkg.getClasses().iterator();
301                 while (it.hasNext()) {
302                     Class JavaDoc cl = (Class JavaDoc)it.next();
303                     if (cl.getGenerationName().startsWith(end)) {
304                         logger.debug("Found class "+cl.getFullName());
305                         result.add(cl.getGenerationFullName());
306                     }
307                 }
308                 it = pkg.getSubPackages().iterator();
309                 while (it.hasNext()) {
310                     Package JavaDoc subPkg = (Package JavaDoc)it.next();
311                     if (subPkg.getGenerationName().startsWith(end)) {
312                         logger.debug("Found package "+subPkg.getPPath());
313                         result.add(subPkg.getPPath());
314                     }
315                 }
316             }
317         }
318     }
319 }
320
Popular Tags