| 1 |
/* |
| 2 |
* Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. All Rights Reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are met: |
| 6 |
* |
| 7 |
* o Redistributions of source code must retain the above copyright notice, |
| 8 |
* this list of conditions and the following disclaimer. |
| 9 |
* |
| 10 |
* o Redistributions in binary form must reproduce the above copyright notice, |
| 11 |
* this list of conditions and the following disclaimer in the documentation |
| 12 |
* and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* o Neither the name of JGoodies Karsten Lentzsch nor the names of |
| 15 |
* its contributors may be used to endorse or promote products derived |
| 16 |
* from this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 20 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 21 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 22 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 23 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 25 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 27 |
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 28 |
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
|
| 31 |
package org.pushingpixels.lafwidget.utils; |
| 32 |
|
| 33 |
import java.awt.*; |
| 34 |
|
| 35 |
import javax.swing.ImageIcon; |
| 36 |
import javax.swing.border.AbstractBorder; |
| 37 |
|
| 38 |
/** |
| 39 |
* A border with a drop shadow intended to be used as the outer border of |
| 40 |
* popups. Can paint the screen background if used with heavy-weight popup |
| 41 |
* windows. |
| 42 |
* |
| 43 |
* @author Stefan Matthias Aust |
| 44 |
* @author Karsten Lentzsch |
| 45 |
* @author Andrej Golovnin |
| 46 |
* |
| 47 |
* see ShadowPopup |
| 48 |
* see ShadowPopupFactory |
| 49 |
*/ |
| 50 |
public final class ShadowPopupBorder extends AbstractBorder { |
| 51 |
|
| 52 |
/** |
| 53 |
* The drop shadow needs 5 pixels at the bottom and the right hand side. |
| 54 |
*/ |
| 55 |
private static final int SHADOW_SIZE = 5; |
| 56 |
|
| 57 |
/** |
| 58 |
* The singleton instance used to draw all borders. |
| 59 |
*/ |
| 60 |
private static ShadowPopupBorder instance = new ShadowPopupBorder(); |
| 61 |
|
| 62 |
/** |
| 63 |
* The drop shadow is created from a PNG image with 8 bit alpha channel. |
| 64 |
*/ |
| 65 |
private static Image shadow = new ImageIcon(ShadowPopupBorder.class |
| 66 |
.getResource("shadow.png")).getImage(); |
| 67 |
|
| 68 |
// Instance Creation ***************************************************** |
| 69 |
|
| 70 |
/** |
| 71 |
* Returns the singleton instance used to draw all borders. |
| 72 |
*/ |
| 73 |
public static ShadowPopupBorder getInstance() { |
| 74 |
return instance; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Paints the border for the specified component with the specified position |
| 79 |
* and size. |
| 80 |
*/ |
| 81 |
@Override |
| 82 |
public void paintBorder(Component c, Graphics g, int x, int y, int width, |
| 83 |
int height) { |
| 84 |
// fake drop shadow effect in case of heavy weight popups |
| 85 |
// JComponent popup = (JComponent) c; |
| 86 |
// Image hShadowBg = (Image) |
| 87 |
// popup.getClientProperty(ShadowPopupFactory.PROP_HORIZONTAL_BACKGROUND); |
| 88 |
// if (hShadowBg != null) { |
| 89 |
// g.drawImage(hShadowBg, x, y + height - 5, c); |
| 90 |
// } |
| 91 |
// Image vShadowBg = (Image) |
| 92 |
// popup.getClientProperty(ShadowPopupFactory.PROP_VERTICAL_BACKGROUND); |
| 93 |
// if (vShadowBg != null) { |
| 94 |
// g.drawImage(vShadowBg, x + width - 5, y, c); |
| 95 |
// } |
| 96 |
|
| 97 |
// draw drop shadow |
| 98 |
g.drawImage(shadow, x + 5, y + height - 5, x + 10, y + height, 0, 6, 5, |
| 99 |
11, null, c); |
| 100 |
g.drawImage(shadow, x + 10, y + height - 5, x + width - 5, y + height, |
| 101 |
5, 6, 6, 11, null, c); |
| 102 |
g.drawImage(shadow, x + width - 5, y + 5, x + width, y + 10, 6, 0, 11, |
| 103 |
5, null, c); |
| 104 |
g.drawImage(shadow, x + width - 5, y + 10, x + width, y + height - 5, |
| 105 |
6, 5, 11, 6, null, c); |
| 106 |
g.drawImage(shadow, x + width - 5, y + height - 5, x + width, y |
| 107 |
+ height, 6, 6, 11, 11, null, c); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Returns the insets of the border. |
| 112 |
*/ |
| 113 |
@Override |
| 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 |
* |
| 121 |
* @param c |
| 122 |
* the component for which this border insets value applies |
| 123 |
* @param insets |
| 124 |
* the object to be reinitialized |
| 125 |
* @return the <code>insets</code> object |
| 126 |
*/ |
| 127 |
@Override |
| 128 |
public Insets getBorderInsets(Component c, Insets insets) { |
| 129 |
insets.left = insets.top = 0; |
| 130 |
insets.right = insets.bottom = SHADOW_SIZE; |
| 131 |
return insets; |
| 132 |
} |
| 133 |
|
| 134 |
} |