1 /*
   2  * Copyright (c) 2002, 2008, 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 javax.swing.*;
  30 import javax.swing.text.*;
  31 import javax.swing.plaf.*;
  32 import javax.swing.plaf.basic.BasicEditorPaneUI;
  33 import java.beans.PropertyChangeEvent;
  34 
  35 /**
  36  * Provides the Synth L&F UI delegate for
  37  * {@link javax.swing.JEditorPane}.
  38  *
  39  * @author  Shannon Hickey
  40  * @since 1.7
  41  */
  42 public class SynthEditorPaneUI extends BasicEditorPaneUI implements SynthUI {
  43     private SynthStyle style;
  44     /*
  45      * I would prefer to use UIResource instad of this.
  46      * Unfortunately Boolean is a final class
  47      */
  48     private Boolean localTrue = Boolean.TRUE;
  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 SynthEditorPaneUI();
  58     }
  59 
  60     /**
  61      * @inheritDoc
  62      */
  63     @Override
  64     protected void installDefaults() {
  65         // Installs the text cursor on the component
  66         super.installDefaults();
  67         JComponent c = getComponent();
  68         Object clientProperty =
  69             c.getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES);
  70         if (clientProperty == null) {
  71             c.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, localTrue);
  72         }
  73         updateStyle(getComponent());
  74     }
  75 
  76     /**
  77      * @inheritDoc
  78      */
  79     @Override
  80     protected void uninstallDefaults() {
  81         SynthContext context = getContext(getComponent(), ENABLED);
  82         JComponent c = getComponent();
  83         c.putClientProperty("caretAspectRatio", null);
  84 
  85         style.uninstallDefaults(context);
  86         context.dispose();
  87         style = null;
  88 
  89         Object clientProperty =
  90             c.getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES);
  91         if (clientProperty == localTrue) {
  92             c.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES,
  93                                              Boolean.FALSE);
  94         }
  95         super.uninstallDefaults();
  96     }
  97 
  98     /**
  99      * This method gets called when a bound property is changed
 100      * on the associated JTextComponent.  This is a hook
 101      * which UI implementations may change to reflect how the
 102      * UI displays bound properties of JTextComponent subclasses.
 103      * This is implemented to rebuild the ActionMap based upon an
 104      * EditorKit change.
 105      *
 106      * @param evt the property change event
 107      */
 108     @Override
 109     protected void propertyChange(PropertyChangeEvent evt) {
 110         if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
 111             updateStyle((JTextComponent)evt.getSource());
 112         }
 113         super.propertyChange(evt);
 114     }
 115 
 116     private void updateStyle(JTextComponent comp) {
 117         SynthContext context = getContext(comp, ENABLED);
 118         SynthStyle oldStyle = style;
 119 
 120         style = SynthLookAndFeel.updateStyle(context, this);
 121 
 122         if (style != oldStyle) {
 123             SynthTextFieldUI.updateStyle(comp, context, getPropertyPrefix());
 124 
 125             if (oldStyle != null) {
 126                 uninstallKeyboardActions();
 127                 installKeyboardActions();
 128             }
 129         }
 130         context.dispose();
 131     }
 132 
 133     /**
 134      * @inheritDoc
 135      */
 136     @Override
 137     public SynthContext getContext(JComponent c) {
 138         return getContext(c, getComponentState(c));
 139     }
 140 
 141     private SynthContext getContext(JComponent c, int state) {
 142         return SynthContext.getContext(SynthContext.class, c,
 143                     SynthLookAndFeel.getRegion(c), style, state);
 144     }
 145 
 146     private int getComponentState(JComponent c) {
 147         return SynthLookAndFeel.getComponentState(c);
 148     }
 149 
 150     /**
 151      * Notifies this UI delegate to repaint the specified component.
 152      * This method paints the component background, then calls
 153      * the {@link #paint(SynthContext,Graphics)} method.
 154      *
 155      * <p>In general, this method does not need to be overridden by subclasses.
 156      * All Look and Feel rendering code should reside in the {@code paint} method.
 157      *
 158      * @param g the {@code Graphics} object used for painting
 159      * @param c the component being painted
 160      * @see #paint(SynthContext,Graphics)
 161      */
 162     @Override
 163     public void update(Graphics g, JComponent c) {
 164         SynthContext context = getContext(c);
 165 
 166         SynthLookAndFeel.update(context, g);
 167         paintBackground(context, g, c);
 168         paint(context, g);
 169         context.dispose();
 170     }
 171 
 172     /**
 173      * Paints the specified component.
 174      *
 175      * @param context context for the component being painted
 176      * @param g the {@code Graphics} object used for painting
 177      * @see #update(Graphics,JComponent)
 178      */
 179     protected void paint(SynthContext context, Graphics g) {
 180         super.paint(g, getComponent());
 181     }
 182 
 183     /**
 184      * @inheritDoc
 185      */
 186     @Override
 187     protected void paintBackground(Graphics g) {
 188         // Overriden to do nothing, all our painting is done from update/paint.
 189     }
 190 
 191     void paintBackground(SynthContext context, Graphics g, JComponent c) {
 192         context.getPainter().paintEditorPaneBackground(context, g, 0, 0,
 193                                                   c.getWidth(), c.getHeight());
 194     }
 195 
 196     /**
 197      * @inheritDoc
 198      */
 199     @Override
 200     public void paintBorder(SynthContext context, Graphics g, int x,
 201                             int y, int w, int h) {
 202         context.getPainter().paintEditorPaneBorder(context, g, x, y, w, h);
 203     }
 204 }