Java Code Examples for javax.swing.undo.CompoundEdit#end()

The following examples show how to use javax.swing.undo.CompoundEdit#end() . 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: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void replace(int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
  if (length == 0) {
    // System.out.println("insert");
    super.replace(offset, length, text, attrs);
  } else {
    // System.out.println("replace");
    compoundEdit = new CompoundEdit();
    super.replace(offset, length, text, attrs);
    compoundEdit.end();
    super.fireUndoableEditUpdate(new UndoableEditEvent(this, compoundEdit));
    compoundEdit = null;
  }
}
 
Example 2
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
  if (length == 0) {
    fb.insertString(offset, text, attrs);
  } else {
    compoundEdit = new CompoundEdit();
    fb.replace(offset, length, text, attrs);
    compoundEdit.end();
    addEdit(compoundEdit);
    compoundEdit = null;
  }
}
 
Example 3
Source File: UndoRedoWrappingExampleTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testDeleteEachTenthCharFromDocument() throws Exception {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < 20; i++) {
        for (int j = 0; j < 10; j++) {
            sb.append((char)('0' + j));
        }
    }
    
    content = sb.toString();
    
    StyledDocument doc = support.openDocument();
    assertEquals("200 chars there", 200, doc.getLength());
    
    CompoundEdit bigEdit = new CompoundEdit();
    support.getUndoRedo().undoableEditHappened(new UndoableEditEvent(doc, bigEdit));
    
    assertTrue("Big edit will consume other edits", bigEdit.isInProgress());
    
    for (int i = 199; i >= 0; i -= 10) {
        doc.remove(i, 1);
    }
    
    assertTrue("Big edit was still in consume mode", bigEdit.isInProgress());
    bigEdit.end();
    assertFalse("Big edit is over", bigEdit.isInProgress());
    assertTrue("Document is modified", modified);
    assertTrue("We can undo", support.getUndoRedo().canUndo());
    
    if (doc.getText(0, doc.getLength()).indexOf('9') != -1) {
        fail("There should be no 9 in the doc:\n" + doc.getText(0, doc.getLength()));
    }
    
    support.getUndoRedo().undo();
    
    assertEquals("Again 200", 200, doc.getLength());
    assertFalse("Not modified anymore", modified);

    
    assertTrue("We can redo", support.getUndoRedo().canRedo());
    support.getUndoRedo().redo();
    
    assertTrue("Document is modified", modified);
    assertTrue("We can undo", support.getUndoRedo().canUndo());
    
    if (doc.getText(0, doc.getLength()).indexOf('9') != -1) {
        fail("There should be no 9 in the doc:\n" + doc.getText(0, doc.getLength()));
    }
    
}
 
Example 4
Source File: UndoRedoWrappingExampleTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testDeleteEachTenthCharOnModifiedDocument() throws Exception {
    
    StyledDocument doc = support.openDocument();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < 20; i++) {
        for (int j = 0; j < 10; j++) {
            sb.append((char)('0' + j));
        }
    }
    assertEquals("empty", 0, doc.getLength());
    doc.insertString(0, sb.toString(), null);
    assertEquals("200 chars there", 200, doc.getLength());
    
    CompoundEdit bigEdit = new CompoundEdit();
    support.getUndoRedo().undoableEditHappened(new UndoableEditEvent(doc, bigEdit));
    
    assertTrue("Big edit will consume other edits", bigEdit.isInProgress());
    
    for (int i = 199; i >= 0; i -= 10) {
        doc.remove(i, 1);
    }
    
    assertTrue("Big edit was still in consume mode", bigEdit.isInProgress());
    bigEdit.end();
    assertFalse("Big edit is over", bigEdit.isInProgress());
    assertTrue("Document is modified", modified);
    assertTrue("We can undo", support.getUndoRedo().canUndo());
    
    if (doc.getText(0, doc.getLength()).indexOf('9') != -1) {
        fail("There should be no 9 in the doc:\n" + doc.getText(0, doc.getLength()));
    }
    
    support.getUndoRedo().undo();
    
    assertEquals("Again 200", 200, doc.getLength());
    assertTrue("Still modified", modified);

    
    assertTrue("We can redo", support.getUndoRedo().canRedo());
    support.getUndoRedo().redo();
    
    assertTrue("Document is modified", modified);
    assertTrue("We can undo", support.getUndoRedo().canUndo());
    
    if (doc.getText(0, doc.getLength()).indexOf('9') != -1) {
        fail("There should be no 9 in the doc:\n" + doc.getText(0, doc.getLength()));
    }
    
}
 
Example 5
Source File: EditToBeUndoneRedoneTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testUndoRedoUndoEdits() throws Exception {

        final StyledDocument d = support.openDocument();

        UndoRedo.Manager ur = support.getUndoRedo();
        UndoRedoManager urMgr = null;

        if (ur instanceof UndoRedoManager) {
            urMgr = (UndoRedoManager) ur;
        }

        d.insertString(d.getLength(), "a", null);
        final CompoundEdit bigEdit = new CompoundEdit();
        d.insertString(d.getLength(), "b", null);
        bigEdit.end();
        support.saveDocument();

        // setting the property to populate urMgr.onSaveTasksEdit field
        d.putProperty("beforeSaveRunnable", new Runnable() {

            public void run() {
                Runnable beforeSaveStart = (Runnable) d.getProperty("beforeSaveStart");
                if (beforeSaveStart != null) {
                    beforeSaveStart.run();
                    support.getUndoRedo().undoableEditHappened(new UndoableEditEvent(d, bigEdit));
                }
            }
        });

        urMgr.undo();
        support.saveDocument();
        d.putProperty("beforeSaveRunnable", null);
        assertEquals("after undo data", "a", d.getText(0, d.getLength()));

        urMgr.redo();
        support.saveDocument();
        assertEquals("after redo data", "ab", d.getText(0, d.getLength()));

        urMgr.undo();
        assertEquals("after redo data", "a", d.getText(0, d.getLength()));
    }