From 089fbdc84e3e4d3a956931e289f81678a1e96bd6 Mon Sep 17 00:00:00 2001 From: Dan Vasilescu Date: Fri, 25 Oct 2024 14:13:55 -0400 Subject: [PATCH] molecular details: NamedColor combobox, editor, renderer --- .../gui/MolecularTypeSpecsTableModel.java | 69 +++++++-- .../gui/MolecularStructuresPanel.java | 133 ++++++++++-------- 2 files changed, 138 insertions(+), 64 deletions(-) diff --git a/vcell-client/src/main/java/cbit/vcell/mapping/gui/MolecularTypeSpecsTableModel.java b/vcell-client/src/main/java/cbit/vcell/mapping/gui/MolecularTypeSpecsTableModel.java index 7e6a273c48..ec3bfb1c96 100644 --- a/vcell-client/src/main/java/cbit/vcell/mapping/gui/MolecularTypeSpecsTableModel.java +++ b/vcell-client/src/main/java/cbit/vcell/mapping/gui/MolecularTypeSpecsTableModel.java @@ -10,7 +10,7 @@ package cbit.vcell.mapping.gui; -import java.awt.Component; +import java.awt.*; import java.beans.PropertyChangeEvent; import java.util.ArrayList; import java.util.Arrays; @@ -18,12 +18,8 @@ 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; @@ -31,6 +27,7 @@ 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; @@ -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. @@ -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){ @@ -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; } @@ -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; } @@ -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; } @@ -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; @@ -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; } @@ -381,8 +400,40 @@ private void refreshData() { GuiUtils.flexResizeTableColumns(ownerTable); updateLocationComboBox(); + updateColorComboBox(); } - + + + + private void updateColorComboBox() { + if(fieldSimulationContext == null) { + return; + } + DefaultComboBoxModel model = new DefaultComboBoxModel<>(); + for(NamedColor namedColor : Colors.COLORARRAY) { + model.addElement(namedColor); + } + JComboBox 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; diff --git a/vcell-client/src/main/java/org/vcell/model/springsalad/gui/MolecularStructuresPanel.java b/vcell-client/src/main/java/org/vcell/model/springsalad/gui/MolecularStructuresPanel.java index 4721df11fe..f42aa9bfe5 100644 --- a/vcell-client/src/main/java/org/vcell/model/springsalad/gui/MolecularStructuresPanel.java +++ b/vcell-client/src/main/java/org/vcell/model/springsalad/gui/MolecularStructuresPanel.java @@ -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; @@ -66,7 +67,7 @@ public class MolecularStructuresPanel extends DocumentEditorSubPanel implements private EditorScrollTable molecularTypeSpecsTable = null; private MolecularTypeSpecsTableModel molecularTypeSpecsTableModel = null; - private JComboBox siteColorComboBox = null; +// private JComboBox siteColorComboBox = null; private JTextField siteXField = null; private JTextField siteYField = null; private JTextField siteZField = null; @@ -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) { @@ -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) { @@ -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); @@ -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; @@ -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()); @@ -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) { @@ -681,33 +704,33 @@ private EditorScrollTable getSpeciesContextSpecsTable() { } // siteColorComboBox - private JComboBox getSiteColorComboBox() { - if (siteColorComboBox == null) { - siteColorComboBox = new JComboBox(); - siteColorComboBox.setName("JComboBox1"); - - DefaultComboBoxModel 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 getSiteColorComboBox() { +// if (siteColorComboBox == null) { +// siteColorComboBox = new JComboBox(); +// siteColorComboBox.setName("JComboBox1"); +// +// DefaultComboBoxModel 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"); @@ -805,7 +828,7 @@ 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); @@ -813,7 +836,7 @@ private void updateInterface() { siteXField.setText(null); siteYField.setText(null); siteZField.setText(null); - getSiteColorComboBox().setSelectedItem(null); +// getSiteColorComboBox().setSelectedItem(null); } }