1 19 20 package org.netbeans.editor; 21 22 import java.awt.event.ActionEvent ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import javax.swing.Action ; 29 import javax.swing.text.BadLocationException ; 30 import javax.swing.text.Document ; 31 import javax.swing.text.JTextComponent ; 32 import javax.swing.text.Caret ; 33 import org.netbeans.lib.editor.util.CharSequenceUtilities; 34 import org.netbeans.lib.editor.util.swing.DocumentUtilities; 35 36 42 43 public class Abbrev implements SettingsChangeListener, PropertyChangeListener { 44 45 52 public static boolean isAbbrevDisabled(JTextComponent component) { 53 Document doc = component.getDocument(); 54 if (doc instanceof BaseDocument) { 55 BaseDocument bdoc = (BaseDocument)doc; 56 SyntaxSupport sup = bdoc.getSyntaxSupport(); 57 if (sup != null) { 58 return sup.isAbbrevDisabled(component.getCaretPosition()); 59 } 60 } 61 return false; } 63 64 67 private StringBuffer abbrevSB = new StringBuffer (); 68 69 72 private boolean checkDocText; 73 74 78 private boolean checkTextDelimiter; 79 80 81 protected EditorUI editorUI; 82 83 84 private Acceptor doExpandAcceptor; 85 86 87 private Acceptor addTypedAcceptor; 88 89 90 private Acceptor resetAcceptor; 91 92 93 private HashMap abbrevMap; 94 95 public Abbrev(EditorUI editorUI, boolean checkDocText, boolean checkTextDelimiter) { 96 this.editorUI = editorUI; 97 this.checkDocText = checkDocText; 98 this.checkTextDelimiter = checkTextDelimiter; 99 100 Settings.addSettingsChangeListener(this); 101 102 synchronized (editorUI.getComponentLock()) { 103 JTextComponent component = editorUI.getComponent(); 105 if (component != null) { 106 propertyChange(new PropertyChangeEvent (editorUI, 107 EditorUI.COMPONENT_PROPERTY, null, component)); 108 } 109 110 editorUI.addPropertyChangeListener(this); 111 } 112 } 113 114 117 public void settingsChange(SettingsChangeEvent evt) { 118 Class kitClass = Utilities.getKitClass(editorUI.getComponent()); 119 120 if (kitClass != null) { 121 String settingName = (evt != null) ? evt.getSettingName() : null; 122 123 if (settingName == null || SettingsNames.ABBREV_ACTION_MAP.equals(settingName) 124 || SettingsNames.ABBREV_MAP.equals(settingName) 125 ) { 126 abbrevMap = new HashMap (); 127 Map m = (Map )Settings.getValue(kitClass, SettingsNames.ABBREV_ACTION_MAP); 129 if (m != null) { 130 BaseKit kit = Utilities.getKit(editorUI.getComponent()); 131 Iterator iter = m.entrySet().iterator(); 132 while (iter.hasNext()) { 133 Map.Entry me = (Map.Entry )iter.next(); 134 Object value = me.getValue(); 135 Action a = null; 136 if (value instanceof String ) { 137 a = kit.getActionByName((String )value); 138 } else if (value instanceof Action ) { 139 a = (Action )value; 140 } 141 142 if (a != null) { 143 abbrevMap.put(me.getKey(), a); 144 } 145 } 146 } 147 148 m = (Map )Settings.getValue(kitClass, SettingsNames.ABBREV_MAP); 149 if (m != null) { 150 Iterator iter = m.entrySet().iterator(); 151 while (iter.hasNext()) { 152 Map.Entry me = (Map.Entry )iter.next(); 153 Object value = me.getValue(); 154 if (value != null) { 155 abbrevMap.put(me.getKey(), value); 156 } 157 } 158 } 159 } 160 161 if (settingName == null || SettingsNames.ABBREV_EXPAND_ACCEPTOR.equals(settingName)) { 162 doExpandAcceptor = SettingsUtil.getAcceptor(kitClass, SettingsNames.ABBREV_EXPAND_ACCEPTOR, AcceptorFactory.FALSE); 163 } 164 if (settingName == null || SettingsNames.ABBREV_ADD_TYPED_CHAR_ACCEPTOR.equals(settingName)) { 165 addTypedAcceptor = SettingsUtil.getAcceptor(kitClass, SettingsNames.ABBREV_ADD_TYPED_CHAR_ACCEPTOR, AcceptorFactory.FALSE); 166 } 167 if (settingName == null || SettingsNames.ABBREV_RESET_ACCEPTOR.equals(settingName)) { 168 resetAcceptor = SettingsUtil.getAcceptor(kitClass, SettingsNames.ABBREV_RESET_ACCEPTOR, AcceptorFactory.TRUE); 169 } 170 } 171 } 172 173 public void propertyChange(PropertyChangeEvent evt) { 174 String propName = evt.getPropertyName(); 175 176 if (EditorUI.COMPONENT_PROPERTY.equals(propName)) { 177 JTextComponent component = (JTextComponent )evt.getNewValue(); 178 if (component != null) { 180 settingsChange(null); 181 182 } else { 185 } 186 187 } 188 } 189 190 191 public void reset() { 192 abbrevSB.setLength(0); 193 } 194 195 196 public void addChar(char ch) { 197 abbrevSB.append(ch); 198 } 199 200 201 public String getAbbrevString() { 202 return abbrevSB.toString(); 203 } 204 205 206 public Map getAbbrevMap() { 207 return abbrevMap; 208 } 209 210 215 public Object translateAbbrev(String abbrev) { 216 String abbStr = (abbrev != null) ? abbrev : abbrevSB.toString(); 217 return getAbbrevMap().get(abbStr); 218 } 219 220 222 public String getExpandString(char typedChar) { 223 return (doExpandAcceptor.accept(typedChar)) ? getExpandString() : null; 224 } 225 226 public String getExpandString() { 227 BaseDocument doc = (BaseDocument)editorUI.getDocument(); 228 String abbrevStr = getAbbrevString(); 229 int abbrevStrLen = abbrevStr.length(); 230 Object expansion = translateAbbrev(abbrevStr); 231 Caret caret = editorUI.getComponent().getCaret(); 232 int dotPos = caret.getDot(); 233 if (abbrevStr != null && expansion != null 234 && dotPos >= abbrevStrLen 235 ) { 236 if (checkDocText) { 237 try { 238 CharSequence prevChars = DocumentUtilities.getText(doc, dotPos - abbrevStrLen, abbrevStrLen); 239 if (CharSequenceUtilities.textEquals(prevChars, abbrevStr)) { if (!checkTextDelimiter || dotPos == abbrevStrLen 241 || resetAcceptor.accept( 242 doc.getChars(dotPos - abbrevStrLen - 1, 1)[0]) 243 ) { 244 return abbrevStr; 245 } 246 } 247 } catch (BadLocationException e) { 248 } 249 } 250 } 251 return null; 252 } 253 254 protected boolean doExpansion(int dotPos, String expandStr, ActionEvent evt) 255 throws BadLocationException { 256 Object expansion = translateAbbrev(expandStr); 257 boolean expanded = false; 258 if (expansion instanceof String ) { BaseDocument doc = editorUI.getDocument(); 260 String ins = (String )expansion; 261 int offset = ins.indexOf('|'); 262 if (offset >= 0) { 263 if (offset > 0) doc.insertString(dotPos, ins.substring(0, offset), null); 264 if (offset+1 < ins.length()) doc.insertString(dotPos + offset, 265 ins.substring(offset + 1), null); 266 Caret caret = editorUI.getComponent().getCaret(); 267 caret.setDot(dotPos + offset); 268 } else { 269 doc.insertString(dotPos, ins, null); 270 } 271 272 if(ins.indexOf("\n") != -1) { Formatter formatter = doc.getFormatter(); 274 formatter.reformat(doc, dotPos, dotPos + ins.length()); 275 } 276 277 expanded = true; 278 } else if (expansion instanceof Action ) { 279 ((Action )expansion).actionPerformed(evt); 280 expanded = true; 281 } 282 return expanded; 283 } 284 285 public boolean expandString(char typedChar, String expandStr, ActionEvent evt) 286 throws BadLocationException { 287 if (expandString(expandStr, evt)) { 288 if (addTypedAcceptor.accept(typedChar)) { 289 int dotPos = editorUI.getComponent().getCaret().getDot(); 290 editorUI.getDocument().insertString(dotPos, String.valueOf(typedChar), null); 291 } 292 return true; 293 } 294 return false; 295 } 296 297 301 public boolean expandString(String expandStr, ActionEvent evt) 302 throws BadLocationException { 303 if (true) { 305 reset(); 306 return true; 307 } 308 309 boolean expanded = false; 310 BaseDocument doc = editorUI.getDocument(); 311 doc.atomicLock(); 312 try { 313 Caret caret = editorUI.getComponent().getCaret(); 314 int pos = caret.getDot() - expandStr.length(); 315 doc.remove(pos, expandStr.length()); 316 expanded = doExpansion(pos, expandStr, evt); 317 } finally { 318 if (expanded) { 319 reset(); 320 } else { 321 doc.breakAtomicLock(); 322 } 323 doc.atomicUnlock(); 324 } 325 return expanded; 326 } 327 328 public boolean checkReset(char typedChar) { 329 if (resetAcceptor.accept(typedChar)) { 330 reset(); 331 return true; 332 } 333 return false; 334 } 335 336 public boolean checkAndExpand(char typedChar, ActionEvent evt) 337 throws BadLocationException { 338 boolean doInsert = true; 339 boolean disableAbbrev = false; 340 341 JTextComponent component = editorUI.getComponent(); 344 Document doc = component.getDocument(); 345 if (doc instanceof BaseDocument) { 346 BaseDocument bdoc = (BaseDocument)doc; 347 SyntaxSupport sup = bdoc.getSyntaxSupport(); 348 disableAbbrev = sup.isAbbrevDisabled(component.getCaretPosition()); 349 } 350 351 if (disableAbbrev) { 352 reset(); 353 } else { 354 String expandStr = getExpandString(typedChar); 355 if (expandStr != null) { doInsert = false; 357 expandString(typedChar, expandStr, evt); 358 } else { 359 addChar(typedChar); 360 } 361 checkReset(typedChar); 362 } 363 return doInsert; 364 } 365 366 public void checkAndExpand(ActionEvent evt) 367 throws BadLocationException { 368 String expandStr = getExpandString(); 369 if (expandStr != null) { 370 expandString(expandStr, evt); 371 } 372 } 373 374 } 375 | Popular Tags |