Java Code Examples for javax.swing.text.Document#addUndoableEditListener()

The following examples show how to use javax.swing.text.Document#addUndoableEditListener() . These examples are extracted from open source projects. 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 Project: netbeans   File: PlainDocumentTest.java    License: Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 2
Source Project: netbeans   File: Editor.java    License: Apache License 2.0 6 votes vote down vote up
private void addEditorPane( JEditorPane pane, Icon icon, File file, boolean created, boolean focus ) {
    final JComponent c = (pane.getUI() instanceof BaseTextUI) ?
    Utilities.getEditorUI(pane).getExtComponent() : new JScrollPane( pane );
    Document doc = pane.getDocument();
    
    doc.addDocumentListener( new MarkingDocumentListener( c ) );
    doc.putProperty( FILE, file );
    doc.putProperty( CREATED, created  ? Boolean.TRUE : Boolean.FALSE );
    
    UndoManager um = new UndoManager();
    doc.addUndoableEditListener( um );
    doc.putProperty( BaseDocument.UNDO_MANAGER_PROP, um );
    
    com2text.put( c, pane );
    tabPane.addTab( file.getName(), icon, c, file.getAbsolutePath() );
    if (focus) {
        tabPane.setSelectedComponent( c );
        pane.requestFocus();
    }
}
 
Example 3
private KeyMasterTextArea(String text) {
	super(text);

	setFont(defaultFont);
	setName("EDITOR");
	setWrapStyleWord(false);
	Document document = getDocument();
	document.addUndoableEditListener(e -> {
		UndoableEdit item = e.getEdit();
		undoStack.push(item);
		redoStack.clear();
		updateUndoRedoAction();
	});
	setCaretPosition(0);
}
 
Example 4
Source Project: netbeans   File: DocumentContentTesting.java    License: Apache License 2.0 4 votes vote down vote up
public static RandomTestContainer createContainer() {
    RandomTestContainer container = new RandomTestContainer();
    container.addCheck(new DocumentContentCheck());
    Document doc = getDocument(container);
    Document expectedDoc = getExpectedDocument(container);
    if (expectedDoc == null) {
        expectedDoc = new ExpectedDocument();
        container.putProperty(ExpectedDocument.class, expectedDoc);
    }
    if (doc == null) {
        doc = new TestEditorDocument();
        container.putProperty(Document.class, doc);
    }
    ((ExpectedDocument)expectedDoc).setTestDocument((TestEditorDocument)doc);
    UndoManager undoManager = getUndoManager(container);
    if (undoManager == null) {
        undoManager = new UndoMgr(container);
        container.putProperty(UndoManager.class, undoManager);
        doc.putProperty(UndoManager.class, undoManager);
        // Add them here to be sure they are added just once (not correct - should analyze existing listeners)
        doc.addUndoableEditListener(undoManager);
        expectedDoc.addUndoableEditListener(undoManager);
    }
    container.putProperty(UNDO_REDO_INVERSE_RATIO, 0.3f);

    // Init a default random text if not yet inited (can be reinited later)
    if (container.getInstanceOrNull(RandomText.class) == null) {
        RandomText randomText = RandomText.join(
                RandomText.lowerCaseAZ(3),
                RandomText.spaceTabNewline(1),
                RandomText.phrase("   ", 1),
                RandomText.phrase("\n\n", 1),
                RandomText.phrase("\t\tabcdef\t", 1));
        container.putProperty(RandomText.class, randomText);
    }

    container.addOp(new InsertOp(INSERT_CHAR));
    container.addOp(new InsertOp(INSERT_TEXT));
    container.addOp(new InsertOp(INSERT_PHRASE));
    container.addOp(new RemoveOp(REMOVE_CHAR));
    container.addOp(new RemoveOp(REMOVE_TEXT));
    container.addOp(new UndoRedoOp(UNDO));
    container.addOp(new UndoRedoOp(REDO));
    container.addOp(new PositionOp(CREATE_POSITION));
    container.addOp(new PositionOp(CREATE_BACKWARD_BIAS_POSITION));
    container.addOp(new PositionOp(RELEASE_POSITION));
    return container;
}
 
Example 5
Source Project: netbeans   File: AbstractModelTest.java    License: Apache License 2.0 4 votes vote down vote up
public void testSourceEditSyncUndo() throws Exception {
    defaultSetup();
    UndoManager urListener = new UndoManager();
    Document doc = mModel.getBaseDocument();
    mModel.addUndoableEditListener(urListener);
    
    mModel.startTransaction();
    B b2 = new B(mModel, 2);
    mModel.getRootComponent().addAfter(b2.getName(), b2, TestComponent3._A);
    mModel.endTransaction();
    assertEquals("first edit setup", 2, mModel.getRootComponent().getChildren(B.class).size());
    
    // see fix for issue 83963, with this fix we need coordinate edits from
    // on XDM model and on document buffer.  This reduce XDM undo/redo efficiency,
    // but is the best we can have to satisfy fine-grained text edit undo requirements.
    mModel.removeUndoableEditListener(urListener);
    doc.addUndoableEditListener(urListener);
    
    Util.setDocumentContentTo(doc, "resources/test2.xml");
    assertEquals("undo sync", 1, mModel.getRootComponent().getChildren(C.class).size());
    mModel.sync();
    doc.removeUndoableEditListener(urListener);
    
    assertEquals("sync setup", 1, mModel.getRootComponent().getChildren(B.class).size());
    assertEquals("sync setup", 0, mModel.getRootComponent().getChildren(C.class).size());
    
    // setDocumentContentTo did delete all, then insert, hence 2 undo's'
    urListener.undo(); urListener.undo(); 
    mModel.sync(); // the above undo's are just on document buffer, needs sync (inefficient).
    assertEquals("undo sync", 1, mModel.getRootComponent().getChildren(C.class).size());
    assertEquals("undo sync", 2, mModel.getRootComponent().getChildren(B.class).size());

    urListener.undo();
    assertEquals("undo first edit before sync", 1, mModel.getRootComponent().getChildren(B.class).size());

    urListener.redo();
    assertEquals("redo first edit", 1, mModel.getRootComponent().getChildren(C.class).size());
    assertEquals("redo first edit", 2, mModel.getRootComponent().getChildren(B.class).size());

    // needs to back track the undo's, still needs sync'
    urListener.redo(); urListener.redo();
    mModel.sync();
    assertEquals("redo to sync", 1, mModel.getRootComponent().getChildren(B.class).size());
    assertEquals("redo to sync", 0, mModel.getRootComponent().getChildren(C.class).size());
}
 
Example 6
private void performTest(String sourceCode, int offset, KeyEvent[] kes, int selectionEnd, String golden, boolean stillInRename) throws Exception {
        clearWorkDir();
        
        FileObject root = FileUtil.toFileObject(getWorkDir());
        
        FileObject sourceDir = root.createFolder("src");
        FileObject buildDir = root.createFolder("build");
        FileObject cacheDir = root.createFolder("cache");
        FileObject testDir  = sourceDir.createFolder("test");
        
        FileObject source = testDir.createData("Test.java");
        
        TestUtilities.copyStringToFile(source, sourceCode.replaceFirst(Pattern.quote("|"), ""));
        
        SourceUtilsTestUtil.prepareTest(sourceDir, buildDir, cacheDir, new FileObject[0]);
        SourceUtilsTestUtil.compileRecursively(sourceDir);
        
        DataObject od = DataObject.find(source);
        
        assertTrue(od instanceof JavaDataObject);
        
        EditorCookie ec = od.getCookie(EditorCookie.class);
        Document doc = ec.openDocument();
        
        assertTrue(doc instanceof BaseDocument);
        
        UndoManager um = new UndoManager();

        doc.addUndoableEditListener(um);
        doc.putProperty(BaseDocument.UNDO_MANAGER_PROP, um);
        doc.putProperty(Language.class, JavaTokenId.language());
        doc.putProperty("mimeType", "text/x-java");
        
        C p = new C();
        
//        p.setEditorKit(new JavaKit());
        
        p.setDocument(doc);
        
        p.setCaretPosition(sourceCode.indexOf('|'));
        
        InstantRenamePerformer.invokeInstantRename(p);
        
        p.setCaretPosition(offset);

        if (selectionEnd != (-1)) {
            p.moveCaretPosition(selectionEnd);
        }
        
        processKeyevents(p, kes);
        
        assertEquals(stillInRename, p.getClientProperty(InstantRenamePerformer.class) != null);
        assertEquals(golden, doc.getText(0, doc.getLength()));
    }