/** * Inserts some content into the document. * Inserting content causes a write lock to be held while the * actual changes are taking place, followed by notification * to the observers on the thread that grabbed the write lock. * <p/> * This method is thread safe, although most Swing methods * are not. Please see * <A HREF="http://java.sun.com/products/jfc/swingdoc-archive/threads.html">Threads * and Swing</A> for more information. * * @param offs the starting offset >= 0 * @param str the string to insert; does nothing with null/empty strings * @param a the attributes for the inserted content * @throws BadLocationException the given insert position is not a valid * position within the document * @see Document#insertString */ public void insertString(int offs, @NotNull String str, AttributeSet a) throws BadLocationException { super.insertString(offs, str, a); int newOffs = offs + str.length(); if (overwrite && newOffs < getLength()) { int len = Math.min(str.length(), getLength() - newOffs); super.remove(newOffs, len); } }
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { final String text = this.getText(0, offs) + str; List oldSuggestions = suggestions; suggestions = recentResourceNamesModel.getResourceNamesStartingWith(text); String appendString = ""; if (!suggestions.isEmpty()) { final String suggestion = (String)suggestions.get(0); appendString = suggestion.substring(text.length()); } super.insertString(offs, str+appendString, a); setCaretPosition(offs); setSelectionStart(text.length()); setSelectionEnd(getLength()); if (!oldSuggestions.equals(suggestions)) { comboBoxModel.fireContentsChanged(); } }
/** * Appends data to the document, removing data from the front if necessary. * Data removed is split at the line level. * WARNING: I'm ignoring the 'offs' value. */ public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { if (str == null) return; // Make sure the string will fit, then append it. truncateDataFor(str.length()); super.insertString(getLength(), str, a); } }
/** {@inheritDoc} */ @Override public void insertString(int offset, String text, AttributeSet attributes) throws BadLocationException { if (text != null) { String newText; if (getLength() == 0) { newText = text; } else { StringBuilder sb = new StringBuilder(getText(0, getLength())); sb.insert(offset, text); newText = sb.toString(); } if (allowNegative && offset == 0 && newText.equals("-")) { value = 0; } else { value = checkInput(newText, offset); } super.insertString(offset, text, attributes); } }
@Override public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { if (!readOnly) { super.insertString(offs, str, a); } }
@Override public void insertString(int i, String string, AttributeSet attributeSet) throws BadLocationException { if(updatingTextField) super.insertString(i, string, attributeSet); }
@Test public void testRead_charArrayArg_allAtOnce() throws Exception { String content = "0123456789"; PlainDocument doc = new PlainDocument(); doc.insertString(0, content, null); DocumentReader r = new DocumentReader(doc); char[] array = new char[10]; Assert.assertEquals(10, r.read(array)); Assert.assertEquals(content, new String(array)); Assert.assertEquals(-1, r.read(array)); Assert.assertEquals(-1, r.read()); r.close(); }
@Test public void testRead_3Arg_allAtOnce() throws Exception { String content = "0123456789"; PlainDocument doc = new PlainDocument(); doc.insertString(0, content, null); DocumentReader r = new DocumentReader(doc); char[] array = new char[10]; Assert.assertEquals(10, r.read(array, 0, array.length)); Assert.assertEquals(content, new String(array)); Assert.assertEquals(-1, r.read(array, 0, array.length)); Assert.assertEquals(-1, r.read()); r.close(); }
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException { if (str == null) { return; } if (getLength() + str.length() <= _maximumLength) { super.insertString(offset, str, attr); } } }
@Test public void testSkip_PastDocumentEnd() throws Exception { String content = "0123456789"; PlainDocument doc = new PlainDocument(); doc.insertString(0, content, null); DocumentReader r = new DocumentReader(doc); Assert.assertEquals('0', r.read()); r.skip(1000); Assert.assertEquals(-1, r.read()); r.close(); }
@Test public void testSeek_PastDocumentEnd() throws Exception { String content = "0123456789"; PlainDocument doc = new PlainDocument(); doc.insertString(0, content, null); DocumentReader r = new DocumentReader(doc); Assert.assertEquals('0', r.read()); r.seek(1000); Assert.assertEquals(-1, r.read()); r.close(); }
@Test public void testRead_charArrayArg_multipleReads() throws Exception { String content = "0123456789"; PlainDocument doc = new PlainDocument(); doc.insertString(0, content, null); DocumentReader r = new DocumentReader(doc); char[] array = new char[3]; Assert.assertEquals(3, r.read(array)); Assert.assertEquals("012", new String(array)); Assert.assertEquals(3, r.read(array)); Assert.assertEquals("345", new String(array)); Assert.assertEquals(3, r.read(array)); Assert.assertEquals("678", new String(array)); Assert.assertEquals(1, r.read(array)); Assert.assertEquals('9', array[0]); Assert.assertEquals(-1, r.read(array)); Assert.assertEquals(-1, r.read()); r.close(); }
@Test public void testRead_3Arg_multipleReads() throws Exception { String content = "0123456789"; PlainDocument doc = new PlainDocument(); doc.insertString(0, content, null); DocumentReader r = new DocumentReader(doc); char[] array = new char[10]; Assert.assertEquals(3, r.read(array, 0, 3)); Assert.assertEquals("012", new String(array, 0, 3)); Assert.assertEquals(3, r.read(array, 0, 3)); Assert.assertEquals("345", new String(array, 0, 3)); Assert.assertEquals(3, r.read(array, 0, 3)); Assert.assertEquals("678", new String(array, 0, 3)); Assert.assertEquals(1, r.read(array, 0, 3)); Assert.assertEquals('9', array[0]); Assert.assertEquals(-1, r.read(array, 0, 3)); Assert.assertEquals(-1, r.read()); r.close(); }
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { super.insertString(offs, str, a); String word = autoComplete(getText(0, getLength())); if (word != null) { super.insertString(offs + str.length(), word, a); comp.setCaretPosition(offs + str.length()); comp.moveCaretPosition(getLength()); } }
@Test public void testSkip_WithinDocument() throws Exception { String content = "0123456789"; PlainDocument doc = new PlainDocument(); doc.insertString(0, content, null); DocumentReader r = new DocumentReader(doc); Assert.assertEquals('0', r.read()); r.skip(6); Assert.assertEquals('7', r.read()); Assert.assertEquals('8', r.read()); Assert.assertEquals('9', r.read()); Assert.assertEquals(-1, r.read()); r.close(); }
@Test public void testMark() throws Exception { PlainDocument doc = new PlainDocument(); doc.insertString(0, "0123456789", null); DocumentReader r = new DocumentReader(doc); Assert.assertEquals('0', r.read()); r.mark(5); Assert.assertEquals('1', r.read()); Assert.assertEquals('2', r.read()); r.reset(); Assert.assertEquals('1', r.read()); Assert.assertEquals('2', r.read()); r.close(); }
@Test public void testReset_MarkedOffset() throws Exception { String content = "0123456789"; PlainDocument doc = new PlainDocument(); doc.insertString(0, content, null); DocumentReader r = new DocumentReader(doc); Assert.assertEquals('0', r.read()); r.mark(5); Assert.assertEquals('1', r.read()); Assert.assertEquals('2', r.read()); r.reset(); Assert.assertEquals('1', r.read()); Assert.assertEquals('2', r.read()); r.close(); }
@Test public void testSeek_WithinDocument() throws Exception { String content = "0123456789"; PlainDocument doc = new PlainDocument(); doc.insertString(0, content, null); DocumentReader r = new DocumentReader(doc); Assert.assertEquals('0', r.read()); r.seek(6); Assert.assertEquals('6', r.read()); Assert.assertEquals('7', r.read()); Assert.assertEquals('8', r.read()); Assert.assertEquals('9', r.read()); Assert.assertEquals(-1, r.read()); r.close(); }
@Test public void testReset_NoMarkedOffset() throws Exception { String content = "0123456789"; PlainDocument doc = new PlainDocument(); doc.insertString(0, content, null); DocumentReader r = new DocumentReader(doc); Assert.assertEquals('0', r.read()); Assert.assertEquals('1', r.read()); Assert.assertEquals('2', r.read()); r.reset(); Assert.assertEquals('0', r.read()); Assert.assertEquals('1', r.read()); Assert.assertEquals('2', r.read()); r.close(); }
@Test public void testRead_intArg() throws Exception { PlainDocument doc = new PlainDocument(); doc.insertString(0, "0123456789", null); DocumentReader r = new DocumentReader(doc); Assert.assertEquals('0', r.read()); Assert.assertEquals('1', r.read()); Assert.assertEquals('2', r.read()); Assert.assertEquals('3', r.read()); Assert.assertEquals('4', r.read()); Assert.assertEquals('5', r.read()); Assert.assertEquals('6', r.read()); Assert.assertEquals('7', r.read()); Assert.assertEquals('8', r.read()); Assert.assertEquals('9', r.read()); Assert.assertEquals(-1, r.read()); Assert.assertEquals(-1, r.read()); r.close(); }