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(); } }
/** * 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); } }
PlainDocument doc = new PlainDocument(); doc.setDocumentFilter(new DocumentFilter() { @Override public void insertString(FilterBypass fb, int off, String str, AttributeSet attr) throws BadLocationException { fb.insertString(off, str.replaceAll("\\D++", ""), attr); // remove non-digits } @Override public void replace(FilterBypass fb, int off, int len, String str, AttributeSet attr) throws BadLocationException { fb.replace(off, len, str.replaceAll("\\D++", ""), attr); // remove non-digits } }); JTextField field = new JTextField(); field.setDocument(doc);
@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(); }
public void remove(int offs, int len) throws BadLocationException { super.remove(offs, len); final List oldSuggestions = suggestions; suggestions = recentResourceNamesModel.getResourceNamesStartingWith(AutoCompletionTextField.this.getText()); if (!oldSuggestions.equals(suggestions)) { comboBoxModel.fireContentsChanged(); } }
public synchronized void setText(final String textValue) { try { if (this.isDocumentUpdateEnabled) { try { this.isDocumentListenerEnabled = false; this.document.remove(0, this.document.getLength()); } finally { this.isDocumentListenerEnabled = true; } this.document.insertString(0, textValue, null); } } catch (final BadLocationException exception) { throw new UnreachableCodeReachedException(exception); } }
@Override public void insertString( int offs, String str, AttributeSet a ) throws BadLocationException { if ( str == null ) return; boolean proceed = true; if ( pattern != null ) { String tmp = super.getText( 0, offs ) + str + (super.getLength()>offs ? super.getText( offs, super.getLength()-offs ) : ""); Matcher m = pattern.matcher( tmp ); proceed = m.matches(); } if ( proceed == true ) super.insertString( offs, str, a ); }
@Override public void remove( int offs, int len ) throws BadLocationException { boolean proceed = true; if ( pattern != null ) { try { String tmp = super.getText( 0, offs ) + (super.getLength()>(offs+len) ? super.getText( offs+len, super.getLength()-(offs+len) ) : ""); Matcher m = pattern.matcher( tmp ); proceed = m.matches(); } catch ( BadLocationException e ) {} } if ( proceed == true ) super.remove( offs, len ); } }
public String getText( final int offset, final int length ) throws BadLocationException { return backend.getText( offset, length ); }
@Override public String convert(final PlainDocument plainDocument) throws RuntimeException { try { final String text = plainDocument.getText(0, plainDocument.getLength()); final StringBuilder builder = new StringBuilder(); for (final char character : text.toCharArray()) { if (!FieldValueController.this.characterFilter.accept(character)) { continue; } builder.append(character); } return text; } catch (final BadLocationException exception) { return ""; //$NON-NLS-1$ } } };
@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(); }
/** {@inheritDoc} */ @Override public void remove(int offset, int length) throws BadLocationException { final int curLen = getLength(); final String currentContent = getText(0, curLen); final String newText = currentContent.substring(0, offset) + currentContent.substring(length + offset, curLen); if (allowNegative && offset == 0 && newText.equals("-")) { value = 0; } else { value = checkInput(newText, offset); } super.remove(offset, length); }
public void getText( final int offset, final int length, final Segment txt ) throws BadLocationException { backend.getText( offset, length, txt ); }
@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(); }
/** * 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); } }
private void setText(String text) throws BadLocationException { // remove all text and insert the completed string super.remove(0, getLength()); super.insertString(0, text, null); }
@Override public void remove(int i, int i1) throws BadLocationException { if(updatingTextField) super.remove(i, i1); } });
searchResultSelectionModel .setSelectionMode(ListSelectionModel.SINGLE_SELECTION); Document searchInput = new PlainDocument();
public String getText( final int offset, final int length ) throws BadLocationException { return backend.getText( offset, length ); }
@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(); }