package undoredo;
/*
* UndoDemo.java
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.undo.*;
public class UndoDemo extends JFrame {
private JMenu mEdit;
private JMenuBar menubar;
private JMenuItem miRedo;
private JMenuItem miResize;
private JMenuItem miUndo;
private BildPanel bildPanel;
private Image bild;
private int x, y, w, h;
/**
* undo system elements:
*/
private UndoManager undoManager; // history list
private UndoableEditSupport undoSupport; // event support
public UndoDemo() {
initComponents();
bildPanel = new BildPanel();
add(bildPanel);
// initialize the undo-redo system:
undoManager= new UndoManager();
undoSupport = new UndoableEditSupport();
undoSupport.addUndoableEditListener(new UndoAdapter());
refreshUndoRedo();
}
private void initComponents() {
menubar = new JMenuBar();
mEdit = new JMenu("Edit");
miUndo = new JMenuItem();
miRedo = new JMenuItem();
miResize = new JMenuItem("Bildgroesse");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
miUndo.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
miUndoActionPerformed(evt);
}
});
mEdit.add(miUndo);
miRedo.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
miRedoActionPerformed(evt);
}
});
mEdit.add(miRedo);
miResize.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
miResizeActionPerformed(evt);
}
});
mEdit.add(miResize);
menubar.add(mEdit);
setJMenuBar(menubar);
setExtendedState(JFrame.MAXIMIZED_BOTH);
}
private void miResizeActionPerformed(final ActionEvent evt) {
int w = Integer.parseInt(JOptionPane.showInputDialog("Breite"));
int h = Integer.parseInt(JOptionPane.showInputDialog("Höhe"));
if(w != this.w && h != this.h) {
// record the effect:
UndoableEdit edit = new SizeEdit(w, h, this.w, this.h);
// notify the listeners:
undoSupport.postEdit( edit );
}
this.w = w;
this.h = h;
bildPanel.repaint();
}
private void miRedoActionPerformed(final ActionEvent evt) {
undoManager.redo();
refreshUndoRedo();
}
private void miUndoActionPerformed(final ActionEvent evt) {
undoManager.undo();
refreshUndoRedo();
}
/**
* An UndoableEditListener. The listener is notified when
* an undoable edit occurs(e.g. add to the list)
* The listener extracts the edit from the event, add it
* to the UndoManager, and refresh the GUI
*/
private class UndoAdapter implements UndoableEditListener {
public void undoableEditHappened(final UndoableEditEvent evt) {
UndoableEdit edit = evt.getEdit();
undoManager.addEdit( edit );
refreshUndoRedo();
}
}
/**
* This method is called after each undoable operation
* in order to refresh the presentation state of the
* undo/redo GUI
*/
public void refreshUndoRedo() {
// refresh undo
miUndo.setText( undoManager.getUndoPresentationName() );
miUndo.setEnabled( undoManager.canUndo() );
// refresh redo
miRedo.setText( undoManager.getRedoPresentationName() );
miRedo.setEnabled( undoManager.canRedo() );
if(bildPanel != null) bildPanel.repaint();
}
public static void main(String args[]) {new UndoDemo().setVisible(true);}
class BildPanel extends JPanel {
public BildPanel(){
bild = new ImageIcon("c:\\Picture1.jpg").getImage();
w = bild.getWidth(null);
h = bild.getHeight(null);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bild, x, y, w, h, null);
}
}
/*
* SizeEdit
* UndoableEdit for the size-property
*/
class SizeEdit extends AbstractUndoableEdit {
private int wNew, hNew, wOld, hOld;
public SizeEdit( final int wNew, final int hNew, final int wOld, final int hOld) {
this.wNew = wNew;
this.hNew = hNew;
this.wOld = wOld;
this.hOld = hOld;
}
public void undo() throws CannotUndoException {
super.undo();
w = wOld;
h = hOld;
bildPanel.repaint();
}
public void redo() throws CannotRedoException {
super.redo();
w = wNew;
h = hNew;
bildPanel.repaint();
}
public boolean canUndo() {return true;}
public boolean canRedo() {return true;}
public String getPresentationName() {return "Bildgrösse";}
}
}