Search in sources :

Example 1 with RecoverableException

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

the class UI method performAction.

/**
     * Wrapper method for invoking performAction; uses this as the
     * transmuter.  ContextMenuManager is currently the only place that
     * invokes performAction as "context".
     *
     * @param id the general semantic LID to lookup and perform action for
     * @param isContextSelection true if we should specialize based on the
     *                           current contextual selection;
     *                           false to use the standard selection
     * @return <code>true</code> if it is ok to continue with action processing
     *         and <code>false</code> if processing should stop.
     */
public boolean performAction(ListenerIdentifier id, boolean isContextSelection) {
    View view = getView();
    boolean wasPerforming = view.isPerformingAction();
    Shell s = getShell();
    Cursor oldCursor = s.getCursor();
    view.setStatusMessage("");
    view.setPerformingAction(true);
    s.setCursor(WindowUtil.BUSY_CURSOR_INSTANCE);
    // false means a normal action, not caused by a context menu selection
    try {
        return lIDMap.performAction(this, id, isContextSelection);
    } catch (RecoverableException ex) {
        RcvrExceptionHandler.recover(ex, getStandardInteraction());
    } finally {
        view.setPerformingAction(wasPerforming);
        if (!s.isDisposed()) {
            s.setCursor(oldCursor);
        }
    }
    return false;
}
Also used : Shell(org.eclipse.swt.widgets.Shell) Cursor(org.eclipse.swt.graphics.Cursor) AboutView(edu.cmu.cs.hcii.cogtool.view.AboutView) View(edu.cmu.cs.hcii.cogtool.view.View) RecoverableException(edu.cmu.cs.hcii.cogtool.util.RecoverableException)

Example 2 with RecoverableException

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

the class ProjectController method exportDesignToXML.

private boolean exportDesignToXML(Design design) {
    String defaultFilename = ((design != null) ? design.getName() : project.getName()) + ".xml";
    File exportFile = null;
    if (interaction != null && CogTool.exportCSVKludgeDir == null) {
        exportFile = interaction.selectXMLFile(false, defaultFilename);
    } else {
        exportFile = new File(CogTool.exportCSVKludgeDir, defaultFilename);
    }
    if (exportFile == null) {
        return false;
    }
    OutputStream oStream = null;
    String completionMsg;
    try {
        try {
            oStream = new FileOutputStream(exportFile);
            Writer xmlWriter = new BufferedWriter(new OutputStreamWriter(oStream, "UTF-8"));
            if (design == null) {
                ExportCogToolXML.exportXML(project, xmlWriter, "UTF-8");
                completionMsg = allDesignsExportedToXml;
            } else {
                Map<ITaskDesign, TaskApplication> designTAs = project.taskApplicationsForDesign(design);
                ExportCogToolXML.exportXML(design, designTAs, xmlWriter, "UTF-8");
                completionMsg = designExportedToXml;
            }
            xmlWriter.flush();
        } finally {
            if (oStream != null) {
                oStream.close();
            }
        }
    } catch (IllegalArgumentException e) {
        throw new RcvrIllegalStateException("Invalid argument exporting to XML", e);
    } catch (IllegalStateException e) {
        throw new RcvrIllegalStateException("Exporting to XML", e);
    } catch (IOException e) {
        throw new RcvrIOSaveException("Exporting to XML", e);
    } catch (Exception e) {
        throw new RecoverableException("Exporting to XML", e);
    }
    if (interaction != null) {
        interaction.setStatusMessage(completionMsg + " " + exportFile.getAbsolutePath());
    }
    return true;
}
Also used : RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) RcvrIOSaveException(edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException) RcvrXMLParsingException(edu.cmu.cs.hcii.cogtool.util.RcvrXMLParsingException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RcvrUnimplementedFnException(edu.cmu.cs.hcii.cogtool.util.RcvrUnimplementedFnException) RcvrCannotUndoRedoException(edu.cmu.cs.hcii.cogtool.util.RcvrCannotUndoRedoException) IOException(java.io.IOException) RcvrImportException(edu.cmu.cs.hcii.cogtool.util.RcvrImportException) RcvrClipboardException(edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException) RcvrImageException(edu.cmu.cs.hcii.cogtool.util.RcvrImageException) RcvrOutOfMemoryException(edu.cmu.cs.hcii.cogtool.util.RcvrOutOfMemoryException) FileNotFoundException(java.io.FileNotFoundException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) SAXException(org.xml.sax.SAXException) RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) RcvrParsingException(edu.cmu.cs.hcii.cogtool.util.RcvrParsingException) RecoverableException(edu.cmu.cs.hcii.cogtool.util.RecoverableException) RcvrIOSaveException(edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException) RcvrIOTempException(edu.cmu.cs.hcii.cogtool.util.RcvrIOTempException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) BufferedWriter(java.io.BufferedWriter) RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) FileWriter(java.io.FileWriter) RecoverableException(edu.cmu.cs.hcii.cogtool.util.RecoverableException)

Example 3 with RecoverableException

use of edu.cmu.cs.hcii.cogtool.util.RecoverableException 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 4 with RecoverableException

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

the class DesignEditorController method createExportDesignToXMLAction.

protected IListenerAction createExportDesignToXMLAction() {
    return new AListenerAction() {

        public boolean performAction(Object actionParms) {
            String defaultFilename = design.getName() + ".xml";
            File exportFile = interaction.selectXMLFile(false, defaultFilename);
            if (exportFile == null) {
                return false;
            }
            OutputStream oStream = null;
            try {
                try {
                    oStream = new FileOutputStream(exportFile);
                    Writer xmlWriter = new BufferedWriter(new OutputStreamWriter(oStream, "UTF-8"));
                    Map<ITaskDesign, TaskApplication> designTAs = project.taskApplicationsForDesign(design);
                    ExportCogToolXML.exportXML(design, designTAs, xmlWriter, "UTF-8");
                    xmlWriter.flush();
                } finally {
                    if (oStream != null) {
                        oStream.close();
                    }
                }
            } catch (IllegalArgumentException e) {
                throw new RcvrIllegalStateException("Invalid argument exporting to XML", e);
            } catch (IllegalStateException e) {
                throw new RcvrIllegalStateException("Exporting to XML", e);
            } catch (IOException e) {
                throw new RcvrIOSaveException("Exporting to XML", e);
            } catch (Exception e) {
                throw new RecoverableException("Exporting to XML", e);
            }
            interaction.setStatusMessage(L10N.get("PC.DesignExportedToXML", "Design exported to XML:") + " " + exportFile.getName());
            return true;
        }
    };
}
Also used : RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) AListenerAction(edu.cmu.cs.hcii.cogtool.util.AListenerAction) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) IOException(java.io.IOException) RcvrIOSaveException(edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException) RcvrClipboardException(edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException) RcvrImageException(edu.cmu.cs.hcii.cogtool.util.RcvrImageException) RcvrOutOfMemoryException(edu.cmu.cs.hcii.cogtool.util.RcvrOutOfMemoryException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) SAXException(org.xml.sax.SAXException) RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) RcvrUIException(edu.cmu.cs.hcii.cogtool.util.RcvrUIException) IOException(java.io.IOException) RecoverableException(edu.cmu.cs.hcii.cogtool.util.RecoverableException) RcvrIOSaveException(edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) BufferedWriter(java.io.BufferedWriter) RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) File(java.io.File) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) RecoverableException(edu.cmu.cs.hcii.cogtool.util.RecoverableException)

Example 5 with RecoverableException

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

the class UI method performAction.

/**
     * Perform the requested semantic action.
     * <p>
     * Finds the code snippet object associated with the given semantic
     * identifier and, if found, executes the application's semantic action.
     * <p>
     * If not found, this simply does nothing.  By not throwing an exception,
     * we allow for flexibility as to how to enable/disable certain
     * functionality.
     * <p>
     * Set-up of the actual <code>performAction</code> call only occurs
     * if cleanup is requested, since set-up might require cleanup!
     *
     * @param id the transmuted key specifying the semantic nature of the
     *           action to be performed
     * @param actionParms data needed by this operation to perform the action
     * @param doCleanup whether or not to perform the cleanup operation
     *                  (non-menuHidden!)
     * @return <code>true</code> if it is ok to continue with action processing
     *         and <code>false</code> if processing should stop.
     * @author mlh
     */
public boolean performAction(ListenerIdentifier id, Object actionParms, boolean doCleanup) {
    View view = getView();
    boolean okToProceed = false;
    boolean wasPerforming = view.isPerformingAction();
    view.setStatusMessage("");
    view.setPerformingAction(true);
    try {
        if (doCleanup) {
            setUpPerformAction(id);
        }
        okToProceed = lIDMap.performAction(id, actionParms);
        if (doCleanup) {
            // not a context menu "hide"!
            cleanup(okToProceed, false);
        }
    } catch (RecoverableException ex) {
        RcvrExceptionHandler.recover(ex, getStandardInteraction());
    } finally {
        view.setPerformingAction(wasPerforming);
    }
    return okToProceed;
}
Also used : AboutView(edu.cmu.cs.hcii.cogtool.view.AboutView) View(edu.cmu.cs.hcii.cogtool.view.View) RecoverableException(edu.cmu.cs.hcii.cogtool.util.RecoverableException)

Aggregations

RecoverableException (edu.cmu.cs.hcii.cogtool.util.RecoverableException)5 File (java.io.File)3 IOException (java.io.IOException)3 ITaskDesign (edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign)2 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)2 RcvrClipboardException (edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException)2 RcvrIOException (edu.cmu.cs.hcii.cogtool.util.RcvrIOException)2 RcvrIOSaveException (edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException)2 RcvrIllegalStateException (edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException)2 RcvrImageException (edu.cmu.cs.hcii.cogtool.util.RcvrImageException)2 RcvrOutOfMemoryException (edu.cmu.cs.hcii.cogtool.util.RcvrOutOfMemoryException)2 AboutView (edu.cmu.cs.hcii.cogtool.view.AboutView)2 View (edu.cmu.cs.hcii.cogtool.view.View)2 BufferedWriter (java.io.BufferedWriter)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 SAXException (org.xml.sax.SAXException)2