1 /*
   2  * Copyright (c) 2002, 2005, 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.beans.*;
  29 import javax.swing.*;
  30 import java.awt.Dimension;
  31 import java.awt.Graphics;
  32 import java.awt.Insets;
  33 import javax.swing.plaf.ComponentUI;
  34 import javax.swing.plaf.SeparatorUI;
  35 import javax.swing.plaf.UIResource;
  36 import javax.swing.plaf.DimensionUIResource;
  37 
  38 /**
  39  * Provides the Synth L&F UI delegate for
  40  * {@link javax.swing.JSeparator}.
  41  *
  42  * @author Shannon Hickey
  43  * @author Joshua Outwater
  44  * @since 1.7
  45  */
  46 public class SynthSeparatorUI extends SeparatorUI
  47                               implements PropertyChangeListener, SynthUI {
  48     private SynthStyle style;
  49 
  50     /**
  51      * Creates a new UI object for the given component.
  52      *
  53      * @param c component to create UI object for
  54      * @return the UI object
  55      */
  56     public static ComponentUI createUI(JComponent c) {
  57         return new SynthSeparatorUI();
  58     }
  59 
  60     /**
  61      * @inheritDoc
  62      */
  63     @Override
  64     public void installUI(JComponent c) {
  65         installDefaults((JSeparator)c);
  66         installListeners((JSeparator)c);
  67     }
  68 
  69     /**
  70      * @inheritDoc
  71      */
  72     @Override
  73     public void uninstallUI(JComponent c) {
  74         uninstallListeners((JSeparator)c);
  75         uninstallDefaults((JSeparator)c);
  76     }
  77 
  78     /**
  79      * Installs default setting. This method is called when a
  80      * {@code LookAndFeel} is installed.
  81      */
  82     public void installDefaults(JSeparator c) {
  83         updateStyle(c);
  84     }
  85 
  86     private void updateStyle(JSeparator sep) {
  87         SynthContext context = getContext(sep, ENABLED);
  88         SynthStyle oldStyle = style;
  89 
  90         style = SynthLookAndFeel.updateStyle(context, this);
  91 
  92         if (style != oldStyle) {
  93             if (sep instanceof JToolBar.Separator) {
  94                 Dimension size = ((JToolBar.Separator)sep).getSeparatorSize();
  95                 if (size == null || size instanceof UIResource) {
  96                     size = (DimensionUIResource)style.get(
  97                                       context, "ToolBar.separatorSize");
  98                     if (size == null) {
  99                         size = new DimensionUIResource(10, 10);
 100                     }
 101                     ((JToolBar.Separator)sep).setSeparatorSize(size);
 102                 }
 103             }
 104         }
 105 
 106         context.dispose();
 107     }
 108 
 109     /**
 110      * Uninstalls default setting. This method is called when a
 111      * {@code LookAndFeel} is uninstalled.
 112      */
 113     public void uninstallDefaults(JSeparator c) {
 114         SynthContext context = getContext(c, ENABLED);
 115 
 116         style.uninstallDefaults(context);
 117         context.dispose();
 118         style = null;
 119     }
 120 
 121     /**
 122      * Installs listeners. This method is called when a
 123      * {@code LookAndFeel} is installed.
 124      */
 125     public void installListeners(JSeparator c) {
 126         c.addPropertyChangeListener(this);
 127     }
 128 
 129     /**
 130      * Uninstalls listeners. This method is called when a
 131      * {@code LookAndFeel} is uninstalled.
 132      */
 133     public void uninstallListeners(JSeparator c) {
 134         c.removePropertyChangeListener(this);
 135     }
 136 
 137     /**
 138      * Notifies this UI delegate to repaint the specified component.
 139      * This method paints the component background, then calls
 140      * the {@link #paint(SynthContext,Graphics)} method.
 141      *
 142      * <p>In general, this method does not need to be overridden by subclasses.
 143      * All Look and Feel rendering code should reside in the {@code paint} method.
 144      *
 145      * @param g the {@code Graphics} object used for painting
 146      * @param c the component being painted
 147      * @see #paint(SynthContext,Graphics)
 148      */
 149     @Override
 150     public void update(Graphics g, JComponent c) {
 151         SynthContext context = getContext(c);
 152 
 153         JSeparator separator = (JSeparator)context.getComponent();
 154         SynthLookAndFeel.update(context, g);
 155         context.getPainter().paintSeparatorBackground(context,
 156                           g, 0, 0, c.getWidth(), c.getHeight(),
 157                           separator.getOrientation());
 158         paint(context, g);
 159         context.dispose();
 160     }
 161 
 162     /**
 163      * Paints the specified component according to the Look and Feel.
 164      * <p>This method is not used by Synth Look and Feel.
 165      * Painting is handled by the {@link #paint(SynthContext,Graphics)} method.
 166      *
 167      * @param g the {@code Graphics} object used for painting
 168      * @param c the component being painted
 169      * @see #paint(SynthContext,Graphics)
 170      */
 171     @Override
 172     public void paint(Graphics g, JComponent c) {
 173         SynthContext context = getContext(c);
 174 
 175         paint(context, g);
 176         context.dispose();
 177     }
 178 
 179     /**
 180      * Paints the specified component.
 181      *
 182      * @param context context for the component being painted
 183      * @param g the {@code Graphics} object used for painting
 184      * @see #update(Graphics,JComponent)
 185      */
 186     protected void paint(SynthContext context, Graphics g) {
 187         JSeparator separator = (JSeparator)context.getComponent();
 188         context.getPainter().paintSeparatorForeground(context, g, 0, 0,
 189                              separator.getWidth(), separator.getHeight(),
 190                              separator.getOrientation());
 191     }
 192 
 193     /**
 194      * @inheritDoc
 195      */
 196     @Override
 197     public void paintBorder(SynthContext context, Graphics g, int x,
 198                             int y, int w, int h) {
 199         JSeparator separator = (JSeparator)context.getComponent();
 200         context.getPainter().paintSeparatorBorder(context, g, x, y, w, h,
 201                                                   separator.getOrientation());
 202     }
 203 
 204     /**
 205      * @inheritDoc
 206      */
 207     @Override
 208     public Dimension getPreferredSize(JComponent c) {
 209         SynthContext context = getContext(c);
 210 
 211         int thickness = style.getInt(context, "Separator.thickness", 2);
 212         Insets insets = c.getInsets();
 213         Dimension size;
 214 
 215         if (((JSeparator)c).getOrientation() == JSeparator.VERTICAL) {
 216             size = new Dimension(insets.left + insets.right + thickness,
 217                                  insets.top + insets.bottom);
 218         } else {
 219             size = new Dimension(insets.left + insets.right,
 220                                  insets.top + insets.bottom + thickness);
 221         }
 222         context.dispose();
 223         return size;
 224     }
 225 
 226     /**
 227      * @inheritDoc
 228      */
 229     @Override
 230     public Dimension getMinimumSize(JComponent c) {
 231         return getPreferredSize(c);
 232     }
 233 
 234     /**
 235      * @inheritDoc
 236      */
 237     @Override
 238     public Dimension getMaximumSize(JComponent c) {
 239         return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
 240     }
 241 
 242     /**
 243      * @inheritDoc
 244      */
 245     @Override
 246     public SynthContext getContext(JComponent c) {
 247         return getContext(c, SynthLookAndFeel.getComponentState(c));
 248     }
 249 
 250     private SynthContext getContext(JComponent c, int state) {
 251         return SynthContext.getContext(SynthContext.class, c,
 252                     SynthLookAndFeel.getRegion(c), style, state);
 253     }
 254 
 255     public void propertyChange(PropertyChangeEvent evt) {
 256         if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
 257             updateStyle((JSeparator)evt.getSource());
 258         }
 259     }
 260 }