KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > CompletionEngine


1 /*
2   Copyright (C) 2003 Renaud Pawlak <renaud@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.aspects.gui.swing;
20
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Vector JavaDoc;
25 import org.apache.log4j.Logger;
26
27 /**
28  * This class must be subclassed to implement specific completion
29  * engines for code editors.
30  */

31 public abstract class CompletionEngine {
32     static Logger logger = Logger.getLogger("completion");
33
34     public static final int BACKWARD=-1;
35     public static final int FORWARD=1;
36
37     protected List baseWords = new Vector JavaDoc();
38
39    /**
40     * Returns the list of the words that are potential completions for
41     * a given context. Implement this methods for specific language
42     * editors.
43     *
44     * @param text the editor's full text
45     * @param position the cursor position
46     * @param writtenText the already written text
47     * @return a list of strings which <b>must not contain duplicates</b>
48     */

49     public abstract List getContextualChoices(String JavaDoc text, int position,
50                                               String JavaDoc writtenText);
51    
52     /**
53     * Returns a proposal from a current text's state.
54     *
55     * @param text the editor's full text
56     * @param position the cursor position
57     * @param writtenText the already written text of the
58     * completionable word if any
59     * @param currentProposal the proposal that is currently made to
60     * the user ("" if none)
61     * @param direction BACKWARD || FORWARD
62     * @return the proposed completion, starting with writtenText
63     */

64     public String JavaDoc getProposal(String JavaDoc text,
65                               int position,
66                               String JavaDoc writtenText,
67                               String JavaDoc currentProposal,
68                               int direction) {
69         logger.info("getProposal("+position+","+
70                      ","+writtenText+","+currentProposal+","+direction+")");
71         String JavaDoc ret = "";
72         List words = getContextualChoices(text,position,writtenText);
73         logger.debug("Proposals = "+words);
74         if (words.size()==0)
75             return ret;
76         if (currentProposal.equals("") && writtenText.equals("")) {
77             ret = (String JavaDoc)words.get(0);
78         } else {
79             // Finds the position of currentProposal in words
80
// We cannot use List.indexOf() because we want case insensitiveness
81
int i = -1;
82             int pos = 0;
83             Iterator JavaDoc it = words.iterator();
84             while (it.hasNext() && i==-1) {
85                 if (currentProposal.compareToIgnoreCase((String JavaDoc)it.next())==0)
86                     i = pos;
87                 pos++;
88             }
89             if (i!=-1 || currentProposal.equals("")) {
90                 if (writtenText.equals("")) {
91                     ret = (String JavaDoc) words.get(next(words,i,direction));
92                 } else {
93                     int count = 0;
94                     logger.debug("before search: "+i);
95                     while(count <= words.size() &&
96                           !((String JavaDoc)words.get(i=next(words,i,direction))).toLowerCase()
97                           .startsWith(writtenText.toLowerCase())) {
98                         count++;
99                     }
100                     logger.debug("after search: "+i);
101                     if (((String JavaDoc)words.get(i)).toLowerCase()
102                         .startsWith(writtenText.toLowerCase())) {
103                         ret = ((String JavaDoc)words.get(i));//.substring(writtenText.length());
104
}
105                 }
106             }
107         }
108         return ret;
109     }
110
111     public abstract void runAutomaticCompletion(SHEditor editor,
112                                                 String JavaDoc text,
113                                                 int position,
114                                                 char c);
115    
116     public abstract boolean isAutomaticCompletionChar(char c);
117
118     int next(List l,int i, int direction) {
119         if(direction==FORWARD) {
120             if(i==l.size()-1)
121                 return 0;
122             else
123                 return i+1;
124         } else if (direction==BACKWARD) {
125             if(i==0)
126                 return l.size()-1;
127             else
128                 return i-1;
129         } else {
130             return i;
131         }
132     }
133
134     public List getBaseWords() {
135         return baseWords;
136     }
137
138     public void addBaseWord(String JavaDoc baseWord) {
139         if (!baseWords.contains(baseWord)) {
140             baseWords.add(baseWord);
141         }
142     }
143
144     public void addBaseWords(Collection JavaDoc baseWords) {
145         logger.debug("addBaseWords "+baseWords);
146         Iterator JavaDoc it = baseWords.iterator();
147         while (it.hasNext()) {
148             Object JavaDoc cur = it.next();
149             if (!this.baseWords.contains(cur)) {
150                 this.baseWords.add(cur);
151             }
152         }
153     }
154
155     public void clearBaseWords() {
156         baseWords.clear();
157     }
158
159 }
160
Popular Tags