use of edu.cmu.tetradapp.editor.ParameterEditor 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.tetradapp.editor.ParameterEditor in project tetrad by cmu-phil.
the class SessionEditorNode method editParameters.
/**
* Tries to edit the parameters, returns true if successfully otherwise
* false is returned
*/
public boolean editParameters(final Class modelClass, Parameters params, Object[] parentModels) {
if (parentModels == null) {
throw new NullPointerException("Parent models array is null.");
}
if (params == null) {
throw new NullPointerException("Parameters cannot be null.");
}
final ParameterEditor paramEditor = getParameterEditor(modelClass);
if (paramEditor == null) {
// if no editor, then consider the params "edited".
return true;
} else {
paramEditor.setParams(params);
paramEditor.setParentModels(parentModels);
}
// If a finalizing editor and a dialog then let it handle things on itself onw.
if (paramEditor instanceof FinalizingParameterEditor && paramEditor instanceof JDialog) {
FinalizingParameterEditor e = (FinalizingParameterEditor) paramEditor;
e.setup();
return e.finalizeEdit();
}
// wrap editor and deal with response.
paramEditor.setup();
JComponent editor = (JComponent) paramEditor;
SessionNodeWrapper nodeWrapper = (SessionNodeWrapper) getModelNode();
String buttonType = nodeWrapper.getButtonType();
editor.setName(buttonType + " Structure Editor");
Component centeringComp = SessionEditorNode.this;
int ret = JOptionPane.showOptionDialog(centeringComp, editor, editor.getName(), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null);
// if finalizing editor, then deal with specially.
return ret == JOptionPane.OK_OPTION && (!(paramEditor instanceof FinalizingParameterEditor) || ((FinalizingParameterEditor) paramEditor).finalizeEdit());
}
Aggregations