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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: LabelProperties.java,v 1.3 2005/10/10 18:01:58 rbair 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;
  22. import java.awt.Color;
  23. import java.awt.Font;
  24. import java.beans.PropertyChangeEvent;
  25. import java.beans.PropertyChangeListener;
  26. import javax.swing.AbstractButton;
  27. import javax.swing.Icon;
  28. import javax.swing.JLabel;
  29. import javax.swing.table.TableCellRenderer;
  30. /**
  31.  * Class used to store label properties in a single object so that they
  32.  * may be applied as a set on renderers.
  33.  * @author Amy Fowler
  34.  * @version 1.0
  35.  */
  36. public class LabelProperties extends JLabel {
  37.     private static final int BACKGROUND_SET = 1;
  38.     private static final int FOREGROUND_SET = 2;
  39.     private static final int FONT_SET = 4;
  40.     private static final int HORIZONTAL_ALIGNMENT_SET = 8;
  41.     private static final int HORIZONTAL_TEXT_POSITION_SET = 16;
  42.     private static final int ICON_SET = 32;
  43.     private static final int ICON_TEXT_GAP_SET = 64;
  44.     private static final int TEXT_SET = 128;
  45.     private static final int VERTICAL_ALIGNMENT_SET = 256;
  46.     private static final int VERTICAL_TEXT_POSITION_SET = 512;
  47.     private int setFlags = 0;
  48.     public LabelProperties() {
  49.         super();
  50.         addPropertyChangeListener(new PropertyChangeListener() {
  51.             public void propertyChange(PropertyChangeEvent e) {
  52.                 String propertyName = e.getPropertyName();
  53.                 Object value = e.getNewValue();
  54.                 if (propertyName.equals("background")) {
  55.                     if (value != null) {
  56.                         setFlags |= BACKGROUND_SET;
  57.                     } else {
  58.                         setFlags &= (~BACKGROUND_SET);
  59.                     }
  60.                 }
  61.                 else if (propertyName.equals("font")) {
  62.                     if (value != null) {
  63.                         setFlags |= FONT_SET;
  64.                     } else {
  65.                         setFlags &= (~FONT_SET);
  66.                     }
  67.                 }
  68.                 else if (propertyName.equals("foreground")) {
  69.                     if (value != null) {
  70.                         setFlags |= FOREGROUND_SET;
  71.                     } else {
  72.                         setFlags &= (~FOREGROUND_SET);
  73.                     }
  74.                 }
  75.                 else if (propertyName.equals("horizontalAlignment")) {
  76.                     if (value != null && ((Integer)value).intValue() != -1) {
  77.                         setFlags |= HORIZONTAL_ALIGNMENT_SET;
  78.                     } else {
  79.                         setFlags &= (~HORIZONTAL_ALIGNMENT_SET);
  80.                     }
  81.                 }
  82.                 else if (propertyName.equals("horizontalTextPosition")) {
  83.                     if (value != null && ((Integer)value).intValue() != -1) {
  84.                         setFlags |= HORIZONTAL_TEXT_POSITION_SET;
  85.                     } else {
  86.                         setFlags &= (~HORIZONTAL_TEXT_POSITION_SET);
  87.                     }
  88.                 }
  89.                 else if (propertyName.equals("icon")) {
  90.                     if (value != null) {
  91.                         setFlags |= ICON_SET;
  92.                     } else {
  93.                         setFlags &= (~ICON_SET);
  94.                     }
  95.                 }
  96.                 else if (propertyName.equals("iconTextGap")) {
  97.                     if (value != null && ((Integer)value).intValue() != -1) {
  98.                         setFlags |= ICON_TEXT_GAP_SET;
  99.                     } else {
  100.                         setFlags &= (~ICON_TEXT_GAP_SET);
  101.                     }
  102.                 }
  103.                 else if (propertyName.equals("text")) {
  104.                     if (value != null) {
  105.                         setFlags |= TEXT_SET;
  106.                     } else {
  107.                         setFlags &= (~TEXT_SET);
  108.                     }
  109.                 }
  110.                 else if (propertyName.equals("verticalAlignment")) {
  111.                     if (value != null && ((Integer)value).intValue() != -1) {
  112.                         setFlags |= VERTICAL_ALIGNMENT_SET;
  113.                     } else {
  114.                         setFlags &= (~VERTICAL_ALIGNMENT_SET);
  115.                     }
  116.                 }
  117.                 else if (propertyName.equals("verticalTextPosition")) {
  118.                     if (value != null && ((Integer)value).intValue() != -1) {
  119.                         setFlags |= VERTICAL_TEXT_POSITION_SET;
  120.                     } else {
  121.                         setFlags &= (~VERTICAL_TEXT_POSITION_SET);
  122.                     }
  123.                 }
  124.             }
  125.         });
  126.     }
  127.     public LabelProperties(Color background, Color foreground, Font font,
  128.                            int horizontalAlignment, int horizontalTextPosition,
  129.                            int verticalAlignment, int verticalTextPosition,
  130.                            Icon icon, int iconTextGap, String text) {
  131.         this();
  132.         setBackground(background);
  133.         setForeground(foreground);
  134.         setFont(font);
  135.         setHorizontalAlignment(horizontalAlignment);
  136.         setHorizontalTextPosition(horizontalTextPosition);
  137.         setVerticalAlignment(verticalAlignment);
  138.         setVerticalTextPosition(verticalTextPosition);
  139.         setIcon(icon);
  140.         setIconTextGap(iconTextGap);
  141.         setText(text);
  142.     }
  143.     public boolean isBackgroundSet() {
  144.         return (setFlags & BACKGROUND_SET) > 0;
  145.     }
  146.     public boolean isForegroundSet() {
  147.         return (setFlags & FOREGROUND_SET) > 0;
  148.     }
  149.     public boolean isFontSet() {
  150.         return (setFlags & FONT_SET) > 0;
  151.     }
  152.     public boolean isHorizontalAlignmentSet() {
  153.         return (setFlags & HORIZONTAL_ALIGNMENT_SET) > 0;
  154.     }
  155.     public boolean isHorizontalTextPositionSet() {
  156.         return (setFlags & HORIZONTAL_TEXT_POSITION_SET) > 0;
  157.     }
  158.     public boolean isIconSet() {
  159.         return (setFlags & ICON_SET) > 0;
  160.     }
  161.     public boolean isIconTextGapSet() {
  162.         return (setFlags & ICON_TEXT_GAP_SET) > 0;
  163.     }
  164.     public boolean isTextSet() {
  165.         return (setFlags & TEXT_SET) > 0;
  166.     }
  167.     public boolean isVerticalAlignmentSet() {
  168.         return (setFlags & VERTICAL_ALIGNMENT_SET) > 0;
  169.     }
  170.     public boolean isVerticalTextPositionSet() {
  171.         return (setFlags & VERTICAL_TEXT_POSITION_SET) > 0;
  172.     }
  173.     public boolean noPropertiesSet() {
  174.         return setFlags == 0;
  175.     }
  176.     public void applyPropertiesTo(JLabel label) {
  177.         if (noPropertiesSet()) {
  178.             return;
  179.         }
  180.         if (isBackgroundSet()) {
  181.             label.setBackground(getBackground());
  182.         }
  183.         if (isForegroundSet()) {
  184.             label.setForeground(getForeground());
  185.         }
  186.         if (isFontSet()) {
  187.             label.setFont(getFont());
  188.         }
  189.         if (isHorizontalAlignmentSet()) {
  190.             label.setHorizontalAlignment(getHorizontalAlignment());
  191.         }
  192.         if (isHorizontalTextPositionSet()) {
  193.             label.setHorizontalTextPosition(getHorizontalTextPosition());
  194.         }
  195.         if (isIconSet()) {
  196.             label.setIcon(getIcon());
  197.         }
  198.         if (isIconTextGapSet()) {
  199.             label.setIconTextGap(getIconTextGap());
  200.         }
  201.         if (isTextSet()) {
  202.             label.setText(getText());
  203.         }
  204.         if (isVerticalAlignmentSet()) {
  205.             label.setVerticalAlignment(getVerticalAlignment());
  206.         }
  207.         if (isVerticalTextPositionSet()) {
  208.             label.setVerticalTextPosition(getVerticalTextPosition());
  209.         }
  210.     }
  211.     public void applyPropertiesTo(AbstractButton button) {
  212.          if (noPropertiesSet()) {
  213.              return;
  214.          }
  215.          if (isBackgroundSet()) {
  216.              button.setBackground(getBackground());
  217.          }
  218.          if (isForegroundSet()) {
  219.              button.setForeground(getForeground());
  220.          }
  221.          if (isFontSet()) {
  222.              button.setFont(getFont());
  223.          }
  224.          if (isHorizontalAlignmentSet()) {
  225.              button.setHorizontalAlignment(getHorizontalAlignment());
  226.          }
  227.          if (isHorizontalTextPositionSet()) {
  228.              button.setHorizontalTextPosition(getHorizontalTextPosition());
  229.          }
  230.          if (isIconSet()) {
  231.              button.setIcon(getIcon());
  232.          }
  233.          if (isIconTextGapSet()) {
  234.              button.setIconTextGap(getIconTextGap());
  235.          }
  236.          if (isTextSet()) {
  237.              button.setText(getText());
  238.          }
  239.          if (isVerticalAlignmentSet()) {
  240.              button.setVerticalAlignment(getVerticalAlignment());
  241.          }
  242.          if (isVerticalTextPositionSet()) {
  243.              button.setVerticalTextPosition(getVerticalTextPosition());
  244.          }
  245.      }
  246.      public void applyPropertiesTo(LabelProperties props) {
  247.          if (noPropertiesSet()) {
  248.              return;
  249.          }
  250.          if (isBackgroundSet()) {
  251.              props.setBackground(getBackground());
  252.          }
  253.          if (isForegroundSet()) {
  254.              props.setForeground(getForeground());
  255.          }
  256.          if (isFontSet()) {
  257.              props.setFont(getFont());
  258.          }
  259.          if (isHorizontalAlignmentSet()) {
  260.              props.setHorizontalAlignment(getHorizontalAlignment());
  261.          }
  262.          if (isHorizontalTextPositionSet()) {
  263.              props.setHorizontalTextPosition(getHorizontalTextPosition());
  264.          }
  265.          if (isIconSet()) {
  266.              props.setIcon(getIcon());
  267.          }
  268.          if (isIconTextGapSet()) {
  269.              props.setIconTextGap(getIconTextGap());
  270.          }
  271.          if (isTextSet()) {
  272.              props.setText(getText());
  273.          }
  274.          if (isVerticalAlignmentSet()) {
  275.              props.setVerticalAlignment(getVerticalAlignment());
  276.          }
  277.          if (isVerticalTextPositionSet()) {
  278.              props.setVerticalTextPosition(getVerticalTextPosition());
  279.          }
  280.      }
  281.      public void applyPropertiesTo(TableCellRenderer renderer) {
  282.          if (renderer instanceof JLabel) {
  283.              applyPropertiesTo( (JLabel) renderer);
  284.          }
  285.          else if (renderer instanceof AbstractButton) {
  286.              applyPropertiesTo( (AbstractButton) renderer);
  287.          }
  288.      }
  289. }