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

The following examples show how to use javax.swing.text.Document#removeUndoableEditListener() . 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
public void startNewDocument() {
	Document oldDoc = textPane.getDocument();
	if (oldDoc != null)
		oldDoc.removeUndoableEditListener(undoHandler);
	HTMLEditorKit editorKit = new HTMLEditorKit();
	document = (HTMLDocument) editorKit.createDefaultDocument();
	textPane.setDocument(document);
	currentFile = null;
	setTitle("HTMLDocumentEditor");
	textPane.getDocument().addUndoableEditListener(undoHandler);
	resetUndoManager();
}
 
Example 2
public void openDocument() {
	try {
		File current = new File(".");
		JFileChooser chooser = new JFileChooser(current);
		chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		chooser.setFileFilter(new HTMLFileFilter());
		int approval = chooser.showSaveDialog(this);
		if (approval == JFileChooser.APPROVE_OPTION) {
			currentFile = chooser.getSelectedFile();
			setTitle(currentFile.getName());
			FileReader fr = new FileReader(currentFile);
			Document oldDoc = textPane.getDocument();
			if (oldDoc != null)
				oldDoc.removeUndoableEditListener(undoHandler);
			HTMLEditorKit editorKit = new HTMLEditorKit();
			document = (HTMLDocument) editorKit.createDefaultDocument();
			editorKit.read(fr, document, 0);
			document.addUndoableEditListener(undoHandler);
			textPane.setDocument(document);
			resetUndoManager();
		}
	} catch (BadLocationException ble) {
		System.err.println("BadLocationException: " + ble.getMessage());
	} catch (FileNotFoundException fnfe) {
		System.err.println("FileNotFoundException: " + fnfe.getMessage());
	} catch (IOException ioe) {
		System.err.println("IOException: " + ioe.getMessage());
	}
}
 
Example 3
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());
}