001: /*
002: * Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package contrib.com.jgoodies.looks.common;
032:
033: import java.awt.*;
034:
035: import javax.swing.ImageIcon;
036: import javax.swing.JComponent;
037: import javax.swing.border.AbstractBorder;
038:
039: /**
040: * A border with a drop shadow intended to be used as the outer border
041: * of popups. Can paint the screen background if used with heavy-weight
042: * popup windows.
043: *
044: * @author Stefan Matthias Aust
045: * @author Karsten Lentzsch
046: * @author Andrej Golovnin
047: * @version $Revision: 1.7 $
048: *
049: * @see ShadowPopup
050: * @see ShadowPopupFactory
051: */
052: final class ShadowPopupBorder extends AbstractBorder {
053:
054: /**
055: * The drop shadow needs 5 pixels at the bottom and the right hand side.
056: */
057: private static final int SHADOW_SIZE = 5;
058:
059: /**
060: * The singleton instance used to draw all borders.
061: */
062: private static ShadowPopupBorder instance = new ShadowPopupBorder();
063:
064: /**
065: * The drop shadow is created from a PNG image with 8 bit alpha channel.
066: */
067: private static Image shadow = new ImageIcon(ShadowPopupBorder.class
068: .getResource("shadow.png")).getImage();
069:
070: // Instance Creation *****************************************************
071:
072: /**
073: * Returns the singleton instance used to draw all borders.
074: */
075: public static ShadowPopupBorder getInstance() {
076: return instance;
077: }
078:
079: /**
080: * Paints the border for the specified component with the specified
081: * position and size.
082: */
083: public void paintBorder(Component c, Graphics g, int x, int y,
084: int width, int height) {
085: // fake drop shadow effect in case of heavy weight popups
086: JComponent popup = (JComponent) c;
087: Image hShadowBg = (Image) popup
088: .getClientProperty(ShadowPopupFactory.PROP_HORIZONTAL_BACKGROUND);
089: if (hShadowBg != null) {
090: g.drawImage(hShadowBg, x, y + height - 5, c);
091: }
092: Image vShadowBg = (Image) popup
093: .getClientProperty(ShadowPopupFactory.PROP_VERTICAL_BACKGROUND);
094: if (vShadowBg != null) {
095: g.drawImage(vShadowBg, x + width - 5, y, c);
096: }
097:
098: // draw drop shadow
099: g.drawImage(shadow, x + 5, y + height - 5, x + 10, y + height,
100: 0, 6, 5, 11, null, c);
101: g.drawImage(shadow, x + 10, y + height - 5, x + width - 5, y
102: + height, 5, 6, 6, 11, null, c);
103: g.drawImage(shadow, x + width - 5, y + 5, x + width, y + 10, 6,
104: 0, 11, 5, null, c);
105: g.drawImage(shadow, x + width - 5, y + 10, x + width, y
106: + height - 5, 6, 5, 11, 6, null, c);
107: g.drawImage(shadow, x + width - 5, y + height - 5, x + width, y
108: + height, 6, 6, 11, 11, null, c);
109: }
110:
111: /**
112: * Returns the insets of the border.
113: */
114: public Insets getBorderInsets(Component c) {
115: return new Insets(0, 0, SHADOW_SIZE, SHADOW_SIZE);
116: }
117:
118: /**
119: * Reinitializes the insets parameter with this Border's current Insets.
120: * @param c the component for which this border insets value applies
121: * @param insets the object to be reinitialized
122: * @return the <code>insets</code> object
123: */
124: public Insets getBorderInsets(Component c, Insets insets) {
125: insets.left = insets.top = 0;
126: insets.right = insets.bottom = SHADOW_SIZE;
127: return insets;
128: }
129:
130: }
|