Example usage for javax.swing.event UndoableEditEvent UndoableEditEvent

List of usage examples for javax.swing.event UndoableEditEvent UndoableEditEvent

Introduction

In this page you can find the example usage for javax.swing.event UndoableEditEvent UndoableEditEvent.

Prototype

public UndoableEditEvent(Object source, UndoableEdit edit) 

Source Link

Document

Constructs an UndoableEditEvent object.

Usage

From source file:UndoManagerDemo.java

public UndoManagerDemo() {
    super("Undo/Redo Demo");

    undoButton.setEnabled(false);/*  w  w  w.  j  a v  a2  s .c  om*/
    redoButton.setEnabled(false);

    JPanel buttonPanel = new JPanel(new GridLayout());
    buttonPanel.add(undoButton);
    buttonPanel.add(redoButton);

    getContentPane().add(buttonPanel, BorderLayout.NORTH);
    getContentPane().add(canvas, BorderLayout.CENTER);

    canvas.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            Point point = new Point(e.getX(), e.getY());
            pointVector.addElement(point);

            undoManager.undoableEditHappened(
                    new UndoableEditEvent(canvas, new UndoablePaintSquare(point, pointVector)));

            undoButton.setText(undoManager.getUndoPresentationName());
            redoButton.setText(undoManager.getRedoPresentationName());
            undoButton.setEnabled(undoManager.canUndo());
            redoButton.setEnabled(undoManager.canRedo());
            canvas.repaint();
        }
    });

    undoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                undoManager.undo();
            } catch (CannotRedoException cre) {
                cre.printStackTrace();
            }
            canvas.repaint();
            undoButton.setEnabled(undoManager.canUndo());
            redoButton.setEnabled(undoManager.canRedo());
        }
    });

    redoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                undoManager.redo();
            } catch (CannotRedoException cre) {
                cre.printStackTrace();
            }
            canvas.repaint();
            undoButton.setEnabled(undoManager.canUndo());
            redoButton.setEnabled(undoManager.canRedo());
        }
    });

    setSize(400, 300);
    setVisible(true);
}

From source file:UndoableToggleApp3.java

protected void fireActionPerformed(ActionEvent ev) {

    // Fire the ActionEvent as usual.
    super.fireActionPerformed(ev);

    if (listener != null) {
        listener.undoableEditHappened(new UndoableEditEvent(this, new UndoableToggleEdit(this)));
    }/*from   ww w  .  j a v a  2  s.  c  om*/
}

From source file:com.hexidec.ekit.component.ExtendedHTMLDocument.java

public void replaceAttributes(Element e, AttributeSet a, Tag tag) {
    if ((e != null) && (a != null)) {
        try {//  w ww  .j a v a2  s.  co m
            writeLock();
            int start = e.getStartOffset();
            DefaultDocumentEvent changes = new DefaultDocumentEvent(start, e.getEndOffset() - start,
                    DocumentEvent.EventType.CHANGE);
            AttributeSet sCopy = a.copyAttributes();
            changes.addEdit(new AttributeUndoableEdit(e, sCopy, false));
            MutableAttributeSet attr = (MutableAttributeSet) e.getAttributes();
            Enumeration aNames = attr.getAttributeNames();
            Object value;
            Object aName;
            while (aNames.hasMoreElements()) {
                aName = aNames.nextElement();
                value = attr.getAttribute(aName);
                if (value != null && !value.toString().equalsIgnoreCase(tag.toString())) {
                    attr.removeAttribute(aName);
                }
            }
            attr.addAttributes(a);
            changes.end();
            fireChangedUpdate(changes);
            fireUndoableEditUpdate(new UndoableEditEvent(this, changes));
        } finally {
            writeUnlock();
        }
    }
}

From source file:com.hexidec.ekit.component.ExtendedHTMLDocument.java

@Override
public void setParagraphAttributes(int offset, int length, AttributeSet s, boolean replace) {
    for (HTMLDocumentBehavior b : behaviors) {
        s = b.beforeSetParagraphAttributes(this, offset, length, s, replace);
    }//  w  w w .  j a v a  2 s  .com

    try {
        writeLock();
        // Make sure we send out a change for the length of the paragraph.
        int end = Math.min(offset + length, getLength());
        Element e = getParagraphElement(offset);
        offset = e.getStartOffset();
        e = getParagraphElement(end);
        length = Math.max(0, e.getEndOffset() - offset);
        DefaultDocumentEvent changes = new DefaultDocumentEvent(offset, length, DocumentEvent.EventType.CHANGE);
        AttributeSet sCopy = s.copyAttributes();
        int lastEnd = Integer.MAX_VALUE;
        for (int pos = offset; pos <= end; pos = lastEnd) {
            Element paragraph = getParagraphElement(pos);
            if (lastEnd == paragraph.getEndOffset()) {
                lastEnd++;
            } else {
                lastEnd = paragraph.getEndOffset();
            }
            MutableAttributeSet attr = (MutableAttributeSet) paragraph.getAttributes();
            changes.addEdit(new AttributeUndoableEdit(paragraph, sCopy, replace));
            if (replace) {
                attr.removeAttributes(attr);
            }

            DocumentUtil.copyAllAttributesRemovingMarked(attr, s);
        }
        changes.end();
        fireChangedUpdate(changes);
        fireUndoableEditUpdate(new UndoableEditEvent(this, changes));
    } finally {
        writeUnlock();
    }
}

From source file:org.fife.ui.rsyntaxtextarea.EOLPreservingRSyntaxDocument.java

@Override
protected void fireUndoableEditUpdate(UndoableEditEvent evt) {
    if (lastEdit != null) {
        CompoundEdit edit = new CompoundEdit();
        edit.addEdit(evt.getEdit());/*  w ww  .ja va2  s  . c o m*/
        edit.addEdit(lastEdit);
        edit.end();
        super.fireUndoableEditUpdate(new UndoableEditEvent(evt.getSource(), edit));
        lastEdit = null;
    } else {
        super.fireUndoableEditUpdate(evt);
    }
}

From source file:org.jcurl.core.ui.ChangeManager.java

/**
 * /* w  ww  .  j a  v  a  2  s.  c  om*/
 * @param <E>
 * @param anEdit
 * @return {@link UndoManager#addEdit(UndoableEdit)}
 */
private boolean addEdit(final UndoableEdit anEdit) {
    final boolean ret = undoer.addEdit(anEdit);
    for (final UndoableEditListener elem : listeners)
        elem.undoableEditHappened(new UndoableEditEvent(undoer, anEdit));
    return ret;
}

From source file:org.jcurl.core.ui.ChangeManager.java

/** Delegate to {@link UndoManager#discardAllEdits()}. */
public void discardAllEdits() {
    undoer.discardAllEdits();//from   ww w  . j  av a2 s.  c o  m
    for (final UndoableEditListener elem : listeners)
        elem.undoableEditHappened(new UndoableEditEvent(undoer, null));
}

From source file:org.jcurl.core.ui.ChangeManager.java

public void redo() {
    undoer.redo();//w w w .  j  av a 2  s.  com
    for (final UndoableEditListener elem : listeners)
        elem.undoableEditHappened(new UndoableEditEvent(undoer, null));
}

From source file:org.jcurl.core.ui.ChangeManager.java

public void undo() {
    undoer.undo();/*from   w w w  .  j  a  v  a2  s . c o m*/
    for (final UndoableEditListener elem : listeners)
        elem.undoableEditHappened(new UndoableEditEvent(undoer, null));
}

From source file:org.shaman.rpg.editor.dialog.DialogVisualElement.java

public void deleteCommand(CommandPanel toDelete) {
    int panelIndex = ArrayUtils.indexOf(contentPanel.getComponents(), toDelete);
    int commandIndex = dialog.getCommands().indexOf(toDelete.getCommand());
    if (panelIndex < 0 || commandIndex < 0) {
        return;//from w  w  w . j  a v  a  2  s  .c o  m
    }
    contentPanel.remove(panelIndex);
    dialog.getCommands().remove(commandIndex);
    commandPanels.remove(panelIndex);
    contentPanel.invalidate();
    contentPanel.validate();
    contentPanel.repaint();
    undoRedo.undoableEditHappened(new UndoableEditEvent(dialog,
            new DeleteCommandUndoableEdit(commandIndex, toDelete.getCommand(), panelIndex, toDelete)));
    LOG.info("delete " + toDelete);
}