-package org.simantics.spreadsheet.ui;\r
-\r
-import java.awt.Color;\r
-import java.awt.Component;\r
-import java.awt.Rectangle;\r
-import java.io.Serializable;\r
-\r
-import javax.swing.JComponent;\r
-import javax.swing.JLabel;\r
-import javax.swing.JTable;\r
-import javax.swing.SwingConstants;\r
-import javax.swing.border.Border;\r
-import javax.swing.table.TableCellRenderer;\r
-\r
-public class ColumnHeaderRenderer extends JLabel implements TableCellRenderer, Serializable {\r
-\r
- private static final long serialVersionUID = 1L;\r
-\r
-\r
- /**\r
- * An empty <code>Border</code>. This field might not be used. To change the\r
- * <code>Border</code> used by this renderer override the \r
- * <code>getTableCellRendererComponent</code> method and set the border\r
- * of the returned component directly.\r
- */\r
-\r
- // We need a place to store the color the JLabel should be returned \r
- // to after its foreground and background colors have been set \r
- // to the selection background color. \r
- // These ivars will be made protected when their names are finalized. \r
- private Color unselectedForeground; \r
- private Color unselectedBackground; \r
-\r
- /**\r
- * Creates a default table cell renderer.\r
- */\r
- public ColumnHeaderRenderer() {\r
- super();\r
- setOpaque(true);\r
- setHorizontalAlignment(SwingConstants.CENTER);\r
- setBackground(DefaultLookup.GRAY);\r
- setBorder(getNoFocusBorder());\r
- setName("Table.cellRenderer");\r
- }\r
-\r
- private Border getNoFocusBorder() {\r
- return TableBorder.BORDER;\r
- }\r
-\r
- /**\r
- * Overrides <code>JComponent.setForeground</code> to assign\r
- * the unselected-foreground color to the specified color.\r
- * \r
- * @param c set the foreground color to this value\r
- */\r
- public void setForeground(Color c) {\r
- super.setForeground(c); \r
- unselectedForeground = c; \r
- }\r
-\r
- /**\r
- * Overrides <code>JComponent.setBackground</code> to assign\r
- * the unselected-background color to the specified color.\r
- *\r
- * @param c set the background color to this value\r
- */\r
- public void setBackground(Color c) {\r
- super.setBackground(c); \r
- unselectedBackground = c; \r
- }\r
-\r
- /**\r
- * Notification from the <code>UIManager</code> that the look and feel\r
- * [L&F] has changed.\r
- * Replaces the current UI object with the latest version from the \r
- * <code>UIManager</code>.\r
- *\r
- * @see JComponent#updateUI\r
- */\r
- public void updateUI() {\r
- super.updateUI(); \r
- setForeground(null);\r
- setBackground(null);\r
- }\r
-\r
- // implements javax.swing.table.TableCellRenderer\r
- /**\r
- *\r
- * Returns the default table cell renderer.\r
- * <p>\r
- * During a printing operation, this method will be called with\r
- * <code>isSelected</code> and <code>hasFocus</code> values of\r
- * <code>false</code> to prevent selection and focus from appearing\r
- * in the printed output. To do other customization based on whether\r
- * or not the table is being printed, check the return value from\r
- * {@link javax.swing.JComponent#isPaintingForPrint()}.\r
- *\r
- * @param table the <code>JTable</code>\r
- * @param value the value to assign to the cell at\r
- * <code>[row, column]</code>\r
- * @param isSelected true if cell is selected\r
- * @param hasFocus true if cell has focus\r
- * @param row the row of the cell to render\r
- * @param column the column of the cell to render\r
- * @return the default table cell renderer\r
- * @see javax.swing.JComponent#isPaintingForPrint()\r
- */\r
- public Component getTableCellRendererComponent(JTable table, Object value,\r
- boolean isSelected, boolean hasFocus, int row, int column) {\r
-\r
- Color fg = null;\r
- Color bg = null;\r
-\r
- JTable.DropLocation dropLocation = table.getDropLocation();\r
- if (dropLocation != null\r
- && !dropLocation.isInsertRow()\r
- && !dropLocation.isInsertColumn()\r
- && dropLocation.getRow() == row\r
- && dropLocation.getColumn() == column) {\r
-\r
- fg = DefaultLookup.getColor(this, ui, "Table.dropCellForeground");\r
- bg = DefaultLookup.getColor(this, ui, "Table.dropCellBackground");\r
-\r
- isSelected = true;\r
- }\r
-\r
- if (isSelected) {\r
- super.setForeground(fg == null ? table.getSelectionForeground()\r
- : fg);\r
- super.setBackground(bg == null ? table.getSelectionBackground()\r
- : bg);\r
- } else {\r
- Color background = unselectedBackground != null\r
- ? unselectedBackground\r
- : table.getBackground();\r
- if (background == null || background instanceof javax.swing.plaf.UIResource) {\r
- Color alternateColor = DefaultLookup.getColor(this, ui, "Table.alternateRowColor");\r
- if (alternateColor != null && row % 2 == 0)\r
- background = alternateColor;\r
- }\r
- super.setForeground(unselectedForeground != null\r
- ? unselectedForeground\r
- : table.getForeground());\r
- super.setBackground(background);\r
- }\r
-\r
- setFont(table.getFont());\r
-\r
- if (hasFocus) {\r
- Border border = null;\r
- if (isSelected) {\r
- border = DefaultLookup.getBorder(this, ui, "Table.focusSelectedCellHighlightBorder");\r
- }\r
- if (border == null) {\r
- border = DefaultLookup.getBorder(this, ui, "Table.focusCellHighlightBorder");\r
- }\r
- setBorder(border);\r
-\r
- if (!isSelected && table.isCellEditable(row, column)) {\r
- Color col;\r
- col = DefaultLookup.getColor(this, ui, "Table.focusCellForeground");\r
- if (col != null) {\r
- super.setForeground(col);\r
- }\r
- col = DefaultLookup.getColor(this, ui, "Table.focusCellBackground");\r
- if (col != null) {\r
- super.setBackground(col);\r
- }\r
- }\r
- } else {\r
- setBorder(getNoFocusBorder());\r
- }\r
-\r
- setValue(value); \r
-\r
- return this;\r
- }\r
-\r
- /*\r
- * The following methods are overridden as a performance measure to \r
- * to prune code-paths are often called in the case of renders\r
- * but which we know are unnecessary. Great care should be taken\r
- * when writing your own renderer to weigh the benefits and \r
- * drawbacks of overriding methods like these.\r
- */\r
-\r
- /**\r
- * Overridden for performance reasons.\r
- * See the <a href="#override">Implementation Note</a> \r
- * for more information.\r
- */\r
- public boolean isOpaque() { \r
- Color back = getBackground();\r
- Component p = getParent(); \r
- if (p != null) { \r
- p = p.getParent(); \r
- }\r
-\r
- // p should now be the JTable. \r
- boolean colorMatch = (back != null) && (p != null) && \r
- back.equals(p.getBackground()) && \r
- p.isOpaque();\r
- return !colorMatch && super.isOpaque(); \r
- }\r
-\r
- /**\r
- * Overridden for performance reasons.\r
- * See the <a href="#override">Implementation Note</a> \r
- * for more information.\r
- *\r
- * @since 1.5\r
- */\r
- public void invalidate() {}\r
-\r
- /**\r
- * Overridden for performance reasons.\r
- * See the <a href="#override">Implementation Note</a> \r
- * for more information.\r
- */\r
- public void validate() {}\r
-\r
- /**\r
- * Overridden for performance reasons.\r
- * See the <a href="#override">Implementation Note</a> \r
- * for more information.\r
- */\r
- public void revalidate() {}\r
-\r
- /**\r
- * Overridden for performance reasons.\r
- * See the <a href="#override">Implementation Note</a> \r
- * for more information.\r
- */\r
- public void repaint(long tm, int x, int y, int width, int height) {}\r
-\r
- /**\r
- * Overridden for performance reasons.\r
- * See the <a href="#override">Implementation Note</a> \r
- * for more information.\r
- */\r
- public void repaint(Rectangle r) { }\r
-\r
- /**\r
- * Overridden for performance reasons.\r
- * See the <a href="#override">Implementation Note</a> \r
- * for more information.\r
- *\r
- * @since 1.5\r
- */\r
- public void repaint() {\r
- }\r
-\r
- /**\r
- * Overridden for performance reasons.\r
- * See the <a href="#override">Implementation Note</a> \r
- * for more information.\r
- */\r
- protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { \r
- // Strings get interned...\r
- if (propertyName=="text"\r
- || propertyName == "labelFor"\r
- || propertyName == "displayedMnemonic"\r
- || ((propertyName == "font" || propertyName == "foreground")\r
- && oldValue != newValue\r
- && getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey) != null)) {\r
-\r
- super.firePropertyChange(propertyName, oldValue, newValue);\r
- }\r
- }\r
-\r
- /**\r
- * Overridden for performance reasons.\r
- * See the <a href="#override">Implementation Note</a> \r
- * for more information.\r
- */\r
- public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { }\r
-\r
-\r
- /**\r
- * Sets the <code>String</code> object for the cell being rendered to\r
- * <code>value</code>.\r
- * \r
- * @param value the string value for this cell; if value is\r
- * <code>null</code> it sets the text value to an empty string\r
- * @see JLabel#setText\r
- * \r
- */\r
- protected void setValue(Object value) {\r
- setText((value == null) ? "" : value.toString());\r
- }\r
-\r
-}\r
+package org.simantics.spreadsheet.ui;
+
+import java.awt.Color;
+import java.awt.Component;
+import java.awt.Rectangle;
+import java.io.Serializable;
+
+import javax.swing.JComponent;
+import javax.swing.JLabel;
+import javax.swing.JTable;
+import javax.swing.SwingConstants;
+import javax.swing.border.Border;
+import javax.swing.table.TableCellRenderer;
+
+public class ColumnHeaderRenderer extends JLabel implements TableCellRenderer, Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+
+ /**
+ * An empty <code>Border</code>. This field might not be used. To change the
+ * <code>Border</code> used by this renderer override the
+ * <code>getTableCellRendererComponent</code> method and set the border
+ * of the returned component directly.
+ */
+
+ // We need a place to store the color the JLabel should be returned
+ // to after its foreground and background colors have been set
+ // to the selection background color.
+ // These ivars will be made protected when their names are finalized.
+ private Color unselectedForeground;
+ private Color unselectedBackground;
+
+ /**
+ * Creates a default table cell renderer.
+ */
+ public ColumnHeaderRenderer() {
+ super();
+ setOpaque(true);
+ setHorizontalAlignment(SwingConstants.CENTER);
+ setBackground(DefaultLookup.GRAY);
+ setBorder(getNoFocusBorder());
+ setName("Table.cellRenderer");
+ }
+
+ private Border getNoFocusBorder() {
+ return TableBorder.BORDER;
+ }
+
+ /**
+ * Overrides <code>JComponent.setForeground</code> to assign
+ * the unselected-foreground color to the specified color.
+ *
+ * @param c set the foreground color to this value
+ */
+ public void setForeground(Color c) {
+ super.setForeground(c);
+ unselectedForeground = c;
+ }
+
+ /**
+ * Overrides <code>JComponent.setBackground</code> to assign
+ * the unselected-background color to the specified color.
+ *
+ * @param c set the background color to this value
+ */
+ public void setBackground(Color c) {
+ super.setBackground(c);
+ unselectedBackground = c;
+ }
+
+ /**
+ * Notification from the <code>UIManager</code> that the look and feel
+ * [L&F] has changed.
+ * Replaces the current UI object with the latest version from the
+ * <code>UIManager</code>.
+ *
+ * @see JComponent#updateUI
+ */
+ public void updateUI() {
+ super.updateUI();
+ setForeground(null);
+ setBackground(null);
+ }
+
+ // implements javax.swing.table.TableCellRenderer
+ /**
+ *
+ * Returns the default table cell renderer.
+ * <p>
+ * During a printing operation, this method will be called with
+ * <code>isSelected</code> and <code>hasFocus</code> values of
+ * <code>false</code> to prevent selection and focus from appearing
+ * in the printed output. To do other customization based on whether
+ * or not the table is being printed, check the return value from
+ * {@link javax.swing.JComponent#isPaintingForPrint()}.
+ *
+ * @param table the <code>JTable</code>
+ * @param value the value to assign to the cell at
+ * <code>[row, column]</code>
+ * @param isSelected true if cell is selected
+ * @param hasFocus true if cell has focus
+ * @param row the row of the cell to render
+ * @param column the column of the cell to render
+ * @return the default table cell renderer
+ * @see javax.swing.JComponent#isPaintingForPrint()
+ */
+ public Component getTableCellRendererComponent(JTable table, Object value,
+ boolean isSelected, boolean hasFocus, int row, int column) {
+
+ Color fg = null;
+ Color bg = null;
+
+ JTable.DropLocation dropLocation = table.getDropLocation();
+ if (dropLocation != null
+ && !dropLocation.isInsertRow()
+ && !dropLocation.isInsertColumn()
+ && dropLocation.getRow() == row
+ && dropLocation.getColumn() == column) {
+
+ fg = DefaultLookup.getColor(this, ui, "Table.dropCellForeground");
+ bg = DefaultLookup.getColor(this, ui, "Table.dropCellBackground");
+
+ isSelected = true;
+ }
+
+ if (isSelected) {
+ super.setForeground(fg == null ? table.getSelectionForeground()
+ : fg);
+ super.setBackground(bg == null ? table.getSelectionBackground()
+ : bg);
+ } else {
+ Color background = unselectedBackground != null
+ ? unselectedBackground
+ : table.getBackground();
+ if (background == null || background instanceof javax.swing.plaf.UIResource) {
+ Color alternateColor = DefaultLookup.getColor(this, ui, "Table.alternateRowColor");
+ if (alternateColor != null && row % 2 == 0)
+ background = alternateColor;
+ }
+ super.setForeground(unselectedForeground != null
+ ? unselectedForeground
+ : table.getForeground());
+ super.setBackground(background);
+ }
+
+ setFont(table.getFont());
+
+ if (hasFocus) {
+ Border border = null;
+ if (isSelected) {
+ border = DefaultLookup.getBorder(this, ui, "Table.focusSelectedCellHighlightBorder");
+ }
+ if (border == null) {
+ border = DefaultLookup.getBorder(this, ui, "Table.focusCellHighlightBorder");
+ }
+ setBorder(border);
+
+ if (!isSelected && table.isCellEditable(row, column)) {
+ Color col;
+ col = DefaultLookup.getColor(this, ui, "Table.focusCellForeground");
+ if (col != null) {
+ super.setForeground(col);
+ }
+ col = DefaultLookup.getColor(this, ui, "Table.focusCellBackground");
+ if (col != null) {
+ super.setBackground(col);
+ }
+ }
+ } else {
+ setBorder(getNoFocusBorder());
+ }
+
+ setValue(value);
+
+ return this;
+ }
+
+ /*
+ * The following methods are overridden as a performance measure to
+ * to prune code-paths are often called in the case of renders
+ * but which we know are unnecessary. Great care should be taken
+ * when writing your own renderer to weigh the benefits and
+ * drawbacks of overriding methods like these.
+ */
+
+ /**
+ * Overridden for performance reasons.
+ * See the <a href="#override">Implementation Note</a>
+ * for more information.
+ */
+ public boolean isOpaque() {
+ Color back = getBackground();
+ Component p = getParent();
+ if (p != null) {
+ p = p.getParent();
+ }
+
+ // p should now be the JTable.
+ boolean colorMatch = (back != null) && (p != null) &&
+ back.equals(p.getBackground()) &&
+ p.isOpaque();
+ return !colorMatch && super.isOpaque();
+ }
+
+ /**
+ * Overridden for performance reasons.
+ * See the <a href="#override">Implementation Note</a>
+ * for more information.
+ *
+ * @since 1.5
+ */
+ public void invalidate() {}
+
+ /**
+ * Overridden for performance reasons.
+ * See the <a href="#override">Implementation Note</a>
+ * for more information.
+ */
+ public void validate() {}
+
+ /**
+ * Overridden for performance reasons.
+ * See the <a href="#override">Implementation Note</a>
+ * for more information.
+ */
+ public void revalidate() {}
+
+ /**
+ * Overridden for performance reasons.
+ * See the <a href="#override">Implementation Note</a>
+ * for more information.
+ */
+ public void repaint(long tm, int x, int y, int width, int height) {}
+
+ /**
+ * Overridden for performance reasons.
+ * See the <a href="#override">Implementation Note</a>
+ * for more information.
+ */
+ public void repaint(Rectangle r) { }
+
+ /**
+ * Overridden for performance reasons.
+ * See the <a href="#override">Implementation Note</a>
+ * for more information.
+ *
+ * @since 1.5
+ */
+ public void repaint() {
+ }
+
+ /**
+ * Overridden for performance reasons.
+ * See the <a href="#override">Implementation Note</a>
+ * for more information.
+ */
+ protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
+ // Strings get interned...
+ if (propertyName=="text"
+ || propertyName == "labelFor"
+ || propertyName == "displayedMnemonic"
+ || ((propertyName == "font" || propertyName == "foreground")
+ && oldValue != newValue
+ && getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey) != null)) {
+
+ super.firePropertyChange(propertyName, oldValue, newValue);
+ }
+ }
+
+ /**
+ * Overridden for performance reasons.
+ * See the <a href="#override">Implementation Note</a>
+ * for more information.
+ */
+ public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { }
+
+
+ /**
+ * Sets the <code>String</code> object for the cell being rendered to
+ * <code>value</code>.
+ *
+ * @param value the string value for this cell; if value is
+ * <code>null</code> it sets the text value to an empty string
+ * @see JLabel#setText
+ *
+ */
+ protected void setValue(Object value) {
+ setText((value == null) ? "" : value.toString());
+ }
+
+}