jdk/src/share/classes/javax/swing/plaf/synth/SynthButtonUI.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1296 b42a874137fc
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.synth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.plaf.basic.BasicButtonUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.plaf.basic.BasicHTML;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.text.View;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.swing.plaf.synth.SynthUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.swing.plaf.synth.DefaultSynthStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Synth's ButtonUI implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
class SynthButtonUI extends BasicButtonUI implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                                 PropertyChangeListener, SynthUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private SynthStyle style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        return new SynthButtonUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    protected void installDefaults(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        updateStyle(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        LookAndFeel.installProperty(b, "rolloverEnabled", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected void installListeners(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        super.installListeners(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        b.addPropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    void updateStyle(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        SynthContext context = getContext(b, SynthConstants.ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        SynthStyle oldStyle = style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        style = SynthLookAndFeel.updateStyle(context, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (style != oldStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            if (b.getMargin() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                (b.getMargin() instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                Insets margin = (Insets)style.get(context,getPropertyPrefix() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                                  "margin");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                if (margin == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    // Some places assume margins are non-null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    margin = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                b.setMargin(margin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            Object value = style.get(context, getPropertyPrefix() + "iconTextGap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                        LookAndFeel.installProperty(b, "iconTextGap", value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            value = style.get(context, getPropertyPrefix() + "contentAreaFilled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            LookAndFeel.installProperty(b, "contentAreaFilled",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                        value != null? value : Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (oldStyle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                uninstallKeyboardActions(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                installKeyboardActions(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    protected void uninstallListeners(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        super.uninstallListeners(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        b.removePropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    protected void uninstallDefaults(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        SynthContext context = getContext(b, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        style.uninstallDefaults(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        style = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public SynthContext getContext(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return getContext(c, getComponentState(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    SynthContext getContext(JComponent c, int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        Region region = getRegion(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return SynthContext.getContext(SynthContext.class, c, region,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                       style, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private Region getRegion(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return SynthLookAndFeel.getRegion(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Returns the current state of the passed in <code>AbstractButton</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private int getComponentState(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        int state = ENABLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (!c.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            state = DISABLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (SynthLookAndFeel.selectedUI == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return SynthLookAndFeel.selectedUIState | SynthConstants.ENABLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        AbstractButton button = (AbstractButton) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        ButtonModel model = button.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (model.isPressed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (model.isArmed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                state = PRESSED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                state = MOUSE_OVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (model.isRollover()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            state |= MOUSE_OVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (model.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            state |= SELECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (c.isFocusOwner() && button.isFocusPainted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            state |= FOCUSED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if ((c instanceof JButton) && ((JButton)c).isDefaultButton()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            state |= DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public int getBaseline(JComponent c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throw new NullPointerException("Component must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (width < 0 || height < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    "Width and height must be >= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        AbstractButton b = (AbstractButton)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        String text = b.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (text == null || "".equals(text)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        Insets i = b.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        Rectangle viewRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        Rectangle textRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        Rectangle iconRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        viewRect.x = i.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        viewRect.y = i.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        viewRect.width = width - (i.right + viewRect.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        viewRect.height = height - (i.bottom + viewRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // layout the text and icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        SynthContext context = getContext(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        FontMetrics fm = context.getComponent().getFontMetrics(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            context.getStyle().getFont(context));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        context.getStyle().getGraphicsUtils(context).layoutText(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            context, fm, b.getText(), b.getIcon(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            b.getHorizontalAlignment(), b.getVerticalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            b.getHorizontalTextPosition(), b.getVerticalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            viewRect, iconRect, textRect, b.getIconTextGap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        View view = (View)b.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        int baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            baseline = BasicHTML.getHTMLBaseline(view, textRect.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                                 textRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (baseline >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                baseline += textRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            baseline = textRect.y + fm.getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    //          Paint Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public void update(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        SynthContext context = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        SynthLookAndFeel.update(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        paintBackground(context, g, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        paint(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        SynthContext context = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        paint(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    protected void paint(SynthContext context, Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        AbstractButton b = (AbstractButton)context.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        g.setColor(context.getStyle().getColor(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                               ColorType.TEXT_FOREGROUND));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        g.setFont(style.getFont(context));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        context.getStyle().getGraphicsUtils(context).paintText(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            context, g, b.getText(), getIcon(b),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            b.getHorizontalAlignment(), b.getVerticalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            b.getHorizontalTextPosition(), b.getVerticalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            b.getIconTextGap(), b.getDisplayedMnemonicIndex(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            getTextShiftOffset(context));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    void paintBackground(SynthContext context, Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (((AbstractButton) c).isContentAreaFilled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            context.getPainter().paintButtonBackground(context, g, 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                                       c.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                                       c.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public void paintBorder(SynthContext context, Graphics g, int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                            int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        context.getPainter().paintButtonBorder(context, g, x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Returns the default icon. This should NOT callback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * to the JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param b AbstractButton the iocn is associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @return default icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    protected Icon getDefaultIcon(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        SynthContext context = getContext(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        Icon icon = context.getStyle().getIcon(context, getPropertyPrefix() + "icon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Returns the Icon to use in painting the button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    protected Icon getIcon(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        Icon icon = b.getIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (!model.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            icon = getSynthDisabledIcon(b, icon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        } else if (model.isPressed() && model.isArmed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            icon = getPressedIcon(b, getSelectedIcon(b, icon));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        } else if (b.isRolloverEnabled() && model.isRollover()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            icon = getRolloverIcon(b, getSelectedIcon(b, icon));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        } else if (model.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            icon = getSelectedIcon(b, icon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            icon = getEnabledIcon(b, icon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if(icon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            return getDefaultIcon(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * This method will return the icon that should be used for a button.  We
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * only want to use the synth icon defined by the style if the specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * icon has not been defined for the button state and the backup icon is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * UIResource (we set it, not the developer).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @param b button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @param specificIcon icon returned from the button for the specific state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @param defaultIcon fallback icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @param state the synth state of the button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    private Icon getIcon(AbstractButton b, Icon specificIcon, Icon defaultIcon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        Icon icon = specificIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (icon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (defaultIcon instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                icon = getSynthIcon(b, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                if (icon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    icon = defaultIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                icon = defaultIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        return icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private Icon getSynthIcon(AbstractButton b, int synthConstant) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return style.getIcon(getContext(b, synthConstant), getPropertyPrefix() + "icon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private Icon getEnabledIcon(AbstractButton b, Icon defaultIcon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (defaultIcon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            defaultIcon = getSynthIcon(b, SynthConstants.ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return defaultIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private Icon getSelectedIcon(AbstractButton b, Icon defaultIcon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return getIcon(b, b.getSelectedIcon(), defaultIcon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                SynthConstants.SELECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    private Icon getRolloverIcon(AbstractButton b, Icon defaultIcon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        Icon icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (model.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            icon = getIcon(b, b.getRolloverSelectedIcon(), defaultIcon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    SynthConstants.MOUSE_OVER | SynthConstants.SELECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            icon = getIcon(b, b.getRolloverIcon(), defaultIcon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    SynthConstants.MOUSE_OVER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    private Icon getPressedIcon(AbstractButton b, Icon defaultIcon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        return getIcon(b, b.getPressedIcon(), defaultIcon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                SynthConstants.PRESSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    private Icon getSynthDisabledIcon(AbstractButton b, Icon defaultIcon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        ButtonModel model = b.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        Icon icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (model.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            icon = getIcon(b, b.getDisabledSelectedIcon(), defaultIcon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    SynthConstants.DISABLED | SynthConstants.SELECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            icon = getIcon(b, b.getDisabledIcon(), defaultIcon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    SynthConstants.DISABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Returns the amount to shift the text/icon when painting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    protected int getTextShiftOffset(SynthContext state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        AbstractButton button = (AbstractButton)state.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        ButtonModel model = button.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (model.isArmed() && model.isPressed() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                               button.getPressedIcon() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            return state.getStyle().getInt(state, getPropertyPrefix() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                           "textShiftOffset", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    //          Layout Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    // ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public Dimension getMinimumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (c.getComponentCount() > 0 && c.getLayout() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        AbstractButton b = (AbstractButton)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        SynthContext ss = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        Dimension size = ss.getStyle().getGraphicsUtils(ss).getMinimumSize(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
               ss, ss.getStyle().getFont(ss), b.getText(), getSizingIcon(b),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
               b.getHorizontalAlignment(), b.getVerticalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
               b.getHorizontalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
               b.getVerticalTextPosition(), b.getIconTextGap(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
               b.getDisplayedMnemonicIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        ss.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    public Dimension getPreferredSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if (c.getComponentCount() > 0 && c.getLayout() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        AbstractButton b = (AbstractButton)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        SynthContext ss = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        Dimension size = ss.getStyle().getGraphicsUtils(ss).getPreferredSize(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
               ss, ss.getStyle().getFont(ss), b.getText(), getSizingIcon(b),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
               b.getHorizontalAlignment(), b.getVerticalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
               b.getHorizontalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
               b.getVerticalTextPosition(), b.getIconTextGap(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
               b.getDisplayedMnemonicIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        ss.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    public Dimension getMaximumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (c.getComponentCount() > 0 && c.getLayout() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        AbstractButton b = (AbstractButton)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        SynthContext ss = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        Dimension size = ss.getStyle().getGraphicsUtils(ss).getMaximumSize(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
               ss, ss.getStyle().getFont(ss), b.getText(), getSizingIcon(b),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
               b.getHorizontalAlignment(), b.getVerticalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
               b.getHorizontalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
               b.getVerticalTextPosition(), b.getIconTextGap(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
               b.getDisplayedMnemonicIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        ss.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * Returns the Icon used in calculating the pref/min/max size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    protected Icon getSizingIcon(AbstractButton b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        // NOTE: this is slightly different than BasicButtonUI, where it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        // would just use getIcon, but this should be ok.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        Icon icon = (b.isEnabled()) ? b.getIcon() : b.getDisabledIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (icon == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            icon = getDefaultIcon(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        return icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (SynthLookAndFeel.shouldUpdateStyle(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            updateStyle((AbstractButton)e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
}