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 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(c, style, state);
  84     }
  85 
  86     private int getComponentState(JComponent c) {
  87         return SynthLookAndFeel.getComponentState(c);
  88     }
  89 
  90     private void updateStyle(JComponent  c) {
  91         SynthContext context = getContext(c, ENABLED);
  92         SynthStyle oldStyle = style;
  93         style = SynthLookAndFeel.updateStyle(context, this);
  94         if (style != oldStyle) {
  95             if (oldStyle != null) {
  96                 uninstallKeyboardActions((JRootPane)c);
  97                 installKeyboardActions((JRootPane)c);
  98             }
  99         }
 100         context.dispose();
 101     }
 102 
 103     /**
 104      * Notifies this UI delegate to repaint the specified component.
 105      * This method paints the component background, then calls
 106      * the {@link #paint(SynthContext,Graphics)} method.
 107      *
 108      * <p>In general, this method does not need to be overridden by subclasses.
 109      * All Look and Feel rendering code should reside in the {@code paint} method.
 110      *
 111      * @param g the {@code Graphics} object used for painting
 112      * @param c the component being painted
 113      * @see #paint(SynthContext,Graphics)
 114      */
 115     @Override
 116     public void update(Graphics g, JComponent c) {
 117         SynthContext context = getContext(c);
 118 
 119         SynthLookAndFeel.update(context, g);
 120         context.getPainter().paintRootPaneBackground(context,
 121                           g, 0, 0, c.getWidth(), c.getHeight());
 122         paint(context, g);
 123         context.dispose();
 124     }
 125 
 126     /**
 127      * Paints the specified component according to the Look and Feel.
 128      * <p>This method is not used by Synth Look and Feel.
 129      * Painting is handled by the {@link #paint(SynthContext,Graphics)} method.
 130      *
 131      * @param g the {@code Graphics} object used for painting
 132      * @param c the component being painted
 133      * @see #paint(SynthContext,Graphics)
 134      */
 135     @Override
 136     public void paint(Graphics g, JComponent c) {
 137         SynthContext context = getContext(c);
 138 
 139         paint(context, g);
 140         context.dispose();
 141     }
 142 
 143     /**
 144      * Paints the specified component. This implementation does nothing.
 145      *
 146      * @param context context for the component being painted
 147      * @param g the {@code Graphics} object used for painting
 148      * @see #update(Graphics,JComponent)
 149      */
 150     protected void paint(SynthContext context, Graphics g) {
 151     }
 152 
 153     /**
 154      * {@inheritDoc}
 155      */
 156     @Override
 157     public void paintBorder(SynthContext context, Graphics g, int x,
 158                             int y, int w, int h) {
 159         context.getPainter().paintRootPaneBorder(context, g, x, y, w, h);
 160     }
 161 
 162     /**
 163      * Invoked when a property changes on the root pane. If the event
 164      * indicates the {@code defaultButton} has changed, this will
 165      * reinstall the keyboard actions.
 166      */
 167     @Override
 168     public void propertyChange(PropertyChangeEvent e) {
 169         if (SynthLookAndFeel.shouldUpdateStyle(e)) {
 170             updateStyle((JRootPane)e.getSource());
 171         }
 172         super.propertyChange(e);
 173     }
 174 }
--- EOF ---