View Javadoc
1   package org.opentrafficsim.editor.render;
2   
3   import java.awt.Component;
4   
5   import javax.swing.JLabel;
6   import javax.swing.JTable;
7   import javax.swing.UIManager;
8   import javax.swing.border.Border;
9   import javax.swing.border.EmptyBorder;
10  import javax.swing.border.LineBorder;
11  import javax.swing.table.TableCellRenderer;
12  
13  import org.opentrafficsim.editor.OtsEditor;
14  import org.opentrafficsim.editor.XsdTreeNode;
15  
16  import de.javagl.treetable.JTreeTable;
17  
18  /**
19   * Renderer for columns in the tree table.
20   * <p>
21   * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
22   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
23   * </p>
24   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
25   */
26  public class StringCellRenderer extends JLabel implements TableCellRenderer
27  {
28  
29      /** */
30      private static final long serialVersionUID = 20230218L;
31  
32      /** Tree table. */
33      private final JTreeTable treeTable;
34  
35      /**
36       * Constructor.
37       * @param treeTable JTreeTable; tree table.
38       */
39      public StringCellRenderer(final JTreeTable treeTable)
40      {
41          this.treeTable = treeTable;
42          setOpaque(true);
43      }
44  
45      /** {@inheritDoc} */
46      @Override
47      public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected,
48              final boolean hasFocus, final int row, final int column)
49      {
50          String val = value == null ? "" : value.toString();
51          setText(val);
52          setFont(table.getFont());
53  
54          if (isSelected)
55          {
56              setBackground(UIManager.getColor("Table.selectionBackground"));
57          }
58          else
59          {
60              int treeColumn = this.treeTable.convertColumnIndexToView(0); // columns may have been moved in view
61              int idColumn = this.treeTable.convertColumnIndexToView(1);
62              int valueColumn = this.treeTable.convertColumnIndexToView(2);
63              XsdTreeNode node = (XsdTreeNode) this.treeTable.getValueAt(row, treeColumn);
64              String message = node.isSelfValid() ? null : (column == idColumn ? node.reportInvalidId()
65                      : (column == valueColumn ? node.reportInvalidValue() : null));
66              if (this.treeTable.isCellEditable(row, column))
67              {
68                  if (message != null)
69                  {
70                      setToolTipText(OtsEditor.limitTooltip(message));
71                      setBackground(OtsEditor.INVALID_COLOR);
72                  }
73                  else
74                  {
75                      setToolTipText(OtsEditor
76                              .limitTooltip(!val.isEmpty() && (column == idColumn || column == valueColumn) ? val : null));
77                      boolean expression = column == idColumn ? node.idIsExpression()
78                              : (column == valueColumn ? node.valueIsExpression() : false);
79                      if (expression)
80                      {
81                          setBackground(OtsEditor.EXPRESSION_COLOR);
82                      }
83                      else
84                      {
85                          setBackground(UIManager.getColor("Table.background"));
86                      }
87                  }
88              }
89              else
90              {
91                  setToolTipText(null);
92                  setBackground(UIManager.getColor("Table.background"));
93              }
94          }
95  
96          Border border;
97          if (this.treeTable.isCellEditable(row, column))
98          {
99              border = new LineBorder(UIManager.getColor("Table.gridColor"));
100         }
101         else
102         {
103             border = new EmptyBorder(0, 0, 0, 0);
104         }
105         setBorder(border);
106 
107         return this;
108     }
109 
110 }