Java Code Examples for org.netbeans.editor.BaseDocument#addUndoableEditListener()

The following examples show how to use org.netbeans.editor.BaseDocument#addUndoableEditListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TrailingWhitespaceRemoveTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void checkTrailingWhitespaceRemove(String policy, String result) throws Exception {
        MockServices.setServices(MockMimeLookup.class);
        MockMimeLookup.setInstances(MimePath.parse(""),
                new TrailingWhitespaceRemove.FactoryImpl()
        );
        
        RandomTestContainer container = DocumentTesting.initContainer(null);
        container.setName(this.getName());
//        container.putProperty(RandomTestContainer.LOG_OP, Boolean.TRUE);
//        container.putProperty(DocumentTesting.LOG_DOC, Boolean.TRUE);
        container.addOp(new Op());
        container.addCheck(new Check());

        // Init trailing spaces removal
//        Document doc = container.getInstance(Document.class);
        BaseDocument doc = new BaseDocument(BaseKit.class, false);
        UndoManager undoManager = new UndoManager();
        doc.addUndoableEditListener(undoManager);
        doc.putProperty(UndoManager.class, undoManager);

        container.putProperty(Document.class, doc); // Replace original doc

        RandomText randomText = RandomText.join(
                RandomText.lowerCaseAZ(1),
                RandomText.spaceTabNewline(1),
                RandomText.phrase(" \n ", 1)
        );
        container.putProperty(RandomText.class, randomText);
        
        // Do a fixed scenario
        DocumentTesting.insert(container.context(), 0, "abc\ndef\n\n123 \n567 \nghi");
        ModRootElement modRootElement = ModRootElement.get(doc);
        modRootElement.resetMods(null);

        DocumentTesting.insert(container.context(), 3, " a ");
        //  000000000011111111111222222222
        //  012345678901234567890123456789
        // "abc a ndefnn123 n567 nghi"
        DocumentTesting.insert(container.context(), 8, " \n ");
        //  000000000011111111111222222222
        //  012345678901234567890123456789
        // "abc a nd n efnn123 n567 nghi"
        DocumentTesting.insert(container.context(), 28, "\n ");
        //  000000000011111111111222222222
        //  012345678901234567890123456789
        // "abc a nd n efnn123 n567 nghin "
        removeTrailingWhitespace(container.context(), policy);
        // Should be
        //  000000000011111111111222222222
        //  012345678901234567890123456789
        // "abc andn efnn123 n567 nghin"
        assertEquals(result, doc.getText(0, doc.getLength()));
        

        RandomTestContainer.Round round = container.addRound();
        round.setOpCount(1000);
        round.setRatio(DocumentTesting.INSERT_CHAR, 6);
        round.setRatio(DocumentTesting.INSERT_TEXT, 3);
        round.setRatio(DocumentTesting.INSERT_PHRASE, 1);
        round.setRatio(DocumentTesting.REMOVE_CHAR, 3);
        round.setRatio(DocumentTesting.REMOVE_TEXT, 1);
        round.setRatio(DocumentTesting.UNDO, 1);
        round.setRatio(DocumentTesting.REDO, 1);
        round.setRatio(Op.NAME, 0.5d);
        container.run(1213202006348L);
//        container.run(0L); // Random operation

    }
 
Example 2
Source File: UndoRedoTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testIssue83963() throws Exception {
    SchemaModel model = Util.loadSchemaModel("resources/undoredo.xsd");
    BaseDocument doc = (BaseDocument) model.getModelSource().
            getLookup().lookup(BaseDocument.class);
    Schema s = model.getSchema();
    TestComponentListener listener = new TestComponentListener();
    model.addComponentListener(listener);
    UndoManager ur = new UndoManager();
    model.addUndoableEditListener(ur);
    
    String original = doc.getText(0, doc.getLength());
    //System.out.println("doc before add ComplexType"+doc.getText(0, doc.getLength()));
    GlobalComplexType gct = model.getFactory().createGlobalComplexType();
    model.startTransaction();
    s.addComplexType(gct);
    model.endTransaction();
    model.removeUndoableEditListener(ur);
    doc.addUndoableEditListener(ur);
    
    //System.out.println("doc after add ComplexType"+doc.getText(0, doc.getLength()));
    
    String stStr = "   <xsd:simpleType name=\"lend\">\n     <xsd:list>\n       <xsd:simpleType>\n         <xsd:restriction base=\"xsd:string\"/>\n       </xsd:simpleType>\n     </xsd:list>\n   </xsd:simpleType>";
    
    String afterInsert = doc.getText(0, doc.getLength());
    //System.out.println("doc after insert simpleType"+doc.getText(290, 10));
    // position was changing which is weird but doesn't matter for undo-redo testing
    int schemaTagPosition = afterInsert.length() - 10;
    doc.insertString(schemaTagPosition, "\n", null);
    model.sync();
    doc.insertString(schemaTagPosition + 1, stStr, null);
    model.sync();
    
    //System.out.println("doc after insert simpleType"+doc.getText(0, doc.getLength()));
    ur.undo();
    //System.out.println("doc after first undo"+doc.getText(0, doc.getLength()));
    ur.undo();
    assertEquals(afterInsert,doc.getText(0, doc.getLength()));
    //System.out.println("doc after second undo"+doc.getText(0, doc.getLength()));
    ur.undo();
    //System.out.println("doc after third undo"+doc.getText(0, doc.getLength()));
    assertEquals(original, doc.getText(0, doc.getLength()));
    
    ur.redo();
    assertEquals(afterInsert,doc.getText(0, doc.getLength()));
    //System.out.println("doc after first redo"+doc.getText(0, doc.getLength()));
    ur.redo();
    //System.out.println("doc after second redo"+doc.getText(0, doc.getLength()));
    ur.redo();
    //System.out.println("doc after third redo"+doc.getText(0, doc.getLength()));
}