use of edu.cmu.tetrad.session.SessionNode in project tetrad by cmu-phil.
the class SessionEditorNode method getPopup.
/**
* Creates the popup for the node.
*/
private JPopupMenu getPopup() {
if (popup != null && popup.isShowing()) {
return popup;
}
popup = new JPopupMenu();
JMenuItem createModel = new JMenuItem("Create Model");
createModel.setToolTipText("<html>Creates a new model for this node" + "<br>of the type selected.</html>");
createModel.addActionListener((e) -> {
try {
if (getSessionNode().getModel() == null) {
createModel(false);
} else {
Component centeringComp = SessionEditorNode.this;
JOptionPane.showMessageDialog(centeringComp, "Please destroy the model model first.");
}
} catch (Exception e1) {
Component centeringComp = SessionEditorNode.this;
JOptionPane.showMessageDialog(centeringComp, "Could not create a model for this box.");
e1.printStackTrace();
}
});
JMenuItem editModel = new JMenuItem("Edit Model");
editModel.setToolTipText("<html>Edits the model in this node.</html>");
editModel.addActionListener((e) -> {
try {
if (getSessionNode().getModel() == null) {
Component centeringComp = SessionEditorNode.this;
JOptionPane.showMessageDialog(centeringComp, "Sorry, no model has been created yet; there's nothing to edit.");
} else {
doDoubleClickAction();
}
} catch (Exception e1) {
Component centeringComp = SessionEditorNode.this;
JOptionPane.showMessageDialog(centeringComp, "Double click failed. See console for exception.");
e1.printStackTrace();
}
});
JMenuItem destroyModel = new JMenuItem("Destroy Model");
destroyModel.setToolTipText("<html>Destroys the model for this node, " + "<br>if it has one, destroying any " + "<br>downstream models as well.</html>");
destroyModel.addActionListener((e) -> {
Component centeringComp = SessionEditorNode.this;
if (getSessionNode().getModel() == null) {
JOptionPane.showMessageDialog(centeringComp, "Sorry, this box does not contain a model to destroy.");
return;
}
Set<SessionNode> children = getSessionNode().getChildren();
boolean found = false;
for (SessionNode child : children) {
if (child.getModel() != null) {
found = true;
}
}
if (found) {
int ret = JOptionPane.showConfirmDialog(centeringComp, "Destroying the model in this box will also destroy models in any boxes\n" + "downstream. Is that OK?", null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if (ret != JOptionPane.YES_OPTION) {
return;
}
}
destroyModel();
});
JMenuItem propagateDownstream = new JMenuItem("Propagate changes downstream");
propagateDownstream.setToolTipText("<html>" + "Fills in this box and downstream boxes with models," + "<br>overwriting any models that already exist.</html>");
propagateDownstream.addActionListener((e) -> {
Component centeringComp = SessionEditorNode.this;
if (getSessionNode().getModel() != null && !getSessionNode().getChildren().isEmpty()) {
int ret = JOptionPane.showConfirmDialog(centeringComp, "You will be rewriting all downstream models. Is that OK?", "Confirm", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if (ret != JOptionPane.YES_OPTION) {
return;
}
}
try {
createDescendantModels(true);
} catch (RuntimeException e1) {
JOptionPane.showMessageDialog(centeringComp, "Could not complete the creation of descendant models.");
e1.printStackTrace();
}
});
JMenuItem renameBox = new JMenuItem("Rename Box");
renameBox.setToolTipText("<html>Renames this session box.</html>");
renameBox.addActionListener((e) -> {
Component centeringComp = SessionEditorNode.this;
String name = JOptionPane.showInputDialog(centeringComp, "New name:");
if (!NamingProtocol.isLegalName(name)) {
JOptionPane.showMessageDialog(centeringComp, NamingProtocol.getProtocolDescription());
return;
}
SessionNodeWrapper wrapper = (SessionNodeWrapper) getModelNode();
wrapper.setSessionName(name);
getSessionDisplayComp().setName(name);
adjustToModel();
});
JMenuItem cloneBox = new JMenuItem("Clone Box");
cloneBox.setToolTipText("<html>" + "Makes a copy of this session box and its contents. To clone<br>" + "a whole subgraph, or to paste into a different sessions, select<br>" + "the subgraph and use the Copy/Paste gadgets in the Edit menu." + "</html>");
cloneBox.addActionListener((e) -> {
firePropertyChange("cloneMe", null, SessionEditorNode.this);
});
JMenuItem deleteBox = new JMenuItem("Delete Box");
deleteBox.setToolTipText("<html>Deletes this box from the workbench</html>");
deleteBox.addActionListener((e) -> {
Component centeringComp = SessionEditorNode.this;
int ret = JOptionPane.showConfirmDialog(centeringComp, "Are you sure you want to delete this box? It contains some work.", null, JOptionPane.YES_NO_OPTION);
if (ret != JOptionPane.YES_OPTION) {
return;
}
firePropertyChange("deleteNode", null, null);
});
popup.add(createModel);
SessionModel model = getSessionNode().getModel();
Class modelClass = (model == null) ? determineTheModelClass(getSessionNode()) : model.getClass();
if (getSessionNode().existsParameterizedConstructor(modelClass)) {
final ParameterEditor paramEditor = getParameterEditor(modelClass);
if (paramEditor != null) {
JMenuItem editSimulationParameters = new JMenuItem("Edit Parameters...");
editSimulationParameters.setToolTipText("<html>");
editSimulationParameters.addActionListener((e) -> {
Parameters param = getSessionNode().getParam(modelClass);
Object[] arguments = getSessionNode().getModelConstructorArguments(modelClass);
if (param != null) {
try {
editParameters(modelClass, param, arguments);
int ret = JOptionPane.showConfirmDialog(JOptionUtils.centeringComp(), "Should I overwrite the contents of this box and all delete the contents\n" + "of all boxes downstream?", "Double check...", JOptionPane.YES_NO_OPTION);
if (ret == JOptionPane.YES_OPTION) {
getSessionNode().destroyModel();
getSessionNode().createModel(modelClass, true);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
popup.add(editSimulationParameters);
}
}
// final SessionNode thisNode = getSessionNode();
//
// // popup.add(getConsistentParentMenuItems(getConsistentParentBoxTypes(thisNode)));
// // popup.add(getConsistentChildBoxMenus(getConsistentChildBoxTypes(thisNode, null)));
//
// addConsistentParentMenuItems(popup, getConsistentParentBoxTypes(thisNode));
// addConsistentChildBoxMenus(popup, getConsistentChildBoxTypes(thisNode, null));
//
// popup.addSeparator();
popup.add(createModel);
popup.add(editModel);
popup.add(destroyModel);
popup.addSeparator();
popup.add(renameBox);
popup.add(cloneBox);
popup.add(deleteBox);
popup.addSeparator();
addEditLoggerSettings(popup);
popup.add(propagateDownstream);
return popup;
}
use of edu.cmu.tetrad.session.SessionNode in project tetrad by cmu-phil.
the class SessionEditorNode method getChildren.
public Set<SessionNode> getChildren() {
SessionNodeWrapper _sessionNodeWrapper = (SessionNodeWrapper) getModelNode();
SessionNode _sessionNode = _sessionNodeWrapper.getSessionNode();
return _sessionNode.getChildren();
}
use of edu.cmu.tetrad.session.SessionNode in project tetrad by cmu-phil.
the class SessionEditorNode method createParamObjects.
/**
* Sets up default parameter objects for the node.
*/
private void createParamObjects(SessionEditorNode sessionEditorNode) {
SessionNode sessionNode = sessionEditorNode.getSessionNode();
Class[] modelClasses = sessionNode.getModelClasses();
for (Class clazz : modelClasses) {
// in from a file.
if (sessionNode.getParam(clazz) == null) {
SessionNodeModelConfig modelConfig = this.config.getModelConfig(clazz);
if (modelConfig == null) {
continue;
}
sessionNode.putParam(clazz, new Parameters(sessionNode.getParameters()));
}
}
}
use of edu.cmu.tetrad.session.SessionNode in project tetrad by cmu-phil.
the class SessionEditorNode method getModelClassFromUser.
/**
* @return the selected model class, or null if no model class was selected.
*/
private Class getModelClassFromUser(Class[] modelClasses, boolean cancelable) {
// Count the number of model classes that can be listed for the user;
// if there's only one, don't ask the user for input.
List<Class> reducedList = new LinkedList<>();
for (Class modelClass : modelClasses) {
if (!(UnlistedSessionModel.class.isAssignableFrom(modelClass))) {
reducedList.add(modelClass);
}
}
if (reducedList.isEmpty()) {
throw new RuntimeException("There is no model to choose.");
}
SessionNodeWrapper sessionNodeWrapper = (SessionNodeWrapper) getModelNode();
SessionNode sessionNode = sessionNodeWrapper.getSessionNode();
ModelChooser chooser = this.config.getModelChooserInstance(sessionNode);
Component centeringComp = SessionEditorNode.this;
if (cancelable) {
int selection = JOptionPane.showOptionDialog(centeringComp, chooser, chooser.getTitle(), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, new String[] { "OK", "Cancel" }, "OK");
if (selection == 0) {
// this.rememberLastClass = modelTypeChooser.getRemembered();
return chooser.getSelectedModel();
} else {
return null;
}
} else {
JOptionPane.showOptionDialog(centeringComp, chooser, chooser.getTitle(), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { "OK" }, "OK");
// this.rememberLastClass = modelTypeChooser.getRemembered();
return chooser.getSelectedModel();
}
}
use of edu.cmu.tetrad.session.SessionNode in project tetrad by cmu-phil.
the class SessionEditorNode method launchEditorVisit.
private void launchEditorVisit() {
try {
// If there is already an editor open, don't launch another one.
if (spawnedEditor() != null) {
return;
}
boolean created = createModel(false);
if (!created) {
return;
}
final SessionNode sessionNode = getSessionNode();
boolean cloned = sessionNode.useClonedModel();
SessionModel model = sessionNode.getModel();
Class<?> modelClass = model.getClass();
SessionNodeModelConfig modelConfig = this.config.getModelConfig(modelClass);
Object[] arguments = new Object[] { model };
JPanel editor = modelConfig.getEditorInstance(arguments);
addEditorListener(editor);
ModificationRegistery.registerEditor(sessionNode, editor);
String descrip = modelConfig.getName();
editor.setName(getName() + " (" + descrip + ")");
EditorWindow editorWindow = new EditorWindow(editor, editor.getName(), "Done", cloned, this);
editorWindow.addInternalFrameListener(new InternalFrameAdapter() {
@Override
public void internalFrameClosing(InternalFrameEvent e) {
if (getChildren().iterator().hasNext()) {
finishedEditingDialog();
}
ModificationRegistery.unregisterSessionNode(sessionNode);
setSpawnedEditor(null);
EditorWindow window = (EditorWindow) e.getSource();
if (window.isCanceled()) {
sessionNode.restoreOriginalModel();
}
sessionNode.forgetSavedModel();
}
});
DesktopController.getInstance().addEditorWindow(editorWindow, JLayeredPane.PALETTE_LAYER);
editorWindow.pack();
editorWindow.setVisible(true);
spawnedEditor = editorWindow;
if (sessionWrapper != null) {
sessionWrapper.setSessionChanged(true);
}
// for (SessionNode child : getChildren()) {
//
// // only break edges to children.
// if (edge.getNode2() == getModelNode()) {
// SessionNodeWrapper otherWrapper =
// (SessionNodeWrapper) edge.getNode1();
// SessionNode other = otherWrapper.getSessionNode();
// if (getChildren().contains(other)) {
// sessionWrapper.removeEdge(edge);
// }
// } else {
// SessionNodeWrapper otherWrapper =
// (SessionNodeWrapper) edge.getNode2();
// SessionNode other = otherWrapper.getSessionNode();
// if (getChildren().contains(other)) {
// sessionWrapper.removeEdge(edge);
// }
// }
// }
// Class[] consistentModelClasses = child.getConsistentModelClasses(false);
// if (consistentModelClasses.length == 0) {
// child.removeParent(sessionNode);
// SessionEditorWorkbench sessionWorkbench = getSessionWorkbench();
// SessionWrapper sessionWrapper = sessionWorkbench.getSessionWrapper();
// Node node1 = sessionWrapper.getNode(sessionNode.getDisplayName());
// Node node2 = sessionWrapper.getNode(child.getDisplayName());
// Edge edge = sessionWrapper.getEdge(node1, node2);
// sessionWrapper.removeEdge(edge);
// }
} catch (CouldNotCreateModelException e) {
SessionUtils.showPermissibleParentsDialog(e.getModelClass(), SessionEditorNode.this, true, true);
e.printStackTrace();
} catch (ClassCastException e) {
e.printStackTrace();
} catch (Exception e) {
Throwable cause = e;
while (cause.getCause() != null) {
cause = cause.getCause();
}
Component centeringComp = SessionEditorNode.this;
String s = cause.getMessage();
if (!"".equals(s)) {
JOptionPane.showMessageDialog(centeringComp, s, null, JOptionPane.WARNING_MESSAGE);
}
e.printStackTrace();
}
}
Aggregations