1 /*
2  * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package javax.swing.plaf.synth;
27 
28 import javax.swing.*;
29 import javax.swing.plaf.*;
30 import javax.swing.plaf.basic.BasicPanelUI;
31 import java.awt.*;
32 import java.beans.*;
33 
34 /**
35  * Provides the Synth L&F UI delegate for
36  * {@link javax.swing.JPanel}.
37  *
38  * @author Steve Wilson
39  * @since 1.7
40  */
41 public class SynthPanelUI extends BasicPanelUI
42                           implements PropertyChangeListener, SynthUI {
43     private SynthStyle style;
44 
45     /**
46      * Creates a new UI object for the given component.
47      *
48      * @param c component to create UI object for
49      * @return the UI object
50      */
createUI(JComponent c)51     public static ComponentUI createUI(JComponent c) {
52         return new SynthPanelUI();
53     }
54 
55     /**
56      * {@inheritDoc}
57      */
58     @Override
installUI(JComponent c)59     public void installUI(JComponent c) {
60         JPanel p = (JPanel)c;
61 
62         super.installUI(c);
63         installListeners(p);
64     }
65 
66     /**
67      * {@inheritDoc}
68      */
69     @Override
uninstallUI(JComponent c)70     public void uninstallUI(JComponent c) {
71         JPanel p = (JPanel)c;
72 
73         uninstallListeners(p);
74         super.uninstallUI(c);
75     }
76 
77     /**
78      * Installs listeners into the panel.
79      *
80      * @param p the {@code JPanel} object
81      */
installListeners(JPanel p)82     protected void installListeners(JPanel p) {
83         p.addPropertyChangeListener(this);
84     }
85 
86     /**
87      * Uninstalls listeners from the panel.
88      *
89      * @param p the {@code JPanel} object
90      */
uninstallListeners(JPanel p)91     protected void uninstallListeners(JPanel p) {
92         p.removePropertyChangeListener(this);
93     }
94 
95     /**
96      * {@inheritDoc}
97      */
98     @Override
installDefaults(JPanel p)99     protected void installDefaults(JPanel p) {
100         updateStyle(p);
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
uninstallDefaults(JPanel p)107     protected void uninstallDefaults(JPanel p) {
108         SynthContext context = getContext(p, ENABLED);
109 
110         style.uninstallDefaults(context);
111         style = null;
112     }
113 
updateStyle(JPanel c)114     private void updateStyle(JPanel c) {
115         SynthContext context = getContext(c, ENABLED);
116         style = SynthLookAndFeel.updateStyle(context, this);
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
getContext(JComponent c)123     public SynthContext getContext(JComponent c) {
124         return getContext(c, getComponentState(c));
125     }
126 
getContext(JComponent c, int state)127     private SynthContext getContext(JComponent c, int state) {
128         return SynthContext.getContext(c, style, state);
129     }
130 
getComponentState(JComponent c)131     private int getComponentState(JComponent c) {
132         return SynthLookAndFeel.getComponentState(c);
133     }
134 
135     /**
136      * Notifies this UI delegate to repaint the specified component.
137      * This method paints the component background, then calls
138      * the {@link #paint(SynthContext,Graphics)} method.
139      *
140      * <p>In general, this method does not need to be overridden by subclasses.
141      * All Look and Feel rendering code should reside in the {@code paint} method.
142      *
143      * @param g the {@code Graphics} object used for painting
144      * @param c the component being painted
145      * @see #paint(SynthContext,Graphics)
146      */
147     @Override
update(Graphics g, JComponent c)148     public void update(Graphics g, JComponent c) {
149         SynthContext context = getContext(c);
150 
151         SynthLookAndFeel.update(context, g);
152         context.getPainter().paintPanelBackground(context,
153                           g, 0, 0, c.getWidth(), c.getHeight());
154         paint(context, g);
155     }
156 
157     /**
158      * Paints the specified component according to the Look and Feel.
159      * <p>This method is not used by Synth Look and Feel.
160      * Painting is handled by the {@link #paint(SynthContext,Graphics)} method.
161      *
162      * @param g the {@code Graphics} object used for painting
163      * @param c the component being painted
164      * @see #paint(SynthContext,Graphics)
165      */
166     @Override
paint(Graphics g, JComponent c)167     public void paint(Graphics g, JComponent c) {
168         SynthContext context = getContext(c);
169 
170         paint(context, g);
171     }
172 
173     /**
174      * Paints the specified component. This implementation does nothing.
175      *
176      * @param context context for the component being painted
177      * @param g the {@code Graphics} object used for painting
178      * @see #update(Graphics,JComponent)
179      */
paint(SynthContext context, Graphics g)180     protected void paint(SynthContext context, Graphics g) {
181         // do actual painting
182     }
183 
184     /**
185      * {@inheritDoc}
186      */
187     @Override
paintBorder(SynthContext context, Graphics g, int x, int y, int w, int h)188     public void paintBorder(SynthContext context, Graphics g, int x,
189                             int y, int w, int h) {
190         context.getPainter().paintPanelBorder(context, g, x, y, w, h);
191     }
192 
193     /**
194      * {@inheritDoc}
195      */
196     @Override
propertyChange(PropertyChangeEvent pce)197     public void propertyChange(PropertyChangeEvent pce) {
198         if (SynthLookAndFeel.shouldUpdateStyle(pce)) {
199             updateStyle((JPanel)pce.getSource());
200         }
201     }
202 }
203