Example usage for javax.swing.undo StateEdit StateEdit

List of usage examples for javax.swing.undo StateEdit StateEdit

Introduction

In this page you can find the example usage for javax.swing.undo StateEdit StateEdit.

Prototype

public StateEdit(StateEditable anObject) 

Source Link

Document

Create and return a new StateEdit.

Usage

From source file:UndoableDrawingPanel.java

public UndoableDrawingPanel() {
    MouseListener mouseListener = new MouseAdapter() {
        public void mouseReleased(MouseEvent mouseEvent) {
            StateEdit stateEdit = new StateEdit(UndoableDrawingPanel.this);
            polygon.addPoint(mouseEvent.getX(), mouseEvent.getY());
            stateEdit.end();//from  w  ww  .ja va2s . co  m
            undoableEditSupport.postEdit(stateEdit);
            repaint();
        }
    };
    addMouseListener(mouseListener);
}

From source file:UndoableDrawingPanel2.java

public UndoableDrawingPanel2() {
    MouseListener mouseListener = new MouseAdapter() {
        public void mouseReleased(MouseEvent mouseEvent) {
            StateEdit stateEdit = new StateEdit(UndoableDrawingPanel2.this);
            polygon.addPoint(mouseEvent.getX(), mouseEvent.getY());
            stateEdit.end();//www  . j a  va  2s .c o m
            undoableEditSupport.postEdit(stateEdit);
            repaint();
        }
    };
    addMouseListener(mouseListener);
}

From source file:UndoableToggleApp4.java

public UndoableToggleApp4() {
    SimpleListener sl = new SimpleListener();
    tog.addActionListener(sl);/*  ww w  .j  a v a 2 s. c  om*/
    cb.addActionListener(sl);
    radio.addActionListener(sl);

    Box buttonBox = new Box(BoxLayout.Y_AXIS);
    buttonBox.add(tog);
    buttonBox.add(cb);
    buttonBox.add(radio);

    startButton.setEnabled(true);
    endButton.setEnabled(false);
    undoButton.setEnabled(false);
    redoButton.setEnabled(false);

    startButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            edit = new StateEdit(UndoableToggleApp4.this);
            startButton.setEnabled(false);
            endButton.setEnabled(true);
            // undoButton.setEnabled(edit.canUndo());
            //
            // NOTE: We really don't want to be able to undo until end() is pressed,
            // but StateEdit does not enforce this for us!
            undoButton.setEnabled(false);
            redoButton.setEnabled(edit.canRedo());
        }
    });

    endButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            edit.end();
            startButton.setEnabled(true);
            endButton.setEnabled(false);
            undoButton.setEnabled(edit.canUndo());
            redoButton.setEnabled(edit.canRedo());
        }
    });

    // Add a listener to the undo button. It attempts to call undo() on the
    // current edit, then enables/disables the undo/redo buttons as appropriate.
    undoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            try {
                edit.undo();
            } catch (CannotUndoException ex) {
                ex.printStackTrace();
            } finally {
                undoButton.setEnabled(edit.canUndo());
                redoButton.setEnabled(edit.canRedo());
            }
        }
    });

    redoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            try {
                edit.redo();
            } catch (CannotRedoException ex) {
                ex.printStackTrace();
            } finally {
                undoButton.setEnabled(edit.canUndo());
                redoButton.setEnabled(edit.canRedo());
            }
        }
    });

    // Lay out the state/end and undo/redo buttons.
    Box undoRedoBox = new Box(BoxLayout.X_AXIS);
    undoRedoBox.add(Box.createGlue());
    undoRedoBox.add(startButton);
    undoRedoBox.add(Box.createHorizontalStrut(2));
    undoRedoBox.add(endButton);
    undoRedoBox.add(Box.createHorizontalStrut(2));
    undoRedoBox.add(undoButton);
    undoRedoBox.add(Box.createHorizontalStrut(2));
    undoRedoBox.add(redoButton);
    undoRedoBox.add(Box.createGlue());

    // Lay out the main frame.
    Container content = getContentPane();
    content.setLayout(new BorderLayout());
    content.add(buttonBox, BorderLayout.CENTER);
    content.add(undoRedoBox, BorderLayout.SOUTH);
    setSize(400, 150);
}

From source file:UndoableToggleApp4.java

public UndoableToggleApp4() {

    // Create some toggle buttons (and subclasses).
    tog = new JToggleButton("ToggleButton");
    cb = new JCheckBox("CheckBox");
    radio = new JRadioButton("RadioButton");

    // Add our listener to the buttons.
    SimpleListener sl = new SimpleListener();
    tog.addActionListener(sl);//from  w w w. ja  va  2s  . co  m
    cb.addActionListener(sl);
    radio.addActionListener(sl);

    // Lay out the buttons.
    Box buttonBox = new Box(BoxLayout.Y_AXIS);
    buttonBox.add(tog);
    buttonBox.add(cb);
    buttonBox.add(radio);

    // Create undo, redo, start, and end buttons.
    startButton = new JButton("Start");
    endButton = new JButton("End");
    undoButton = new JButton("Undo");
    redoButton = new JButton("Redo");
    startButton.setEnabled(true);
    endButton.setEnabled(false);
    undoButton.setEnabled(false);
    redoButton.setEnabled(false);

    // Add a listener to the start button. It creates a new StateEdit,
    // passing in this frame as the StateEditable.
    startButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            edit = new StateEdit(UndoableToggleApp4.this);
            startButton.setEnabled(false);
            endButton.setEnabled(true);
            //undoButton.setEnabled(edit.canUndo());
            //
            // NOTE: We really don't want to be able to undo until end() is
            // pressed,
            // but StateEdit does not enforce this for us!
            undoButton.setEnabled(false);
            redoButton.setEnabled(edit.canRedo());
        }
    });

    // Add a listener to the end button. It will call end() on the
    // StateEdit.
    endButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            edit.end();
            startButton.setEnabled(true);
            endButton.setEnabled(false);
            undoButton.setEnabled(edit.canUndo());
            redoButton.setEnabled(edit.canRedo());
        }
    });

    // Add a listener to the undo button. It attempts to call undo() on the
    // current edit, then enables/disables the undo/redo buttons as
    // appropriate.
    undoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            try {
                edit.undo();
            } catch (CannotUndoException ex) {
                ex.printStackTrace();
            } finally {
                undoButton.setEnabled(edit.canUndo());
                redoButton.setEnabled(edit.canRedo());
            }
        }
    });

    // Add a redo listener: just like the undo listener.
    redoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            try {
                edit.redo();
            } catch (CannotRedoException ex) {
                ex.printStackTrace();
            } finally {
                undoButton.setEnabled(edit.canUndo());
                redoButton.setEnabled(edit.canRedo());
            }
        }
    });

    // Lay out the state/end and undo/redo buttons.
    Box undoRedoBox = new Box(BoxLayout.X_AXIS);
    undoRedoBox.add(Box.createGlue());
    undoRedoBox.add(startButton);
    undoRedoBox.add(Box.createHorizontalStrut(2));
    undoRedoBox.add(endButton);
    undoRedoBox.add(Box.createHorizontalStrut(2));
    undoRedoBox.add(undoButton);
    undoRedoBox.add(Box.createHorizontalStrut(2));
    undoRedoBox.add(redoButton);
    undoRedoBox.add(Box.createGlue());

    // Lay out the main frame.
    Container content = getContentPane();
    content.setLayout(new BorderLayout());
    content.add(buttonBox, BorderLayout.CENTER);
    content.add(undoRedoBox, BorderLayout.SOUTH);
    setSize(400, 150);
}