KickJava   Java API By Example, From Geeks To Geeks.

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


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.Graphics JavaDoc;
34 import java.awt.Rectangle JavaDoc;
35 import java.beans.PropertyChangeEvent JavaDoc;
36 import java.beans.PropertyChangeListener JavaDoc;
37
38 import javax.swing.JComponent JavaDoc;
39 import javax.swing.LookAndFeel JavaDoc;
40 import javax.swing.plaf.ComponentUI JavaDoc;
41 import javax.swing.plaf.basic.BasicMenuBarUI JavaDoc;
42
43 import com.jgoodies.looks.BorderStyle;
44 import com.jgoodies.looks.HeaderStyle;
45 import com.jgoodies.looks.Options;
46
47
48 /**
49  * The JGoodies Plastic look and feel implemenation of <code>MenuBarUI</code>.
50  * Can handle optional <code>Border</code> types as specified by the
51  * <code>BorderStyle</code> or <code>HeaderStyle</code> client properties.
52  *
53  * @author Karsten Lentzsch
54  * @version $Revision: 1.2 $
55  */

56 public final class PlasticMenuBarUI extends BasicMenuBarUI JavaDoc {
57     
58     
59     private PropertyChangeListener JavaDoc listener;
60     
61     
62     public static ComponentUI JavaDoc createUI(JComponent JavaDoc b) {
63         return new PlasticMenuBarUI();
64     }
65     
66     
67     // Handling Special Borders *********************************************
68

69     protected void installDefaults() {
70         super.installDefaults();
71         installSpecialBorder();
72     }
73     
74     
75     protected void installListeners() {
76         super.installListeners();
77         listener = createBorderStyleListener();
78         menuBar.addPropertyChangeListener(listener);
79     }
80     
81     
82     protected void uninstallListeners() {
83         menuBar.removePropertyChangeListener(listener);
84         super.uninstallListeners();
85     }
86     
87     
88     private PropertyChangeListener JavaDoc createBorderStyleListener() {
89         return new PropertyChangeListener JavaDoc() {
90             
91             public void propertyChange(PropertyChangeEvent JavaDoc e) {
92                 String JavaDoc prop = e.getPropertyName();
93                 if(prop.equals(Options.HEADER_STYLE_KEY) ||
94                    prop.equals(PlasticLookAndFeel.BORDER_STYLE_KEY)) {
95                    PlasticMenuBarUI.this.installSpecialBorder();
96                 }
97             }
98             
99         };
100     }
101     
102     
103     /**
104      * Installs a special border, if either a look-dependent <code>BorderStyle</code>
105      * or a look-independent <code>HeaderStyle</code> has been specified.
106      * A look specific <code>BorderStyle<code> shadows a <code>HeaderStyle</code>.<p>
107      *
108      * We recommend to specify a <code>HeaderStyle</code>.
109      */

110     public void installSpecialBorder() {
111         String JavaDoc suffix;
112         BorderStyle borderStyle = BorderStyle.from(menuBar,
113                                                 PlasticLookAndFeel.BORDER_STYLE_KEY);
114         if (borderStyle == BorderStyle.EMPTY)
115             suffix = "emptyBorder";
116         else if (borderStyle == BorderStyle.ETCHED)
117             suffix = "etchedBorder";
118         else if (borderStyle == BorderStyle.SEPARATOR)
119             suffix = "separatorBorder";
120         else {
121             HeaderStyle headerStyle = HeaderStyle.from(menuBar);
122             if (headerStyle == HeaderStyle.BOTH)
123                 suffix = "headerBorder";
124             else if (headerStyle == HeaderStyle.SINGLE && is3D())
125                 suffix = "etchedBorder";
126             else
127                 return;
128         }
129
130         LookAndFeel.installBorder(menuBar, "MenuBar." + suffix);
131     }
132     
133
134     // 3D Effect ************************************************************************
135

136     public void update(Graphics JavaDoc g, JComponent JavaDoc c) {
137         if (c.isOpaque()) {
138             g.setColor(c.getBackground());
139             g.fillRect(0, 0, c.getWidth(), c.getHeight());
140         }
141         if (is3D())
142             PlasticUtils.addLight3DEffekt(g, new Rectangle JavaDoc(0, 0, c.getWidth(), c.getHeight()), true);
143
144         paint(g, c);
145     }
146     
147     
148     /**
149      * Checks and answers if we should add a pseudo 3D effect.
150      */

151     private boolean is3D() {
152         if (PlasticUtils.force3D(menuBar))
153             return true;
154         if (PlasticUtils.forceFlat(menuBar))
155             return false;
156         return PlasticUtils.is3D("MenuBar.") &&
157                 (HeaderStyle.from(menuBar) != null) &&
158                 (BorderStyle.from(menuBar, PlasticLookAndFeel.BORDER_STYLE_KEY)
159                     != BorderStyle.EMPTY);
160     }
161     
162 }
Popular Tags