]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/ColumnHeaderRenderer.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.spreadsheet.ui / src / org / simantics / spreadsheet / ui / ColumnHeaderRenderer.java
diff --git a/bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/ColumnHeaderRenderer.java b/bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/ColumnHeaderRenderer.java
new file mode 100644 (file)
index 0000000..aa2b128
--- /dev/null
@@ -0,0 +1,292 @@
+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