Search in sources :

Example 6 with ProjectSelectionState

use of edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState 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)

Example 7 with ProjectSelectionState

use of edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState in project cogtool by cogtool.

the class ProjectController method createDisplayTraces.

// Action for DisplayTraces
protected IListenerAction createDisplayTraces() {
    return new IListenerAction() {

        public Class<?> getParameterClass() {
            return ProjectSelectionState.class;
        }

        public boolean performAction(Object actionParms) {
            ProjectSelectionState sel = (ProjectSelectionState) actionParms;
            Design design = sel.getSelectedDesign();
            AUndertaking[] tasks = sel.getSelectedTasks(TaskSelectionState.PRUNE_SELECTION | TaskSelectionState.ORDER_SELECTION);
            if (design != null) {
                if ((tasks != null) && (tasks.length > 0)) {
                    for (AUndertaking task : tasks) {
                        displayTraces(task, design);
                    }
                } else {
                    Iterator<AUndertaking> allTasks = project.getUndertakings().iterator();
                    while (allTasks.hasNext()) {
                        displayTraces(allTasks.next(), design);
                    }
                }
            } else if ((tasks != null) && (tasks.length > 0)) {
                for (AUndertaking task : tasks) {
                    Iterator<Design> allDesigns = project.getDesigns().iterator();
                    while (allDesigns.hasNext()) {
                        displayTraces(task, allDesigns.next());
                    }
                }
            }
            return true;
        }
    };
}
Also used : 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) ProjectSelectionState(edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) Iterator(java.util.Iterator)

Example 8 with ProjectSelectionState

use of edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState in project cogtool by cogtool.

the class ProjectController method createImportHumanCSVFileAction.

// Action for ImportHumanCSVFile
protected IListenerAction createImportHumanCSVFileAction() {
    return new IListenerAction() {

        public Class<?> getParameterClass() {
            return ProjectSelectionState.class;
        }

        public boolean performAction(Object prms) {
            ProjectSelectionState seln = (ProjectSelectionState) prms;
            // Must have selected tasks and design
            Design design = seln.getSelectedDesign();
            AUndertaking[] tasks = seln.getSelectedTasks(TaskSelectionState.PRUNE_SELECTION);
            if ((design == null) || (tasks == null) || (tasks.length == 0)) {
                return false;
            }
            List<String> outputLines = new LinkedList<String>();
            List<String> errorLines = new LinkedList<String>();
            // Get csv file
            File dataFile = interaction.selectCSVFile();
            if (dataFile == null) {
                interaction.setStatusMessage(IMPORT_FAIL_NOFILE_MSG);
                return false;
            }
            // Read lines out of file
            FileReader reader = null;
            try {
                reader = new FileReader(dataFile);
                FileUtil.readLines(reader, outputLines);
            } catch (FileNotFoundException e) {
                interaction.setStatusMessage(IMPORT_FAIL_NOFILE_MSG);
                throw new RcvrIOTempException("Error in parsing csv", e);
            } catch (IOException e) {
                interaction.setStatusMessage(IMPORT_FAIL_NOREAD_MSG);
                throw new RcvrParsingException("Error in parsing csv", e);
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                    // ignore
                    }
                    reader = null;
                }
            }
            // parse ResultSteps out of trace lines
            TraceParser<ResultStep> parser = new HumanCSVParser();
            List<ResultStep> steps;
            try {
                steps = parser.parseTrace(outputLines);
            } catch (TraceParser.ParseException e) {
                interaction.setStatusMessage(IMPORT_FAIL_PARSE_IMPL_MSG);
                throw new RcvrParsingException("Error in parsing implementation", e);
            }
            System.out.println(steps);
            if (steps.size() < 1) {
                interaction.setStatusMessage(IMPORT_FAIL_PARSE_MSG);
                return false;
            }
            double taskTime = -1.0;
            Iterator<ResultStep> stepIt = steps.iterator();
            while (stepIt.hasNext()) {
                ResultStep step = stepIt.next();
                taskTime = Math.max(taskTime, step.startTime + step.duration);
            }
            // Scale time to seconds.
            taskTime /= 1000.0;
            // -------------- Done parsing
            DemoStateManager demoMgr = DemoStateManager.getStateManager(project, design);
            final TaskApplication[] taskApps = new TaskApplication[tasks.length];
            final APredictionResult[] oldResults = new APredictionResult[tasks.length];
            final APredictionResult[] results = new APredictionResult[tasks.length];
            final IPredictionAlgo[] oldActiveAlgos = new IPredictionAlgo[tasks.length];
            for (int i = 0; i < tasks.length; i++) {
                taskApps[i] = ensureTaskApplication(tasks[i], design, MODELGEN_ALG, demoMgr);
                // If for some reason there are no result steps,
                // create a TimePredictionResult that
                // reflects COMPUTATION_FAILED.
                // Otherwise, create a normal one.
                Script script = taskApps[i].getScript(MODELGEN_ALG);
                if (taskTime < 0.0) {
                    results[i] = new TimePredictionResult(dataFile.getName(), script, HumanDataAlgo.ONLY, outputLines, errorLines, steps);
                } else {
                    results[i] = new TimePredictionResult(dataFile.getName(), script, HumanDataAlgo.ONLY, outputLines, errorLines, steps, taskTime);
                }
                oldResults[i] = taskApps[i].getResult(MODELGEN_ALG, HumanDataAlgo.ONLY);
                taskApps[i].setResult(MODELGEN_ALG, HumanDataAlgo.ONLY, results[i]);
                oldActiveAlgos[i] = taskApps[i].getActiveAlgorithm();
                taskApps[i].setActiveAlgorithm(HumanDataAlgo.ONLY);
            }
            IUndoableEdit edit = new AUndoableEdit(ProjectLID.RecomputeScript) {

                @Override
                public String getPresentationName() {
                    return IMPORT_HUMAN_CSV;
                }

                @Override
                public void redo() {
                    super.redo();
                    for (int i = 0; i < taskApps.length; i++) {
                        taskApps[i].setResult(MODELGEN_ALG, HumanDataAlgo.ONLY, results[i]);
                        taskApps[i].setActiveAlgorithm(HumanDataAlgo.ONLY);
                    }
                }

                @Override
                public void undo() {
                    super.undo();
                    for (int i = 0; i < taskApps.length; i++) {
                        taskApps[i].setResult(MODELGEN_ALG, HumanDataAlgo.ONLY, oldResults[i]);
                        taskApps[i].setActiveAlgorithm(oldActiveAlgos[i]);
                    }
                }
            };
            undoMgr.addEdit(edit);
            interaction.setStatusMessage(IMPORT_SUCCESS_MSG);
            return true;
        }
    };
}
Also used : TraceParser(edu.cmu.cs.hcii.cogtool.model.TraceParser) FileNotFoundException(java.io.FileNotFoundException) RcvrParsingException(edu.cmu.cs.hcii.cogtool.util.RcvrParsingException) Design(edu.cmu.cs.hcii.cogtool.model.Design) ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) ResultStep(edu.cmu.cs.hcii.cogtool.model.ResultStep) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) HumanCSVParser(edu.cmu.cs.hcii.cogtool.model.HumanCSVParser) TimePredictionResult(edu.cmu.cs.hcii.cogtool.model.TimePredictionResult) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) FileReader(java.io.FileReader) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) Script(edu.cmu.cs.hcii.cogtool.model.Script) IPredictionAlgo(edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo) ProjectSelectionState(edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState) APredictionResult(edu.cmu.cs.hcii.cogtool.model.APredictionResult) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) LinkedList(java.util.LinkedList) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) RcvrIOTempException(edu.cmu.cs.hcii.cogtool.util.RcvrIOTempException) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) File(java.io.File)

Example 9 with ProjectSelectionState

use of edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState in project cogtool by cogtool.

the class ProjectController method createShowModelVisualizationAction.

// Action for ShowVisualization
protected IListenerAction createShowModelVisualizationAction() {
    return new IListenerAction() {

        public Class<?> getParameterClass() {
            return ProjectSelectionState.class;
        }

        public boolean performAction(Object prms) {
            ProjectSelectionState seln = (ProjectSelectionState) prms;
            // Must have selected tasks and design
            Design design = seln.getSelectedDesign();
            AUndertaking[] tasks = seln.getSelectedTasks(TaskSelectionState.PRUNE_SELECTION);
            if ((design == null) || (tasks == null) || (tasks.length == 0)) {
                return false;
            }
            boolean visCreated = false;
            for (AUndertaking task : tasks) {
                if (createVisualization(design, task, -1)) {
                    visCreated = true;
                    break;
                }
            }
            if (!visCreated) {
                interaction.reportProblem(visualizationNotCreated, noResultsToVisualize);
            }
            return visCreated;
        }
    };
}
Also used : 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) ProjectSelectionState(edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking)

Example 10 with ProjectSelectionState

use of edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState in project cogtool by cogtool.

the class ProjectController method createScriptEditorAction.

// Action for EditScript
protected IListenerAction createScriptEditorAction() {
    return new IListenerAction() {

        public Class<?> getParameterClass() {
            return ProjectSelectionState.class;
        }

        public boolean performAction(Object prms) {
            if (prms != null) {
                ProjectSelectionState seln = (ProjectSelectionState) prms;
                // Must have selected tasks and design
                Design design = seln.getSelectedDesign();
                AUndertaking[] tasks = seln.getSelectedTasks(TaskSelectionState.PRUNE_SELECTION);
                if ((design == null) || (tasks == null) || (tasks.length == 0)) {
                    return false;
                }
                DemoStateManager demoMgr = DemoStateManager.getStateManager(project, design);
                // Editing a script only applies to tasks, not task groups
                for (int i = 0; i < tasks.length; i++) {
                    if (!tasks[i].isTaskGroup()) {
                        CognitiveModelGenerator gen = MODELGEN_ALG;
                        TaskGroup group = tasks[i].getTaskGroup();
                        if (group != null) {
                            Object isSnifAct = group.getAttribute(WidgetAttributes.SNIFACT_CONTEXT_ATTR);
                            if (isSnifAct != null) {
                                gen = IdentityModelGenerator.ONLY;
                            }
                        }
                        // If no script set exists for this cell, create
                        TaskApplication ta = ensureTaskApplication(tasks[i], design, gen, demoMgr);
                        Demonstration demo = ta.getDemonstration();
                        if (CogToolPref.HCIPA.getBoolean()) {
                            HCIPACmd.checkStartFrame(project, ta, gen);
                        }
                        // Determine which window to open/create
                        if ((demo.getStartFrame() == null) || !demo.isStartFrameChosen()) {
                            // No start frame; present ui to choose one
                            SEFrameChooserController.openController(ta, gen, project);
                        } else {
                            // Start frame chosen; go straight to demo ui
                            try {
                                SEDemoController.openController(ta, gen, project);
                            } catch (GraphicsUtil.ImageException ex) {
                                interaction.protestInvalidImageFile();
                            }
                        }
                    }
                // Else do nothing when a TaskGroup cell is "edited"
                }
                return true;
            }
            interaction.protestNoSelection();
            return false;
        }
    };
}
Also used : ProjectSelectionState(edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState) CognitiveModelGenerator(edu.cmu.cs.hcii.cogtool.model.CognitiveModelGenerator) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) 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) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) GraphicsUtil(edu.cmu.cs.hcii.cogtool.util.GraphicsUtil) Demonstration(edu.cmu.cs.hcii.cogtool.model.Demonstration) TaskGroup(edu.cmu.cs.hcii.cogtool.model.TaskGroup)

Aggregations

ProjectSelectionState (edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState)21 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)20 Design (edu.cmu.cs.hcii.cogtool.model.Design)19 ITaskDesign (edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign)19 AUndertaking (edu.cmu.cs.hcii.cogtool.model.AUndertaking)15 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)12 RcvrIOException (edu.cmu.cs.hcii.cogtool.util.RcvrIOException)7 IOException (java.io.IOException)7 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)6 IPredictionAlgo (edu.cmu.cs.hcii.cogtool.model.IPredictionAlgo)6 Script (edu.cmu.cs.hcii.cogtool.model.Script)6 File (java.io.File)6 TaskGroup (edu.cmu.cs.hcii.cogtool.model.TaskGroup)4 AUndoableEdit (edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)4 RcvrUnimplementedFnException (edu.cmu.cs.hcii.cogtool.util.RcvrUnimplementedFnException)4 ACTRPredictionAlgo (edu.cmu.cs.hcii.cogtool.model.ACTRPredictionAlgo)3 ISimilarityDictionary (edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary)3 RcvrIllegalStateException (edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException)3 RcvrIOSaveException (edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException)2 FileOutputStream (java.io.FileOutputStream)2