KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 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
35 import javax.swing.LookAndFeel JavaDoc;
36 import javax.swing.Popup JavaDoc;
37 import javax.swing.PopupFactory JavaDoc;
38
39 import com.jgoodies.looks.Options;
40
41
42 /**
43  * The JGoodies Looks implementation of <code>PopupFactory</code>.
44  * Adds a drop shadow border to all popups except ComboBox popups.
45  * It is installed by the JGoodies Plastic L&amp;F, as well as by
46  * the JGoodies Windows L&amp;F during the Look&amp;Feel initialization,
47  * see {@link com.jgoodies.looks.plastic.PlasticLookAndFeel#initialize} and
48  * {@link com.jgoodies.looks.windows.WindowsLookAndFeel#initialize}.<p>
49  *
50  * This factory shall not be used on platforms that provide native drop shadows,
51  * such as the Mac OS X. Therefore the invocation of the {@link #install()}
52  * method will have no effect on such platforms.<p>
53  *
54  * <strong>Note:</strong> To be used in a sandbox environment, this PopupFactory
55  * requires two AWT permissions: <code>createRobot</code> and
56  * <code>readDisplayPixels</code>. The reason for it is, that in the case of
57  * the heavy weight popups this PopupFactory uses a Robot to snapshot
58  * the screen background to simulate the drop shadow effect.
59  *
60  * @author Andrej Golovnin
61  * @author Karsten Lentzsch
62  * @version $Revision: 1.1 $
63  *
64  * @see java.awt.AWTPermission
65  * @see java.awt.Robot
66  * @see javax.swing.Popup
67  * @see LookAndFeel#initialize
68  * @see LookAndFeel#uninitialize
69  */

70 public final class ShadowPopupFactory extends PopupFactory JavaDoc {
71
72     /**
73      * In the case of heavy weight popups, snapshots of the screen background
74      * will be stored as client properties of the popup contents' parent.
75      * These snapshots will be used by the popup border to simulate the drop
76      * shadow effect. The two following constants define the names of
77      * these client properties.
78      *
79      * @see com.jgoodies.looks.common.ShadowPopupBorder
80      */

81     static final String JavaDoc PROP_HORIZONTAL_BACKGROUND = "jgoodies.hShadowBg";
82     static final String JavaDoc PROP_VERTICAL_BACKGROUND = "jgoodies.vShadowBg";
83
84     /**
85      * The PopupFactory used before this PopupFactory has been installed
86      * in <code>#install</code>. Used to restored the original state
87      * in <code>#uninstall</code>.
88      */

89     private final PopupFactory JavaDoc storedFactory;
90
91     
92     // Instance Creation ******************************************************
93

94     private ShadowPopupFactory(PopupFactory JavaDoc storedFactory) {
95         this.storedFactory = storedFactory;
96     }
97     
98
99     // API ********************************************************************
100

101     /**
102      * Installs the ShadowPopupFactory as the shared popup factory.
103      * Also stores the previously set factory in the replacement,
104      * so that it can be restored in <code>#uninstall</code>.
105      *
106      * @see #uninstall
107      */

108     public static void install() {
109         PopupFactory JavaDoc factory = PopupFactory.getSharedInstance();
110         if (factory instanceof ShadowPopupFactory)
111             return;
112         
113         PopupFactory.setSharedInstance(new ShadowPopupFactory(factory));
114     }
115
116     /**
117      * Uninstalls the ShadowPopupFactory and restores the original
118      * popup factory as the new shared popup factory.
119      *
120      * @see #install
121      */

122     public static void uninstall() {
123         PopupFactory JavaDoc factory = PopupFactory.getSharedInstance();
124         if (!(factory instanceof ShadowPopupFactory))
125             return;
126         
127         PopupFactory JavaDoc stored = ((ShadowPopupFactory) factory).storedFactory;
128         PopupFactory.setSharedInstance(stored);
129     }
130     
131
132     /** {@inheritDoc} */
133     public Popup JavaDoc getPopup(Component JavaDoc owner, Component JavaDoc contents, int x, int y)
134             throws IllegalArgumentException JavaDoc {
135         Popup JavaDoc popup = storedFactory.getPopup(owner, contents, x, y);
136         return Options.isPopupDropShadowActive()
137             ? ShadowPopup.getInstance(owner, contents, x, y, popup)
138             : popup;
139     }
140
141 }
142
Popular Tags