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();
}
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;
}
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();
}
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);
}
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;
}
Aggregations