jdk/src/share/classes/javax/swing/plaf/synth/SynthToolBarUI.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5769 2a390122edff
child 20168 137788883a22
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5769
diff changeset
     2
 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4962
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4962
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4962
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4962
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4962
diff changeset
    23
 * questions.
2
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
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    28
import java.awt.Component;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    29
import java.awt.Container;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    30
import java.awt.Dimension;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    31
import java.awt.Graphics;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    32
import java.awt.Insets;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    33
import java.awt.LayoutManager;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    34
import java.awt.Rectangle;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    35
import java.beans.PropertyChangeEvent;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    36
import java.beans.PropertyChangeListener;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    37
import javax.swing.Box;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    38
import javax.swing.Icon;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    39
import javax.swing.JComponent;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    40
import javax.swing.JSeparator;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    41
import javax.swing.JToolBar;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    42
import javax.swing.plaf.ComponentUI;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.swing.plaf.basic.BasicToolBarUI;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    44
import sun.swing.plaf.synth.SynthIcon;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    47
 * Provides the Synth L&F UI delegate for
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    48
 * {@link javax.swing.JToolBar}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    50
 * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    52
public class SynthToolBarUI extends BasicToolBarUI
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    53
                            implements PropertyChangeListener, SynthUI {
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    54
    private Icon handleIcon = null;
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    55
    private Rectangle contentRect = new Rectangle();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private SynthStyle style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private SynthStyle contentStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private SynthStyle dragWindowStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    61
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    62
     * Creates a new UI object for the given component.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    63
     *
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    64
     * @param c component to create UI object for
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    65
     * @return the UI object
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    66
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return new SynthToolBarUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    71
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    72
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    73
     */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    74
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        toolBar.setLayout(createLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        updateStyle(toolBar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    80
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    81
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    82
     */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    83
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        super.installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        toolBar.addPropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    89
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    90
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    91
     */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    92
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        super.uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        toolBar.removePropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private void updateStyle(JToolBar c) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
    99
        SynthContext context = getContext(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   100
                c, Region.TOOL_BAR_CONTENT, null, ENABLED);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   101
        contentStyle = SynthLookAndFeel.updateStyle(context, this);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   102
        context.dispose();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   103
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   104
        context = getContext(c, Region.TOOL_BAR_DRAG_WINDOW, null, ENABLED);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   105
        dragWindowStyle = SynthLookAndFeel.updateStyle(context, this);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   106
        context.dispose();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   107
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   108
        context = getContext(c, ENABLED);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        SynthStyle oldStyle = style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        style = SynthLookAndFeel.updateStyle(context, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (oldStyle != style) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            handleIcon =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                style.getIcon(context, "ToolBar.handleIcon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (oldStyle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                uninstallKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   123
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   124
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   125
     */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   126
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    protected void uninstallDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        SynthContext context = getContext(toolBar, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        style.uninstallDefaults(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        style = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        handleIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   136
        context = getContext(toolBar, Region.TOOL_BAR_CONTENT,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   137
                             contentStyle, ENABLED);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        contentStyle.uninstallDefaults(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        contentStyle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   142
        context = getContext(toolBar, Region.TOOL_BAR_DRAG_WINDOW,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   143
                             dragWindowStyle, ENABLED);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        dragWindowStyle.uninstallDefaults(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        dragWindowStyle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        toolBar.setLayout(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   151
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   152
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   153
     */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   154
    @Override
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   155
    protected void installComponents() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   157
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   158
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   159
     */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   160
    @Override
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   161
    protected void uninstallComponents() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   163
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   164
     * Creates a {@code LayoutManager} to use with the toolbar.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   165
     *
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   166
     * @return a {@code LayoutManager} instance
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   167
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    protected LayoutManager createLayout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return new SynthToolBarLayoutManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   172
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   173
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   174
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   175
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public SynthContext getContext(JComponent c) {
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   177
        return getContext(c, SynthLookAndFeel.getComponentState(c));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private SynthContext getContext(JComponent c, int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return SynthContext.getContext(SynthContext.class, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    SynthLookAndFeel.getRegion(c), style, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   185
    private SynthContext getContext(JComponent c, Region region, SynthStyle style) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   186
        return SynthContext.getContext(SynthContext.class, c, region,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   187
                                       style, getComponentState(c, region));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   190
    private SynthContext getContext(JComponent c, Region region,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   191
                                    SynthStyle style, int state) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return SynthContext.getContext(SynthContext.class, c, region,
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   193
                                       style, state);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private int getComponentState(JComponent c, Region region) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return SynthLookAndFeel.getComponentState(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   200
    /**
4848
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   201
     * Notifies this UI delegate to repaint the specified component.
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   202
     * This method paints the component background, then calls
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   203
     * the {@link #paint(SynthContext,Graphics)} method.
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   204
     *
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   205
     * <p>In general, this method does not need to be overridden by subclasses.
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   206
     * All Look and Feel rendering code should reside in the {@code paint} method.
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   207
     *
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   208
     * @param g the {@code Graphics} object used for painting
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   209
     * @param c the component being painted
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   210
     * @see #paint(SynthContext,Graphics)
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   211
     */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   212
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public void update(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        SynthContext context = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        SynthLookAndFeel.update(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        context.getPainter().paintToolBarBackground(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                          g, 0, 0, c.getWidth(), c.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                          toolBar.getOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        paint(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   224
    /**
4848
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   225
     * Paints the specified component according to the Look and Feel.
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   226
     * <p>This method is not used by Synth Look and Feel.
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   227
     * Painting is handled by the {@link #paint(SynthContext,Graphics)} method.
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   228
     *
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   229
     * @param g the {@code Graphics} object used for painting
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   230
     * @param c the component being painted
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   231
     * @see #paint(SynthContext,Graphics)
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   232
     */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   233
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        SynthContext context = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        paint(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   241
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   242
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   243
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   244
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public void paintBorder(SynthContext context, Graphics g, int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                            int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        context.getPainter().paintToolBarBorder(context, g, x, y, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                                toolBar.getOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   251
    /**
4962
8e27c99de375 6918447: SynthToolBarUI.setBorderToXXXX() methods don't correspond inherited spec. They do nothing.
rupashka
parents: 4848
diff changeset
   252
     * This implementation does nothing, because the {@code rollover}
8e27c99de375 6918447: SynthToolBarUI.setBorderToXXXX() methods don't correspond inherited spec. They do nothing.
rupashka
parents: 4848
diff changeset
   253
     * property of the {@code JToolBar} class is not used
8e27c99de375 6918447: SynthToolBarUI.setBorderToXXXX() methods don't correspond inherited spec. They do nothing.
rupashka
parents: 4848
diff changeset
   254
     * in the Synth Look and Feel.
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   255
     */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   256
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    protected void setBorderToNonRollover(Component c) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   259
    /**
4962
8e27c99de375 6918447: SynthToolBarUI.setBorderToXXXX() methods don't correspond inherited spec. They do nothing.
rupashka
parents: 4848
diff changeset
   260
     * This implementation does nothing, because the {@code rollover}
8e27c99de375 6918447: SynthToolBarUI.setBorderToXXXX() methods don't correspond inherited spec. They do nothing.
rupashka
parents: 4848
diff changeset
   261
     * property of the {@code JToolBar} class is not used
8e27c99de375 6918447: SynthToolBarUI.setBorderToXXXX() methods don't correspond inherited spec. They do nothing.
rupashka
parents: 4848
diff changeset
   262
     * in the Synth Look and Feel.
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   263
     */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   264
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    protected void setBorderToRollover(Component c) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   267
    /**
4962
8e27c99de375 6918447: SynthToolBarUI.setBorderToXXXX() methods don't correspond inherited spec. They do nothing.
rupashka
parents: 4848
diff changeset
   268
     * This implementation does nothing, because the {@code rollover}
8e27c99de375 6918447: SynthToolBarUI.setBorderToXXXX() methods don't correspond inherited spec. They do nothing.
rupashka
parents: 4848
diff changeset
   269
     * property of the {@code JToolBar} class is not used
8e27c99de375 6918447: SynthToolBarUI.setBorderToXXXX() methods don't correspond inherited spec. They do nothing.
rupashka
parents: 4848
diff changeset
   270
     * in the Synth Look and Feel.
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   271
     */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   272
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    protected void setBorderToNormal(Component c) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   275
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   276
     * Paints the toolbar.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   277
     *
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   278
     * @param context context for the component being painted
4848
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   279
     * @param g the {@code Graphics} object used for painting
ffcc849b9351 6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents: 4394
diff changeset
   280
     * @see #update(Graphics,JComponent)
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   281
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    protected void paint(SynthContext context, Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (handleIcon != null && toolBar.isFloatable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            int startX = toolBar.getComponentOrientation().isLeftToRight() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                0 : toolBar.getWidth() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    SynthIcon.getIconWidth(handleIcon, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            SynthIcon.paintIcon(handleIcon, context, g, startX, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    SynthIcon.getIconWidth(handleIcon, context),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    SynthIcon.getIconHeight(handleIcon, context));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   292
        SynthContext subcontext = getContext(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   293
                toolBar, Region.TOOL_BAR_CONTENT, contentStyle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        paintContent(subcontext, g, contentRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        subcontext.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   298
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   299
     * Paints the toolbar content.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   300
     *
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   301
     * @param context context for the component being painted
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   302
     * @param g {@code Graphics} object used for painting
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   303
     * @param bounds bounding box for the toolbar
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   304
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   305
    protected void paintContent(SynthContext context, Graphics g,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            Rectangle bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        SynthLookAndFeel.updateSubregion(context, g, bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        context.getPainter().paintToolBarContentBackground(context, g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                             bounds.x, bounds.y, bounds.width, bounds.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                             toolBar.getOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        context.getPainter().paintToolBarContentBorder(context, g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                             bounds.x, bounds.y, bounds.width, bounds.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                             toolBar.getOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   316
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   317
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   318
     */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   319
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    protected void paintDragWindow(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        int w = dragWindow.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        int h = dragWindow.getHeight();
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   323
        SynthContext context = getContext(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   324
                toolBar, Region.TOOL_BAR_DRAG_WINDOW, dragWindowStyle);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   325
        SynthLookAndFeel.updateSubregion(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   326
                context, g, new Rectangle(0, 0, w, h));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        context.getPainter().paintToolBarDragWindowBackground(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                                           g, 0, 0, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                                           dragWindow.getOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        context.getPainter().paintToolBarDragWindowBorder(context, g, 0, 0, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                                          dragWindow.getOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    // PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   339
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   340
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   341
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   342
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (SynthLookAndFeel.shouldUpdateStyle(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            updateStyle((JToolBar)e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    class SynthToolBarLayoutManager implements LayoutManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        public void addLayoutComponent(String name, Component comp) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        public void removeLayoutComponent(Component comp) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        public Dimension minimumLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            JToolBar tb = (JToolBar)parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            Insets insets = tb.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            Dimension dim = new Dimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            SynthContext context = getContext(tb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            if (tb.getOrientation() == JToolBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                dim.width = tb.isFloatable() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    SynthIcon.getIconWidth(handleIcon, context) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                Dimension compDim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                for (int i = 0; i < tb.getComponentCount(); i++) {
5769
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   366
                    Component component = tb.getComponent(i);
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   367
                    if (component.isVisible()) {
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   368
                        compDim = component.getMinimumSize();
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   369
                        dim.width += compDim.width;
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   370
                        dim.height = Math.max(dim.height, compDim.height);
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   371
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                dim.height = tb.isFloatable() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    SynthIcon.getIconHeight(handleIcon, context) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                Dimension compDim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                for (int i = 0; i < tb.getComponentCount(); i++) {
5769
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   378
                    Component component = tb.getComponent(i);
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   379
                    if (component.isVisible()) {
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   380
                        compDim = component.getMinimumSize();
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   381
                        dim.width = Math.max(dim.width, compDim.width);
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   382
                        dim.height += compDim.height;
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   383
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            dim.width += insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            dim.height += insets.top + insets.bottom;
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
            return dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        public Dimension preferredLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            JToolBar tb = (JToolBar)parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            Insets insets = tb.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            Dimension dim = new Dimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            SynthContext context = getContext(tb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            if (tb.getOrientation() == JToolBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                dim.width = tb.isFloatable() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    SynthIcon.getIconWidth(handleIcon, context) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                Dimension compDim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                for (int i = 0; i < tb.getComponentCount(); i++) {
5769
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   404
                    Component component = tb.getComponent(i);
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   405
                    if (component.isVisible()) {
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   406
                        compDim = component.getPreferredSize();
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   407
                        dim.width += compDim.width;
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   408
                        dim.height = Math.max(dim.height, compDim.height);
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   409
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                dim.height = tb.isFloatable() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    SynthIcon.getIconHeight(handleIcon, context) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                Dimension compDim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                for (int i = 0; i < tb.getComponentCount(); i++) {
5769
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   416
                    Component component = tb.getComponent(i);
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   417
                    if (component.isVisible()) {
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   418
                        compDim = component.getPreferredSize();
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   419
                        dim.width = Math.max(dim.width, compDim.width);
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   420
                        dim.height += compDim.height;
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   421
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            dim.width += insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            dim.height += insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            return dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        public void layoutContainer(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            JToolBar tb = (JToolBar)parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            Insets insets = tb.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            boolean ltr = tb.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            SynthContext context = getContext(tb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            Component c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            Dimension d;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   439
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   440
            // JToolBar by default uses a somewhat modified BoxLayout as
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   441
            // its layout manager. For compatibility reasons, we want to
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   442
            // support Box "glue" as a way to move things around on the
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   443
            // toolbar. "glue" is represented in BoxLayout as a Box.Filler
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   444
            // with a minimum and preferred size of (0,0).
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   445
            // So what we do here is find the number of such glue fillers
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   446
            // and figure out how much space should be allocated to them.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   447
            int glueCount = 0;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   448
            for (int i=0; i<tb.getComponentCount(); i++) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   449
                if (isGlue(tb.getComponent(i))) glueCount++;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   450
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   451
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (tb.getOrientation() == JToolBar.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                int handleWidth = tb.isFloatable() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    SynthIcon.getIconWidth(handleIcon, context) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                // Note: contentRect does not take insets into account
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                // since it is used for determining the bounds that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                // passed to paintToolBarContentBackground().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                contentRect.x = ltr ? handleWidth : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                contentRect.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                contentRect.width = tb.getWidth() - handleWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                contentRect.height = tb.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                // However, we do take the insets into account here for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                // the purposes of laying out the toolbar child components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                int x = ltr ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    handleWidth + insets.left :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    tb.getWidth() - handleWidth - insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                int baseY = insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                int baseH = tb.getHeight() - insets.top - insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   472
                // we need to get the minimum width for laying things out
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   473
                // so that we can calculate how much empty space needs to
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   474
                // be distributed among the "glue", if any
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   475
                int extraSpacePerGlue = 0;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   476
                if (glueCount > 0) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   477
                    int minWidth = minimumLayoutSize(parent).width;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   478
                    extraSpacePerGlue = (tb.getWidth() - minWidth) / glueCount;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   479
                    if (extraSpacePerGlue < 0) extraSpacePerGlue = 0;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   480
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   481
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                for (int i = 0; i < tb.getComponentCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    c = tb.getComponent(i);
5769
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   484
                    if (c.isVisible()) {
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   485
                        d = c.getPreferredSize();
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   486
                        int y, h;
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   487
                        if (d.height >= baseH || c instanceof JSeparator) {
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   488
                            // Fill available height
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   489
                            y = baseY;
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   490
                            h = baseH;
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   491
                        } else {
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   492
                            // Center component vertically in the available space
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   493
                            y = baseY + (baseH / 2) - (d.height / 2);
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   494
                            h = d.height;
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   495
                        }
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   496
                        //if the component is a "glue" component then add to its
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   497
                        //width the extraSpacePerGlue it is due
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   498
                        if (isGlue(c)) d.width += extraSpacePerGlue;
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   499
                        c.setBounds(ltr ? x : x - d.width, y, d.width, h);
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   500
                        x = ltr ? x + d.width : x - d.width;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                int handleHeight = tb.isFloatable() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    SynthIcon.getIconHeight(handleIcon, context) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                // See notes above regarding the use of insets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                contentRect.x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                contentRect.y = handleHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                contentRect.width = tb.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                contentRect.height = tb.getHeight() - handleHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                int baseX = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                int baseW = tb.getWidth() - insets.left - insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                int y = handleHeight + insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   517
                // we need to get the minimum height for laying things out
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   518
                // so that we can calculate how much empty space needs to
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   519
                // be distributed among the "glue", if any
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   520
                int extraSpacePerGlue = 0;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   521
                if (glueCount > 0) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   522
                    int minHeight = minimumLayoutSize(parent).height;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   523
                    extraSpacePerGlue = (tb.getHeight() - minHeight) / glueCount;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   524
                    if (extraSpacePerGlue < 0) extraSpacePerGlue = 0;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   525
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   526
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                for (int i = 0; i < tb.getComponentCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    c = tb.getComponent(i);
5769
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   529
                    if (c.isVisible()) {
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   530
                        d = c.getPreferredSize();
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   531
                        int x, w;
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   532
                        if (d.width >= baseW || c instanceof JSeparator) {
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   533
                            // Fill available width
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   534
                            x = baseX;
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   535
                            w = baseW;
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   536
                        } else {
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   537
                            // Center component horizontally in the available space
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   538
                            x = baseX + (baseW / 2) - (d.width / 2);
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   539
                            w = d.width;
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   540
                        }
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   541
                        //if the component is a "glue" component then add to its
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   542
                        //height the extraSpacePerGlue it is due
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   543
                        if (isGlue(c)) d.height += extraSpacePerGlue;
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   544
                        c.setBounds(x, y, w, d.height);
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   545
                        y += d.height;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   551
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   552
        private boolean isGlue(Component c) {
5769
2a390122edff 6739756: JToolBar leaves space for non-visible items under Nimbus L&F
alexp
parents: 5506
diff changeset
   553
            if (c.isVisible() && c instanceof Box.Filler) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   554
                Box.Filler f = (Box.Filler)c;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   555
                Dimension min = f.getMinimumSize();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   556
                Dimension pref = f.getPreferredSize();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   557
                return min.width == 0 &&  min.height == 0 &&
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   558
                        pref.width == 0 && pref.height == 0;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   559
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   560
            return false;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   561
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
}