Search in sources :

Example 1 with ProjectContextSelectionState

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

the class CogTool method main.

public static void main(String[] args) {
    // Do a clean exit if running on a machine with an old JRE.
    if (!OSUtils.supportCogTool()) {
        System.out.println("JRE 1.5 or later is required to run CogTool");
        // TODO: add simple window with message?
        System.exit(1024);
    }
    if (revision == null) {
        // we're running under the development environment; get the revision
        // dynamically, and also make our subprocesses echo what's going
        // on to stdout
        revision = getRevisionAtRuntime();
        Subprocess.setDebug(true);
    }
    // Insert the two phases into the delayed work manager, selection
    // first and repaint second, since selection can cause repaint requests
    delayedWorkMgr.addDelayedWork(selectionPhase);
    delayedWorkMgr.addDelayedWork(repaintPhase);
    try {
        if (OSUtils.MACOSX) {
            if (!OSUtils.isIntelMac()) {
                System.out.println("CogTool no longer runs on PowerPC");
                System.exit(16);
            }
            // we need to create the RootController, but will never
            // actually need to interact with it programmatically
            rootCtl = new RootController();
        }
        // TODO temporary kludge until we update the preference dialog to supply
        //      a UI for turning this on and off
        CogToolPref.IS_LOGGING.setBoolean(true);
        enableLogging(CogToolPref.IS_LOGGING.getBoolean());
        OptionParser parser = new OptionParser("f:i:re:s:qQ");
        // The psn is supplied on MacOS when a GUI application is double-clicked;
        // we just ignore it, but need to recognize it so we can ignore it.
        parser.accepts("psn", "process serial number (ignored)").withRequiredArg();
        OptionSet opts = parser.parse(args);
        if (opts.has("Q")) {
            quietCommands = true;
        }
        List<String> filesToLoad = new ArrayList<String>();
        for (Object obj : opts.nonOptionArguments()) {
            filesToLoad.add((String) obj);
        }
        List<RecoverableException> loadExceptions = new ArrayList<RecoverableException>();
        ProjectController openedProject = null;
        File file = null;
        if (!filesToLoad.isEmpty()) {
            ObjectPersister persister = ObjectPersister.ONLY;
            for (String fn : filesToLoad) {
                try {
                    file = new File(fn);
                    Project proj = (Project) persister.load(file);
                    openedProject = ProjectController.openController(proj, false, true);
                } catch (Exception e) {
                    RecoverableException re = new RecoverableException("Error occurred while loading: " + fn, e);
                    RcvrExceptionHandler.recover(re, interaction);
                }
            }
        }
        if (opts.has("f")) {
            openedProject = (new CommandFile((String) opts.valueOf("f"))).run();
        } else {
            if (opts.has("i")) {
                openedProject = ProjectController.newProjectController();
                openedProject.importFile = new File((String) opts.valueOf("i"));
                openedProject.importFileComputes = opts.has("r");
                openedProject.performAction(CogToolLID.ImportXML, new ProjectContextSelectionState(openedProject.getModel()));
            }
            if (openedProject != null && opts.has("e")) {
                openedProject.exportFile = (String) opts.valueOf("e");
                openedProject.exportResultsToCSV();
            }
            if (openedProject != null && opts.has("s")) {
                openedProject.saveAsFilename((String) opts.valueOf("s"));
            }
            if (opts.has("q")) {
                System.exit(0);
            }
        }
        if (openedProject != null) {
            if (System.getProperty("edu.cmu.cs.hcii.cogtool.ExportCSVKludge") != null) {
                exportCSVKludgeDir = file.getAbsoluteFile().getParentFile();
                openedProject.exportCSVKludge();
                System.exit(0);
            }
        }
        if (openedProject == null) {
            // no project was opened successfully, or none were specified,
            // so create a fresh project and use its Interaction to
            // report load errors
            ProjectController c = ProjectController.newProjectController();
            c.setLocation(25.0, 25.0);
            String response = c.getInteraction().createNewOrOpenExisting();
            if (response == ProjectInteraction.CREATE) {
                // Populate the empty project to avoid a "blank screen".
                c.populateProject();
            } else if (response == null) {
                c.requestClose();
            } else {
                if (response == ProjectInteraction.OPEN) {
                    c.openExistingProject(c.getLocation());
                } else {
                    c.performAction(CogToolLID.OpenProjectFile, response, false);
                }
                if (ControllerRegistry.ONLY.openControllerCount() > 1) {
                    c.requestClose();
                }
            }
        }
        //            } else if (command.equals("export-csv")) {
        //                if (openedProject == null) {
        //                    System.err.println(String.format(
        //                            L10N.get("CT.NoFileForCSV",
        //                                     "Couldn't open %s for CSV export."),
        //                            arguments.get(0)));
        //                    System.exit(32);
        //                }
        //                exportCSVKludgeDir =
        //                    file.getAbsoluteFile().getParentFile();
        //                openedProject.exportCSVKludge();
        //                System.exit(0);
        //            }
        // Note that the following catches and does not rethrow any
        // SWTExceptions. This means reportTopLevelException never gets
        // a chance to report these to the user.
        WindowUtil.interact();
    } catch (RecoverableException e) {
        RcvrExceptionHandler.recover(e, interaction);
    } catch (Exception e) {
        System.err.println("Catching exception...");
        reportTopLevelAnomaly(e);
    } catch (Error e) {
        // Before trying to throw up a dialog it is important that we dump
        // the trace to stderr, the same way as would happen if this went
        // uncaught. At least that way if we are in so hosed a state that
        // we can't even popup the dialog we've made the patient no worse
        // than it would have been had we not caught this. We don't even
        // wait for reportTopLevelAnomoly before printing it in case just
        // that extra level of function call will make a difference in
        // whether or not we succeed.
        System.err.println("Catching error...");
        e.printStackTrace();
        reportTopLevelAnomaly(e);
    } catch (Throwable e) {
        // This shouldn't be possible -- the only unchecked Throwables
        // are Errors and RuntimeExceptions, both of which should have
        // been caught above. But just in case someone does something
        // deeply bizarre and has something we call in this try able to
        // throw a checked non-Exception Throwable, let's be extra paranoid.
        System.err.println("Catching throwable...");
        reportTopLevelAnomaly(new Exception(L10N.get("CT.UncaughtThrowable", ("Uncaught non-Exception, non-Error Throwable " + "propagated all the way to top-level.")), e));
    }
    // here.
    if (!WindowUtil.GLOBAL_DISPLAY.isDisposed()) {
        WindowUtil.GLOBAL_DISPLAY.close();
    }
    // Just calling the preceding is *not* sufficient to ensure we quit,
    // in the rare case where we've thrown a non-recoverable Exception
    // but left a background thread alive
    System.exit(-1);
}
Also used : ProjectContextSelectionState(edu.cmu.cs.hcii.cogtool.ui.ProjectContextSelectionState) ObjectPersister(edu.cmu.cs.hcii.cogtool.util.ObjectPersister) ArrayList(java.util.ArrayList) RootController(edu.cmu.cs.hcii.cogtool.controller.RootController) OptionParser(joptsimple.OptionParser) ProjectController(edu.cmu.cs.hcii.cogtool.controller.ProjectController) CommandFile(edu.cmu.cs.hcii.cogtool.controller.CommandFile) IOException(java.io.IOException) RecoverableException(edu.cmu.cs.hcii.cogtool.util.RecoverableException) Project(edu.cmu.cs.hcii.cogtool.model.Project) OptionSet(joptsimple.OptionSet) CommandFile(edu.cmu.cs.hcii.cogtool.controller.CommandFile) File(java.io.File) RecoverableException(edu.cmu.cs.hcii.cogtool.util.RecoverableException)

Example 2 with ProjectContextSelectionState

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

the class ProjectController method createImportAction.

protected IListenerAction createImportAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object prms) {
            boolean computeScripts = CogToolPref.COMPSCR.getBoolean();
            if (importFile == null) {
                importFile = interaction.selectXMLFile(true, null);
            } else {
                computeScripts = importFileComputes;
            }
            if (importFile == null) {
                return false;
            }
            CompoundUndoableEdit editSeq = new CompoundUndoableEdit(importXMLPresentation, ProjectLID.ImportXML);
            Map<Design, Collection<Demonstration>> parsedDesigns = null;
            Set<AUndertaking> newUndertakings = null;
            TaskParent parent = project;
            try {
                if (CogToolPref.HCIPA.getBoolean()) {
                    TaskGroup importGroup = new TaskGroup(importFile.getName(), GroupNature.SUM);
                    parent = importGroup;
                    project.addUndertaking(importGroup);
                    editSeq.addEdit(createNewTaskUndo(project, Project.AT_END, importGroup, ProjectLID.ImportXML, importXMLPresentation));
                }
                ImportCogToolXML importer = new ImportCogToolXML();
                if (!importer.importXML(importFile, parent, MODELGEN_ALG) || (importer.getDesigns().size() == 0)) {
                    List<String> errors = importer.getObjectFailures();
                    if ((errors != null) && (errors.size() > 0)) {
                        interaction.reportProblems(importXMLPresentation, errors);
                    } else {
                        interaction.reportProblem(importXMLPresentation, xmlParseFailed);
                    }
                    return false;
                }
                List<String> warnings = importer.getGenerationWarnings();
                if (warnings.size() > 0) {
                    interaction.reportWarnings(importXMLPresentation, warnings);
                }
                parsedDesigns = importer.getDesigns();
                newUndertakings = importer.getNewUndertakings();
            } catch (ImportCogToolXML.ImportFailedException ex) {
                throw new RcvrXMLParsingException("Missing XML component", ex);
            } catch (GraphicsUtil.ImageException ex) {
                throw new RcvrImageException("Image error during loading XML", ex);
            } catch (IOException ex) {
                throw new RcvrXMLParsingException("IO error loading XML", ex);
            } catch (SAXException ex) {
                throw new RcvrXMLParsingException("Error parsing XML", ex);
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                importFile = null;
                importFileComputes = false;
            }
            for (AUndertaking u : newUndertakings) {
                parent.addUndertaking(u);
                final AUndertaking uu = u;
                final TaskParent pp = parent;
                editSeq.addEdit(new AUndoableEdit(ProjectLID.ImportXML) {

                    @Override
                    public void redo() {
                        super.redo();
                        pp.addUndertaking(uu);
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        pp.removeUndertaking(uu);
                    }
                });
            }
            Iterator<Map.Entry<Design, Collection<Demonstration>>> designDemos = parsedDesigns.entrySet().iterator();
            while (designDemos.hasNext()) {
                Map.Entry<Design, Collection<Demonstration>> entry = designDemos.next();
                importDesign(parent, (DesignSelectionState) prms, entry.getKey(), entry.getValue(), newUndertakings, editSeq);
            }
            editSeq.end();
            undoMgr.addEdit(editSeq);
            if (computeScripts) {
                //compute predictions for imported project
                ProjectContextSelectionState seln = new ProjectContextSelectionState(project);
                seln.addSelectedDesigns(project.getDesigns());
                ui.selectAllTasks();
                recomputeScripts(seln);
                ui.deselectAllTasks();
            }
            return true;
        }
    };
}
Also used : ProjectContextSelectionState(edu.cmu.cs.hcii.cogtool.ui.ProjectContextSelectionState) SAXException(org.xml.sax.SAXException) Design(edu.cmu.cs.hcii.cogtool.model.Design) ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) URLCrawlEntry(edu.cmu.cs.hcii.cogtool.model.URLCrawlEntry) ImportCogToolXML(edu.cmu.cs.hcii.cogtool.model.ImportCogToolXML) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) RcvrImageException(edu.cmu.cs.hcii.cogtool.util.RcvrImageException) TaskParent(edu.cmu.cs.hcii.cogtool.model.TaskParent) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) ProjectSelectionState(edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) AUndertaking(edu.cmu.cs.hcii.cogtool.model.AUndertaking) Collection(java.util.Collection) GraphicsUtil(edu.cmu.cs.hcii.cogtool.util.GraphicsUtil) RcvrXMLParsingException(edu.cmu.cs.hcii.cogtool.util.RcvrXMLParsingException) Demonstration(edu.cmu.cs.hcii.cogtool.model.Demonstration) TaskGroup(edu.cmu.cs.hcii.cogtool.model.TaskGroup) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

ProjectContextSelectionState (edu.cmu.cs.hcii.cogtool.ui.ProjectContextSelectionState)2 IOException (java.io.IOException)2 CommandFile (edu.cmu.cs.hcii.cogtool.controller.CommandFile)1 ProjectController (edu.cmu.cs.hcii.cogtool.controller.ProjectController)1 RootController (edu.cmu.cs.hcii.cogtool.controller.RootController)1 AUndertaking (edu.cmu.cs.hcii.cogtool.model.AUndertaking)1 Demonstration (edu.cmu.cs.hcii.cogtool.model.Demonstration)1 Design (edu.cmu.cs.hcii.cogtool.model.Design)1 ImportCogToolXML (edu.cmu.cs.hcii.cogtool.model.ImportCogToolXML)1 Project (edu.cmu.cs.hcii.cogtool.model.Project)1 ITaskDesign (edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign)1 TaskGroup (edu.cmu.cs.hcii.cogtool.model.TaskGroup)1 TaskParent (edu.cmu.cs.hcii.cogtool.model.TaskParent)1 URLCrawlEntry (edu.cmu.cs.hcii.cogtool.model.URLCrawlEntry)1 ProjectSelectionState (edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState)1 AUndoableEdit (edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)1 CompoundUndoableEdit (edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit)1 GraphicsUtil (edu.cmu.cs.hcii.cogtool.util.GraphicsUtil)1 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)1 ObjectPersister (edu.cmu.cs.hcii.cogtool.util.ObjectPersister)1