Search in sources :

Example 1 with SessionEditor

use of edu.cmu.tetradapp.app.SessionEditor 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)

Aggregations

Session (edu.cmu.tetrad.session.Session)1 Version (edu.cmu.tetrad.util.Version)1 DecompressibleInputStream (edu.cmu.tetradapp.app.DecompressibleInputStream)1 SessionEditor (edu.cmu.tetradapp.app.SessionEditor)1 SessionEditorWorkbench (edu.cmu.tetradapp.app.SessionEditorWorkbench)1 SessionWrapper (edu.cmu.tetradapp.model.SessionWrapper)1 TetradMetadata (edu.cmu.tetradapp.model.TetradMetadata)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 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 List (java.util.List)1