Search in sources :

Example 21 with WatchedProcess

use of edu.cmu.tetradapp.util.WatchedProcess in project tetrad by cmu-phil.

the class GraphSelectionEditor method resetWorkbenches.

private JButton resetWorkbenches(final GraphSelectionWrapper wrapper) {
    final JButton executeButton = new JButton("Graph It!");
    executeButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            Window owner = (Window) getTopLevelAncestor();
            new WatchedProcess(owner) {

                public void watch() {
                    GraphWorkbench workbench = getWorkbench();
                    List<DisplayNode> displayNodes = workbench.getSelectedNodes();
                    List<Node> newSelected = new ArrayList<>();
                    for (DisplayNode node : displayNodes) {
                        newSelected.add(node.getModelNode());
                    }
                    if (!newSelected.isEmpty()) {
                        editorPanel.setSelected(newSelected);
                    }
                    resetGraphs(wrapper);
                }
            };
        }
    });
    forWorkbenchScrolls.validate();
    return executeButton;
}
Also used : Window(java.awt.Window) DisplayNode(edu.cmu.tetradapp.workbench.DisplayNode) ActionListener(java.awt.event.ActionListener) GraphWorkbench(edu.cmu.tetradapp.workbench.GraphWorkbench) ActionEvent(java.awt.event.ActionEvent) WatchedProcess(edu.cmu.tetradapp.util.WatchedProcess) JButton(javax.swing.JButton) List(java.util.List) ArrayList(java.util.ArrayList) JList(javax.swing.JList)

Example 22 with WatchedProcess

use of edu.cmu.tetradapp.util.WatchedProcess in project tetrad by cmu-phil.

the class SessionEditorNode method doDoubleClickAction.

/**
 * Launches the editor associates with this node.
 *
 * @param sessionWrapper Needed to allow the option of deleting edges
 */
@Override
public void doDoubleClickAction(Graph sessionWrapper) {
    this.sessionWrapper = (SessionWrapper) sessionWrapper;
    Window owner = (Window) getTopLevelAncestor();
    new WatchedProcess(owner) {

        public void watch() {
            TetradLogger.getInstance().setTetradLoggerConfig(getSessionNode().getLoggerConfig());
            launchEditorVisit();
        }
    };
}
Also used : EditorWindow(edu.cmu.tetradapp.editor.EditorWindow) Window(java.awt.Window) WatchedProcess(edu.cmu.tetradapp.util.WatchedProcess)

Example 23 with WatchedProcess

use of edu.cmu.tetradapp.util.WatchedProcess in project tetrad by cmu-phil.

the class SessionEditorNode method executeSessionNode.

private void executeSessionNode(final SessionNode sessionNode, final boolean overwrite) {
    Window owner = (Window) getTopLevelAncestor();
    new WatchedProcess(owner) {

        @Override
        public void watch() {
            Class c = SessionEditorWorkbench.class;
            Container container = SwingUtilities.getAncestorOfClass(c, SessionEditorNode.this);
            SessionEditorWorkbench workbench = (SessionEditorWorkbench) container;
            System.out.println("Executing" + sessionNode);
            workbench.getSimulationStudy().execute(sessionNode, overwrite);
        }
    };
}
Also used : EditorWindow(edu.cmu.tetradapp.editor.EditorWindow) Window(java.awt.Window) Container(java.awt.Container) WatchedProcess(edu.cmu.tetradapp.util.WatchedProcess)

Example 24 with WatchedProcess

use of edu.cmu.tetradapp.util.WatchedProcess 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)

Aggregations

WatchedProcess (edu.cmu.tetradapp.util.WatchedProcess)24 ActionEvent (java.awt.event.ActionEvent)13 ActionListener (java.awt.event.ActionListener)13 GraphWorkbench (edu.cmu.tetradapp.workbench.GraphWorkbench)8 LayoutMenu (edu.cmu.tetradapp.workbench.LayoutMenu)7 TitledBorder (javax.swing.border.TitledBorder)6 CharArrayWriter (java.io.CharArrayWriter)5 PrintWriter (java.io.PrintWriter)5 DataModel (edu.cmu.tetrad.data.DataModel)4 IKnowledge (edu.cmu.tetrad.data.IKnowledge)4 Knowledge2 (edu.cmu.tetrad.data.Knowledge2)4 Parameters (edu.cmu.tetrad.util.Parameters)4 Window (java.awt.Window)4 PatternToDag (edu.cmu.tetrad.search.PatternToDag)3 EditorWindow (edu.cmu.tetradapp.editor.EditorWindow)3 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)2 Graph (edu.cmu.tetrad.graph.Graph)2 Container (java.awt.Container)2 IOException (java.io.IOException)2 List (java.util.List)2