Skip to content

Commit

Permalink
molecular details: NamedColor combobox, editor, renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
danv61 committed Oct 25, 2024
1 parent 16eda45 commit 089fbdc
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,24 @@

package cbit.vcell.mapping.gui;

import java.awt.Component;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.SwingConstants;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;

import org.vcell.model.rbm.ComponentStatePattern;
import org.vcell.model.rbm.MolecularComponent;
import org.vcell.model.rbm.MolecularComponentPattern;
import org.vcell.model.rbm.MolecularType;
import org.vcell.model.rbm.MolecularTypePattern;
import org.vcell.model.rbm.SpeciesPattern;
import org.vcell.util.gui.ColorIcon;
import org.vcell.util.gui.GuiUtils;
import org.vcell.util.gui.ScrollTable;

Expand All @@ -44,6 +41,8 @@
import cbit.vcell.model.SpeciesContext;
import cbit.vcell.model.Structure;
import cbit.vcell.parser.Expression;
import org.vcell.util.springsalad.Colors;
import org.vcell.util.springsalad.NamedColor;

/**
* Insert the type's description here.
Expand All @@ -63,7 +62,8 @@ private enum ColumnType {
COLUMN_STRUCTURE("Location"),
COLUMN_STATE("Initial State"),
COLUMN_RADIUS("Radius (nm)"),
COLUMN_DIFFUSION("Diffusion Rate (um^2/s)");
COLUMN_DIFFUSION("Diff. Rate (um^2/s)"),
COLUMN_COLOR("Color");

public final String label;
private ColumnType(String label){
Expand Down Expand Up @@ -95,6 +95,8 @@ public Class<?> getColumnClass(int column) {
case COLUMN_RADIUS:
case COLUMN_DIFFUSION:
return Expression.class;
case COLUMN_COLOR:
return NamedColor.class;
default:
return Object.class;
}
Expand Down Expand Up @@ -151,6 +153,11 @@ public Object getValueAt(int row, int col) {
return null;
}
return sas.getDiffusionRate(); // um^2/s
case COLUMN_COLOR:
if(sas == null) {
return null;
}
return sas.getColor();
default:
return null;
}
Expand Down Expand Up @@ -204,6 +211,16 @@ public void setValueAt(Object aValue, int row, int col) {
sas.setDiffusionRate(result);
return;
}
case COLUMN_COLOR:
if (aValue instanceof NamedColor) {
NamedColor namedColor = (NamedColor)aValue;
SiteAttributesSpec sas = getSpeciesContextSpec().getSiteAttributesMap().get(mcp);
if(sas == null) {
sas = new SiteAttributesSpec(fieldSpeciesContextSpec, mcp, getSpeciesContextSpec().getSpeciesContext().getStructure());
}
sas.setColor(namedColor);
return;
}
default:
return;
}
Expand All @@ -227,6 +244,7 @@ public boolean isCellEditable(int row, int col) {
case COLUMN_STRUCTURE:
case COLUMN_RADIUS:
case COLUMN_DIFFUSION:
case COLUMN_COLOR:
return true;
default:
return false;
Expand All @@ -251,6 +269,7 @@ public int compare(MolecularComponentPattern mcp1, MolecularComponentPattern mcp
case COLUMN_STATE:
case COLUMN_RADIUS:
case COLUMN_DIFFUSION:
case COLUMN_COLOR:
default:
return 1;
}
Expand Down Expand Up @@ -381,8 +400,40 @@ private void refreshData() {
GuiUtils.flexResizeTableColumns(ownerTable);

updateLocationComboBox();
updateColorComboBox();
}




private void updateColorComboBox() {
if(fieldSimulationContext == null) {
return;
}
DefaultComboBoxModel<NamedColor> model = new DefaultComboBoxModel<>();
for(NamedColor namedColor : Colors.COLORARRAY) {
model.addElement(namedColor);
}
JComboBox<NamedColor> colorComboBox = new JComboBox<>();
colorComboBox.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
setHorizontalTextPosition(SwingConstants.LEFT);
if (value instanceof NamedColor) {
NamedColor namedColor = (NamedColor)value;
setText(namedColor.getName());
Icon icon = new ColorIcon(10,10,namedColor.getColor(), true); // small square icon with subdomain color
setHorizontalTextPosition(SwingConstants.RIGHT);
setIcon(icon);
}
return this;
}
});
colorComboBox.setModel(model);
ownerTable.getColumnModel().getColumn(ColumnType.COLUMN_COLOR.ordinal()).setCellEditor(new DefaultCellEditor(colorComboBox));
}

private void updateLocationComboBox() {
if(fieldSimulationContext == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.vcell.model.rbm.MolecularTypePattern;
import org.vcell.model.rbm.SpeciesPattern;
import org.vcell.util.Coordinate;
import org.vcell.util.gui.ColorIcon;
import org.vcell.util.gui.DefaultScrollTableCellRenderer;
import org.vcell.util.gui.EditorScrollTable;
import org.vcell.util.gui.ScrollTable.ScrollTableBooleanCellRenderer;
Expand Down Expand Up @@ -66,7 +67,7 @@ public class MolecularStructuresPanel extends DocumentEditorSubPanel implements
private EditorScrollTable molecularTypeSpecsTable = null;
private MolecularTypeSpecsTableModel molecularTypeSpecsTableModel = null;

private JComboBox<String> siteColorComboBox = null;
// private JComboBox<String> siteColorComboBox = null;
private JTextField siteXField = null;
private JTextField siteYField = null;
private JTextField siteZField = null;
Expand Down Expand Up @@ -127,8 +128,8 @@ public void actionPerformed(ActionEvent e) {
} else if(source == deleteLinkButton) {
deleteLinkActionPerformed();
refreshSiteLinksList();
} else if(source == getSiteColorComboBox()) {
updateSiteColor();
// } else if(source == getSiteColorComboBox()) {
// updateSiteColor();
}
}
public void focusGained(FocusEvent e) {
Expand Down Expand Up @@ -217,7 +218,7 @@ private void initConnections() throws java.lang.Exception { // listeners here!
addLinkButton.addActionListener(eventHandler);
deleteLinkButton.addActionListener(eventHandler);

getSiteColorComboBox().addActionListener(eventHandler);
// getSiteColorComboBox().addActionListener(eventHandler);

ListSelectionModel lsm = getSpeciesContextSpecsTable().getSelectionModel();
if(lsm instanceof DefaultListSelectionModel) {
Expand Down Expand Up @@ -509,7 +510,7 @@ public Component getTableCellRendererComponent(JTable table, Object value, boole
gbc = new GridBagConstraints();
gbc.gridx = 3;
gbc.gridy = 0;
gbc.weightx = 0.5;
gbc.weightx = 0.1;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(3, 2, 2, 3);
Expand All @@ -528,8 +529,34 @@ public Component getTableCellRendererComponent(JTable table, Object value, boole
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.insets = new Insets(2, 3, 3, 4);
sitesPanel.add(pb, gbc);

sitesPanel.add(pb, gbc); // MolecularTypeSpecsTable

// The NamedColor combobox cell renderer in the MolecularTypeSpecsTable
DefaultScrollTableCellRenderer namedColorTableCellRenderer = new DefaultScrollTableCellRenderer() {
final Color lightBlueBackground = new Color(214, 234, 248);
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (table.getModel() instanceof MolecularTypeSpecsTableModel) {
if (value instanceof NamedColor) {
System.out.println("NamedColor table cell");
NamedColor namedColor = (NamedColor)value;
setText(namedColor.getName());
Icon icon = new ColorIcon(10,10,namedColor.getColor(), true); // small square icon with subdomain color
setHorizontalTextPosition(SwingConstants.RIGHT);
setIcon(icon);
}
}
return this;
}
};

getMolecularTypeSpecsTable().setDefaultRenderer(String.class, new DefaultScrollTableCellRenderer());
getMolecularTypeSpecsTable().setDefaultRenderer(Structure.class, structuresTableCellRenderer); // The Structures combobox cell renderer
getMolecularTypeSpecsTable().setDefaultRenderer(NamedColor.class, namedColorTableCellRenderer); // NamedColor combobox cell renderer


gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 5;
Expand Down Expand Up @@ -578,21 +605,21 @@ public Component getTableCellRendererComponent(JTable table, Object value, boole
gbc.insets = new Insets(2, 2, 2, 2);
sitesPanel.add(siteZField, gbc);

gbc = new GridBagConstraints();
gbc.gridx = 6;
gbc.gridy = 5;
gbc.anchor = GridBagConstraints.SOUTH;
gbc.insets = new Insets(2, 2, 2, 2);
sitesPanel.add(new JLabel(" Color "), gbc);

gbc = new GridBagConstraints();
gbc.gridx = 7;
gbc.gridy = 5;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.SOUTH;
gbc.insets = new Insets(2, 2, 2, 2);
sitesPanel.add(getSiteColorComboBox(), gbc);
// gbc = new GridBagConstraints();
// gbc.gridx = 6;
// gbc.gridy = 5;
// gbc.anchor = GridBagConstraints.SOUTH;
// gbc.insets = new Insets(2, 2, 2, 2);
// sitesPanel.add(new JLabel(" Color "), gbc);

// gbc = new GridBagConstraints();
// gbc.gridx = 7;
// gbc.gridy = 5;
// gbc.weightx = 1.0;
// gbc.fill = GridBagConstraints.HORIZONTAL;
// gbc.anchor = GridBagConstraints.SOUTH;
// gbc.insets = new Insets(2, 2, 2, 2);
// sitesPanel.add(getSiteColorComboBox(), gbc);

// // --- links -----------------------------------------------
linksPanel.setLayout(new GridBagLayout());
Expand Down Expand Up @@ -637,10 +664,6 @@ public Component getTableCellRendererComponent(JTable table, Object value, boole
gbc.insets = new Insets(5, 2, 2, 3);
linksPanel.add(deleteLinkButton, gbc);

getMolecularTypeSpecsTable().setDefaultRenderer(String.class, new DefaultScrollTableCellRenderer());
getMolecularTypeSpecsTable().setDefaultRenderer(Structure.class, structuresTableCellRenderer); // The Structures combobox cell renderer


initConnections(); // adding listeners

} catch(Throwable e) {
Expand Down Expand Up @@ -681,33 +704,33 @@ private EditorScrollTable getSpeciesContextSpecsTable() {
}

// siteColorComboBox
private JComboBox<String> getSiteColorComboBox() {
if (siteColorComboBox == null) {
siteColorComboBox = new JComboBox<String>();
siteColorComboBox.setName("JComboBox1");
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
for(NamedColor namedColor : Colors.COLORARRAY) {
model.addElement(namedColor.getName());
}
siteColorComboBox.setModel(model);
// siteColorComboBox.setRenderer(new DefaultListCellRenderer() {
// see ReactionRuleKineticsPropertiesPanel.getKineticsTypeComboBox() for complex renderer
// });
}
return siteColorComboBox;
}
private void updateSiteColor() {
String colorName = (String)getSiteColorComboBox().getSelectedItem();
if(colorName == null) {
return;
}
NamedColor namedColor = Colors.getColorByName(colorName);
SiteAttributesSpec sas = fieldSpeciesContextSpec.getSiteAttributesMap().get(fieldMolecularComponentPattern);
if(namedColor != null && namedColor != sas.getColor()) {
sas.setColor(namedColor);
}
}
// private JComboBox<String> getSiteColorComboBox() {
// if (siteColorComboBox == null) {
// siteColorComboBox = new JComboBox<String>();
// siteColorComboBox.setName("JComboBox1");
//
// DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
// for(NamedColor namedColor : Colors.COLORARRAY) {
// model.addElement(namedColor.getName());
// }
// siteColorComboBox.setModel(model);
//// siteColorComboBox.setRenderer(new DefaultListCellRenderer() {
//// see ReactionRuleKineticsPropertiesPanel.getKineticsTypeComboBox() for complex renderer
//// });
// }
// return siteColorComboBox;
// }
// private void updateSiteColor() {
// String colorName = (String)getSiteColorComboBox().getSelectedItem();
// if(colorName == null) {
// return;
// }
// NamedColor namedColor = Colors.getColorByName(colorName);
// SiteAttributesSpec sas = fieldSpeciesContextSpec.getSiteAttributesMap().get(fieldMolecularComponentPattern);
// if(namedColor != null && namedColor != sas.getColor()) {
// sas.setColor(namedColor);
// }
// }

private void handleException(Throwable exception) {
System.out.println("--------- UNCAUGHT EXCEPTION --------- in cbit.vcell.mapping.InitialConditionPanel");
Expand Down Expand Up @@ -805,15 +828,15 @@ private void updateInterface() {
siteXField.setText(sas.getCoordinate().getX()+"");
siteYField.setText(sas.getCoordinate().getY()+"");
siteZField.setText(sas.getCoordinate().getZ()+"");
getSiteColorComboBox().setSelectedItem(sas.getColor().getName());
// getSiteColorComboBox().setSelectedItem(sas.getColor().getName());
} else {
siteXField.setEditable(false);
siteYField.setEditable(false);
siteZField.setEditable(false);
siteXField.setText(null);
siteYField.setText(null);
siteZField.setText(null);
getSiteColorComboBox().setSelectedItem(null);
// getSiteColorComboBox().setSelectedItem(null);
}
}

Expand Down

0 comments on commit 089fbdc

Please sign in to comment.