KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > common > ShadowPopupBorder


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 com.jgoodies.looks.common;
32
33 import java.awt.Component JavaDoc;
34 import java.awt.Graphics JavaDoc;
35 import java.awt.Image JavaDoc;
36 import java.awt.Insets JavaDoc;
37
38 import javax.swing.ImageIcon JavaDoc;
39 import javax.swing.JComponent JavaDoc;
40 import javax.swing.border.AbstractBorder JavaDoc;
41
42 /**
43  * A border with a drop shadow intended to be used as the outer border
44  * of popups. Can paint the screen background if used with heavy-weight
45  * popup windows.
46  *
47  * @author Stefan Matthias Aust
48  * @author Karsten Lentzsch
49  * @author Andrej Golovnin
50  * @version $Revision: 1.5 $
51  *
52  * @see ShadowPopup
53  * @see ShadowPopupFactory
54  */

55 final class ShadowPopupBorder extends AbstractBorder JavaDoc {
56     
57     /**
58      * The drop shadow needs 5 pixels at the bottom and the right hand side.
59      */

60     private static final int SHADOW_SIZE = 5;
61     
62     /**
63      * The singleton instance used to draw all borders.
64      */

65     private static ShadowPopupBorder instance = new ShadowPopupBorder();
66
67     /**
68      * The drop shadow is created from a PNG image with 8 bit alpha channel.
69      */

70     private static Image JavaDoc shadow
71         = new ImageIcon JavaDoc(ShadowPopupBorder.class.getResource("shadow.png")).getImage();
72
73     
74     // Instance Creation *****************************************************
75

76     /**
77      * Returns the singleton instance used to draw all borders.
78      */

79     public static ShadowPopupBorder getInstance() {
80         return instance;
81     }
82     
83     
84     /**
85      * Paints the border for the specified component with the specified
86      * position and size.
87      */

88     public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
89         // fake drop shadow effect in case of heavy weight popups
90
JComponent JavaDoc popup = (JComponent JavaDoc) c;
91         Image JavaDoc hShadowBg = (Image JavaDoc) popup.getClientProperty(ShadowPopupFactory.PROP_HORIZONTAL_BACKGROUND);
92         if (hShadowBg != null) {
93             g.drawImage(hShadowBg, x, y + height - 5, c);
94         }
95         Image JavaDoc vShadowBg = (Image JavaDoc) popup.getClientProperty(ShadowPopupFactory.PROP_VERTICAL_BACKGROUND);
96         if (vShadowBg != null) {
97             g.drawImage(vShadowBg, x + width - 5, y, c);
98         }
99         
100         // draw drop shadow
101
g.drawImage(shadow, x + 5, y + height - 5, x + 10, y + height, 0, 6, 5, 11, null, c);
102         g.drawImage(shadow, x + 10, y + height - 5, x + width - 5, y + height, 5, 6, 6, 11, null, c);
103         g.drawImage(shadow, x + width - 5, y + 5, x + width, y + 10, 6, 0, 11, 5, null, c);
104         g.drawImage(shadow, x + width - 5, y + 10, x + width, y + height - 5, 6, 5, 11, 6, null, c);
105         g.drawImage(shadow, x + width - 5, y + height - 5, x + width, y + height, 6, 6, 11, 11, null, c);
106     }
107
108     
109     /**
110      * Returns the insets of the border.
111      */

112     public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
113         return new Insets JavaDoc(0, 0, SHADOW_SIZE, SHADOW_SIZE);
114     }
115     
116     
117     /**
118      * Reinitializes the insets parameter with this Border's current Insets.
119      * @param c the component for which this border insets value applies
120      * @param insets the object to be reinitialized
121      * @return the <code>insets</code> object
122      */

123     public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
124         insets.left = insets.top = 0;
125         insets.right = insets.bottom = SHADOW_SIZE;
126         return insets;
127     }
128     
129 }
130
Popular Tags