use of edu.cmu.cs.hcii.cogtool.util.IUndoableEdit in project cogtool by cogtool.
the class ProjectController method moveTaskAction.
protected boolean moveTaskAction(TaskSelectionState seln, boolean moveEarlier) {
AUndertaking[] tasks = seln.getSelectedTasks(TaskSelectionState.PRUNE_SELECTION | TaskSelectionState.ORDER_SELECTION);
if ((tasks == null) || (tasks.length == 0)) {
interaction.protestNoSelection();
return false;
}
if (tasks.length > 1) {
interaction.protestTooManySelectedTasks();
return false;
}
final AUndertaking task = tasks[0];
final TaskParent parent = project.getTaskParent(task);
List<AUndertaking> siblings = parent.getUndertakings();
int siblingCount = siblings.size();
if (siblingCount == 1) {
interaction.setStatusMessage(taskIsOnlyChild);
} else {
final int oldTaskIndex = siblings.indexOf(task);
IUndoableEdit edit;
if (moveEarlier) {
if (oldTaskIndex == 0) {
interaction.protestCannotMoveEarlier();
return false;
}
parent.removeUndertaking(task);
parent.addUndertaking(oldTaskIndex - 1, task);
edit = new AUndoableEdit(ProjectLID.MoveTaskEarlier) {
@Override
public String getPresentationName() {
return MOVE_TASK_EARLIER;
}
@Override
public void redo() {
super.redo();
parent.removeUndertaking(task);
parent.addUndertaking(oldTaskIndex - 1, task);
}
@Override
public void undo() {
super.undo();
parent.removeUndertaking(task);
parent.addUndertaking(oldTaskIndex, task);
}
};
} else {
if (oldTaskIndex == siblingCount - 1) {
interaction.protestCannotMoveLater();
return false;
}
parent.removeUndertaking(task);
parent.addUndertaking(oldTaskIndex + 1, task);
edit = new AUndoableEdit(ProjectLID.MoveTaskEarlier) {
@Override
public String getPresentationName() {
return MOVE_TASK_LATER;
}
@Override
public void redo() {
super.redo();
parent.removeUndertaking(task);
parent.addUndertaking(oldTaskIndex + 1, task);
}
@Override
public void undo() {
super.undo();
parent.removeUndertaking(task);
parent.addUndertaking(oldTaskIndex, task);
}
};
}
undoMgr.addEdit(edit);
}
return true;
}
use of edu.cmu.cs.hcii.cogtool.util.IUndoableEdit in project cogtool by cogtool.
the class ProjectController method openProjectOnCompute.
/**
* editSeq will be null if no regeneration occurred; otherwise,
* the edit sequence will contain the undoable edit for the regeneration.
*/
public static boolean openProjectOnCompute(Project project, final Script script, final APredictionResult newResult, CompoundUndoableEdit editSeq) {
boolean notModified;
try {
notModified = UndoManager.isAtSavePoint(project);
} catch (IllegalStateException ex) {
System.err.println("Ignoring that isAtSavePoint failed.");
notModified = false;
}
ProjectController c = ProjectController.openController(project, false, notModified);
final CognitiveModelGenerator modelGen = script.getModelGenerator();
final IPredictionAlgo computeAlg = newResult.getPredictionAlgorithm();
final TaskApplication taskApp = script.getDemonstration().getTaskApplication();
final APredictionResult oldResult = taskApp.getResult(modelGen, computeAlg);
taskApp.setResult(modelGen, computeAlg, PredictionResultProxy.getLatestResult(newResult));
UndoManager scriptUndoMgr = UndoManager.getUndoManager(script, project);
IUndoableEdit edit = new AUndoableEdit(ProjectLID.RecomputeScript) {
protected APredictionResult redoResult = newResult;
protected APredictionResult undoResult = oldResult;
@Override
public String getPresentationName() {
return COMPUTE_SCRIPT;
}
@Override
public void redo() {
super.redo();
redoResult = PredictionResultProxy.getLatestResult(redoResult);
taskApp.setResult(modelGen, computeAlg, redoResult);
}
@Override
public void undo() {
super.undo();
undoResult = PredictionResultProxy.getLatestResult(undoResult);
taskApp.setResult(modelGen, computeAlg, undoResult);
}
};
if (editSeq != null) {
editSeq.addEdit(edit);
editSeq.end();
edit = editSeq;
}
// Add to script's undo mgr first to set owner properly
scriptUndoMgr.addEdit(edit);
c.undoMgr.addEdit(edit);
c.takeFocus();
return true;
}
use of edu.cmu.cs.hcii.cogtool.util.IUndoableEdit in project cogtool by cogtool.
the class FrameEditorController method createUngroupElementsAction.
private IListenerAction createUngroupElementsAction() {
return new IListenerAction() {
public Class<?> getParameterClass() {
return FrameEditorSelectionState.class;
}
public boolean performAction(Object prms) {
FrameEditorSelectionState selection = (FrameEditorSelectionState) prms;
Iterator<FrameElement> selectedElts = selection.getSelectedElementsIterator();
final Set<FrameElementGroup> selectedGroups = new HashSet<FrameElementGroup>();
while (selectedElts.hasNext()) {
FrameElement elt = selectedElts.next();
if (elt instanceof FrameElementGroup) {
selectedGroups.add((FrameElementGroup) elt);
} else {
selectedGroups.addAll(elt.getRootElement().getEltGroups());
}
}
if (selectedGroups.size() > 0) {
CompoundUndoableEdit editSequence = new CompoundUndoableEdit(UNGROUP_ELEMENTS, FrameEditorLID.Ungroup);
Iterator<FrameElementGroup> groups = selectedGroups.iterator();
while (groups.hasNext()) {
FrameElementGroup group = groups.next();
ungroup(group, editSequence);
removeRootElement(UNGROUP_ELEMENTS, group, null, editSequence);
}
IUndoableEdit edit = new AUndoableEdit(FrameEditorLID.Ungroup) {
@Override
public String getPresentationName() {
return UNGROUP_ELEMENTS;
}
@Override
public void redo() {
super.redo();
Iterator<FrameElementGroup> groups = selectedGroups.iterator();
while (groups.hasNext()) {
FrameElementGroup group = groups.next();
ungroup(group, null);
}
}
@Override
public void undo() {
super.undo();
Iterator<FrameElementGroup> groups = selectedGroups.iterator();
while (groups.hasNext()) {
FrameElementGroup group = groups.next();
regroup(group);
}
}
};
editSequence.addEdit(edit);
// Commit the edit
editSequence.end();
// Only add this edit if it is significant
if (editSequence.isSignificant()) {
undoMgr.addEdit(editSequence);
}
return true;
}
interaction.protestNoSelection();
return false;
}
};
}
use of edu.cmu.cs.hcii.cogtool.util.IUndoableEdit in project cogtool by cogtool.
the class DesignEditorCmd method pasteElements.
// pasteFrameElementGroup
public static int pasteElements(Design design, final Frame frame, Collection<Object> objects, DemoStateManager mgr, IUndoableEditSequence editSequence) {
// If given objects contain widgets, insert into the given frame
if ((objects != null) && (objects.size() > 0)) {
Iterator<Object> objIt = objects.iterator();
Set<SimpleWidgetGroup> addedGroups = new HashSet<SimpleWidgetGroup>();
final Set<IWidget> addedRemoteLabels = new HashSet<IWidget>();
final Set<FrameElementGroup> addedEltGroups = new HashSet<FrameElementGroup>();
int numPasted = 0;
// May need to add a device
int currentDeviceTypes = DeviceType.buildDeviceSet(design.getDeviceTypes());
// create them.
while (objIt.hasNext()) {
Object o = objIt.next();
if (o instanceof IWidget) {
IWidget widget = (IWidget) o;
numPasted += pasteWidget(widget, design, currentDeviceTypes, frame, mgr, editSequence, addedEltGroups, addedRemoteLabels);
SimpleWidgetGroup group = widget.getParentGroup();
if (group != null) {
addedGroups.add(group);
}
} else if (o instanceof FrameElementGroup) {
numPasted += pasteFrameElementGroup((FrameElementGroup) o, design, currentDeviceTypes, frame, mgr, editSequence, addedEltGroups, addedRemoteLabels);
}
}
Iterator<SimpleWidgetGroup> groupsIter = addedGroups.iterator();
while (groupsIter.hasNext()) {
SimpleWidgetGroup group = groupsIter.next();
repositionChildren(group);
if (group instanceof GridButtonGroup) {
((GridButtonGroup) group).recalculateOffsets();
}
addedEltGroups.addAll(group.getEltGroups());
}
Iterator<FrameElementGroup> eltGroups = addedEltGroups.iterator();
while (eltGroups.hasNext()) {
frame.addEltGroup(eltGroups.next());
}
Iterator<IWidget> remoteLabels = addedRemoteLabels.iterator();
while (remoteLabels.hasNext()) {
IWidget remoteLabel = remoteLabels.next();
if (!frame.containsWidget(remoteLabel)) {
String uniqueName = NamedObjectUtil.makeNameUnique(remoteLabel.getName(), frame.getWidgets());
remoteLabel.setName(uniqueName);
frame.addWidget(remoteLabel);
}
}
IUndoableEdit edit = new AUndoableEdit(CogToolLID.Paste) {
@Override
public String getPresentationName() {
return PASTE;
}
@Override
public void redo() {
super.redo();
Iterator<FrameElementGroup> eltGroups = addedEltGroups.iterator();
while (eltGroups.hasNext()) {
frame.addEltGroup(eltGroups.next());
}
Iterator<IWidget> remoteLabels = addedRemoteLabels.iterator();
while (remoteLabels.hasNext()) {
IWidget remoteLabel = remoteLabels.next();
if (!frame.containsWidget(remoteLabel)) {
frame.addWidget(remoteLabel);
}
}
}
@Override
public void undo() {
super.undo();
Iterator<FrameElementGroup> eltGroups = addedEltGroups.iterator();
while (eltGroups.hasNext()) {
frame.removeEltGroup(eltGroups.next());
}
Iterator<IWidget> remoteLabels = addedRemoteLabels.iterator();
while (remoteLabels.hasNext()) {
IWidget remoteLabel = remoteLabels.next();
if (frame.containsWidget(remoteLabel)) {
frame.removeWidget(remoteLabel);
}
}
}
};
editSequence.addEdit(edit);
return numPasted;
}
return 0;
}
use of edu.cmu.cs.hcii.cogtool.util.IUndoableEdit in project cogtool by cogtool.
the class DemoScriptCmd method regenerateScripts.
public static boolean regenerateScripts(Project project, Demonstration demo, Script exceptScript, DemoStateManager demoStateMgr, Interaction interaction, IUndoableEditSequence editSeq) {
if (demo.isObsolete()) /*&& ! demo.isInvalid()*/
{
final DemoStateManager.IConformanceUndoRedo conformanceUndoRedo = demoStateMgr.restoreConformance(demo);
// Collection of ScriptUndoRedo instances
final Collection<ComputationUndoRedo> scriptsUndoRedoData = regenerateScripts(demo, 0, demo.getStepAt(0), interaction);
IUndoableEdit edit = new AUndoableEdit(SEDemoLID.RegenerateScript) {
@Override
public String getPresentationName() {
return L10N.get("UNDO.FC.RegenerateScript", "Regenerate Script(s)");
}
@Override
public void redo() {
super.redo();
conformanceUndoRedo.redo();
redoAllChanges(scriptsUndoRedoData);
}
@Override
public void undo() {
super.undo();
conformanceUndoRedo.undo();
undoAllChanges(scriptsUndoRedoData);
}
};
editSeq.addEdit(edit);
addUndoableEditToScripts(editSeq, demo, exceptScript, project);
}
return true;
}
Aggregations