use of cbit.gui.ScopedExpression in project vcell by virtualcell.
the class MathOverridesTableModel method setValueAt.
/**
* getValueAt method comment.
*/
public void setValueAt(Object object, int r, int c) {
try {
String name = (String) getValueAt(r, 0);
if (c == COLUMN_ACTUAL) {
if (object instanceof ConstantArraySpec) {
editScanValues(name, r);
} else if (object instanceof ScopedExpression) {
throw new RuntimeException("unexpected value type ScopedExpression");
} else if (object instanceof String) {
String inputValue = (String) object;
Expression expression = null;
if (inputValue == null || inputValue.trim().length() == 0) {
expression = new Expression((String) getValueAt(r, COLUMN_DEFAULT));
} else {
expression = new Expression(inputValue);
}
Constant constant = new Constant(name, expression);
getMathOverrides().putConstant(constant);
fireTableCellUpdated(r, c);
this.fireTableDataChanged();
setModified(true);
}
} else if (c == COLUMN_SCAN) {
if (((Boolean) object).booleanValue()) {
// setting scan values
editScanValues(name, r);
} else {
// resetting to default
setValueAt(getValueAt(r, COLUMN_DEFAULT), r, COLUMN_ACTUAL);
}
}
} catch (Throwable exc) {
PopupGenerator.showErrorDialog(ownerTable, exc.getMessage() + "\nOld value was restored.", exc);
}
}
use of cbit.gui.ScopedExpression in project vcell by virtualcell.
the class ScopedExpressionTableCellRenderer method getTableCellRendererComponent.
/**
* This method is sent to the renderer by the drawing table to
* configure the renderer appropriately before drawing. Return
* the Component used for drawing.
*
* @param table the JTable that is asking the renderer to draw.
* This parameter can be null.
* @param value the value of the cell to be rendered. It is
* up to the specific renderer to interpret
* and draw the value. eg. if value is the
* String "true", it could be rendered as a
* string or it could be rendered as a check
* box that is checked. null is a valid value.
* @param isSelected true is the cell is to be renderer with
* selection highlighting
* @param row the row index of the cell being drawn. When
* drawing the header the rowIndex is -1.
* @param column the column index of the cell being drawn
*/
public java.awt.Component getTableCellRendererComponent(javax.swing.JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
try {
TableModel tableModel = table.getModel();
if (value == null) {
templateJLabel.setIcon(null);
templateJLabel.setText(null);
templateJLabel.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (tableModel instanceof VCellSortTableModel) {
if (row < ((VCellSortTableModel<?>) tableModel).getRowCount()) {
Object rowObject = ((VCellSortTableModel<?>) tableModel).getValueAt(row);
if (rowObject instanceof SpeciesContextSpecParameter) {
templateJLabel.setText(((SpeciesContextSpecParameter) rowObject).getNullExpressionDescription());
}
}
}
return templateJLabel;
}
if (!(value instanceof ScopedExpression)) {
return stringRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
imageRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
JLabel renderer = imageRenderer;
ScopedExpression scopedExpression = (ScopedExpression) value;
String scopedExpressInfix = scopedExpression.infix();
ReusableExpressionIcon rei = scopedExpressionImageIconHash.get(scopedExpressInfix);
if (rei != null && rei.background.equals(imageRenderer.getBackground()) && rei.foreground.equals(imageRenderer.getForeground())) {
// Re-use existing ImageIcon is it exists and is same state
imageRenderer.setIcon(rei.imageIcon);
} else {
// Create new ImageIcon
try {
ExpressionPrintFormatter epf = new ExpressionPrintFormatter(scopedExpression.getRenamedExpression());
//
// Use graphicsContextProvider BufferedImage to get started because
// theTable may have a null GC and epf needs a non-null GC
//
java.awt.Graphics2D tempG2D = (java.awt.Graphics2D) graphicsContextProvider.getGraphics();
// Must set here before epf.getSize is called
tempG2D.setFont(italicFont);
// Determine how big the expression image will be
java.awt.Dimension dim = epf.getSize(tempG2D);
// Create and render the expression image
java.awt.image.BufferedImage bi = new java.awt.image.BufferedImage(dim.width, dim.height, java.awt.image.BufferedImage.TYPE_INT_RGB);
java.awt.Graphics2D g2d = bi.createGraphics();
// epf.paint needs a non-null clipBounds
g2d.setClip(0, 0, dim.width, dim.height);
// set the SAME font used in epf.getSize
g2d.setFont(italicFont);
if (table != null && imageRenderer != null) {
g2d.setBackground(imageRenderer.getBackground());
g2d.setColor(imageRenderer.getForeground());
}
// paint background
g2d.clearRect(0, 0, dim.width, dim.height);
// paint expression into image
epf.paint(g2d);
// Limit cacheing in case a large number of DIFFERENT expressions are being serviced by this TableCellRenderer.
if ((scopedExpressionCacheSize + (dim.width * dim.height)) >= CACHE_SIZE_LIMIT) {
scopedExpressionImageIconHash.clear();
scopedExpressionCacheSize = 0;
}
javax.swing.ImageIcon newImageIcon = new javax.swing.ImageIcon(bi);
rei = new ReusableExpressionIcon(newImageIcon, imageRenderer.getBackground(), imageRenderer.getForeground());
if (scopedExpressionImageIconHash.put(scopedExpressInfix, rei) == null) {
scopedExpressionCacheSize += dim.width * dim.height;
}
imageRenderer.setIcon(newImageIcon);
} catch (Exception e) {
// Fallback to String
e.printStackTrace();
templateJLabel.setText(scopedExpression.infix());
renderer = templateJLabel;
}
}
ExpressionBindingException expressionBindingException = scopedExpression.getExpressionBindingException();
if (expressionBindingException != null) {
renderer.setBorder(BorderFactory.createLineBorder(Color.red));
renderer.setToolTipText(expressionBindingException.getMessage());
} else {
renderer.setBorder(BorderFactory.createEmptyBorder());
renderer.setToolTipText(null);
}
if (tableModel instanceof VCellSortTableModel) {
List<Issue> issueList = ((VCellSortTableModel<?>) tableModel).getIssues(row, column, Issue.SEVERITY_ERROR);
if (issueList.size() > 0) {
renderer.setToolTipText(Issue.getHtmlIssueMessage(issueList));
if (column == 0) {
renderer.setBorder(new MatteBorder(1, 1, 1, 0, Color.red));
} else if (column == table.getColumnCount() - 1) {
renderer.setBorder(new MatteBorder(1, 0, 1, 1, Color.red));
} else {
renderer.setBorder(new MatteBorder(1, 0, 1, 0, Color.red));
}
} else {
renderer.setBorder(BORDER);
}
}
return renderer;
} catch (Exception e) {
e.printStackTrace();
templateJLabel.setText("ScopedExpressionTableCellRenderer Error - " + e.getMessage() + " " + value);
return templateJLabel;
}
}
use of cbit.gui.ScopedExpression in project vcell by virtualcell.
the class ReactionRulePropertiesTableModel method setValueAt.
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Object o = getValueAt(rowIndex);
if (!(o instanceof Parameter)) {
return;
}
Parameter parameter = (Parameter) o;
// try {
switch(columnIndex) {
case COLUMN_NAME:
{
try {
if (aValue instanceof String) {
String newName = (String) aValue;
if (!parameter.getName().equals(newName)) {
if (parameter instanceof LocalParameter) {
reactionRule.getKineticLaw().renameParameter(parameter.getName(), newName);
} else if (parameter instanceof LocalProxyParameter) {
parameter.setName(newName);
}
fireTableRowsUpdated(rowIndex, rowIndex);
}
}
} catch (ExpressionException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Error changing parameter name:\n" + e.getMessage());
} catch (PropertyVetoException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Error changing parameter name:\n" + e.getMessage());
}
break;
}
case COLUMN_IS_GLOBAL:
{
if (aValue.equals(Boolean.FALSE)) {
// check box has been <unset> (<true> to <false>) : change param from global to local
if ((parameter instanceof LocalProxyParameter) && ((((LocalProxyParameter) parameter).getTarget() instanceof Model.ReservedSymbol) || (((LocalProxyParameter) parameter).getTarget() instanceof SpeciesContext) || (((LocalProxyParameter) parameter).getTarget() instanceof ModelQuantity))) {
PopupGenerator.showErrorDialog(ownerTable, "Parameter : \'" + parameter.getName() + "\' is a " + ((LocalProxyParameter) parameter).getTarget().getClass() + " in the model; cannot convert it to a local kinetic parameter.");
} else {
try {
reactionRule.getKineticLaw().convertParameterType(parameter, false);
} catch (PropertyVetoException pve) {
pve.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Unable to convert parameter : \'" + parameter.getName() + "\' to local kinetics parameter : " + pve.getMessage());
} catch (ExpressionBindingException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Unable to convert parameter : \'" + parameter.getName() + "\' to local kinetics parameter : " + e.getMessage());
}
}
} else {
// check box has been <set> (<false> to <true>) : change param from local to global
if ((parameter instanceof LocalParameter) && (((LocalParameter) parameter).getRole() != RbmKineticLaw.RbmKineticLawParameterType.UserDefined)) {
PopupGenerator.showErrorDialog(ownerTable, "Parameter : \'" + parameter.getName() + "\' is a pre-defined kinetics parameter (not user-defined); cannot convert it to a model level (global) parameter.");
} else {
ModelParameter mp = reactionRule.getModel().getModelParameter(parameter.getName());
// model already had the model parameter 'param', but check if 'param' value is different from
// model parameter with same name. If it is, the local value will be overridden by global (model) param
// value, and user should be warned.
String choice = "Ok";
if (mp != null && !(mp.getExpression().compareEqual(parameter.getExpression()))) {
String msgStr = "Model already has a global parameter named : \'" + parameter.getName() + "\'; with value = \'" + mp.getExpression().infix() + "\'; This local parameter \'" + parameter.getName() + "\' with value = \'" + parameter.getExpression().infix() + "\' will be overridden by the global value. \nPress \'Ok' to override " + "local value with global value of \'" + parameter.getName() + "\'. \nPress \'Cancel\' to retain new local value.";
choice = PopupGenerator.showWarningDialog(ownerTable, msgStr, new String[] { "Ok", "Cancel" }, "Ok");
}
if (choice.equals("Ok")) {
try {
// Now 'parameter' is a local kinetic parameter. If it is not numeric, and if its expression
// contains other local kinetic parameters, warn user that 'parameter' cannot be promoted because
// of its expression containing other local parameters.
boolean bPromoteable = true;
if (!parameter.getExpression().isNumeric()) {
String[] symbols = parameter.getExpression().getSymbols();
for (int i = 0; i < symbols.length; i++) {
if (reactionRule.getKineticLaw().getLocalParameter(symbols[i]) != null) {
PopupGenerator.showErrorDialog(ownerTable, "Parameter \'" + parameter.getName() + "\' contains other local kinetic parameters; Cannot convert it to global until the referenced parameters are global.");
bPromoteable = false;
}
}
}
if (bPromoteable) {
reactionRule.getKineticLaw().convertParameterType(parameter, true);
}
} catch (PropertyVetoException pve) {
pve.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Cannot convert parameter \'" + parameter.getName() + "\' to global parameter : " + pve.getMessage());
} catch (ExpressionBindingException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Cannot convert parameter \'" + parameter.getName() + "\' to global parameter : " + e.getMessage());
}
}
}
}
fireTableRowsUpdated(rowIndex, rowIndex);
break;
}
case COLUMN_VALUE:
{
try {
if (aValue instanceof ScopedExpression) {
// }
throw new RuntimeException("unexpected value type ScopedExpression");
} else if (aValue instanceof String) {
String newExpressionString = (String) aValue;
if (parameter instanceof LocalParameter) {
LocalParameter localParameter = (LocalParameter) parameter;
reactionRule.getKineticLaw().setParameterValue(localParameter, new Expression(newExpressionString), true);
} else if (parameter instanceof LocalProxyParameter) {
parameter.setExpression(new Expression(newExpressionString));
}
}
reactionRule.getKineticLaw().resolveUndefinedUnits();
fireTableRowsUpdated(rowIndex, rowIndex);
} catch (java.beans.PropertyVetoException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Error:\n" + e.getMessage());
} catch (ExpressionException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Expression error:\n" + e.getMessage());
}
break;
}
case COLUMN_UNITS:
{
try {
if (aValue instanceof String && parameter instanceof LocalParameter && ((LocalParameter) parameter).getRole() == RbmKineticLaw.RbmKineticLawParameterType.UserDefined) {
String newUnitString = (String) aValue;
LocalParameter kineticsParm = (LocalParameter) parameter;
ModelUnitSystem modelUnitSystem = reactionRule.getModel().getUnitSystem();
if (!kineticsParm.getUnitDefinition().getSymbol().equals(newUnitString)) {
kineticsParm.setUnitDefinition(modelUnitSystem.getInstance(newUnitString));
reactionRule.getKineticLaw().resolveUndefinedUnits();
fireTableRowsUpdated(rowIndex, rowIndex);
}
}
} catch (VCUnitException e) {
e.printStackTrace(System.out);
PopupGenerator.showErrorDialog(ownerTable, "Error changing parameter unit:\n" + e.getMessage());
}
break;
}
}
// }catch (java.beans.PropertyVetoException e){
// e.printStackTrace(System.out);
// }
}
use of cbit.gui.ScopedExpression in project vcell by virtualcell.
the class GeneralConstraintsTableModel method setValueAt.
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
System.out.println("GeneralConstraintsTableModel().setValueAt(" + aValue + "," + rowIndex + "," + columnIndex + ")");
if (rowIndex < 0 || rowIndex >= getRowCount()) {
throw new RuntimeException("GeneralConstraintsTableModel.setValueAt(), row = " + rowIndex + " out of range [" + 0 + "," + (getRowCount() - 1) + "]");
}
if (columnIndex < 0 || columnIndex >= NUM_COLUMNS) {
throw new RuntimeException("GeneralConstraintsTableModel.setValueAt(), column = " + columnIndex + " out of range [" + 0 + "," + (NUM_COLUMNS - 1) + "]");
}
GeneralConstraint generalConstraint = getConstraintContainerImpl().getGeneralConstraints(rowIndex);
try {
switch(columnIndex) {
case COLUMN_EXPRESSION:
{
try {
if (aValue instanceof ScopedExpression) {
// generalConstraint.setExpression(exp);
throw new RuntimeException("unexpected value type ScopedExpression");
} else if (aValue instanceof String) {
String newExpressionString = (String) aValue;
generalConstraint.setExpression(new Expression(newExpressionString));
}
fireTableRowsUpdated(rowIndex, rowIndex);
} catch (ExpressionException e) {
e.printStackTrace(System.out);
DialogUtils.showErrorDialog(ownerTable, "Expression error:\n" + e.getMessage());
}
break;
}
case COLUMN_DESCRIPTION:
{
if (aValue instanceof String) {
generalConstraint.setDescription((String) aValue);
fireTableRowsUpdated(rowIndex, rowIndex);
} else {
System.out.println("ConstraintsTableModel.setValueAt(), unsupported type " + aValue.getClass().getName() + " for COLUMN_DESCRIPTION");
}
break;
}
case COLUMN_INCLUDED:
{
if (aValue instanceof Boolean) {
getConstraintContainerImpl().setActive(generalConstraint, ((Boolean) aValue).booleanValue());
fireTableRowsUpdated(rowIndex, rowIndex);
} else {
System.out.println("ConstraintsTableModel.setValueAt(), unsupported type " + aValue.getClass().getName() + " for COLUMN_INCLUDED");
}
break;
}
}
} catch (java.beans.PropertyVetoException e) {
e.printStackTrace(System.out);
}
}
use of cbit.gui.ScopedExpression in project vcell by virtualcell.
the class GeometrySubVolumeTableModel method setValueAt.
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (rowIndex < 0 || rowIndex >= getRowCount()) {
throw new RuntimeException("GeometrySubVolumeTableModel.setValueAt(), row = " + rowIndex + " out of range [" + 0 + "," + (getRowCount() - 1) + "]");
}
if (columnIndex < 0 || columnIndex >= getColumnCount()) {
throw new RuntimeException("GeometrySubVolumeTableModel.setValueAt(), column = " + columnIndex + " out of range [" + 0 + "," + (getColumnCount() - 1) + "]");
}
final SubVolume subVolume = getValueAt(rowIndex);
try {
switch(columnIndex) {
case COLUMN_NAME:
{
final String newName = (String) aValue;
final String oldName = subVolume.getName();
subVolume.setName(newName);
AsynchClientTask task1 = new AsynchClientTask("changing the name", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
getGeometry().precomputeAll(new GeometryThumbnailImageFactoryAWT());
GeometrySpec gs = getGeometry().getGeometrySpec();
if (gs != null) {
gs.geometryNameChanged(oldName, newName);
} else {
if (lg.isEnabledFor(Level.WARN)) {
lg.warn(getGeometry().getDescription() + " has no GeometrySpec?");
}
}
}
};
ClientTaskDispatcher.dispatch(ownerTable, new Hashtable<String, Object>(), new AsynchClientTask[] { task1 }, false);
break;
}
case COLUMN_VALUE:
{
if (subVolume instanceof AnalyticSubVolume) {
final AnalyticSubVolume analyticSubVolume = (AnalyticSubVolume) subVolume;
if (aValue instanceof ScopedExpression) {
throw new RuntimeException("unexpected value type ScopedExpression");
} else if (aValue instanceof String) {
final String newExpressionString = (String) aValue;
analyticSubVolume.setExpression(new Expression(newExpressionString));
AsynchClientTask task1 = new AsynchClientTask("changing the expression", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
getGeometry().precomputeAll(new GeometryThumbnailImageFactoryAWT());
}
};
ClientTaskDispatcher.dispatch(ownerTable, new Hashtable<String, Object>(), new AsynchClientTask[] { task1 }, false);
}
}
break;
}
}
} catch (Exception e) {
e.printStackTrace(System.out);
DialogUtils.showErrorDialog(ownerTable, e.getMessage(), e);
}
}
Aggregations