Search in sources :

Example 6 with SessionWrapper

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

the class CloseSessionAction method actionPerformed.

/**
 * Closes the frontmost session of this action's desktop.
 */
public void actionPerformed(ActionEvent e) {
    // Get the frontmost SessionWrapper.
    SessionEditorIndirectRef sessionEditorRef = DesktopController.getInstance().getFrontmostSessionEditor();
    SessionEditor sessionEditor = (SessionEditor) sessionEditorRef;
    SessionEditorWorkbench graph = sessionEditor.getSessionWorkbench();
    SessionWrapper sessionWrapper = graph.getSessionWrapper();
    if (sessionWrapper.isSessionChanged()) {
        String name = sessionWrapper.getName();
        // check to make sure user wants to evaporate this window...
        String msg = "Do you want to save the changes you made to " + name + "?";
        int response = JOptionPane.showConfirmDialog(JOptionUtils.centeringComp(), msg, "Fair Warning", JOptionPane.YES_NO_CANCEL_OPTION);
        if (response == JOptionPane.YES_OPTION) {
            SaveSessionAction saveSessionAction = new SaveSessionAction();
            saveSessionAction.actionPerformed(e);
            this.saved = saveSessionAction.isSaved();
        } else if (response == JOptionPane.CANCEL_OPTION) {
            return;
        }
    }
    DesktopController.getInstance().closeFrontmostSession();
}
Also used : SessionEditorIndirectRef(edu.cmu.tetradapp.util.SessionEditorIndirectRef) SessionWrapper(edu.cmu.tetradapp.model.SessionWrapper)

Example 7 with SessionWrapper

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

the class ConstructTemplateAction method nextName.

/**
 * Returns the next string in the sequence.
 *
 * @param base the string base of the name--for example, "Graph".
 * @return the next string in the sequence--for example, "Graph1".
 */
private static String nextName(String base) {
    SessionEditorIndirectRef sessionEditorRef = DesktopController.getInstance().getFrontmostSessionEditor();
    SessionEditor sessionEditor = (SessionEditor) sessionEditorRef;
    SessionEditorWorkbench sessionWorkbench = sessionEditor.getSessionWorkbench();
    SessionWrapper graph = sessionWorkbench.getSessionWrapper();
    if (base == null) {
        throw new NullPointerException("Base name must be non-null.");
    }
    // Sequence 1, 2, 3, ...
    int i = 0;
    loop: while (true) {
        i++;
        String name = base + i;
        for (Object o : graph.getNodes()) {
            Node node = (Node) (o);
            if (node.getName().equals(name)) {
                continue loop;
            }
        }
        break;
    }
    return base + i;
}
Also used : Node(edu.cmu.tetrad.graph.Node) SessionNode(edu.cmu.tetrad.session.SessionNode) SessionEditorIndirectRef(edu.cmu.tetradapp.util.SessionEditorIndirectRef) Endpoint(edu.cmu.tetrad.graph.Endpoint) SessionWrapper(edu.cmu.tetradapp.model.SessionWrapper)

Example 8 with SessionWrapper

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

the class AbstractWorkbench method setGraphWithoutNotify.

// ============================PRIVATE METHODS=========================//
/**
 * Sets the display workbench model to the indicated model. (Called when the
 * workbench is first constructed as well as whenever the workbench model is
 * changed.)
 */
private void setGraphWithoutNotify(Graph graph) {
    if (graph == null) {
        throw new IllegalArgumentException("Graph model cannot be null.");
    }
    if (graph instanceof SessionWrapper) {
        this.graph = graph;
    } else {
        this.graph = graph;
        if (graph.isPag()) {
            GraphUtils.addPagColoring(new EdgeListGraph(graph));
        }
    }
    this.modelEdgesToDisplay = new HashMap<>();
    this.modelNodesToDisplay = new HashMap<>();
    this.displayToModel = new HashMap();
    this.displayToLabels = new HashMap();
    removeAll();
    graph.addPropertyChangeListener(this.propChangeHandler);
    // extract the current contents from the model...
    List<Node> nodes = graph.getNodes();
    for (Node node : nodes) {
        if (!getModelNodesToDisplay().containsKey(node)) {
            addNode(node);
        }
    }
    Set<Edge> edges = graph.getEdges();
    for (Edge edge : edges) {
        if (!getModelEdgesToDisplay().containsKey(edge)) {
            addEdge(edge);
        }
    }
    adjustPreferredSize();
    if (getPreferredSize().getWidth() > getMaxX()) {
        setMaxX((int) getPreferredSize().getWidth());
    }
    if (getPreferredSize().getHeight() > getMaxY()) {
        setMaxY((int) getPreferredSize().getHeight());
    }
    revalidate();
    repaint();
}
Also used : HashMap(java.util.HashMap) Node(edu.cmu.tetrad.graph.Node) GraphNode(edu.cmu.tetrad.graph.GraphNode) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Edge(edu.cmu.tetrad.graph.Edge) SessionWrapper(edu.cmu.tetradapp.model.SessionWrapper)

Example 9 with SessionWrapper

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

the class SessionFileTransferHandler method importData.

@Override
public boolean importData(TransferSupport support) {
    try {
        List<File> files = (List<File>) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
        for (File file : files) {
            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 false;
                    }
                }
            }
            try (InputStream in = Files.newInputStream(file.toPath())) {
                DecompressibleInputStream objIn = new DecompressibleInputStream(in);
                Object o = objIn.readObject();
                TetradMetadata metadata = null;
                SessionWrapper sessionWrapper = null;
                if (o instanceof TetradMetadata) {
                    metadata = (TetradMetadata) o;
                    sessionWrapper = (SessionWrapper) objIn.readObject();
                } 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 false;
                }
                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 exception) {
                LOGGER.error("", exception);
                JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "That wasn't a TETRAD session file: " + file);
            } catch (Exception exception) {
                LOGGER.error("", exception);
                JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "An error occurred attempting to load the session.");
            }
        }
    } catch (UnsupportedFlavorException | IOException exception) {
        LOGGER.error("", exception);
    }
    return super.importData(support);
}
Also used : TetradMetadata(edu.cmu.tetradapp.model.TetradMetadata) DecompressibleInputStream(edu.cmu.tetradapp.app.DecompressibleInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) DecompressibleInputStream(edu.cmu.tetradapp.app.DecompressibleInputStream) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) Date(java.util.Date) SessionEditorWorkbench(edu.cmu.tetradapp.app.SessionEditorWorkbench) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Version(edu.cmu.tetrad.util.Version) List(java.util.List) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) SessionEditor(edu.cmu.tetradapp.app.SessionEditor) Session(edu.cmu.tetrad.session.Session) SessionWrapper(edu.cmu.tetradapp.model.SessionWrapper)

Example 10 with SessionWrapper

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

the class TetradDesktop method closeAllSessions.

/**
 * Queries the user as to whether they would like to save their sessions.
 *
 * @return true if the transaction was ended successfully, false if not
 * (that is, canceled).
 */
public boolean closeAllSessions() {
    while (existsSession()) {
        SessionEditor sessionEditor = getFrontmostSessionEditor();
        SessionEditorWorkbench workbench = sessionEditor.getSessionWorkbench();
        SessionWrapper wrapper = workbench.getSessionWrapper();
        if (!wrapper.isSessionChanged()) {
            closeFrontmostSession();
            continue;
        }
        String name = sessionEditor.getName();
        int ret = JOptionPane.showConfirmDialog(JOptionUtils.centeringComp(), "Would you like to save the changes you made to " + name + "?", "Advise needed...", JOptionPane.YES_NO_CANCEL_OPTION);
        if (ret == JOptionPane.NO_OPTION) {
            closeFrontmostSession();
            continue;
        } else if (ret == JOptionPane.CANCEL_OPTION) {
            return false;
        }
        SaveSessionAsAction action = new SaveSessionAsAction();
        action.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Dummy close action"));
        if (!action.isSaved()) {
            int ret2 = JOptionPane.showConfirmDialog(JOptionUtils.centeringComp(), "This session was not saved. Close session and continue anyway?", "Advise needed...", JOptionPane.OK_CANCEL_OPTION);
            if (ret2 == JOptionPane.CANCEL_OPTION) {
                return false;
            }
        }
        closeFrontmostSession();
    }
    return true;
}
Also used : ActionEvent(java.awt.event.ActionEvent) Point(java.awt.Point) 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