1 /*
   2  * Copyright (c) 2002, 2003, 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      */
  51     public static ComponentUI createUI(JComponent c) {
  52         return new SynthPanelUI();
  53     }
  54 
  55     /**
  56      * @inheritDoc
  57      */
  58     @Override
  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
  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      */
  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      */
  91     protected void uninstallListeners(JPanel p) {
  92         p.removePropertyChangeListener(this);
  93     }
  94 
  95     /**
  96      * @inheritDoc
  97      */
  98     @Override
  99     protected void installDefaults(JPanel p) {
 100         updateStyle(p);
 101     }
 102 
 103     /**
 104      * @inheritDoc
 105      */
 106     @Override
 107     protected void uninstallDefaults(JPanel p) {
 108         SynthContext context = getContext(p, ENABLED);
 109 
 110         style.uninstallDefaults(context);
 111         context.dispose();
 112         style = null;
 113     }
 114 
 115     private void updateStyle(JPanel c) {
 116         SynthContext context = getContext(c, ENABLED);
 117         style = SynthLookAndFeel.updateStyle(context, this);
 118         context.dispose();
 119     }
 120 
 121     /**
 122      * @inheritDoc
 123      */
 124     @Override
 125     public SynthContext getContext(JComponent c) {
 126         return getContext(c, getComponentState(c));
 127     }
 128 
 129     private SynthContext getContext(JComponent c, int state) {
 130         return SynthContext.getContext(SynthContext.class, c,
 131                     SynthLookAndFeel.getRegion(c), style, state);
 132     }
 133 
 134     private int getComponentState(JComponent c) {
 135         return SynthLookAndFeel.getComponentState(c);
 136     }
 137 
 138     /**
 139      * Notifies this UI delegate to repaint the specified component.
 140      * This method paints the component background, then calls
 141      * the {@link #paint(SynthContext,Graphics)} method.
 142      *
 143      * <p>In general, this method does not need to be overridden by subclasses.
 144      * All Look and Feel rendering code should reside in the {@code paint} method.
 145      *
 146      * @param g the {@code Graphics} object used for painting
 147      * @param c the component being painted
 148      * @see #paint(SynthContext,Graphics)
 149      */
 150     @Override
 151     public void update(Graphics g, JComponent c) {
 152         SynthContext context = getContext(c);
 153 
 154         SynthLookAndFeel.update(context, g);
 155         context.getPainter().paintPanelBackground(context,
 156                           g, 0, 0, c.getWidth(), c.getHeight());
 157         paint(context, g);
 158         context.dispose();
 159     }
 160 
 161     /**
 162      * Paints the specified component according to the Look and Feel.
 163      * <p>This method is not used by Synth Look and Feel.
 164      * Painting is handled by the {@link #paint(SynthContext,Graphics)} method.
 165      *
 166      * @param g the {@code Graphics} object used for painting
 167      * @param c the component being painted
 168      * @see #paint(SynthContext,Graphics)
 169      */
 170     @Override
 171     public void paint(Graphics g, JComponent c) {
 172         SynthContext context = getContext(c);
 173 
 174         paint(context, g);
 175         context.dispose();
 176     }
 177 
 178     /**
 179      * Paints the specified component. This implementation does nothing.
 180      *
 181      * @param context context for the component being painted
 182      * @param g the {@code Graphics} object used for painting
 183      * @see #update(Graphics,JComponent)
 184      */
 185     protected void paint(SynthContext context, Graphics g) {
 186         // do actual painting
 187     }
 188 
 189     /**
 190      * @inheritDoc
 191      */
 192     @Override
 193     public void paintBorder(SynthContext context, Graphics g, int x,
 194                             int y, int w, int h) {
 195         context.getPainter().paintPanelBorder(context, g, x, y, w, h);
 196     }
 197 
 198     /**
 199      * @inheritDoc
 200      */
 201     @Override
 202     public void propertyChange(PropertyChangeEvent pce) {
 203         if (SynthLookAndFeel.shouldUpdateStyle(pce)) {
 204             updateStyle((JPanel)pce.getSource());
 205         }
 206     }
 207 }