Search in sources :

Example 6 with TetradMetadata

use of edu.cmu.tetradapp.model.TetradMetadata in project tetrad by cmu-phil.

the class SaveSessionAction method actionPerformed.

/**
 * Performs the action of loading a session from a file.
 */
public void actionPerformed(ActionEvent e) {
    // Get the frontmost SessionWrapper.
    SessionEditorIndirectRef sessionEditorRef = DesktopController.getInstance().getFrontmostSessionEditor();
    SessionEditor sessionEditor = (SessionEditor) sessionEditorRef;
    SessionEditorWorkbench workbench = sessionEditor.getSessionWorkbench();
    SessionWrapper sessionWrapper = workbench.getSessionWrapper();
    TetradMetadata metadata = new TetradMetadata();
    File file = new File(Preferences.userRoot().get("sessionSaveLocation", Preferences.userRoot().absolutePath()), sessionWrapper.getName());
    if (!file.exists() || sessionWrapper.isNewSession()) {
        SaveSessionAsAction saveSessionAsAction = new SaveSessionAsAction();
        saveSessionAsAction.actionPerformed(e);
        this.saved = saveSessionAsAction.isSaved();
        return;
    }
    if (file.exists()) {
        int ret = JOptionPane.showConfirmDialog(JOptionUtils.centeringComp(), "File already exists. Overwrite?", "Save", JOptionPane.YES_NO_OPTION);
        if (ret == JOptionPane.NO_OPTION) {
            SaveSessionAsAction saveSessionAsAction = new SaveSessionAsAction();
            saveSessionAsAction.actionPerformed(e);
            this.saved = saveSessionAsAction.isSaved();
            return;
        }
    }
    // Save it.
    try {
        FileOutputStream out = new FileOutputStream(file);
        ObjectOutputStream objOut = new ObjectOutputStream(out);
        sessionWrapper.setNewSession(false);
        objOut.writeObject(metadata);
        objOut.writeObject(sessionWrapper);
        out.close();
        // JOptionPane.showMessageDialog(JOptionUtils.centeringComp(),
        // "Session saved.");
        FileInputStream in = new FileInputStream(file);
        ObjectInputStream objIn = new ObjectInputStream(in);
        objIn.readObject();
    } catch (Exception e2) {
        e2.printStackTrace();
        JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "An error occurred while attempting to save the session as " + file.getAbsolutePath() + ".");
    }
    sessionWrapper.setSessionChanged(false);
    DesktopController.getInstance().putMetadata(sessionWrapper, metadata);
}
Also used : TetradMetadata(edu.cmu.tetradapp.model.TetradMetadata) SessionEditorIndirectRef(edu.cmu.tetradapp.util.SessionEditorIndirectRef) SessionWrapper(edu.cmu.tetradapp.model.SessionWrapper)

Aggregations

SessionWrapper (edu.cmu.tetradapp.model.SessionWrapper)6 TetradMetadata (edu.cmu.tetradapp.model.TetradMetadata)6 SessionEditorIndirectRef (edu.cmu.tetradapp.util.SessionEditorIndirectRef)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Session (edu.cmu.tetrad.session.Session)2 Version (edu.cmu.tetrad.util.Version)2 Date (java.util.Date)2 DecompressibleInputStream (edu.cmu.tetradapp.app.DecompressibleInputStream)1 SessionEditor (edu.cmu.tetradapp.app.SessionEditor)1 SessionEditorWorkbench (edu.cmu.tetradapp.app.SessionEditorWorkbench)1 TetradMetadataIndirectRef (edu.cmu.tetradapp.util.TetradMetadataIndirectRef)1 WatchedProcess (edu.cmu.tetradapp.util.WatchedProcess)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 List (java.util.List)1