UndoableToggleApp.java Source code

Java tutorial

Introduction

Here is the source code for UndoableToggleApp.java

Source

/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/
// UndoableToggleApp.java
//A sample app showing the use of UndoableToggleEdit.
//

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;
import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoableEdit;

public class UndoableToggleApp extends JFrame {

    private UndoableEdit edit;

    private JButton undoButton;

    private JButton redoButton;

    // Create the main frame and everything in it.
    public UndoableToggleApp() {

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

        // Add our listener to each toggle button
        SimpleListener sl = new SimpleListener();
        tog.addActionListener(sl);
        cb.addActionListener(sl);
        radio.addActionListener(sl);

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

        // Create undo and redo buttons (initially disabled)
        undoButton = new JButton("Undo");
        redoButton = new JButton("Redo");
        undoButton.setEnabled(false);
        redoButton.setEnabled(false);

        // 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, but for redo this
        // time.
        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());
                }
            }
        });

        // Layout the undo/redo buttons
        Box undoRedoBox = new Box(BoxLayout.X_AXIS);
        undoRedoBox.add(Box.createGlue());
        undoRedoBox.add(undoButton);
        undoRedoBox.add(Box.createHorizontalStrut(2));
        undoRedoBox.add(redoButton);
        undoRedoBox.add(Box.createGlue());

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

    public class SimpleListener implements ActionListener {
        // When a toggle button is clicked, we create a new UndoableToggleEdit
        // (which replaces any previous edit). We then get the edit's undo/redo
        // names and set the undo/redo button labels. Finally, we
        // enable/disable these buttons by asking the edit what we are
        // allowed to do.
        public void actionPerformed(ActionEvent ev) {
            JToggleButton tb = (JToggleButton) ev.getSource();
            edit = new UndoableToggleEdit(tb);
            undoButton.setText(edit.getUndoPresentationName());
            redoButton.setText(edit.getRedoPresentationName());
            undoButton.getParent().validate();
            undoButton.setEnabled(edit.canUndo());
            redoButton.setEnabled(edit.canRedo());
        }
    }

    // Main program just creates the frame and displays it.
    public static void main(String[] args) {
        JFrame f = new UndoableToggleApp();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

//UndoableToggleEdit.java
//An UndoableEdit used to undo the pressing of a JToggleButton.
//

class UndoableToggleEdit extends AbstractUndoableEdit {

    private JToggleButton button;

    private boolean selected;

    // Create a new edit for a JToggleButton that has just been toggled.
    public UndoableToggleEdit(JToggleButton button) {
        this.button = button;
        selected = button.isSelected();
    }

    // Return a reasonable name for this edit.
    public String getPresentationName() {
        return "Toggle " + button.getText() + " " + (selected ? "on" : "off");
    }

    // Redo by setting the button state as it was initially.
    public void redo() throws CannotRedoException {
        super.redo();
        button.setSelected(selected);
    }

    // Undo by setting the button state to the opposite value.
    public void undo() throws CannotUndoException {
        super.undo();
        button.setSelected(!selected);
    }
}