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 java.awt.*;
  29 import java.beans.PropertyChangeEvent;
  30 import javax.swing.*;
  31 import javax.swing.plaf.*;
  32 import javax.swing.plaf.basic.BasicRootPaneUI;
  33 
  34 /**
  35  * Provides the Synth L&F UI delegate for
  36  * {@link javax.swing.JRootPane}.
  37  *
  38  * @author Scott Violet
  39  * @since 1.7
  40  */
  41 public class SynthRootPaneUI extends BasicRootPaneUI implements SynthUI {
  42     private SynthStyle style;
  43 
  44     /**
  45      * Creates a new UI object for the given component.
  46      *
  47      * @param c component to create UI object for
  48      * @return the UI object
  49      */
  50     public static ComponentUI createUI(JComponent c) {
  51         return new SynthRootPaneUI();
  52     }
  53 
  54     /**
  55      * @inheritDoc
  56      */
  57     @Override
  58     protected void installDefaults(JRootPane c){
  59         updateStyle(c);
  60     }
  61 
  62     /**
  63      * @inheritDoc
  64      */
  65     @Override
  66     protected void uninstallDefaults(JRootPane root) {
  67         SynthContext context = getContext(root, ENABLED);
  68 
  69         style.uninstallDefaults(context);
  70         context.dispose();
  71         style = null;
  72     }
  73 
  74     /**
  75      * @inheritDoc
  76      */
  77     @Override
  78     public SynthContext getContext(JComponent c) {
  79         return getContext(c, getComponentState(c));
  80     }
  81 
  82     private SynthContext getContext(JComponent c, int state) {
  83         return SynthContext.getContext(SynthContext.class, c,
  84                     SynthLookAndFeel.getRegion(c), style, state);
  85     }
  86 
  87     private int getComponentState(JComponent c) {
  88         return SynthLookAndFeel.getComponentState(c);
  89     }
  90 
  91     private void updateStyle(JComponent  c) {
  92         SynthContext context = getContext(c, ENABLED);
  93         SynthStyle oldStyle = style;
  94         style = SynthLookAndFeel.updateStyle(context, this);
  95         if (style != oldStyle) {
  96             if (oldStyle != null) {
  97                 uninstallKeyboardActions((JRootPane)c);
  98                 installKeyboardActions((JRootPane)c);
  99             }
 100         }
 101         context.dispose();
 102     }
 103 
 104     /**
 105      * Notifies this UI delegate to repaint the specified component.
 106      * This method paints the component background, then calls
 107      * the {@link #paint(SynthContext,Graphics)} method.
 108      *
 109      * <p>In general, this method does not need to be overridden by subclasses.
 110      * All Look and Feel rendering code should reside in the {@code paint} method.
 111      *
 112      * @param g the {@code Graphics} object used for painting
 113      * @param c the component being painted
 114      * @see #paint(SynthContext,Graphics)
 115      */
 116     @Override
 117     public void update(Graphics g, JComponent c) {
 118         SynthContext context = getContext(c);
 119 
 120         SynthLookAndFeel.update(context, g);
 121         context.getPainter().paintRootPaneBackground(context,
 122                           g, 0, 0, c.getWidth(), c.getHeight());
 123         paint(context, g);
 124         context.dispose();
 125     }
 126 
 127     /**
 128      * Paints the specified component according to the Look and Feel.
 129      * <p>This method is not used by Synth Look and Feel.
 130      * Painting is handled by the {@link #paint(SynthContext,Graphics)} method.
 131      *
 132      * @param g the {@code Graphics} object used for painting
 133      * @param c the component being painted
 134      * @see #paint(SynthContext,Graphics)
 135      */
 136     @Override
 137     public void paint(Graphics g, JComponent c) {
 138         SynthContext context = getContext(c);
 139 
 140         paint(context, g);
 141         context.dispose();
 142     }
 143 
 144     /**
 145      * Paints the specified component. This implementation does nothing.
 146      *
 147      * @param context context for the component being painted
 148      * @param g the {@code Graphics} object used for painting
 149      * @see #update(Graphics,JComponent)
 150      */
 151     protected void paint(SynthContext context, Graphics g) {
 152     }
 153 
 154     /**
 155      * @inheritDoc
 156      */
 157     @Override
 158     public void paintBorder(SynthContext context, Graphics g, int x,
 159                             int y, int w, int h) {
 160         context.getPainter().paintRootPaneBorder(context, g, x, y, w, h);
 161     }
 162 
 163     /**
 164      * Invoked when a property changes on the root pane. If the event
 165      * indicates the <code>defaultButton</code> has changed, this will
 166      * reinstall the keyboard actions.
 167      */
 168     @Override
 169     public void propertyChange(PropertyChangeEvent e) {
 170         if (SynthLookAndFeel.shouldUpdateStyle(e)) {
 171             updateStyle((JRootPane)e.getSource());
 172         }
 173         super.propertyChange(e);
 174     }
 175 }