use of cbit.vcell.parser.ExpressionPrintFormatter 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.vcell.parser.ExpressionPrintFormatter in project vcell by virtualcell.
the class ExpressionCanvas method refreshGraphics.
private void refreshGraphics() {
try {
offScreenImageSize = new Dimension(getSize().width, getSize().height);
offScreenImage = createImage(offScreenImageSize.width, offScreenImageSize.height);
if (offScreenImage == null) {
return;
}
Graphics2D g = (Graphics2D) offScreenImage.getGraphics();
// g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g.setClip(0, 0, getSize().width, getSize().height);
g.setFont(new Font("SansSerif", Font.ITALIC, 11));
g.setColor(getBackground());
g.fillRect(0, 0, getSize().width, getSize().height);
if (fieldExpressions != null) {
g.setColor(getForeground());
Rectangle rect = g.getClipBounds();
int posY = Math.max(10, (rect.height - getSize().height) / 2);
int posX = Math.max(10, (rect.width - getSize().width) / 2);
for (int i = 0; i < fieldExpressions.length; i++) {
int prefixWidth = 0;
ExpressionPrintFormatter expPrintFormatter = new ExpressionPrintFormatter(fieldExpressions[i]);
Dimension expressionDim = expPrintFormatter.getSize(g);
if (fieldPrefixLabels != null && fieldPrefixLabels[i] != null) {
prefixWidth = g.getFontMetrics().stringWidth(fieldPrefixLabels[i]) + 10;
g.setClip(posX, posY, getSize().width, getSize().height);
g.setColor(Color.blue);
g.drawString(fieldPrefixLabels[i], posX, posY + expressionDim.height / 2 + g.getFontMetrics().getDescent());
}
g.setColor(getForeground());
g.setClip(posX + prefixWidth, posY, expressionDim.width, expressionDim.height);
expPrintFormatter.paint(g);
if (fieldSuffixLabels != null && fieldSuffixLabels[i] != null) {
g.setColor(Color.blue);
g.setClip(posX, posY, getSize().width, getSize().height);
g.drawString(fieldSuffixLabels[i], posX + expressionDim.width + prefixWidth + 20, posY + expressionDim.height / 2 + g.getFontMetrics().getDescent());
}
posY += expressionDim.height + 20;
}
}
if (fieldStrings != null) {
// added Oct 3rd, 2006 to display the stochastic stuff in expression canvas
g.setColor(getForeground());
Rectangle rect = g.getClipBounds();
int posY = Math.max(10, (rect.height - getSize().height) / 2);
int posX = Math.max(10, (rect.width - getSize().width) / 2);
for (int i = 0; i < fieldStrings.length; i++) {
g.setClip(posX, posY, getSize().width, getSize().height);
g.setColor(getForeground());
g.drawString(fieldStrings[i], posX, posY + 10);
posY += 40;
}
}
} catch (Exception e) {
offScreenImage = null;
System.out.println("exception in ExpressionCanvas.refreshGraphics()");
e.printStackTrace(System.out);
}
bImageDirty = false;
return;
}
use of cbit.vcell.parser.ExpressionPrintFormatter in project vcell by virtualcell.
the class ExpressionCanvas method getMinimumSize.
/**
* Insert the method's description here.
* Creation date: (11/19/2002 11:26:26 AM)
* @return java.awt.Dimension
*/
public Dimension getMinimumSize() {
Graphics2D g = (Graphics2D) getGraphics();
if (g != null) {
g.setClip(0, 0, getSize().width, getSize().height);
Font canvasFont = new Font("SansSerif", Font.ITALIC, 11);
g.setFont(canvasFont);
FontMetrics fontMetrics = g.getFontMetrics();
try {
Dimension minSize = new Dimension(10, 10);
if (fieldExpressions != null) {
for (int i = 0; i < fieldExpressions.length; i++) {
ExpressionPrintFormatter expPrintFormatter = new ExpressionPrintFormatter(fieldExpressions[i]);
Dimension expressionDim = expPrintFormatter.getSize(g);
int labelWidth = 0;
if (fieldPrefixLabels != null) {
labelWidth += g.getFontMetrics().stringWidth(fieldPrefixLabels[i]) + 20;
}
if (fieldSuffixLabels != null) {
labelWidth += g.getFontMetrics().stringWidth(fieldSuffixLabels[i]) + 20;
}
minSize.width = Math.max(minSize.width, expressionDim.width + 20 + labelWidth);
minSize.height += expressionDim.height + 20;
}
} else {
for (String expString : fieldStrings) {
int stringWidth = fontMetrics.stringWidth(expString);
int stringHeight = fontMetrics.getHeight();
minSize.width = Math.max(minSize.width, stringWidth);
minSize.height += stringHeight + 20;
}
}
return minSize;
} catch (ExpressionException e) {
e.printStackTrace(System.out);
return super.getMinimumSize();
}
} else {
return super.getMinimumSize();
}
}
use of cbit.vcell.parser.ExpressionPrintFormatter in project vcell by virtualcell.
the class ExpressionCanvas method getExpressionImageSize.
public static Dimension getExpressionImageSize(Expression[] expArray, Graphics2D g) throws ExpressionException {
g.setFont(new Font("SansSerif", Font.ITALIC, 25));
Dimension minSize = new Dimension(10, 10);
for (int i = 0; i < expArray.length; i++) {
ExpressionPrintFormatter expPrintFormatter = new ExpressionPrintFormatter(expArray[i]);
java.awt.Dimension expressionDim = expPrintFormatter.getSize(g);
int labelWidth = 0;
minSize.width = Math.max(minSize.width, expressionDim.width + 20 + labelWidth);
minSize.height += expressionDim.height + 20;
}
return minSize;
}
use of cbit.vcell.parser.ExpressionPrintFormatter in project vcell by virtualcell.
the class ExpressionCanvas method getExpressionAsImage.
// used by the publish package.
public static void getExpressionAsImage(Expression[] expArray, BufferedImage image, int scale) {
if (expArray == null || image == null) {
throw new IllegalArgumentException("One or more invalid arguments for refreshGraphics()");
}
try {
int width = image.getWidth();
int height = image.getHeight();
Graphics2D g = (Graphics2D) image.getGraphics();
g.setClip(0, 0, width, height);
g.setBackground(Color.white);
g.fillRect(0, 0, width, height);
g.setFont(new Font("SansSerif", Font.ITALIC, 11));
// System.out.println("size of 'hello' after scale is ["+sizeHelloAfterScale.getWidth()+","+sizeHelloAfterScale.getHeight()+"]");
if (expArray != null) {
g.setColor(Color.black);
Rectangle rect = g.getClipBounds();
int posY = Math.max(10, (rect.height - height) / 2);
int posX = Math.max(10, (rect.width - width) / 2);
for (int i = 0; i < expArray.length; i++) {
int prefixWidth = 0;
ExpressionPrintFormatter expPrintFormatter = new ExpressionPrintFormatter(expArray[i]);
Dimension expressionDim = expPrintFormatter.getSize(g);
g.setClip(posX + prefixWidth, posY, expressionDim.width, expressionDim.height);
expPrintFormatter.paint(g);
posY += expressionDim.height + 20;
}
}
} catch (Exception e) {
System.out.println("exception in ExpressionCanvas.refreshGraphics()");
e.printStackTrace(System.out);
}
}
Aggregations