/******************************************************************************* | |
* Copyright (c) 2004-2008 Istvan Rath and Daniel Varro | |
* All rights reserved. This program and the accompanying materials | |
* are made available under the terms of the Eclipse Public License v1.0 | |
* which accompanies this distribution, and is available at | |
* http://www.eclipse.org/legal/epl-v10.html | |
* | |
* Contributors: | |
* Istvan Rath - initial API and implementation | |
*******************************************************************************/ | |
package org.eclipse.viatra2.treeeditor.commands; | |
import org.eclipse.viatra2.errors.VPMCoreException; | |
import org.eclipse.viatra2.treeeditor.ViatraTreeEditor; | |
import java.util.Vector; | |
public class ViatraEditorCommandStack { | |
/** | |
* Internal marker class used for marking save points on the Command Stack. | |
* @author Istvan Rath | |
* | |
*/ | |
private class SaveMark extends ViatraEditorCommand { | |
@Override | |
public void execute() throws VPMCoreException { } | |
@Override | |
public void undo() throws VPMCoreException { } | |
} | |
private Vector<ViatraEditorCommand> iCommands; | |
private ViatraTreeEditor iVTE; | |
/** | |
* Points to the last command which has been executed. | |
*/ | |
private int iCurrentIndex = 0; | |
public ViatraEditorCommandStack(ViatraTreeEditor vte) { | |
iCommands = new Vector<ViatraEditorCommand>(); | |
iCommands.add(new SaveMark()); // add initial savemark | |
iVTE = vte; | |
} | |
public void execute(ViatraEditorCommand cmd) { | |
try { | |
cmd.execute(); | |
for (int i=iCurrentIndex+1; i<iCommands.size(); i++) | |
iCommands.remove(i); | |
iCommands.add(cmd); | |
iCurrentIndex++; | |
} | |
catch (VPMCoreException e) { | |
iVTE.getFramework().getLogger().fatal(e.toString()); | |
iVTE.getFramework().getTopmodel().getTransactionManager().abortTransaction(); | |
} | |
// iVTE.setDirty(); | |
// debugmsg(); | |
} | |
public void undo() { | |
try { | |
iCommands.get(iCurrentIndex).undo(); | |
iCurrentIndex--; | |
} catch (VPMCoreException e) { | |
iVTE.getFramework().getLogger().fatal(e.toString()); | |
} | |
iVTE.updateActions(); | |
// debugmsg(); | |
} | |
public void redo() { | |
try { | |
iCommands.get(iCurrentIndex+1).execute(); | |
iCurrentIndex++; | |
} catch (VPMCoreException e) { | |
iVTE.getFramework().getLogger().fatal(e.toString()); | |
} | |
iVTE.updateActions(); | |
// debugmsg(); | |
} | |
private void debugmsg() { | |
iVTE.getFramework().getLogger().debug("[VECommandStack] currindex is "+iCurrentIndex+", element at currindex is "+iCommands.get(iCurrentIndex).toString()); | |
} | |
public boolean isUndoable() { | |
return isDirty(); | |
} | |
public boolean isRedoable() { | |
return iCurrentIndex<iCommands.size()-1; | |
} | |
public void markSave() { | |
this.execute(new SaveMark()); | |
} | |
public boolean isDirty() { | |
return !(iCommands.get(iCurrentIndex) instanceof SaveMark); | |
} | |
} |