Java 类javax.swing.text.PlainDocument 实例源码

项目:incubator-netbeans    文件:HighlightingManagerTest.java   
public void testExcludeTwoLayers() {
    OffsetsBag bag = new OffsetsBag(new PlainDocument());

    MemoryMimeDataProvider.reset(null);
    MemoryMimeDataProvider.addInstances(
        "text/plain", new SingletonLayerFactory("layer", ZOrder.DEFAULT_RACK, true, bag));

    JEditorPane pane = new JEditorPane();
    String [] removed = new String[] {"^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\..*$", "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.TextSelectionHighlighting$"};
    pane.putClientProperty("HighlightsLayerExcludes", removed);
    pane.setContentType("text/plain");
    assertEquals("The pane has got wrong mime type", "text/plain", pane.getContentType());

    HighlightingManager hm = HighlightingManager.getInstance(pane);
    HighlightsContainer hc = hm.getHighlights(HighlightsLayerFilter.IDENTITY);

    assertNotNull("Can't get fixed HighlightsContainer", hc);
    assertFalse("There should be no fixed highlights", hc.getHighlights(0, Integer.MAX_VALUE).moveNext());
}
项目:incubator-netbeans    文件:ProxyHighlightsContainerTest.java   
public void testSimple() {
    PlainDocument doc = new PlainDocument();
    HighlightsContainer layer = createRandomBag(doc, "layer");
    HighlightsSequence highlights = layer.getHighlights(0, 100);

    ProxyHighlightsContainer proxyLayer = new ProxyHighlightsContainer(doc, new HighlightsContainer [] { layer });
    HighlightsSequence proxyHighlights = proxyLayer.getHighlights(0, 100);

    for ( ; highlights.moveNext(); ) {
        // Ignore empty highlights
        if (highlights.getStartOffset() == highlights.getEndOffset()) {
            continue;
        }

        assertTrue("Wrong number of proxy highlights", proxyHighlights.moveNext());

        assertEquals("Start offset does not match", highlights.getStartOffset(), proxyHighlights.getStartOffset());
        assertEquals("End offset does not match", highlights.getEndOffset(), proxyHighlights.getEndOffset());
        assertTrue("Attributes do not match", highlights.getAttributes().isEqual(proxyHighlights.getAttributes()));
    }
}
项目:incubator-netbeans    文件:ProxyHighlightsContainerTest.java   
public void testOrdering() {
    PlainDocument doc = new PlainDocument();
    PositionsBag hsA = new PositionsBag(doc);
    PositionsBag hsB = new PositionsBag(doc);

    SimpleAttributeSet attribsA = new SimpleAttributeSet();
    SimpleAttributeSet attribsB = new SimpleAttributeSet();

    attribsA.addAttribute("attribute", "value-A");
    attribsB.addAttribute("attribute", "value-B");

    hsA.addHighlight(new SimplePosition(5), new SimplePosition(15), attribsA);
    hsB.addHighlight(new SimplePosition(10), new SimplePosition(20), attribsB);

    ProxyHighlightsContainer chc = new ProxyHighlightsContainer(doc, new HighlightsContainer [] { hsA, hsB });
    HighlightsSequence highlights = chc.getHighlights(0, Integer.MAX_VALUE);

    assertTrue("Wrong number of highlights", highlights.moveNext());
    assertEquals("1. highlight - wrong attribs", "value-A", highlights.getAttributes().getAttribute("attribute"));

    assertTrue("Wrong number of highlights", highlights.moveNext());
    assertEquals("2. highlight - wrong attribs", "value-B", highlights.getAttributes().getAttribute("attribute"));

    assertTrue("Wrong number of highlights", highlights.moveNext());
    assertEquals("3. highlight - wrong attribs", "value-B", highlights.getAttributes().getAttribute("attribute"));
}
项目:incubator-netbeans    文件:ProxyHighlightsContainerTest.java   
public void testEvents() {
    PlainDocument doc = new PlainDocument();
    PositionsBag hsA = new PositionsBag(doc);
    PositionsBag hsB = new PositionsBag(doc);

    ProxyHighlightsContainer chc = new ProxyHighlightsContainer(doc, new HighlightsContainer [] { hsA, hsB });
    Listener listener = new Listener();
    chc.addHighlightsChangeListener(listener);

    hsA.addHighlight(new SimplePosition(10), new SimplePosition(20), new SimpleAttributeSet());
    assertEquals("Wrong number of events", 1, listener.eventsCnt);
    assertEquals("Wrong change start offset", 10, listener.lastEventStartOffset);
    assertEquals("Wrong change end offset", 20, listener.lastEventEndOffset);

    listener.reset();
    hsB.addHighlight(new SimplePosition(11), new SimplePosition(12), new SimpleAttributeSet());
    assertEquals("Wrong number of events", 1, listener.eventsCnt);
    assertEquals("Wrong change start offset", 11, listener.lastEventStartOffset);
    assertEquals("Wrong change end offset", 12, listener.lastEventEndOffset);
}
项目:incubator-netbeans    文件:ProxyHighlightsContainerTest.java   
public void testEvents2() {
    PlainDocument doc = new PlainDocument();
    PositionsBag hsA = new PositionsBag(doc);
    PositionsBag hsB = new PositionsBag(doc);

    hsA.addHighlight(new SimplePosition(10), new SimplePosition(20), new SimpleAttributeSet());
    hsB.addHighlight(new SimplePosition(11), new SimplePosition(12), new SimpleAttributeSet());

    ProxyHighlightsContainer chc = new ProxyHighlightsContainer();
    Listener listener = new Listener();
    chc.addHighlightsChangeListener(listener);

    // changing delegate layers fires event covering 'all' offsets
    chc.setLayers(doc, new HighlightsContainer [] { hsA, hsB });
    assertEquals("Wrong number of events", 1, listener.eventsCnt);
    assertEquals("Wrong change start offset", 0, listener.lastEventStartOffset);
    assertEquals("Wrong change end offset", Integer.MAX_VALUE, listener.lastEventEndOffset);
}
项目:incubator-netbeans    文件:HighlightsMergeTesting.java   
public static RandomTestContainer createContainer() {
    RandomTestContainer container = new RandomTestContainer();
    Document doc = new PlainDocument();
    container.putProperty(Document.class, doc);
    // Fill document
    try {
        doc.insertString(0, "abcdef\n", null);
        int docLen;
        while ((docLen = doc.getLength()) < DOCUMENT_LENGTH) {
            int insertLen = Math.min(docLen, DOCUMENT_LENGTH - docLen);
            doc.insertString(docLen, doc.getText(0, insertLen), null);
        }
    } catch (BadLocationException ex) {
        throw new IllegalStateException(ex);
    }
    container.putProperty(DirectMergeContainer.class, new DirectMergeContainer(new HighlightsContainer[0], false));
    container.putProperty(CompoundHighlightsContainer.class,
            new CompoundHighlightsContainer(doc, new HighlightsContainer[0]));

    container.addOp(new AddLayerOp(ADD_LAYER));
    container.addOp(new AddLayerOp(ADD_EMPTY_LAYER));
    container.addOp(new RemoveLayerOp(REMOVE_LAYER));
    container.addCheck(new MergeCheck());
    return container;
}
项目:incubator-netbeans    文件:CompoundHighlightsContainerTest.java   
public void testSimple() {
    PlainDocument doc = new PlainDocument();
    HighlightsContainer layer = createRandomBag(doc, "layer");
    HighlightsSequence highlights = layer.getHighlights(0, 100);

    CompoundHighlightsContainer proxyLayer = new CompoundHighlightsContainer(doc, new HighlightsContainer [] { layer });
    HighlightsSequence proxyHighlights = proxyLayer.getHighlights(0, 100);

    for ( ; highlights.moveNext(); ) {
        // Ignore empty highlights
        if (highlights.getStartOffset() == highlights.getEndOffset()) {
            continue;
        }

        assertTrue("Wrong number of proxy highlights", proxyHighlights.moveNext());

        assertEquals("Start offset does not match", highlights.getStartOffset(), proxyHighlights.getStartOffset());
        assertEquals("End offset does not match", highlights.getEndOffset(), proxyHighlights.getEndOffset());
        assertTrue("Attributes do not match", highlights.getAttributes().isEqual(proxyHighlights.getAttributes()));
    }
}
项目:incubator-netbeans    文件:CompoundHighlightsContainerTest.java   
public void testOrdering() {
    PlainDocument doc = new PlainDocument();
    PositionsBag hsA = new PositionsBag(doc);
    PositionsBag hsB = new PositionsBag(doc);

    SimpleAttributeSet attribsA = new SimpleAttributeSet();
    SimpleAttributeSet attribsB = new SimpleAttributeSet();

    attribsA.addAttribute("attribute", "value-A");
    attribsB.addAttribute("attribute", "value-B");

    hsA.addHighlight(new SimplePosition(5), new SimplePosition(15), attribsA);
    hsB.addHighlight(new SimplePosition(10), new SimplePosition(20), attribsB);

    CompoundHighlightsContainer chc = new CompoundHighlightsContainer(doc, new HighlightsContainer [] { hsA, hsB });
    HighlightsSequence highlights = chc.getHighlights(0, Integer.MAX_VALUE);

    assertTrue("Wrong number of highlights", highlights.moveNext());
    assertEquals("1. highlight - wrong attribs", "value-A", highlights.getAttributes().getAttribute("attribute"));

    assertTrue("Wrong number of highlights", highlights.moveNext());
    assertEquals("2. highlight - wrong attribs", "value-B", highlights.getAttributes().getAttribute("attribute"));

    assertTrue("Wrong number of highlights", highlights.moveNext());
    assertEquals("3. highlight - wrong attribs", "value-B", highlights.getAttributes().getAttribute("attribute"));
}
项目:incubator-netbeans    文件:CompoundHighlightsContainerTest.java   
public void testConcurrentModification() throws Exception {
    PlainDocument doc = new PlainDocument();
    PositionsBag bag = createRandomBag(doc, "layer");
    HighlightsContainer [] layers = new HighlightsContainer [] { bag };

    CompoundHighlightsContainer hb = new CompoundHighlightsContainer(doc, layers);
    HighlightsSequence hs = hb.getHighlights(0, Integer.MAX_VALUE);

    assertTrue("No highlights", hs.moveNext());
    int s = hs.getStartOffset();
    int e = hs.getEndOffset();
    AttributeSet a = hs.getAttributes();

    // Change the layers
    hb.setLayers(doc, layers);

    assertEquals("Different startOffset", s, hs.getStartOffset());
    assertEquals("Different endOffset", e, hs.getEndOffset());
    assertEquals("Different attributes", a, hs.getAttributes());

    assertFalse("There should be no further highlighs after co-modification", hs.moveNext());
}
项目:incubator-netbeans    文件:CompoundHighlightsContainerTest.java   
public void testEvents() {
    PlainDocument doc = new PlainDocument();
    PositionsBag hsA = new PositionsBag(doc);
    PositionsBag hsB = new PositionsBag(doc);

    CompoundHighlightsContainer chc = new CompoundHighlightsContainer(doc, new HighlightsContainer [] { hsA, hsB });
    Listener listener = new Listener();
    chc.addHighlightsChangeListener(listener);

    hsA.addHighlight(new SimplePosition(10), new SimplePosition(20), new SimpleAttributeSet());
    assertEquals("Wrong number of events", 1, listener.eventsCnt);
    assertEquals("Wrong change start offset", 10, listener.lastEventStartOffset);
    assertEquals("Wrong change end offset", 20, listener.lastEventEndOffset);

    listener.reset();
    hsB.addHighlight(new SimplePosition(11), new SimplePosition(12), new SimpleAttributeSet());
    assertEquals("Wrong number of events", 1, listener.eventsCnt);
    assertEquals("Wrong change start offset", 11, listener.lastEventStartOffset);
    assertEquals("Wrong change end offset", 12, listener.lastEventEndOffset);
}
项目:incubator-netbeans    文件:CompoundHighlightsContainerTest.java   
public void testEvents2() {
    PlainDocument doc = new PlainDocument();
    PositionsBag hsA = new PositionsBag(doc);
    PositionsBag hsB = new PositionsBag(doc);

    hsA.addHighlight(new SimplePosition(10), new SimplePosition(20), new SimpleAttributeSet());
    hsB.addHighlight(new SimplePosition(11), new SimplePosition(12), new SimpleAttributeSet());

    CompoundHighlightsContainer chc = new CompoundHighlightsContainer();
    Listener listener = new Listener();
    chc.addHighlightsChangeListener(listener);

    // changing delegate layers fires event covering 'all' offsets
    chc.setLayers(doc, new HighlightsContainer [] { hsA, hsB });
    assertEquals("Wrong number of events", 1, listener.eventsCnt);
    assertEquals("Wrong change start offset", 0, listener.lastEventStartOffset);
    assertEquals("Wrong change end offset", Integer.MAX_VALUE, listener.lastEventEndOffset);
}
项目:incubator-netbeans    文件:CompoundHighlightsContainerTest.java   
public void testZeroPosition() throws BadLocationException {
    PlainDocument doc = new PlainDocument();
    TestHighlighsContainer thc = new TestHighlighsContainer();
    CompoundHighlightsContainer chc = new CompoundHighlightsContainer();

    chc.setLayers(doc, new HighlightsContainer[] { thc });
    doc.insertString(0, "0123456789", null);

    chc.getHighlights(0, Integer.MAX_VALUE);
    assertEquals("Should have been queried", 2, thc.queries.size());
    assertEquals("Wrong query startOffset", 0, (int) thc.queries.get(0));

    thc.queries.clear();
    doc.insertString(0, "abcd", null);
    assertEquals("Should not have been queried", 0, thc.queries.size());

    chc.getHighlights(0, Integer.MAX_VALUE);
    assertEquals("Should have been queried again", 2, thc.queries.size());
    assertEquals("Wrong query startOffset", 0, (int) thc.queries.get(0));

    thc.queries.clear();
    chc.getHighlights(0, Integer.MAX_VALUE);
    assertEquals("Should not have been queried again", 0, thc.queries.size());
}
项目:incubator-netbeans    文件:WordMatchTest.java   
public void testOffset0Forward() throws Exception {
    Document doc = new PlainDocument();
    WordMatch wordMatch = WordMatch.get(doc);
    //                   012345678901234567890123456789
    doc.insertString(0, "abc abc dab ab a abab+c", null);
    int docLen = doc.getLength();
    wordMatch.matchWord(0, true);
    compareText(doc, 0, "abca", docLen + 3);
    wordMatch.matchWord(0, true);
    compareText(doc, 0, "daba", docLen + 3);
    wordMatch.matchWord(0, true);
    compareText(doc, 0, "aba", docLen + 2);
    wordMatch.matchWord(0, true);
    compareText(doc, 0, "aa", docLen + 1);
    wordMatch.matchWord(0, true);
    compareText(doc, 0, "ababa", docLen + 4);
    wordMatch.matchWord(0, true);
    compareText(doc, 0, "ca", docLen + 1);
    wordMatch.matchWord(0, true);
    compareText(doc, 0, "ca", docLen + 1);

    wordMatch.matchWord(0, false);
    compareText(doc, 0, "ababa", docLen + 4);
}
项目:incubator-netbeans    文件:WordMatchTest.java   
public void testOffsetDocLenBackward() throws Exception {
    Document doc = new PlainDocument();
    WordMatch wordMatch = WordMatch.get(doc);
    //                   012345678901234567890123456789
    doc.insertString(0, "abc abc dab ab a", null);
    int docLen = doc.getLength();
    wordMatch.matchWord(docLen, false);
    compareText(doc, docLen - 1, "ab", docLen + 1);
    wordMatch.matchWord(docLen, false);
    compareText(doc, docLen - 1, "abc", docLen + 2);
    wordMatch.matchWord(docLen, false);
    compareText(doc, docLen - 1, "abc", docLen + 2);

    wordMatch.matchWord(docLen, true);
    compareText(doc, docLen - 1, "ab", docLen + 1);
    wordMatch.matchWord(docLen, true);
    compareText(doc, docLen - 1, "a", docLen);
}
项目:incubator-netbeans    文件:WordMatchTest.java   
public void testOffsetInsideBackward() throws Exception {
    Document doc = new PlainDocument();
    WordMatch wordMatch = WordMatch.get(doc);
    //                   012345678901234567890123456789
    doc.insertString(0, "abc abc dab ab a xyz ab abd", null);
    int docLen = doc.getLength();
    int offset = 15;
    wordMatch.matchWord(offset, false);
    compareText(doc, offset, "aba", docLen + 2);
    wordMatch.matchWord(docLen, true);
    compareText(doc, offset, "a ", docLen);
    wordMatch.matchWord(docLen, true);
    compareText(doc, offset, "aa ", docLen + 1);
    wordMatch.matchWord(docLen, true);
    compareText(doc, offset, "xyza", docLen + 3);
    wordMatch.matchWord(docLen, true);
    compareText(doc, offset, "abda", docLen + 3);
    wordMatch.matchWord(docLen, true);
    compareText(doc, offset, "abca", docLen + 3);
}
项目:incubator-netbeans    文件:PlainDocumentTest.java   
public void testBehaviour() throws Exception {
    Document doc = new PlainDocument();
    doc.insertString(0, "test hello world", null);
    UndoManager undo = new UndoManager();
    doc.addUndoableEditListener(undo);
    Position pos = doc.createPosition(2);
    doc.remove(0, 3);
    assert (pos.getOffset() == 0);
    undo.undo();
    assert (pos.getOffset() == 2);

    Position pos2 = doc.createPosition(5);
    doc.remove(4, 2);
    Position pos3 = doc.createPosition(4);
    assertSame(pos2, pos3);
    undo.undo();
    assert (pos3.getOffset() == 5);
}
项目:incubator-netbeans    文件:PlainDocumentTest.java   
public void testCuriosities() throws Exception {
    // Test position at offset 0 does not move after insert
    Document doc = new PlainDocument();
    doc.insertString(0, "test", null);
    Position pos = doc.createPosition(0);
    assertEquals(0, pos.getOffset());
    doc.insertString(0, "a", null);
    assertEquals(0, pos.getOffset());

    // Test there is an extra newline above doc.getLength()
    assertEquals("\n", doc.getText(doc.getLength(), 1));
    assertEquals("atest\n", doc.getText(0, doc.getLength() + 1));

    // Test the last line element contains the extra newline
    Element lineElem = doc.getDefaultRootElement().getElement(0);
    assertEquals(0, lineElem.getStartOffset());
    assertEquals(doc.getLength() + 1, lineElem.getEndOffset());

    // Test that once position gets to zero it won't go anywhere else (unless undo performed)
    pos = doc.createPosition(1);
    doc.remove(0, 1);
    assertEquals(0, pos.getOffset());
    doc.insertString(0, "b", null);
    assertEquals(0, pos.getOffset());
}
项目:incubator-netbeans    文件:PositionsBagMemoryTest.java   
private void checkMemoryConsumption(boolean merging, boolean bestCase) {
    PositionsBag bag = new PositionsBag(new PlainDocument(), merging);

    for(int i = 0; i < CNT; i++) {
        if (bestCase) {
            bag.addHighlight(new SimplePosition(i * 10), new SimplePosition((i + 1) * 10), SimpleAttributeSet.EMPTY);
        } else {
            bag.addHighlight(new SimplePosition(i * 10), new SimplePosition(i* 10 + 5), SimpleAttributeSet.EMPTY);
        }
    }

    compact(bag);

    assertSize("PositionsBag of " + CNT + " highlights " + (bestCase ? "(best case)" : "(worst case)"),
        Collections.singleton(bag), bestCase ? 8500 : 16500, new MF());
}
项目:incubator-netbeans    文件:PositionsBagRandomTest.java   
private PositionsBag mergeContainers(boolean merge, String [] layerNames, HighlightsContainer[] containers) {
    PositionsBag bag = new PositionsBag(new PlainDocument(), merge);

    for (int i = 0; i < containers.length; i++) {
        HighlightsSequence layerHighlights = 
            containers[i].getHighlights(START, END);

        for ( ; layerHighlights.moveNext(); ) {
            bag.addHighlight(
                new SimplePosition(layerHighlights.getStartOffset()), 
                new SimplePosition(layerHighlights.getEndOffset()), 
                layerHighlights.getAttributes());
        }
    }

    return bag;
}
项目:incubator-netbeans    文件:IndentTest.java   
public void testFindIndentTaskFactory() throws BadLocationException {
    TestIndentTask.TestFactory factory = new TestIndentTask.TestFactory();

    MockServices.setServices(MockMimeLookup.class);
    MockMimeLookup.setInstances(MimePath.parse(MIME_TYPE), factory);

    Document doc = new PlainDocument();
    doc.putProperty("mimeType", MIME_TYPE);
    Indent indent = Indent.get(doc);
    indent.lock();
    try {
        //doc.atomicLock();
        try {
            indent.reindent(0);
        } finally {
            //doc.atomicUnlock();
        }
    } finally {
        indent.unlock();
    }
    // Check that the factory was used
    assertTrue(TestIndentTask.TestFactory.lastCreatedTask.indentPerformed);
}
项目:incubator-netbeans    文件:ReadOnlyAccess.java   
@Override
public javax.swing.text.Document loadSwingDocument(InputStream in)
throws IOException, BadLocationException {

    javax.swing.text.Document sd = new PlainDocument();
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    try {
        String line = null;
        while ((line = br.readLine()) != null) {
            sd.insertString(sd.getLength(), line+System.getProperty("line.separator"), null); // NOI18N
        }
    } finally {
        br.close();
    }
    return sd;
}
项目:incubator-netbeans    文件:ComplexPositionsTest.java   
@Test
public void testPos() throws Exception {
    Document doc = new PlainDocument();
    doc.insertString(0, "\t\t\n\n", null);
    Position pos1 = doc.createPosition(1);
    Position pos2 = doc.createPosition(2);

    Position pos10 = ComplexPositions.create(pos1, 0);
    Position pos11 = ComplexPositions.create(pos1, 1);
    Position pos20 = ComplexPositions.create(pos2, 0);
    Position pos21 = ComplexPositions.create(pos2, 1);

    assertEquals(0, ComplexPositions.getSplitOffset(pos1));
    assertEquals(0, ComplexPositions.getSplitOffset(pos10));
    assertEquals(1, ComplexPositions.getSplitOffset(pos11));
    comparePos(pos1, pos10, 0);
    comparePos(pos10, pos11, -1);
    comparePos(pos1, pos2, -1);
    comparePos(pos10, pos20, -1);
    comparePos(pos20, pos21, -1);
}
项目:incubator-netbeans    文件:DocumentUtilities.java   
private static int visualColumn(Document doc, int lineStartOffset, int offset) {
    Integer tabSizeInteger = (Integer) doc.getProperty(PlainDocument.tabSizeAttribute);
    int tabSize = (tabSizeInteger != null) ? tabSizeInteger : 8;
    CharSequence docText = getText(doc);
    // Expected that offset <= docText.length()
    int column = 0;
    for (int i = lineStartOffset; i < offset; i++) {
        char c = docText.charAt(i);
        if (c == '\t') {
            column = (column + tabSize) / tabSize * tabSize;
        } else {
            column++;
        }
    }
    return column;
}
项目:incubator-netbeans    文件:JWSProjectProperties.java   
private Document createCBTextFieldDocument() {
    Document doc = new PlainDocument();
    String valueType = evaluator.getProperty(JNLP_CBASE_TYPE);
    String docString = "";
    if (CB_TYPE_LOCAL.equals(valueType)) {
        docString = getProjectDistDir();
    } else if (CB_TYPE_WEB.equals(valueType)) {
        docString = CB_URL_WEB;
    } else if (CB_TYPE_USER.equals(valueType)) {
        docString = getCodebaseLocation();
    }
    try {
        doc.insertString(0, docString, null);
    } catch (BadLocationException ex) {
        // do nothing, just return PlainDocument
        // XXX log the exc
    }
    return doc;
}
项目:incubator-netbeans    文件:LanguageManagerTest.java   
public void testKnownMimeType() {
    PlainDocument doc = new PlainDocument();
    doc.putProperty("mimeType", MIME_TYPE_KNOWN);

    TokenHierarchy th = TokenHierarchy.get(doc);
    ((AbstractDocument)doc).readLock();
    try {
        Language lang = th.tokenSequence().language();
        assertNotNull("There should be language for " + MIME_TYPE_KNOWN, lang);

        assertNotNull("Invalid mime type", lang.mimeType());
        assertEquals("Wrong language's mime type", MIME_TYPE_KNOWN, lang.mimeType());
    } finally {
        ((AbstractDocument)doc).readUnlock();
    }
}
项目:Equella    文件:JValidatingTextField.java   
public JValidatingTextField(final Validation... vs)
{
    setDocument(new PlainDocument()
    {
        @Override
        public void insertString(int offset, String s, AttributeSet attributeSet) throws BadLocationException
        {
            for( Validation v : vs )
            {
                s = v.validate(this, offset, s, attributeSet);
                if( s == null )
                {
                    Toolkit.getDefaultToolkit().beep();
                    return;
                }
            }
            super.insertString(offset, s, attributeSet);
        }
    });
}
项目:libraries    文件:FieldValueController.java   
public FieldValueController(
    final PlainDocument document,
    final IObjectModel<T> model,
    final IBooleanProvider isEditableProvider,
    final IConverter<String, T, RuntimeException> toObjectConverter,
    final IConverter<T, String, RuntimeException> toStringConverter,
    final IObjectModel<IValidationResult> validStateModel,
    final IValidator<String> validator) {
  this.document = document;
  this.model = model;
  this.isEditableProvider = isEditableProvider;
  this.toObjectConverter = toObjectConverter;
  this.toStringConverter = toStringConverter;
  this.validStateModel = validStateModel;
  this.validator = validator;
}
项目:ftc    文件:DocumentReaderTest.java   
@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();

}
项目:ftc    文件:DocumentReaderTest.java   
@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();

}
项目:ftc    文件:DocumentReaderTest.java   
@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();

}
项目:ftc    文件:DocumentReaderTest.java   
@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();

}
项目:ftc    文件:DocumentReaderTest.java   
@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();

}
项目:ftc    文件:DocumentReaderTest.java   
@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();

}
项目:ftc    文件:DocumentReaderTest.java   
@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();

}
项目:ftc    文件:DocumentReaderTest.java   
@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();

}
项目:taxonaut    文件:CitationModel.java   
/**
    * Creates a <CODE>CitationModel</CODE> representing
    * given <CODE>publication</CODE>
    *
    * @param publication <CODE>Publication</CODE> to be represented
    */
   public CitationModel(Publication publication)
   {
super();
authors = new PlainDocument();
affiliations = new PlainDocument();
doi = new PlainDocument();
isxn = new PlainDocument();
citationTitle = new PlainDocument();
contentsTitle = new PlainDocument();
year = new PlainDocument();
volume = new PlainDocument();
issue = new PlainDocument();
firstPage = new PlainDocument();
lastPage = new PlainDocument();
publisher = new PlainDocument();
place = new PlainDocument();
received = new PlainDocument();
revised = new PlainDocument();
accepted = new PlainDocument();
published = new PlainDocument();
setPublication(publication);
   }
项目:intellij-ce-playground    文件:TestNGConfigurationModel.java   
private void setDocumentText(final Object document, final String value) {
  if (document instanceof PlainDocument) {
    try {
      ((PlainDocument)document).remove(0, ((PlainDocument)document).getLength());
      ((PlainDocument)document).insertString(0, value, null);
    } catch (BadLocationException e) {
        throw new RuntimeException(e);
    }
  }  else {
    WriteCommandAction.runWriteCommandAction(project, new Runnable() {
      public void run() {
        ((com.intellij.openapi.editor.Document)document)
          .replaceString(0, ((com.intellij.openapi.editor.Document)document).getTextLength(), value);
      }
    });
  }

}
项目:intellij-ce-playground    文件:JUnitConfigurationModel.java   
private void setDocumentText(final int index, final String text, final Object[] documents) {
  final Object document = documents[index];
  if (document instanceof PlainDocument) {
    try {
      ((PlainDocument)document).remove(0, ((PlainDocument)document).getLength());
      ((PlainDocument)document).insertString(0, text, null);
    }
    catch (BadLocationException e) {
      throw new RuntimeException(e);
    }
  }
  else {
    WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {
      public void run() {
        ((Document)document).replaceString(0, ((Document)document).getTextLength(), text);
      }
    });
  }
}
项目:icedtea-web    文件:NetworkSettingsPanel.java   
/**
 * Creates a PlainDocument that only take numbers if it will create a valid port number.
 * @return PlainDocument which will ensure numeric values only and is a valid port number.
 */
public static PlainDocument getPortNumberDocument(){
    return new PlainDocument(){
        @Override
        public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
            if (str != null) {
                try {
                    Integer.valueOf(str);
                    int val = Integer.valueOf(this.getText(0, this.getLength()) + str);
                    if (val < 1 || val > 65535) { // Invalid port number if true
                        throw new NumberFormatException("Invalid port number");
                    }
                    super.insertString(offs, str, a);
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, Translator.R("CPInvalidPort"), Translator.R("CPInvalidPortTitle")
                            , JOptionPane.WARNING_MESSAGE);
                }
            }
        }
    };
}
项目:yummy-xml-UI    文件:ReadCardAction.java   
protected void setEditorDocument() {
    JTextField editor = (JTextField) owner.getEditor().getEditorComponent();
    editor.setDocument(new PlainDocument() {
        @Override
        public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
            if (str == null) {
                return;
            }

            if (str.length() < 2) {
                return;
            }
            super.insertString(offset, str, attr);
        }

        @Override
        public void remove(int offs, int len) throws BadLocationException {
            if (len < 2) {
                return;
            }
            super.remove(offs, len);
        }
    });
}