Search in sources :

Example 1 with RcvrClipboardException

use of edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException in project cogtool by cogtool.

the class ProjectController method deleteTasks.

// modifyTaskGroup
/**
     * Utility to delete the specified tasks.
     * If an object saver is specified (that is, not <code>null</code>),
     * the deleted tasks are serialized as well.
     *
     * @param tasksToDelete the subtasks to delete
     * @param saver if not null, the saver to use for serializing the
     *              deleted tasks (used saving to the system clipboard for the
     *              "cut" action)
     * @param editSequence to hold the undoable edit
     * @throws RcvrClipboardException if the saver is given and serialization
     *         failed for some reason; NOTE!!! Delete will have succeeded!
     * @author mlh
     */
protected void deleteTasks(final AUndertaking[] tasksToDelete, ObjectSaver saver, IUndoableEditSequence editSequence) {
    @SuppressWarnings("unchecked") final Map<ITaskDesign, TaskApplication>[] assocTAs = new Map[tasksToDelete.length];
    RcvrClipboardException firstException = null;
    // Delete each task and serialize if desired
    for (int i = 0; i < tasksToDelete.length; i++) {
        assocTAs[i] = project.taskApplicationsForRemovedTask(tasksToDelete[i]);
        if (saver != null) {
            try {
                saver.saveObject(tasksToDelete[i]);
            } catch (IOException ex) {
                if (firstException != null) {
                    firstException = new RcvrClipboardException("Could not save object" + " to clipboard.", ex);
                }
            }
        }
    }
    final TaskParent[] oldParents = new TaskParent[tasksToDelete.length];
    final int[] indexes = new int[tasksToDelete.length];
    removeChildTasks(tasksToDelete, oldParents, indexes);
    // Create undo/redo step and add to undo manager
    IUndoableEdit edit = new AUndoableEdit(ProjectLID.DeleteTask) {

        protected Map<ITaskDesign, TaskApplication>[] associatedTAs = assocTAs;

        protected boolean recoverMgrs = true;

        @Override
        public String getPresentationName() {
            return (tasksToDelete.length > 1) ? DELETE_TASKS : DELETE_TASK;
        }

        @Override
        public void redo() {
            super.redo();
            recoverMgrs = true;
            for (int i = 0; i < tasksToDelete.length; i++) {
                associatedTAs[i] = project.taskApplicationsForRemovedTask(tasksToDelete[i]);
            }
            removeChildTasks(tasksToDelete, null, null);
        }

        @Override
        public void undo() {
            super.undo();
            recoverMgrs = false;
            // Un-delete children; IMPORTANT: reverse order!
            for (int i = tasksToDelete.length - 1; 0 <= i; i--) {
                // Add to old parent at old index
                oldParents[i].addUndertaking(indexes[i], tasksToDelete[i]);
                project.restoreRemovedTaskApplications(associatedTAs[i]);
            }
        }

        @Override
        public void die() {
            super.die();
            if (recoverMgrs) {
                for (int i = 0; i < tasksToDelete.length; i++) {
                    recoverManagers(tasksToDelete[i], associatedTAs[i]);
                }
            }
        }
    };
    editSequence.addEdit(edit);
    if (firstException != null) {
        throw firstException;
    }
}
Also used : ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) RcvrClipboardException(edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) TaskParent(edu.cmu.cs.hcii.cogtool.model.TaskParent) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with RcvrClipboardException

use of edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException 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;
        }
    };
}
Also used : ObjectSaver(edu.cmu.cs.hcii.cogtool.util.ObjectSaver) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) RcvrOutOfMemoryException(edu.cmu.cs.hcii.cogtool.util.RcvrOutOfMemoryException) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) TaskSelectionState(edu.cmu.cs.hcii.cogtool.ui.TaskSelectionState) RcvrClipboardException(edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException)

Example 3 with RcvrClipboardException

use of edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException 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;
        }
    };
}
Also used : Design(edu.cmu.cs.hcii.cogtool.model.Design) ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) ObjectSaver(edu.cmu.cs.hcii.cogtool.util.ObjectSaver) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) RcvrOutOfMemoryException(edu.cmu.cs.hcii.cogtool.util.RcvrOutOfMemoryException) RcvrClipboardException(edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) DesignSelectionState(edu.cmu.cs.hcii.cogtool.ui.DesignSelectionState)

Example 4 with RcvrClipboardException

use of edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException in project cogtool by cogtool.

the class ProjectController method saveDesignToClipboard.

// createPasteAction
// Support for copying design and associated TA's to clipboard
protected void saveDesignToClipboard(Design design, ObjectSaver saver) {
    try {
        saver.saveObject(design);
        Iterator<AUndertaking> rootTasks = project.getUndertakings().iterator();
        while (rootTasks.hasNext()) {
            AUndertaking childTask = rootTasks.next();
            saver.saveObject(childTask);
        }
        Map<ITaskDesign, TaskApplication> associatedTAs = project.taskApplicationsForDesign(design);
        Iterator<TaskApplication> taskApps = associatedTAs.values().iterator();
        while (taskApps.hasNext()) {
            TaskApplication ta = taskApps.next();
            saver.saveObject(ta);
        }
    } catch (IOException ex) {
        throw new RcvrClipboardException(ex);
    }
}
Also used : ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) RcvrClipboardException(edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException)

Example 5 with RcvrClipboardException

use of edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException 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);
            }
        }
    };
}
Also used : ProjectSelectionState(edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState) HashMap(java.util.HashMap) CogToolClipboard(edu.cmu.cs.hcii.cogtool.CogToolClipboard) RcvrClipboardException(edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) SAXException(org.xml.sax.SAXException) Design(edu.cmu.cs.hcii.cogtool.model.Design) ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) ClipboardUtil(edu.cmu.cs.hcii.cogtool.util.ClipboardUtil) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TaskGroup(edu.cmu.cs.hcii.cogtool.model.TaskGroup)

Aggregations

RcvrClipboardException (edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException)11 IOException (java.io.IOException)11 RcvrIOException (edu.cmu.cs.hcii.cogtool.util.RcvrIOException)9 RcvrOutOfMemoryException (edu.cmu.cs.hcii.cogtool.util.RcvrOutOfMemoryException)6 ITaskDesign (edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign)5 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)5 ObjectSaver (edu.cmu.cs.hcii.cogtool.util.ObjectSaver)5 AUndertaking (edu.cmu.cs.hcii.cogtool.model.AUndertaking)4 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)4 Design (edu.cmu.cs.hcii.cogtool.model.Design)3 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)3 ClipboardUtil (edu.cmu.cs.hcii.cogtool.util.ClipboardUtil)3 CompoundUndoableEdit (edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 SAXException (org.xml.sax.SAXException)3 CogToolClipboard (edu.cmu.cs.hcii.cogtool.CogToolClipboard)2 Frame (edu.cmu.cs.hcii.cogtool.model.Frame)2 DesignSelectionState (edu.cmu.cs.hcii.cogtool.ui.DesignSelectionState)2 TaskSelectionState (edu.cmu.cs.hcii.cogtool.ui.TaskSelectionState)2 AListenerAction (edu.cmu.cs.hcii.cogtool.util.AListenerAction)2