1 18 19 package org.objectweb.jac.aspects.gui.swing; 20 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Vector ; 25 import org.apache.log4j.Logger; 26 27 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 (); 38 39 49 public abstract List getContextualChoices(String text, int position, 50 String writtenText); 51 52 64 public String getProposal(String text, 65 int position, 66 String writtenText, 67 String currentProposal, 68 int direction) { 69 logger.info("getProposal("+position+","+ 70 ","+writtenText+","+currentProposal+","+direction+")"); 71 String 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 )words.get(0); 78 } else { 79 int i = -1; 82 int pos = 0; 83 Iterator it = words.iterator(); 84 while (it.hasNext() && i==-1) { 85 if (currentProposal.compareToIgnoreCase((String )it.next())==0) 86 i = pos; 87 pos++; 88 } 89 if (i!=-1 || currentProposal.equals("")) { 90 if (writtenText.equals("")) { 91 ret = (String ) 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 )words.get(i=next(words,i,direction))).toLowerCase() 97 .startsWith(writtenText.toLowerCase())) { 98 count++; 99 } 100 logger.debug("after search: "+i); 101 if (((String )words.get(i)).toLowerCase() 102 .startsWith(writtenText.toLowerCase())) { 103 ret = ((String )words.get(i)); } 105 } 106 } 107 } 108 return ret; 109 } 110 111 public abstract void runAutomaticCompletion(SHEditor editor, 112 String 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 baseWord) { 139 if (!baseWords.contains(baseWord)) { 140 baseWords.add(baseWord); 141 } 142 } 143 144 public void addBaseWords(Collection baseWords) { 145 logger.debug("addBaseWords "+baseWords); 146 Iterator it = baseWords.iterator(); 147 while (it.hasNext()) { 148 Object 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 |