Search in sources :

Example 11 with SessionWrapper

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

the class TetradDesktop method getNewSessionName.

/**
 * @return the next available session name in the series untitled1.tet,
 * untitled2.tet, etc.
 */
private String getNewSessionName() {
    String base = "untitled";
    String suffix = ".tet";
    // Sequence 1, 2, 3, ...
    int i = 0;
    loop: while (true) {
        i++;
        String name = base + i + suffix;
        for (Object _o : framesMap.keySet()) {
            if (_o instanceof SessionEditor) {
                SessionEditor sessionEditor = (SessionEditor) _o;
                SessionEditorWorkbench workbench = sessionEditor.getSessionWorkbench();
                SessionWrapper sessionWrapper = workbench.getSessionWrapper();
                if (sessionWrapper.getName().equals(name)) {
                    continue loop;
                }
            }
        }
        return name;
    }
}
Also used : Point(java.awt.Point) SessionWrapper(edu.cmu.tetradapp.model.SessionWrapper)

Example 12 with SessionWrapper

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

the class LoadSessionAction method actionPerformed.

/**
 * Performs the action of opening a session from a file.
 */
public void actionPerformed(ActionEvent e) {
    Window owner = (Window) JOptionUtils.centeringComp().getTopLevelAncestor();
    // select a file to open using the file chooser
    JFileChooser chooser = new JFileChooser();
    String sessionSaveLocation = Preferences.userRoot().get("sessionSaveLocation", "");
    chooser.setCurrentDirectory(new File(sessionSaveLocation));
    chooser.addChoosableFileFilter(new TetFileFilter());
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    int ret1 = chooser.showOpenDialog(JOptionUtils.centeringComp());
    if (!(ret1 == JFileChooser.APPROVE_OPTION)) {
        return;
    }
    final File file = chooser.getSelectedFile();
    if (file == null) {
        return;
    }
    Preferences.userRoot().put("sessionSaveLocation", file.getParent());
    Session session = DesktopController.getInstance().getSessionByName(file.getName());
    if (session != null) {
        if (session.isEmpty()) {
            DesktopController.getInstance().closeSessionByName(file.getName());
        } else {
            int ret = JOptionPane.showConfirmDialog(JOptionUtils.centeringComp(), "Replace existing session by that name?.", "Confirm", JOptionPane.YES_NO_OPTION);
            if (ret == JOptionPane.YES_OPTION) {
                DesktopController.getInstance().closeSessionByName(file.getName());
            } else {
                return;
            }
        }
    }
    // The watcher thread is causing a race condition with JFileChooser.showOpenDialog somehow. Placing that
    // code outside the thread.
    new WatchedProcess(owner) {

        public void watch() {
            try {
                FileInputStream in = new FileInputStream(file);
                // ObjectInputStream objIn = new ObjectInputStream(in);
                DecompressibleInputStream objIn = new DecompressibleInputStream(in);
                Object o = objIn.readObject();
                TetradMetadata metadata = null;
                SessionWrapper sessionWrapper = null;
                if (o instanceof TetradMetadata) {
                    metadata = (TetradMetadata) o;
                    try {
                        sessionWrapper = (SessionWrapper) objIn.readObject();
                    } catch (ClassNotFoundException e1) {
                        throw e1;
                    } catch (Exception e2) {
                        e2.printStackTrace();
                        sessionWrapper = null;
                    }
                } else if (o instanceof SessionWrapper) {
                    metadata = null;
                    sessionWrapper = (SessionWrapper) o;
                }
                in.close();
                if (metadata == null) {
                    throw new NullPointerException("Could not read metadata.");
                }
                if (sessionWrapper == null) {
                    Version version = metadata.getVersion();
                    Date date = metadata.getDate();
                    SimpleDateFormat df = new SimpleDateFormat("MMM dd, yyyy");
                    JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "Could not load session. The version of the session was \n" + version + "; it was saved on " + df.format(date) + ". You " + "\nmight try loading it with that version instead.");
                    return;
                }
                SessionEditorWorkbench graph = new SessionEditorWorkbench(sessionWrapper);
                String name = file.getName();
                sessionWrapper.setName(name);
                SessionEditor editor = new SessionEditor(name, graph);
                DesktopController.getInstance().addSessionEditor(editor);
                DesktopController.getInstance().closeEmptySessions();
                DesktopController.getInstance().putMetadata(sessionWrapper, metadata);
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "That wasn't a TETRAD session file: " + file);
            } catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "An error occurred attempting to load the session.");
            }
        }
    };
}
Also used : TetradMetadata(edu.cmu.tetradapp.model.TetradMetadata) WatchedProcess(edu.cmu.tetradapp.util.WatchedProcess) Date(java.util.Date) Version(edu.cmu.tetrad.util.Version) SimpleDateFormat(java.text.SimpleDateFormat) Session(edu.cmu.tetrad.session.Session) SessionWrapper(edu.cmu.tetradapp.model.SessionWrapper)

Example 13 with SessionWrapper

use of edu.cmu.tetradapp.model.SessionWrapper 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)13 TetradMetadata (edu.cmu.tetradapp.model.TetradMetadata)6 SessionEditorIndirectRef (edu.cmu.tetradapp.util.SessionEditorIndirectRef)5 Session (edu.cmu.tetrad.session.Session)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Node (edu.cmu.tetrad.graph.Node)2 Version (edu.cmu.tetrad.util.Version)2 Point (java.awt.Point)2 Date (java.util.Date)2 Edge (edu.cmu.tetrad.graph.Edge)1 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)1 Endpoint (edu.cmu.tetrad.graph.Endpoint)1 GraphNode (edu.cmu.tetrad.graph.GraphNode)1 SessionNode (edu.cmu.tetrad.session.SessionNode)1 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 DisplayNode (edu.cmu.tetradapp.workbench.DisplayNode)1