use of edu.cmu.cs.hcii.cogtool.util.IListenerAction in project cogtool by cogtool.
the class ProjectController method createCopyTaskAction.
// createRenameDesignAction
// Action for CopyTask
protected IListenerAction createCopyTaskAction() {
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);
// Can only copy to clipboard if one or more tasks are selected
if ((tasks != null) && (tasks.length > 0)) {
try {
ObjectSaver s = CogToolClipboard.startClipboardSave(CogToolClipboard.CopyTasks, CogToolClipboard.SAVE_TO_CLIPBOARD);
for (AUndertaking task : tasks) {
s.saveObject(task);
}
s.finish();
if (tasks.length == 1) {
interaction.setStatusMessage(TASK_COPIED);
} else {
interaction.setStatusMessage(TASKS_COPIED);
}
return true;
} catch (IOException e) {
throw new RcvrClipboardException(e);
} catch (OutOfMemoryError error) {
throw new RcvrOutOfMemoryException("Copying Tasks", error);
}
} else {
interaction.protestNoSelection();
}
return false;
}
};
}
use of edu.cmu.cs.hcii.cogtool.util.IListenerAction in project cogtool by cogtool.
the class ProjectController method createNewTaskAction.
// Action for NewTask
protected IListenerAction createNewTaskAction() {
return new IListenerAction() {
public Class<?> getParameterClass() {
return TaskSelectionState.class;
}
public boolean performAction(Object prms) {
TaskSelectionState selection = (TaskSelectionState) prms;
// Figure out what the selection is
AUndertaking[] selectedTasks = selection.getSelectedTasks(TaskSelectionState.ORDER_SELECTION);
Task newTask = null;
if ((selectedTasks == null) || (selectedTasks.length == 0)) {
// No task selection -- add a new root task
String newTaskName = computeNewTaskName(project, DEFAULT_TASK_PREFIX);
newTask = addTask(project, project.getUndertakings().size(), newTaskName);
} else {
// At least one selected task: add a new sibling after
// Multiple selected tasks:
// if all tasks have same parent,
// add new sibling after last selection
// if tasks do not have same parent,
// add new root task after last selected root
// but, if no selected root, add new root at end
// Get parent
TaskGroup parent = selection.getSelectedTaskParent();
if (parent == null) {
String newTaskName = computeNewTaskName(project, DEFAULT_TASK_PREFIX);
newTask = addTask(project, findLastSelectedRoot(selectedTasks), newTaskName);
} else {
String newTaskName = computeNewTaskName(parent, DEFAULT_TASK_PREFIX);
AUndertaking last = selectedTasks[selectedTasks.length - 1];
newTask = addTask(parent, parent.getUndertakings().indexOf(last) + 1, newTaskName);
}
}
// Task is automatically selected when added -- rename
// Cannot put this into the ui because of undo/redo
ui.initiateTaskRename(newTask);
return true;
}
};
}
use of edu.cmu.cs.hcii.cogtool.util.IListenerAction in project cogtool by cogtool.
the class ProjectController method createChangeTaskPositionAction.
protected IListenerAction createChangeTaskPositionAction() {
return new IListenerAction() {
public Class<?> getParameterClass() {
return ProjectUI.ChangeTaskPositionParms.class;
}
public boolean performAction(Object actionParms) {
ProjectUI.ChangeTaskPositionParms prms = (ProjectUI.ChangeTaskPositionParms) actionParms;
if ((prms.placeBeforeTask != null) && prms.tasks.isInSelectedHierarchy(prms.placeBeforeTask)) {
// Nothing to do
return false;
}
if ((prms.tasks == null) || (prms.tasks.getSelectedTaskCount() == 0)) {
interaction.protestNoSelection();
return false;
}
final AUndertaking[] selectedTasks = prms.tasks.getSelectedTasks(TaskSelectionState.PRUNE_SELECTION | TaskSelectionState.ORDER_SELECTION);
final TaskParent[] oldParents = new TaskParent[selectedTasks.length];
final int[] indexes = new int[selectedTasks.length];
final String[] oldNames = new String[selectedTasks.length];
final TaskParent placeBeforeTaskParent = (prms.placeBeforeTask != null) ? project.getTaskParent(prms.placeBeforeTask) : project;
final List<AUndertaking> siblings = placeBeforeTaskParent.getUndertakings();
// If the place-before-task is selected, find the next
// sibling that is not selected.
AUndertaking placeBefore = prms.placeBeforeTask;
if (prms.tasks.isTaskSelected(placeBefore)) {
int siblingIndex = siblings.indexOf(placeBefore);
int siblingCount = siblings.size();
while (++siblingIndex < siblingCount) {
placeBefore = siblings.get(siblingIndex);
if (!prms.tasks.isTaskSelected(placeBefore)) {
break;
}
}
if (siblingIndex >= siblingCount) {
placeBefore = null;
}
}
// Remove first so that the atIndex computation works!
removeChildTasks(selectedTasks, oldParents, indexes);
final int atIndex = (placeBefore != null) ? siblings.indexOf(placeBefore) : siblings.size();
// Add each selected task as siblings before the given task
for (int i = 0; i < selectedTasks.length; i++) {
oldNames[i] = selectedTasks[i].getName();
String newName = NamedObjectUtil.makeNameUnique(oldNames[i], siblings);
if (!newName.equals(oldNames[i])) {
selectedTasks[i].setName(newName);
}
placeBeforeTaskParent.addUndertaking(atIndex + i, selectedTasks[i]);
}
// Create undo/redo step and add to undo manager
IUndoableEdit edit = new AUndoableEdit(ProjectLID.ChangeTaskPosition) {
@Override
public String getPresentationName() {
return (selectedTasks.length > 1) ? MOVE_TASKS : MOVE_TASK;
}
@Override
public void redo() {
super.redo();
removeChildTasks(selectedTasks, null, null);
// before the given task
for (int i = 0; i < selectedTasks.length; i++) {
String newName = NamedObjectUtil.makeNameUnique(oldNames[i], siblings);
if (!newName.equals(oldNames[i])) {
selectedTasks[i].setName(newName);
}
placeBeforeTaskParent.addUndertaking(atIndex + i, selectedTasks[i]);
}
}
@Override
public void undo() {
super.undo();
removeChildTasks(selectedTasks, null, null);
// Un-delete children; IMPORTANT: reverse order!
for (int i = selectedTasks.length - 1; 0 <= i; i--) {
if (!oldNames[i].equals(selectedTasks[i].getName())) {
selectedTasks[i].setName(oldNames[i]);
}
// Add to old parent at old index
oldParents[i].addUndertaking(indexes[i], selectedTasks[i]);
}
}
};
undoMgr.addEdit(edit);
return true;
}
};
}
use of edu.cmu.cs.hcii.cogtool.util.IListenerAction in project cogtool by cogtool.
the class ProjectController method createCutDesignAction.
// Action for CutDesign
protected IListenerAction createCutDesignAction() {
return new IListenerAction() {
public Class<?> getParameterClass() {
return DesignSelectionState.class;
}
public boolean performAction(Object prms) {
DesignSelectionState selection = (DesignSelectionState) prms;
Design selectedDesign = selection.getSelectedDesign();
// Can only cut if a design is currently selected.
if (selectedDesign != null) {
if (interaction.confirmDeleteDesign(selectedDesign)) {
try {
ObjectSaver s = CogToolClipboard.startClipboardDesignSave(project, CogToolClipboard.SAVE_TO_CLIPBOARD);
// Delete selected design, copying to the clipboard
deleteDesign(selectedDesign, s);
s.finish();
return true;
} catch (IOException e) {
throw new RcvrClipboardException(e);
} catch (OutOfMemoryError error) {
throw new RcvrOutOfMemoryException("Cutting Design", error);
}
}
} else {
interaction.protestNoSelection();
}
return false;
}
};
}
use of edu.cmu.cs.hcii.cogtool.util.IListenerAction 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;
}
};
}
Aggregations