use of edu.cmu.tetradapp.model.TetradMetadata in project tetrad by cmu-phil.
the class SaveSessionAsAction method actionPerformed.
/**
* Performs the action of saving a session to 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();
// Select the file to save this to.
String sessionSaveLocation = Preferences.userRoot().get("sessionSaveLocation", "");
File file = EditorUtils.getSaveFileWithPath(sessionEditor.getName(), "tet", JOptionUtils.centeringComp(), true, "Save Session As...", sessionSaveLocation);
if (file == null) {
this.saved = false;
return;
}
if ((DesktopController.getInstance().existsSessionByName(file.getName()) && !(sessionWrapper.getName().equals(file.getName())))) {
this.saved = false;
JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "Another session by that name is currently open. Please " + "\nclose that session first.");
return;
}
sessionWrapper.setName(file.getName());
sessionEditor.setName(file.getName());
// Save it.
try {
FileOutputStream out = new FileOutputStream(file);
ObjectOutputStream objOut = new ObjectOutputStream(out);
objOut.writeObject(metadata);
objOut.writeObject(sessionWrapper);
out.close();
FileInputStream in = new FileInputStream(file);
ObjectInputStream objIn = new ObjectInputStream(in);
objIn.readObject();
sessionWrapper.setSessionChanged(false);
sessionWrapper.setNewSession(false);
this.saved = true;
} catch (Exception e2) {
this.saved = false;
e2.printStackTrace();
JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "An error occurred while attempting to save the session.");
}
DesktopController.getInstance().putMetadata(sessionWrapper, metadata);
sessionEditor.firePropertyChange("name", null, file.getName());
}
use of edu.cmu.tetradapp.model.TetradMetadata in project tetrad by cmu-phil.
the class TetradDesktop method putMetadata.
public void putMetadata(SessionWrapperIndirectRef sessionWrapperRef, TetradMetadataIndirectRef metadataRef) {
SessionWrapper sessionWrapper = (SessionWrapper) sessionWrapperRef;
TetradMetadata metadata = (TetradMetadata) metadataRef;
this.metadataMap.put(sessionWrapper, metadata);
}
use of edu.cmu.tetradapp.model.TetradMetadata in project tetrad by cmu-phil.
the class SessionVersionAction 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();
TetradMetadataIndirectRef metadataRef = DesktopController.getInstance().getTetradMetadata(sessionWrapper);
TetradMetadata metadata = (TetradMetadata) metadataRef;
StringBuilder buf = new StringBuilder();
if (metadata == null) {
buf.append("This session has not yet been saved or loaded. The model\n");
buf.append("version you are working in is ");
buf.append(Version.currentViewableVersion());
buf.append(".");
JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), buf.toString());
return;
}
SimpleDateFormat df = new SimpleDateFormat("MMM dd, yyyy");
buf.append("Version information for \"");
buf.append(sessionWrapper.getName());
buf.append("\":\n\n");
buf.append("Last saved using Tetrad ");
buf.append(metadata.getVersion());
buf.append(" (");
buf.append(df.format(metadata.getDate()));
buf.append(").\n");
buf.append("You are running Tetrad ");
buf.append(Version.currentViewableVersion());
buf.append(".");
JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), buf.toString());
}
use of edu.cmu.tetradapp.model.TetradMetadata 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);
}
use of edu.cmu.tetradapp.model.TetradMetadata 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.");
}
}
};
}
Aggregations