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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: MatteBorderExt.java,v 1.4 2005/10/10 18:02:55 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.border;
  22. import java.awt.Color;
  23. import java.awt.Component;
  24. import java.awt.Graphics;
  25. import java.awt.Insets;
  26. import javax.swing.Icon;
  27. import javax.swing.border.MatteBorder;
  28. /**
  29.  * Matte border that allows specialized icons for corners and sides.
  30.  *
  31.  * @author Ramesh Gupta
  32.  */
  33. public class MatteBorderExt extends MatteBorder {
  34.     protected Icon[] tileIcons = null;
  35.     private Icon defaultIcon = null;
  36.     /**
  37.      * Draws a matte border using specialized icons for corners and sides. If
  38.          * tileIcons is null, or if the length of tileIcons array is less than 2, this
  39.          * defaults to the {@link javax.swing.border.MatteBorder superclass} behavior.
  40.          * Otherwise, tileIcons must specify icons in clockwise order, starting with
  41.          * the top-left icon at index zero, culminating with the left icon at index 7.
  42.      * If the length of the tileIcons array is greater than 1, but less than 8,
  43.      * then tileIcons[0] is used to paint the corners, and tileIcons[1] is used
  44.          * to paint the sides, with icons rotated as necessary. Other icons, if any,
  45.      * are ignored.
  46.      *
  47.      * @param top top inset
  48.      * @param left left inset
  49.      * @param bottom bottom inset
  50.      * @param right right inset
  51.      * @param tileIcons array of icons starting with top-left in index 0,
  52.      * continuing clockwise through the rest of the indices
  53.      */
  54.     public MatteBorderExt(int top, int left, int bottom, int right,
  55.                           Icon[] tileIcons) {
  56.         super(top, left, bottom, right,
  57.               (tileIcons == null) || (tileIcons.length == 0) ? null :
  58.               tileIcons[0]);
  59.         this.tileIcons = tileIcons;
  60.     }
  61.     /**
  62.      * @see MatteBorder#MatteBorder(int, int, int, int, java.awt.Color)
  63.      */
  64.     public MatteBorderExt(int top, int left, int bottom, int right,
  65.                           Color matteColor) {
  66.         super(top, left, bottom, right, matteColor);
  67.     }
  68.     /**
  69.      * @see MatteBorder#MatteBorder(java.awt.Insets, java.awt.Color)
  70.      */
  71.     public MatteBorderExt(Insets borderInsets, Color matteColor) {
  72.         super(borderInsets, matteColor);
  73.     }
  74.     /**
  75.      * @see MatteBorder#MatteBorder(int, int, int, int, javax.swing.Icon)
  76.      */
  77.     public MatteBorderExt(int top, int left, int bottom, int right,
  78.                           Icon tileIcon) {
  79.         super(top, left, bottom, right, tileIcon);
  80.     }
  81.     /**
  82.      * @see MatteBorder#MatteBorder(java.awt.Insets, javax.swing.Icon)
  83.      */
  84.     public MatteBorderExt(Insets borderInsets, Icon tileIcon) {
  85.         super(borderInsets, tileIcon);
  86.     }
  87.     /**
  88.      * @see MatteBorder#MatteBorder(javax.swing.Icon)
  89.      */
  90.     public MatteBorderExt(Icon tileIcon) {
  91.         super(tileIcon);
  92.     }
  93.     /**
  94.      * Returns the icons used by this border
  95.      *
  96.      * @return the icons used by this border
  97.      */
  98.     public Icon[] getTileIcons() {
  99.         return tileIcons;
  100.     }
  101.     /**
  102.      * {@inheritDoc}
  103.      */
  104.     public void paintBorder(Component c, Graphics g, int x, int y,
  105.                             int width, int height) {
  106.         if ( (tileIcons == null) || (tileIcons.length < 2)) {
  107.             super.paintBorder(c, g, x, y, width, height);
  108.             return;
  109.         }
  110.         Insets insets = getBorderInsets(c);
  111.         int clipWidth, clipHeight;
  112.         clipWidth = Math.min(width, insets.left); // clip to component width or insets
  113.         clipHeight = Math.min(height, insets.top); // clip to component height or insets
  114.         if ( (clipWidth <= 0) || (clipHeight <= 0)) {
  115.             return; // nothing to paint
  116.         }
  117.         // We now know that we have at least two icons!
  118.         Color oldColor = g.getColor(); // restore before exiting
  119.         g.translate(x, y); // restore before exiting
  120.         for (int i = 0; i < tileIcons.length; i++) {
  121.             // Make sure we have an icon to paint with
  122.             if (tileIcons[i] == null) {
  123.                 tileIcons[i] = getDefaultIcon();
  124.             }
  125.         }
  126.         paintTopLeft(c, g, 0, 0, insets.left, insets.top);
  127.         paintTop(c, g, insets.left, 0, width - insets.left - insets.right, insets.top);
  128.         paintTopRight(c, g, width - insets.right, 0, insets.right, insets.top);
  129.         paintRight(c, g, width - insets.right, insets.top, insets.right, height - insets.top - insets.bottom);
  130.         paintBottomRight(c, g, width - insets.right, height - insets.bottom, insets.right, insets.bottom);
  131.         paintBottom(c, g, insets.left, height - insets.bottom, width - insets.left - insets.right, insets.bottom);
  132.         paintBottomLeft(c, g, 0, height - insets.bottom, insets.left, insets.bottom);
  133.         paintLeft(c, g, 0, insets.top, insets.left, height - insets.top - insets.bottom);
  134.         g.translate( -x, -y); // restore
  135.         g.setColor(oldColor); // restore
  136.     }
  137.     protected void paint(Icon icon, Component c, Graphics g, int x, int y,
  138.                              int width, int height) {
  139.         Graphics cg = g.create();
  140.         cg.setClip(x, y, width, height);
  141.         int tileW = icon.getIconWidth();
  142.         int tileH = icon.getIconHeight();
  143.         int xpos, ypos, startx, starty;
  144.         for (ypos = 0; height - ypos > 0; ypos += tileH) {
  145.             for (xpos = 0; width - xpos > 0; xpos += tileW) {
  146.                 icon.paintIcon(c, cg, x+xpos, y+ypos);
  147.             }
  148.         }
  149.         cg.dispose();
  150.     }
  151.     /**
  152.      * Only called by paintBorder()
  153.      */
  154.     protected void paintTopLeft(Component c, Graphics g, int x, int y, int width, int height) {
  155.         Graphics cg = g.create();
  156.         cg.setClip(x, y, width, height);
  157.         tileIcons[0].paintIcon(c, cg, x, y);
  158.         cg.dispose();
  159.     }
  160.     /**
  161.      * Only called by paintBorder()
  162.      */
  163.     protected void paintTop(Component c, Graphics g, int x, int y, int width, int height) {
  164.         paint(tileIcons[1], c, g, x, y, width, height);
  165.     }
  166.     /**
  167.      * Only called by paintBorder()
  168.      */
  169.     protected void paintTopRight(Component c, Graphics g, int x, int y, int width, int height) {
  170.         if (tileIcons.length == 8) {
  171.             paint(tileIcons[2], c, g, x, y, width, height);
  172.         }
  173.         else {
  174.             Icon icon = tileIcons[0];
  175.             /** @todo Rotate -90 and paint icon */
  176.         }
  177.     }
  178.     /**
  179.      * Only called by paintBorder()
  180.      */
  181.     protected void paintRight(Component c, Graphics g, int x, int y, int width, int height) {
  182.         if (tileIcons.length == 8) {
  183.             paint(tileIcons[3], c, g, x, y, width, height);
  184.         }
  185.         else {
  186.             Icon icon = tileIcons[1];
  187.             /** @todo Rotate -90 and paint icon */
  188.         }
  189.     }
  190.     /**
  191.      * Only called by paintBorder()
  192.      */
  193.     protected void paintBottomRight(Component c, Graphics g, int x, int y, int width, int height) {
  194.         if (tileIcons.length == 8) {
  195.             paint(tileIcons[4], c, g, x, y, width, height);
  196.         }
  197.         else {
  198.             Icon icon = tileIcons[0];
  199.             /** @todo Rotate -180 and paint icon */
  200.         }
  201.     }
  202.     /**
  203.      * Only called by paintBorder()
  204.      */
  205.     protected void paintBottom(Component c, Graphics g, int x, int y, int width, int height) {
  206.         if (tileIcons.length == 8) {
  207.             paint(tileIcons[5], c, g, x, y, width, height);
  208.         }
  209.         else {
  210.             Icon icon = tileIcons[1];
  211.             /** @todo Rotate -180 and paint icon */
  212.         }
  213.     }
  214.     /**
  215.      * Only called by paintBorder()
  216.      */
  217.     protected void paintBottomLeft(Component c, Graphics g, int x, int y, int width, int height) {
  218.         if (tileIcons.length == 8) {
  219.             paint(tileIcons[6], c, g, x, y, width, height);
  220.         }
  221.         else {
  222.             Icon icon = tileIcons[0];
  223.             /** @todo Rotate -270 and paint icon */
  224.         }
  225.     }
  226.     /**
  227.      * Only called by paintBorder()
  228.      */
  229.     protected void paintLeft(Component c, Graphics g, int x, int y, int width, int height) {
  230.         if (tileIcons.length == 8) {
  231.             paint(tileIcons[7], c, g, x, y, width, height);
  232.         }
  233.         else {
  234.             Icon icon = tileIcons[1];
  235.             /** @todo Rotate -270 and paint icon */
  236.         }
  237.     }
  238.     /**
  239.      * Only called by paintBorder()
  240.      */
  241.     protected Icon getDefaultIcon() {
  242.         if (defaultIcon == null) {
  243.             defaultIcon = new Icon() {
  244.                 private int width = 3;
  245.                 private int height = 3;
  246.                 public int getIconWidth() {
  247.                     return width;
  248.                 }
  249.                 public int getIconHeight() {
  250.                     return height;
  251.                 }
  252.                 public void paintIcon(Component c, Graphics g, int x, int y) {
  253.                     g.setColor(c.getBackground().darker().darker());
  254.                     //g.translate(x, y);
  255.                     g.fillRect(x, y, width, height);
  256.                 }
  257.             };
  258.         }
  259.         return defaultIcon;
  260.     }
  261. }