use of edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit in project cogtool by cogtool.
the class ProjectController method regenerateScripts.
protected boolean regenerateScripts(AUndertaking task, Design design, DemoStateManager demoStateMgr, IUndoableEditSequence editSequence) {
if (task.isTaskGroup()) {
Iterator<AUndertaking> allTasks = ((TaskGroup) task).getUndertakings().iterator();
CompoundUndoableEdit groupEditSeq = new CompoundUndoableEdit(REGENERATE_SCRIPTS, ProjectLID.RegenerateScript);
while (allTasks.hasNext()) {
if (!regenerateScripts(allTasks.next(), design, demoStateMgr, groupEditSeq)) {
return false;
}
}
if (groupEditSeq.isSignificant()) {
groupEditSeq.end();
editSequence.addEdit(groupEditSeq);
}
return true;
}
TaskApplication ta = project.getTaskApplication(task, design);
if (ta != null) {
Demonstration demo = ta.getDemonstration();
return DemoScriptCmd.regenerateScripts(project, demo, demoStateMgr, interaction, editSequence);
}
return true;
}
use of edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit in project cogtool by cogtool.
the class ProjectController method computeSnifAct.
// assignActions
protected boolean computeSnifAct(Design design, AUndertaking task, IUndoableEditSequence editSequence, SNIFACTGroupParameters defaults) {
// TODO: L10N required for error titles and messages.
if (design == null) {
interaction.reportProblem("SNIF-ACT", "No design is selected.");
return false;
}
if (design.getFrames().size() == 0) {
interaction.reportProblem("SNIF-ACT", "Selected design is empty.");
return false;
}
if (task.isTaskGroup()) {
// We can only recompute with this algorithm if the
// group was generated by a previous SNIF-ACT
// computation (and thus has the execution
// context attribute)
Object contextAttr = task.getAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR);
if (NullSafe.equals(contextAttr, WidgetAttributes.NO_CONTEXT)) {
interaction.reportProblem("SNIF-ACT", "Can't recompute with this algorithm from this cell.");
return false;
}
}
ISimilarityDictionary dict = (ISimilarityDictionary) design.getAttribute(WidgetAttributes.DICTIONARY_ATTR);
if (dict == null) {
interaction.reportProblem("Export Dictionary", "No dictionary exists for the selected design");
return false;
}
List<Frame> sortedFrames = NamedObjectUtil.getSortedList(design.getFrames());
SNIFACTParameters parms;
int addGroupMode;
boolean hasScript = false;
final SNIFACTExecContext prevContext;
if (task.isTaskGroup()) {
// Since we got this far, we know the group already has
// parameters associated with it, so use those values as
// defaults in the dialog box
prevContext = (SNIFACTExecContext) task.getAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR);
parms = prevContext.getParameters();
addGroupMode = SNIFACTDialog.ENABLED;
} else {
// Otherwise, create a new set of default values for this
// execution
String defaultName = sortedFrames.get(0).getName();
List<String> targets = new ArrayList<String>();
targets.add(defaultName);
parms = new SNIFACTParameters(task.getName(), NUM_SNIF_ACT_TRIALS, SNIF_ACT_K_VALUE, defaultName, targets, ITermSimilarity.ALL);
addGroupMode = SNIFACTDialog.NONE;
prevContext = null;
TaskApplication ta = project.getTaskApplication(task, design);
hasScript = (ta != null) && ta.hasScript();
}
SNIFACTGroupParameters groupParms = null;
if (defaults == null) {
groupParms = interaction.requestSNIFACTParameters(hasScript, sortedFrames, parms, dict.getAlgorithmsInUse(), addGroupMode);
} else {
groupParms = new SNIFACTGroupParameters(defaults.taskName, defaults.numRuns, defaults.kValue, defaults.startFrame, defaults.targetFrames, defaults.algorithm, ((addGroupMode != SNIFACTDialog.NONE) && defaults.addToGroup));
}
if (groupParms == null) {
return false;
}
SNIFACTPredictionAlgo.ONLY.setParameters(groupParms);
TaskParent parent = task.getTaskGroup();
if (parent == null) {
parent = project;
}
final TaskGroup group;
if (groupParms.addToGroup) {
// user wants to add new trial tasks to the same group
group = (TaskGroup) task;
} else {
Collection<AUndertaking> siblings = parent.getUndertakings();
group = new TaskGroup(SNIFACTCmd.getGroupName(groupParms, siblings), GroupNature.MEAN);
}
final SNIFACTExecContext context = SNIFACTCmd.computeInBackground(project, design, interaction, group, groupParms);
if (context == null) {
return true;
}
group.setAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR, context);
CompoundUndoableEdit snifActEditSeq = new CompoundUndoableEdit(SNIFACT_COMPUTATION, ProjectLID.RecomputeScript);
if (!task.isTaskGroup()) {
snifActEditSeq.addEdit(HCIPACmd.replaceTask(project, parent, task, group, SNIFACT_COMPUTATION));
} else {
if (groupParms.addToGroup) {
snifActEditSeq.addEdit(SNIFACTCmd.addTasksToGroup(project, group, context, SNIFACT_COMPUTATION));
} else {
snifActEditSeq.addEdit(SNIFACTCmd.addGroup(project, parent, group, SNIFACT_COMPUTATION));
}
}
snifActEditSeq.addEdit(new AUndoableEdit(ProjectLID.RecomputeScript) {
@Override
public String getPresentationName() {
return SNIFACT_COMPUTATION;
}
@Override
public void redo() {
super.redo();
group.setAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR, context);
}
@Override
public void undo() {
super.undo();
group.setAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR, prevContext);
}
});
snifActEditSeq.end();
if (editSequence != null) {
editSequence.addEdit(snifActEditSeq);
}
return true;
}
use of edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit in project cogtool by cogtool.
the class ProjectController method createPromoteTaskAction.
protected IListenerAction createPromoteTaskAction() {
return new IListenerAction() {
public Class<?> getParameterClass() {
return TaskSelectionState.class;
}
public boolean performAction(Object prms) {
TaskSelectionState seln = (TaskSelectionState) prms;
AUndertaking[] tasks = seln.getSelectedTasks(TaskSelectionState.PRUNE_SELECTION | TaskSelectionState.ORDER_SELECTION);
if ((tasks == null) || (tasks.length == 0)) {
interaction.protestNoSelection();
return false;
}
List<String> errors = new ArrayList<String>();
String editLabel = (tasks.length > 1) ? PROMOTE_TASKS : PROMOTE_TASK;
CompoundUndoableEdit editSeq = new CompoundUndoableEdit(editLabel, ProjectLID.PromoteTask);
for (AUndertaking task : tasks) {
String promoteError = promoteTask(task, ProjectLID.PromoteTask, editSeq);
if (promoteError != null) {
errors.add(promoteError);
}
}
if (errors.size() > 0) {
interaction.reportProblems(editLabel, errors);
}
if (editSeq.isSignificant()) {
editSeq.end();
undoMgr.addEdit(editSeq);
}
return true;
}
};
}
use of edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit in project cogtool by cogtool.
the class ProjectController method recomputeScripts.
protected boolean recomputeScripts(AUndertaking task, Design design, DemoStateManager demoStateMgr, ComputeMessages computeMsgs, IUndoableEditSequence editSequence) {
if (CogToolPref.isTracingOverride == null && !CogToolPref.IS_TRACING.getBoolean()) {
Boolean answer = getInteraction().confirmNoTracing();
if (answer == null) {
// canceled
return false;
} else if (answer.booleanValue()) {
CogToolPref.IS_TRACING.setBoolean(true);
}
}
if (task.isTaskGroup()) {
if (!NullSafe.equals(WidgetAttributes.NO_CONTEXT, task.getAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR))) {
return computeSnifAct(design, task, editSequence, null);
}
Iterator<AUndertaking> allTasks = ((TaskGroup) task).getUndertakings().iterator();
CompoundUndoableEdit groupEditSeq = new CompoundUndoableEdit(RECOMPUTE_SCRIPTS, ProjectLID.RecomputeScript);
while (allTasks.hasNext()) {
if (!recomputeScripts(allTasks.next(), design, demoStateMgr, computeMsgs, groupEditSeq)) {
return false;
}
}
if (groupEditSeq.isSignificant()) {
groupEditSeq.end();
editSequence.addEdit(groupEditSeq);
}
return true;
}
TaskApplication ta = project.getTaskApplication(task, design);
if (ta != null) {
IPredictionAlgo activeAlg = ta.determineActiveAlgorithm(project);
if (activeAlg == SNIFACTPredictionAlgo.ONLY) {
return computeSnifAct(design, task, editSequence, null);
}
if (computeMsgs.canComputeScript(ta, demoStateMgr, editSequence)) {
if (!activeAlg.allowsComputation()) {
interaction.setStatusMessage(algDisallowsComputation);
interaction.reportProblem(RECOMPUTE_SCRIPTS, algDisallowsComputation);
return false;
} else if (!ta.getDemonstration().isStartFrameChosen()) {
// nothing to compute
interaction.setStatusMessage(noStartFrameChosen);
return false;
}
IUndoableEdit edit = ComputePredictionCmd.computeAllPredictions(project, ta, activeAlg, ta.determineComputeInBackground(project), interaction);
if (edit != null) {
ta.setActiveAlgorithm(activeAlg);
editSequence.addEdit(edit);
} else {
interaction.setStatusMessage(computeHadNoResult);
}
} else {
interaction.setStatusMessage(cannotRecomputeInvalid);
}
} else {
if (project.getDefaultAlgo() == SNIFACTPredictionAlgo.ONLY) {
return computeSnifAct(design, task, editSequence, null);
}
interaction.setStatusMessage(cannotRecomputeNoDemo);
}
return true;
}
use of edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit in project cogtool by cogtool.
the class ProjectController method createPasteAction.
// Action for Paste
protected IListenerAction createPasteAction() {
return new IListenerAction() {
public Class<?> getParameterClass() {
return ProjectSelectionState.class;
}
public boolean performAction(Object prms) {
ProjectSelectionState seln = (ProjectSelectionState) prms;
try {
// Check for CogTool objects to paste
Collection<Object> objects = CogToolClipboard.fetchCogToolObjects(project);
// designs and tasks only
int numObjectsPasted = 0;
if ((objects != null) && (objects.size() > 0)) {
// Paste tasks as children of the selected TaskGroup
AUndertaking[] selectedTasks = seln.getSelectedTasks(TaskSelectionState.ORDER_SELECTION);
TaskGroup taskParent = seln.getSelectedTaskParent();
int index;
// index at which new pasted Tasks will be placed.
if (taskParent != null) {
AUndertaking last = selectedTasks[selectedTasks.length - 1];
index = taskParent.getUndertakings().indexOf(last) + 1;
} else {
index = findLastSelectedRoot(selectedTasks);
}
// Create undo support
String presentationName = PASTE;
CompoundUndoableEdit editSeq = new CompoundUndoableEdit(presentationName, ProjectLID.Paste);
// If task applications are pasted, they will have
// null Design fields; they should "arrive" after
// the Design being pasted at the same time!
Design newDesign = null;
// Existing tasks are to be re-used if the paste
// is within the same project as the copy/cut.
Map<AUndertaking, AUndertaking> reuseTasks = null;
Iterator<Object> objIt = objects.iterator();
while (objIt.hasNext()) {
Object o = objIt.next();
// unique name and create and place the design.
if (o instanceof Design) {
newDesign = (Design) o;
makeDesignNameUnique(newDesign);
// Add an undo step after creating/placing
ProjectCmd.addNewDesign(project, newDesign, seln.getSelectedDesign(), presentationName, editSeq);
numObjectsPasted++;
} else // or to the Project. Create and place.
if (o instanceof AUndertaking) {
AUndertaking task = (AUndertaking) o;
// project or pasting copied tasks
if (reuseTasks == null) {
pasteTask(task, taskParent, index++, editSeq, presentationName);
numObjectsPasted++;
} else {
// In this case, a copied design is
// being pasted into the same project.
// Map each undertaking to the
// corresponding one in the current project
mapProjectTasks(task, project, reuseTasks, editSeq, presentationName);
}
} else // along when copying an Design.
if (o instanceof TaskApplication) {
TaskApplication taskApp = (TaskApplication) o;
if (reuseTasks != null) {
AUndertaking reuseTask = reuseTasks.get(taskApp.getTask());
if (reuseTask != null) {
taskApp.setTask(reuseTask);
}
}
// The undo edit for adding the Design will
// remove and restore the task-applications;
// simply add to the project.
project.setTaskApplication(taskApp);
} else if (o instanceof CogToolClipboard.ProjectScope) {
CogToolClipboard.ProjectScope projectScope = (CogToolClipboard.ProjectScope) o;
// a copied design into the same project
if (projectScope.getProject() != null) {
reuseTasks = new HashMap<AUndertaking, AUndertaking>();
}
}
}
// Done with undo/redo steps; add the compound change
// to the undo manager.
editSeq.end();
undoMgr.addEdit(editSeq);
interaction.setStatusMessage(numObjectsPasted + " " + pasteComplete);
} else {
interaction.setStatusMessage(nothingPasted);
}
return true;
} catch (IOException e) {
throw new RcvrClipboardException(e);
} catch (SAXException e) {
throw new RcvrClipboardException(e);
} catch (ParserConfigurationException e) {
throw new RcvrClipboardException(e);
} catch (ClipboardUtil.ClipboardException e) {
throw new RcvrClipboardException(e);
}
}
};
}
Aggregations