1 19 20 package org.netbeans.modules.editor.lib2; 21 22 import java.lang.reflect.Method ; 23 import java.util.logging.Level ; 24 import java.util.logging.Logger ; 25 import javax.swing.text.BadLocationException ; 26 import javax.swing.text.Document ; 27 import javax.swing.text.Element ; 28 import org.openide.util.NbBundle; 29 30 35 public final class DocUtils { 36 37 private static final Logger LOG = Logger.getLogger(DocUtils.class.getName()); 38 39 public static int getRowStart(Document doc, int offset, int lineShift) 40 throws BadLocationException { 41 42 checkOffsetValid(doc, offset); 43 44 if (lineShift != 0) { 45 Element lineRoot = doc.getDefaultRootElement(); 46 int line = lineRoot.getElementIndex(offset); 47 line += lineShift; 48 if (line < 0 || line >= lineRoot.getElementCount()) { 49 return -1; } 51 return lineRoot.getElement(line).getStartOffset(); 52 53 } else { return doc.getDefaultRootElement().getElement( 55 doc.getDefaultRootElement().getElementIndex(offset)).getStartOffset(); 56 } 57 } 58 59 public static int getRowEnd(Document doc, int offset) 60 throws BadLocationException { 61 checkOffsetValid(doc, offset); 62 63 return doc.getDefaultRootElement().getElement( 64 doc.getDefaultRootElement().getElementIndex(offset)).getEndOffset() - 1; 65 } 66 67 73 public static int getLineOffset(Document doc, int offset) throws BadLocationException { 74 checkOffsetValid(offset, doc.getLength() + 1); 75 76 Element lineRoot = doc.getDefaultRootElement(); 77 return lineRoot.getElementIndex(offset); 78 } 79 80 public static String debugPosition(Document doc, int offset) { 81 String ret; 82 83 if (offset >= 0) { 84 try { 85 int line = getLineOffset(doc, offset) + 1; 86 int col = getVisualColumn(doc, offset) + 1; 87 ret = String.valueOf(line) + ":" + String.valueOf(col); } catch (BadLocationException e) { 89 ret = NbBundle.getBundle(DocUtils.class).getString("wrong_position") 90 + ' ' + offset + " > " + doc.getLength(); } 92 } else { 93 ret = String.valueOf(offset); 94 } 95 96 return ret; 97 } 98 99 104 public static int getVisualColumn(Document doc, int offset) throws BadLocationException { 105 int docLen = doc.getLength(); 106 if (offset == docLen + 1) { offset = docLen; 108 } 109 110 try { 112 Method m = doc.getClass().getMethod("getVisColFromPos", Integer.TYPE); 113 return (Integer ) m.invoke(doc, offset); 114 } catch (Exception e) { 116 return -1; 117 } 118 } 119 120 public static boolean isIdentifierPart(Document doc, char ch) { 121 return AcceptorFactory.LETTER_DIGIT.accept(ch); 123 } 124 125 public static boolean isWhitespace(char ch) { 126 return AcceptorFactory.WHITESPACE.accept(ch); 128 } 129 130 public static void atomicLock(Document doc) { 131 try { 133 Method lockMethod = doc.getClass().getMethod("atomicLock"); 134 lockMethod.invoke(doc); 135 } catch (Exception e) { 136 LOG.log(Level.WARNING, e.getMessage(), e); 137 } 138 } 139 140 public static void atomicUnlock(Document doc) { 141 try { 143 Method unlockMethod = doc.getClass().getMethod("atomicUnlock"); 144 unlockMethod.invoke(doc); 145 } catch (Exception e) { 146 LOG.log(Level.WARNING, e.getMessage(), e); 147 } 148 } 149 150 private static void checkOffsetValid(Document doc, int offset) throws BadLocationException { 151 checkOffsetValid(offset, doc.getLength()); 152 } 153 154 private static void checkOffsetValid(int offset, int limitOffset) throws BadLocationException { 155 if (offset < 0 || offset > limitOffset) { 156 throw new BadLocationException ("Invalid offset=" + offset + " not within <0, " + limitOffset + ">", offset); 159 } 160 } 161 162 163 private DocUtils() { 164 } 165 166 } 167 | Popular Tags |