1 19 20 package org.netbeans.modules.web.jsf.palette; 21 22 import javax.swing.text.BadLocationException ; 23 import javax.swing.text.Caret ; 24 import javax.swing.text.Document ; 25 import javax.swing.text.JTextComponent ; 26 import org.netbeans.editor.BaseDocument; 27 import org.netbeans.editor.Formatter; 28 import org.netbeans.editor.TokenItem; 29 30 34 public final class JSFPaletteUtilities { 35 36 public static void insert(String s, JTextComponent target) 37 throws BadLocationException 38 { 39 insert(s, target, true); 40 } 41 42 public static void insert(String s, JTextComponent target, boolean reformat) 43 throws BadLocationException 44 { 45 Document doc = target.getDocument(); 46 if (doc == null) 47 return; 48 49 56 if (s == null) 57 s = ""; 58 59 if (doc instanceof BaseDocument) 60 ((BaseDocument)doc).atomicLock(); 61 62 int start = insert(s, target, doc); 63 64 if (reformat && start >= 0 && doc instanceof BaseDocument) { int end = start + s.length(); 66 Formatter f = ((BaseDocument)doc).getFormatter(); 67 f.reformat((BaseDocument)doc, start, end); 68 } 69 70 78 if (doc instanceof BaseDocument) 79 ((BaseDocument)doc).atomicUnlock(); 80 81 } 82 83 private static int insert(String s, JTextComponent target, Document doc) 84 throws BadLocationException 85 { 86 87 int start = -1; 88 try { 89 Caret caret = target.getCaret(); 91 int p0 = Math.min(caret.getDot(), caret.getMark()); 92 int p1 = Math.max(caret.getDot(), caret.getMark()); 93 doc.remove(p0, p1 - p0); 94 95 start = caret.getDot(); 97 doc.insertString(start, s, null); 98 } 99 catch (BadLocationException ble) {} 100 101 return start; 102 } 103 104 } 105 | Popular Tags |