ColumnHeaderRenderer.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:9k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: ColumnHeaderRenderer.java,v 1.10 2005/10/13 08:59:56 kleopatra Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.table;
  22. import java.awt.Color;
  23. import java.awt.Component;
  24. import java.awt.Font;
  25. import javax.swing.Icon;
  26. import javax.swing.JComponent;
  27. import javax.swing.JLabel;
  28. import javax.swing.JTable;
  29. import javax.swing.UIManager;
  30. import javax.swing.border.Border;
  31. import javax.swing.border.CompoundBorder;
  32. import javax.swing.plaf.UIResource;
  33. import javax.swing.table.JTableHeader;
  34. import javax.swing.table.TableCellRenderer;
  35. import org.jdesktop.swingx.JXTable;
  36. import org.jdesktop.swingx.LabelProperties;
  37. import org.jdesktop.swingx.border.IconBorder;
  38. import org.jdesktop.swingx.decorator.Sorter;
  39. import org.jdesktop.swingx.icon.SortArrowIcon;
  40. import org.jdesktop.swingx.plaf.ColumnHeaderRendererAddon;
  41. import org.jdesktop.swingx.plaf.LookAndFeelAddons;
  42. /**
  43.  * Header renderer class which renders column sort feedback (arrows).
  44.  * 
  45.  * PENDING: #25, #169 - Header doesn't look right in winXP/mac
  46.  * 
  47.  * 
  48.  * @author Amy Fowler
  49.  * @author Ramesh Gupta
  50.  * @author Jeanette Winzenburg
  51.  */
  52. public class ColumnHeaderRenderer extends JComponent implements TableCellRenderer {
  53.     // the inheritance is only to make sure we are updated on LF change
  54.     public static final String UP_ICON_KEY = "ColumnHeaderRenderer.upIcon";
  55.     public static final String DOWN_ICON_KEY = "ColumnHeaderRenderer.downIcon";
  56.     static {
  57.         LookAndFeelAddons.contribute(new ColumnHeaderRendererAddon());
  58.     }
  59.     private static TableCellRenderer sharedInstance = null;
  60.     private static Icon defaultDownIcon = new SortArrowIcon(false);
  61.     private static Icon defaultUpIcon = new SortArrowIcon(true);
  62.     private Icon downIcon = defaultDownIcon;
  63.     private Icon upIcon = defaultUpIcon;
  64.     private IconBorder iconBorder = new IconBorder();
  65.     private boolean antiAliasedText = false;
  66.     private TableCellRenderer delegateRenderer;
  67.     private LabelProperties label;
  68.     public static TableCellRenderer getSharedInstance() {
  69.         if (sharedInstance == null) {
  70.             sharedInstance = new ColumnHeaderRenderer();
  71.         }
  72.         return sharedInstance;
  73.     }
  74.     public static ColumnHeaderRenderer createColumnHeaderRenderer() {
  75.         return new ColumnHeaderRenderer();
  76.     }
  77.     /*
  78.      * JW: a story ...
  79.      *
  80.      * latest: don't use a custom component and don't add the original
  81.      * and the arrow - use the original only and compound a border with 
  82.      * arrow icon. How does it look in XP/Mac?
  83.      * 
  84.      * 
  85.      * ----------------- below is the comment as of ColumnHeaderRenderer
  86.      * Original used a Label to show the typical text/icon part and another
  87.      * Label to show the up/down arrows, added both to this and configured both
  88.      * directly in getTableCellRendererComponent.
  89.      * 
  90.      * My first shot to solve the issues was to delegate the text/icon part to
  91.      * the defaultRenderer as returned by the JTableHeader: replace the first
  92.      * label with the rendererComponent of the renderer. In
  93.      * getTableCellRendererComponent let the renderer configure the comp and
  94.      * "move" the border from the delegateComp to this - so it's bordering both
  95.      * the comp and the arrow.
  96.      * 
  97.      * Besides not working (WinXP style headers are still not shown :-( it has
  98.      * issues with opaqueness: different combinations of this.opaque and
  99.      * delegate.opaque all have issues 
  100.      *  1. if the delegate is not explicitly set to false the border looks wrong 
  101.      *  2. if this is set to true we can have custom background 
  102.      *     per cell but no setting the header background has no
  103.      *     effect - and changing LF doesn't take up the LF default background ...
  104.      *  3. if this is set to false we can't have custom cell background
  105.      * 
  106.      * Any ideas?
  107.      * 
  108.      * 
  109.      */
  110.     private ColumnHeaderRenderer() {
  111.         label = new LabelProperties();
  112.         initDelegate();
  113.         
  114.     }
  115.     private void initDelegate() {
  116.         JTableHeader header = new JTableHeader();
  117.         delegateRenderer = header.getDefaultRenderer();
  118.     }
  119.     public Component getTableCellRendererComponent(JTable table, Object value,
  120.             boolean isSelected, boolean hasFocus, int rowIndex, int columnIndex) {
  121.         Component comp = configureDelegate(table, value, isSelected, hasFocus, rowIndex,
  122.                 columnIndex);
  123.         if ((table instanceof JXTable) && (comp instanceof JComponent)) {
  124.             // We no longer limit ourselves to a single "currently sorted
  125.             // column"
  126.             Sorter sorter = ((JXTable) table).getSorter(columnIndex);
  127.             Border border = UIManager.getBorder("TableHeader.cellBorder");
  128.             if (sorter != null) {
  129.                 iconBorder.setIcon(sorter.isAscending() ? upIcon : downIcon);
  130.                 Border origBorder = ((JComponent) comp).getBorder();
  131.                 border = new CompoundBorder(origBorder, iconBorder);
  132.                 ((JComponent) comp).setBorder(border);
  133.             }
  134.         }
  135.         adjustComponentOrientation(comp);
  136.         return comp;
  137.     }
  138.     /**
  139.      * adjusts the Component's orientation to JXTable's CO if appropriate.
  140.      * Here: always.
  141.      * 
  142.      * @param stamp
  143.      */
  144.     protected void adjustComponentOrientation(Component stamp) {
  145.         if (stamp.getComponentOrientation().equals(getComponentOrientation())) return;
  146.         stamp.applyComponentOrientation(getComponentOrientation());
  147.     }
  148.     private Component configureDelegate(JTable table, Object value,
  149.             boolean isSelected, boolean hasFocus, int rowIndex, int columnIndex) {
  150.         Component comp = delegateRenderer.getTableCellRendererComponent(table,
  151.                 value, isSelected, hasFocus, rowIndex, columnIndex);
  152.         applyLabelProperties(comp);
  153.         return comp;
  154.     }
  155.     private void applyLabelProperties(Component delegateRendererComponent) {
  156.         if (delegateRendererComponent instanceof JLabel) {
  157.             label.applyPropertiesTo((JLabel) delegateRendererComponent);
  158.         } else {
  159.             label.applyPropertiesTo(delegateRenderer);
  160.         }
  161.     }
  162.     public void setAntiAliasedText(boolean antiAlias) {
  163.         this.antiAliasedText = antiAlias;
  164.     }
  165.     public boolean getAntiAliasedText() {
  166.         return antiAliasedText;
  167.     }
  168.     public void setBackground(Color background) {
  169.         // this is called somewhere along initialization of super?
  170.         if (label != null) {
  171.             label.setBackground(background);
  172.         }
  173.     }
  174.     public void setForeground(Color foreground) {
  175.          if (label != null) {
  176.             label.setForeground(foreground);
  177.         }
  178.     }
  179.     public void setFont(Font font) {
  180.         if (label != null) {
  181.             label.setFont(font);
  182.         }
  183.     }
  184.     public void setDownIcon(Icon icon) {
  185.         this.downIcon = icon;
  186.     }
  187.     public Icon getDownIcon() {
  188.         return downIcon;
  189.     }
  190.     public void setUpIcon(Icon icon) {
  191.         this.upIcon = icon;
  192.     }
  193.     public Icon getUpIcon() {
  194.         return upIcon;
  195.     }
  196.     public void setHorizontalAlignment(int alignment) {
  197.         label.setHorizontalAlignment(alignment);
  198.     }
  199.     public int getHorizontalAlignment() {
  200.         return label.getHorizontalAlignment();
  201.     }
  202.     public void setHorizontalTextPosition(int textPosition) {
  203.         label.setHorizontalTextPosition(textPosition);
  204.     }
  205.     public int getHorizontalTextPosition() {
  206.         return label.getHorizontalTextPosition();
  207.     }
  208.     public void setIcon(Icon icon) {
  209.         label.setIcon(icon);
  210.     }
  211.     public Icon getIcon() {
  212.         return label.getIcon();
  213.     }
  214.     public void setIconTextGap(int iconTextGap) {
  215.         label.setIconTextGap(iconTextGap);
  216.     }
  217.     public int getIconTextGap() {
  218.         return label.getIconTextGap();
  219.     }
  220.     public void setVerticalAlignment(int alignment) {
  221.         label.setVerticalAlignment(alignment);
  222.     }
  223.     public int getVerticalAlignment() {
  224.         return label.getVerticalAlignment();
  225.     }
  226.     public void setVerticalTextPosition(int textPosition) {
  227.         label.setVerticalTextPosition(textPosition);
  228.     }
  229.     public int getVerticalTextPosition() {
  230.         return label.getVerticalTextPosition();
  231.     }
  232.     public void updateUI() {
  233.         super.updateUI();
  234.         initDelegate();
  235.         updateIconUI();
  236.     }
  237.     private void updateIconUI() {
  238.         if (getUpIcon() instanceof UIResource) {
  239.             Icon icon = UIManager.getIcon(UP_ICON_KEY);
  240.             setUpIcon(icon != null ? icon : defaultUpIcon);
  241.             
  242.         }
  243.         if (getDownIcon() instanceof UIResource) {
  244.             Icon icon = UIManager.getIcon(DOWN_ICON_KEY);
  245.             setDownIcon(icon != null ? icon : defaultDownIcon);
  246.             
  247.         }
  248.     }
  249. }