package vg.userInterface.utilPlugins;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Observable;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import vg.core.AMenuItem;
import vg.core.IGraphView;
import vg.core.event.AUIEvent;
import vg.core.event.UIEventChangeView;
import vg.core.exception.PluginException;
import vg.core.plugin.IPlugin;
import vg.core.plugin.PluginParameter;
public class UndoAndRedo implements IPlugin{
private JMenuItem redoMenuItem, undoMenuItem;
private PluginParameter param;
private IGraphView view;
//-------------------------------------------
public void install(PluginParameter param) throws PluginException {
this.param = param;
this.view = null;
this.redoMenuItem = new JMenuItem("Redo");
this.undoMenuItem = new JMenuItem("Undo");
//---------------------------------------
this.redoMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(UndoAndRedo.this.view!=null) {
UndoAndRedo.this.view.redo();
}
}
});
this.undoMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(UndoAndRedo.this.view!=null) {
UndoAndRedo.this.view.undo();
}
}
});
this.undoMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_DOWN_MASK));
this.redoMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_DOWN_MASK));
//---------------------------------------
this.param.userInterface.addMenuItem(new AMenuItem(this.undoMenuItem) {
public void update(Observable o, Object arg) {
changeView(arg);
}
}, "edit");
this.param.userInterface.addMenuItem(new AMenuItem(this.redoMenuItem) {
public void update(Observable o, Object arg) {
changeView(arg);
}
}, "edit");
}
private void changeView(Object obj) {
if(obj==null) return;
if(obj instanceof AUIEvent) {
AUIEvent event = (AUIEvent)obj;
switch(event.getType()) {
case DEF_CHANGE_VIEW:
{
UIEventChangeView bufEvent = (UIEventChangeView)event;
this.view = bufEvent.getView();
break;
}
}
}
}
}