KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > plastic > PlasticToolBarUI


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.plastic;
32
33 import java.awt.Component JavaDoc;
34 import java.awt.Container JavaDoc;
35 import java.awt.Graphics JavaDoc;
36 import java.awt.Rectangle JavaDoc;
37 import java.beans.PropertyChangeEvent JavaDoc;
38 import java.beans.PropertyChangeListener JavaDoc;
39
40 import javax.swing.AbstractButton JavaDoc;
41 import javax.swing.JComponent JavaDoc;
42 import javax.swing.LookAndFeel JavaDoc;
43 import javax.swing.border.Border JavaDoc;
44 import javax.swing.plaf.ComponentUI JavaDoc;
45 import javax.swing.plaf.metal.MetalToolBarUI JavaDoc;
46
47 import com.jgoodies.looks.BorderStyle;
48 import com.jgoodies.looks.HeaderStyle;
49 import com.jgoodies.looks.Options;
50
51 /**
52  * The JGoodies Plastic look and feel implementation of <code>ToolBarUI</code>.<p>
53  *
54  * Corrects the rollover borders and can handle optional <code>Border</code> types,
55  * as specified by the <code>BorderStyle</code> or <code>HeaderStyle</code>
56  * client properties.
57  *
58  * @author Karsten Lentzsch
59  * @version $Revision: 1.2 $
60  */

61
62 public final class PlasticToolBarUI extends MetalToolBarUI JavaDoc {
63
64     private static final String JavaDoc PROPERTY_PREFIX = "ToolBar.";
65
66     private PropertyChangeListener JavaDoc listener;
67
68     public static ComponentUI JavaDoc createUI(JComponent JavaDoc b) {
69         return new PlasticToolBarUI();
70     }
71
72     // Rollover Borders *****************************************************
73

74     protected Border JavaDoc createRolloverBorder() {
75         return PlasticBorders.getRolloverButtonBorder();
76     }
77
78     protected void setBorderToRollover(Component JavaDoc c) {
79         if (c instanceof AbstractButton JavaDoc) {
80             super.setBorderToRollover(c);
81         } else if (c instanceof Container JavaDoc) {
82             Container JavaDoc cont = (Container JavaDoc) c;
83             for (int i = 0; i < cont.getComponentCount(); i++)
84                 super.setBorderToRollover(cont.getComponent(i));
85         }
86     }
87
88     // Handling Special Borders *********************************************
89

90     /**
91      * Installs a special border, if indicated by the <code>HeaderStyle</code>.
92      */

93     protected void installDefaults() {
94         super.installDefaults();
95         installSpecialBorder();
96     }
97
98     protected void installListeners() {
99         super.installListeners();
100         listener = createBorderStyleListener();
101         toolBar.addPropertyChangeListener(listener);
102     }
103
104     protected void uninstallListeners() {
105         toolBar.removePropertyChangeListener(listener);
106         super.uninstallListeners();
107     }
108
109     private PropertyChangeListener JavaDoc createBorderStyleListener() {
110         return new PropertyChangeListener JavaDoc() {
111
112             public void propertyChange(PropertyChangeEvent JavaDoc e) {
113                 String JavaDoc prop = e.getPropertyName();
114                 if (prop.equals(Options.HEADER_STYLE_KEY)
115                     || prop.equals(PlasticLookAndFeel.BORDER_STYLE_KEY)) {
116                     PlasticToolBarUI.this.installSpecialBorder();
117                 }
118             }
119
120         };
121     }
122
123     /**
124      * Installs a special border, if either a look-dependent
125      * <code>BorderStyle</code> or a look-independent
126      * <code>HeaderStyle</code> has been specified.
127      * A look specific <code>BorderStyle<code> shadows
128      * a <code>HeaderStyle</code>.<p>
129      *
130      * Specifying a <code>HeaderStyle</code> is recommend.
131      */

132     private void installSpecialBorder() {
133         String JavaDoc suffix;
134         BorderStyle borderStyle =
135             BorderStyle.from(toolBar, PlasticLookAndFeel.BORDER_STYLE_KEY);
136         if (borderStyle == BorderStyle.EMPTY)
137             suffix = "emptyBorder";
138         else if (borderStyle == BorderStyle.ETCHED)
139             suffix = "etchedBorder";
140         else if (borderStyle == BorderStyle.SEPARATOR)
141             suffix = "separatorBorder";
142         else {
143             HeaderStyle headerStyle = HeaderStyle.from(toolBar);
144             if (headerStyle == HeaderStyle.BOTH)
145                 suffix = "headerBorder";
146             else if (headerStyle == HeaderStyle.SINGLE && is3D())
147                 suffix = "etchedBorder";
148             else
149                 return;
150         }
151
152         LookAndFeel.installBorder(toolBar, PROPERTY_PREFIX + suffix);
153     }
154
155     // 3D Effect ************************************************************
156

157     public void update(Graphics JavaDoc g, JComponent JavaDoc c) {
158         if (c.isOpaque()) {
159             g.setColor(c.getBackground());
160             g.fillRect(0, 0, c.getWidth(), c.getHeight());
161         }
162         if (is3D()) {
163             Rectangle JavaDoc bounds =
164                 new Rectangle JavaDoc(0, 0, c.getWidth(), c.getHeight());
165             PlasticUtils.addLight3DEffekt(g, bounds, true);
166         }
167         paint(g, c);
168     }
169
170     /**
171      * Checks and answers if we should add a pseudo 3D effect.
172      */

173     private boolean is3D() {
174         if (PlasticUtils.force3D(toolBar))
175             return true;
176         if (PlasticUtils.forceFlat(toolBar))
177             return false;
178         return PlasticUtils.is3D(PROPERTY_PREFIX)
179             && (HeaderStyle.from(toolBar) != null)
180             && (BorderStyle.from(toolBar, PlasticLookAndFeel.BORDER_STYLE_KEY)
181                 != BorderStyle.EMPTY);
182     }
183
184 }
Popular Tags