1 30 package com.genimen.djeneric.ui; 31 32 import java.awt.Point ; 33 import java.awt.event.InputEvent ; 34 import java.awt.event.KeyEvent ; 35 import java.awt.event.KeyListener ; 36 import java.util.ArrayList ; 37 import java.util.Collections ; 38 import java.util.HashMap ; 39 import java.util.Iterator ; 40 41 import javax.swing.text.JTextComponent ; 42 43 import com.genimen.djeneric.repository.DjExtent; 44 import com.genimen.djeneric.repository.DjRelation; 45 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 46 import com.genimen.djeneric.util.DjStringComparator; 47 48 public class DjPropertyPathCompleter implements KeyListener 49 { 50 JTextComponent _editor; 51 DjExtent _rootExtent; 52 ArrayList _allListeners = new ArrayList (); 53 ArrayList _parameterNames = new ArrayList (); 54 boolean _includeOne2Many = false; 55 HashMap _parameters = new HashMap (); 56 57 public DjPropertyPathCompleter() 58 { 59 } 60 61 public DjPropertyPathCompleter(DjExtent rootExtent, JTextComponent editor) 62 { 63 setEditor(editor); 64 _rootExtent = rootExtent; 65 } 66 67 public void setIncludeOne2Many(boolean b) 68 { 69 _includeOne2Many = b; 70 } 71 72 public void showPopup() throws ObjectNotDefinedException 73 { 74 DjEditorPositionInfo wi = new DjEditorPositionInfo(_editor, true); 75 76 HashMap possibles = getCompletionMap(wi); 77 doShow(possibles, wi); 78 } 79 80 public ArrayList getCompletions() throws ObjectNotDefinedException 81 { 82 DjEditorPositionInfo wi = new DjEditorPositionInfo(_editor, true); 83 ArrayList lst = new ArrayList (); 84 Iterator it = getCompletionMap(wi).keySet().iterator(); 85 while (it.hasNext()) 86 lst.add(it.next()); 87 Collections.sort(lst, new DjStringComparator(false)); 88 return lst; 89 } 90 91 protected HashMap getCompletionMap(DjEditorPositionInfo wi) throws ObjectNotDefinedException 92 { 93 String propStart = wi.getWord(); 94 95 DjExtent extent = _rootExtent; 96 HashMap possibles = new HashMap (); 97 98 completeParameters(wi, possibles); 99 completeProperties(wi, possibles); 100 return possibles; 101 } 102 103 private void completeProperties(DjEditorPositionInfo wi, HashMap possibles) throws ObjectNotDefinedException 104 { 105 String propStart = wi.getWord(); 106 107 DjExtent extent = _rootExtent; 108 int dotIdx = propStart.indexOf("."); 109 if (dotIdx != -1) 110 { 111 dotIdx = propStart.lastIndexOf("."); 112 113 String leadingPath = propStart.substring(0, dotIdx); 114 wi.setStart(wi.getStart() + leadingPath.length() + 1); 115 116 propStart = propStart.substring(dotIdx + 1); 117 118 extent = extent.resolveType(leadingPath); 119 } 120 121 for (int i = 0; i < extent.getPropertyCount(); i++) 122 { 123 if (extent.getProperty(i).getName().toLowerCase().startsWith(propStart.toLowerCase())) 124 { 125 possibles.put(extent.getProperty(i).getName(), extent.getProperty(i).getName()); 126 } 127 } 128 for (int i = 0; i < extent.getDetailRelationCount(); i++) 129 { 130 DjRelation rel = extent.getDetailRelation(i); 131 132 if ((rel.isOneToOne() || _includeOne2Many) && rel.getName().toLowerCase().startsWith(propStart.toLowerCase())) 133 { 134 possibles.put(rel.getName(), rel.getName()); 135 } 136 } 137 } 138 139 private void completeParameters(DjEditorPositionInfo wi, HashMap possibles) throws ObjectNotDefinedException 140 { 141 String propStart = wi.getWord(); 142 DjExtent extent; 143 for (int i = 0; i < _parameterNames.size(); i++) 144 { 145 String name = ":" + _parameterNames.get(i); 146 if (name.toLowerCase().startsWith(propStart.toLowerCase())) 147 { 148 possibles.put(name, name); 149 } 150 } 151 } 152 153 private void doShow(HashMap possibles, DjEditorPositionInfo wi) 154 { 155 if (possibles.size() > 0) 156 { 157 DjCodeCompleter sel = new DjCodeCompleter(possibles, getEditor(), wi); 158 Point p = getEditor().getCaret().getMagicCaretPosition(); 159 int x = 0; 160 int y = 0; 161 if (p != null) 162 { 163 x = p.x; 164 y = p.y; 165 } 166 167 sel.show(getEditor(), x, y); 168 } 169 } 170 171 public JTextComponent getEditor() 172 { 173 return _editor; 174 } 175 176 public void setEditor(JTextComponent editor) 177 { 178 if (_editor != null && _editor != editor) _editor.removeKeyListener(this); 179 _editor = editor; 180 _editor.addKeyListener(this); 181 } 182 183 public void keyPressed(KeyEvent e) 184 { 185 if ((e.getKeyCode() == KeyEvent.VK_SPACE) && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0)) 186 { 187 try 188 { 189 showPopup(); 190 setStatusMessage("", true); 191 } 192 catch (ObjectNotDefinedException onde) 193 { 194 setStatusMessage(onde.getMessage(), false); 195 } 196 } 197 } 198 199 public void keyReleased(KeyEvent e) 200 { 201 } 202 203 public void keyTyped(KeyEvent e) 204 { 205 } 206 207 public void setStatusMessage(String msg, boolean isInformative) 208 { 209 for (int i = 0; i < _allListeners.size(); i++) 210 { 211 DjStatusListener lsnr = (DjStatusListener) _allListeners.get(i); 212 lsnr.setStatusMessage(msg, isInformative); 213 } 214 } 215 216 public void addStatusListener(DjStatusListener lsnr) 217 { 218 _allListeners.add(lsnr); 219 } 220 221 public void removeStatusListener(DjStatusListener lsnr) 222 { 223 _allListeners.remove(lsnr); 224 } 225 226 public DjExtent getRootExtent() 227 { 228 return _rootExtent; 229 } 230 231 public void setRootExtent(DjExtent rootExtent) 232 { 233 _rootExtent = rootExtent; 234 } 235 236 public void addParameter(String paramName, DjExtent type) 237 { 238 _parameters.put(paramName, type); 239 _parameterNames.add(paramName); 240 } 241 242 public void setParameters(HashMap parameters) 243 { 244 _parameters = parameters; 245 Iterator it = _parameters.keySet().iterator(); 246 while (it.hasNext()) 247 _parameterNames.add(it.next()); 248 } 249 } | Popular Tags |