jdk/src/share/classes/javax/swing/plaf/synth/SynthToolBarUI.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2658 43e06bc950ec
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 javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.border.*;
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.BasicToolBarUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.swing.plaf.synth.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A Synth L&F implementation of ToolBarUI.  This implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * is a "combined" view/controller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
class SynthToolBarUI extends BasicToolBarUI implements PropertyChangeListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
           SynthUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    protected Icon handleIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    protected Rectangle contentRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private SynthStyle style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private SynthStyle contentStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private SynthStyle dragWindowStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        return new SynthToolBarUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        toolBar.setLayout(createLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        updateStyle(toolBar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        super.installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        toolBar.addPropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        super.uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        toolBar.removePropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private void updateStyle(JToolBar c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        SynthContext context = getContext(c, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        SynthStyle oldStyle = style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        style = SynthLookAndFeel.updateStyle(context, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (oldStyle != style) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            handleIcon =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                style.getIcon(context, "ToolBar.handleIcon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (oldStyle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                uninstallKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        context = getContext(c, Region.TOOL_BAR_CONTENT, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        contentStyle = SynthLookAndFeel.updateStyle(context, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        context = getContext(c, Region.TOOL_BAR_DRAG_WINDOW, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        dragWindowStyle = SynthLookAndFeel.updateStyle(context, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    protected void uninstallDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        SynthContext context = getContext(toolBar, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        style.uninstallDefaults(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        style = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        handleIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        context = getContext(toolBar, Region.TOOL_BAR_CONTENT, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        contentStyle.uninstallDefaults(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        contentStyle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        context = getContext(toolBar, Region.TOOL_BAR_DRAG_WINDOW, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        dragWindowStyle.uninstallDefaults(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        dragWindowStyle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        toolBar.setLayout(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    protected void installComponents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    protected void uninstallComponents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    protected LayoutManager createLayout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return new SynthToolBarLayoutManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public SynthContext getContext(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return getContext(c, getComponentState(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private SynthContext getContext(JComponent c, int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return SynthContext.getContext(SynthContext.class, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    SynthLookAndFeel.getRegion(c), style, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private SynthContext getContext(JComponent c, Region region) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return getContext(c, region, getComponentState(c, region));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private SynthContext getContext(JComponent c, Region region, int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return SynthContext.getContext(SynthContext.class, c, region,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                       dragWindowStyle, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private Region getRegion(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return SynthLookAndFeel.getRegion(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private int getComponentState(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return SynthLookAndFeel.getComponentState(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private int getComponentState(JComponent c, Region region) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return SynthLookAndFeel.getComponentState(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public void update(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        SynthContext context = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        SynthLookAndFeel.update(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        context.getPainter().paintToolBarBackground(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                          g, 0, 0, c.getWidth(), c.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                          toolBar.getOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        paint(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        SynthContext context = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        paint(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public void paintBorder(SynthContext context, Graphics g, int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                            int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        context.getPainter().paintToolBarBorder(context, g, x, y, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                                toolBar.getOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    // Overloaded to do nothing so we can share listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    protected void setBorderToNonRollover(Component c) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    // Overloaded to do nothing so we can share listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    protected void setBorderToRollover(Component c) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    // Overloaded to do nothing so we can share listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    protected void setBorderToNormal(Component c) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    protected void paint(SynthContext context, Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (handleIcon != null && toolBar.isFloatable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            int startX = toolBar.getComponentOrientation().isLeftToRight() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                0 : toolBar.getWidth() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    SynthIcon.getIconWidth(handleIcon, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            SynthIcon.paintIcon(handleIcon, context, g, startX, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    SynthIcon.getIconWidth(handleIcon, context),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    SynthIcon.getIconHeight(handleIcon, context));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        SynthContext subcontext = getContext(toolBar, Region.TOOL_BAR_CONTENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        paintContent(subcontext, g, contentRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        subcontext.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public void paintContent(SynthContext context, Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            Rectangle bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        SynthLookAndFeel.updateSubregion(context, g, bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        context.getPainter().paintToolBarContentBackground(context, g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                             bounds.x, bounds.y, bounds.width, bounds.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                             toolBar.getOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        context.getPainter().paintToolBarContentBorder(context, g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                             bounds.x, bounds.y, bounds.width, bounds.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                             toolBar.getOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    protected void paintDragWindow(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        int w = dragWindow.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        int h = dragWindow.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        SynthContext context = getContext(toolBar,Region.TOOL_BAR_DRAG_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        SynthLookAndFeel.updateSubregion(context, g, new Rectangle(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                         0, 0, w, h));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        context.getPainter().paintToolBarDragWindowBackground(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                                           g, 0, 0, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                                           dragWindow.getOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        context.getPainter().paintToolBarDragWindowBorder(context, g, 0, 0, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                                          dragWindow.getOrientation());
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
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    // PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (SynthLookAndFeel.shouldUpdateStyle(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            updateStyle((JToolBar)e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    class SynthToolBarLayoutManager implements LayoutManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        public void addLayoutComponent(String name, Component comp) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        public void removeLayoutComponent(Component comp) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        public Dimension minimumLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            JToolBar tb = (JToolBar)parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            Insets insets = tb.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            Dimension dim = new Dimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            SynthContext context = getContext(tb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (tb.getOrientation() == JToolBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                dim.width = tb.isFloatable() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    SynthIcon.getIconWidth(handleIcon, context) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                Dimension compDim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                for (int i = 0; i < tb.getComponentCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    compDim = tb.getComponent(i).getMinimumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    dim.width += compDim.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    dim.height = Math.max(dim.height, compDim.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                dim.height = tb.isFloatable() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    SynthIcon.getIconHeight(handleIcon, context) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                Dimension compDim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                for (int i = 0; i < tb.getComponentCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    compDim = tb.getComponent(i).getMinimumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    dim.width = Math.max(dim.width, compDim.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    dim.height += compDim.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            dim.width += insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            dim.height += insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            return dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        public Dimension preferredLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            JToolBar tb = (JToolBar)parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            Insets insets = tb.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            Dimension dim = new Dimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            SynthContext context = getContext(tb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (tb.getOrientation() == JToolBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                dim.width = tb.isFloatable() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    SynthIcon.getIconWidth(handleIcon, context) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                Dimension compDim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                for (int i = 0; i < tb.getComponentCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    compDim = tb.getComponent(i).getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    dim.width += compDim.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    dim.height = Math.max(dim.height, compDim.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                dim.height = tb.isFloatable() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    SynthIcon.getIconHeight(handleIcon, context) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                Dimension compDim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                for (int i = 0; i < tb.getComponentCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    compDim = tb.getComponent(i).getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    dim.width = Math.max(dim.width, compDim.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    dim.height += compDim.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            dim.width += insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            dim.height += insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        public void layoutContainer(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            JToolBar tb = (JToolBar)parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            Insets insets = tb.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            boolean ltr = tb.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            SynthContext context = getContext(tb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            Component c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            Dimension d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (tb.getOrientation() == JToolBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                int handleWidth = tb.isFloatable() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    SynthIcon.getIconWidth(handleIcon, context) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                // Note: contentRect does not take insets into account
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                // since it is used for determining the bounds that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                // passed to paintToolBarContentBackground().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                contentRect.x = ltr ? handleWidth : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                contentRect.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                contentRect.width = tb.getWidth() - handleWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                contentRect.height = tb.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                // However, we do take the insets into account here for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                // the purposes of laying out the toolbar child components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                int x = ltr ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    handleWidth + insets.left :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    tb.getWidth() - handleWidth - insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                int baseY = insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                int baseH = tb.getHeight() - insets.top - insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                for (int i = 0; i < tb.getComponentCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    c = tb.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    d = c.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    int y, h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    if (d.height >= baseH || c instanceof JSeparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        // Fill available height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        y = baseY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                        h = baseH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                        // Center component vertically in the available space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                        y = baseY + (baseH / 2) - (d.height / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        h = d.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    c.setBounds(ltr ? x : x - d.width, y, d.width, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    x = ltr ? x + d.width : x - d.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                int handleHeight = tb.isFloatable() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    SynthIcon.getIconHeight(handleIcon, context) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                // See notes above regarding the use of insets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                contentRect.x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                contentRect.y = handleHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                contentRect.width = tb.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                contentRect.height = tb.getHeight() - handleHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                int baseX = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                int baseW = tb.getWidth() - insets.left - insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                int y = handleHeight + insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                for (int i = 0; i < tb.getComponentCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    c = tb.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    d = c.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    int x, w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    if (d.width >= baseW || c instanceof JSeparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        // Fill available width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                        x = baseX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        w = baseW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        // Center component horizontally in the available space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        x = baseX + (baseW / 2) - (d.width / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        w = d.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    c.setBounds(x, y, w, d.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    y += d.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
}