0N/A/*
3261N/A * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing.plaf.synth;
0N/A
1173N/Aimport java.awt.Component;
1173N/Aimport java.awt.Container;
1173N/Aimport java.awt.Dimension;
1173N/Aimport java.awt.Graphics;
1173N/Aimport java.awt.Insets;
1173N/Aimport java.awt.LayoutManager;
1173N/Aimport java.awt.Rectangle;
1173N/Aimport java.beans.PropertyChangeEvent;
1173N/Aimport java.beans.PropertyChangeListener;
1173N/Aimport javax.swing.Box;
1173N/Aimport javax.swing.Icon;
1173N/Aimport javax.swing.JComponent;
1173N/Aimport javax.swing.JSeparator;
1173N/Aimport javax.swing.JToolBar;
1173N/Aimport javax.swing.plaf.ComponentUI;
0N/Aimport javax.swing.plaf.basic.BasicToolBarUI;
1173N/Aimport sun.swing.plaf.synth.SynthIcon;
0N/A
0N/A/**
1999N/A * Provides the Synth L&F UI delegate for
1999N/A * {@link javax.swing.JToolBar}.
0N/A *
1999N/A * @since 1.7
0N/A */
1999N/Apublic class SynthToolBarUI extends BasicToolBarUI
1999N/A implements PropertyChangeListener, SynthUI {
1999N/A private Icon handleIcon = null;
1999N/A private Rectangle contentRect = new Rectangle();
0N/A
0N/A private SynthStyle style;
0N/A private SynthStyle contentStyle;
0N/A private SynthStyle dragWindowStyle;
0N/A
1999N/A /**
1999N/A * Creates a new UI object for the given component.
1999N/A *
1999N/A * @param c component to create UI object for
1999N/A * @return the UI object
1999N/A */
0N/A public static ComponentUI createUI(JComponent c) {
0N/A return new SynthToolBarUI();
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1173N/A @Override
0N/A protected void installDefaults() {
0N/A toolBar.setLayout(createLayout());
0N/A updateStyle(toolBar);
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1173N/A @Override
0N/A protected void installListeners() {
0N/A super.installListeners();
0N/A toolBar.addPropertyChangeListener(this);
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1173N/A @Override
0N/A protected void uninstallListeners() {
0N/A super.uninstallListeners();
0N/A toolBar.removePropertyChangeListener(this);
0N/A }
0N/A
0N/A private void updateStyle(JToolBar c) {
1173N/A SynthContext context = getContext(
1173N/A c, Region.TOOL_BAR_CONTENT, null, ENABLED);
1173N/A contentStyle = SynthLookAndFeel.updateStyle(context, this);
1173N/A context.dispose();
1173N/A
1173N/A context = getContext(c, Region.TOOL_BAR_DRAG_WINDOW, null, ENABLED);
1173N/A dragWindowStyle = SynthLookAndFeel.updateStyle(context, this);
1173N/A context.dispose();
1173N/A
1173N/A context = getContext(c, ENABLED);
0N/A SynthStyle oldStyle = style;
0N/A
0N/A style = SynthLookAndFeel.updateStyle(context, this);
0N/A if (oldStyle != style) {
0N/A handleIcon =
0N/A style.getIcon(context, "ToolBar.handleIcon");
0N/A if (oldStyle != null) {
0N/A uninstallKeyboardActions();
0N/A installKeyboardActions();
0N/A }
0N/A }
0N/A context.dispose();
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1173N/A @Override
0N/A protected void uninstallDefaults() {
0N/A SynthContext context = getContext(toolBar, ENABLED);
0N/A
0N/A style.uninstallDefaults(context);
0N/A context.dispose();
0N/A style = null;
0N/A
0N/A handleIcon = null;
0N/A
1173N/A context = getContext(toolBar, Region.TOOL_BAR_CONTENT,
1173N/A contentStyle, ENABLED);
0N/A contentStyle.uninstallDefaults(context);
0N/A context.dispose();
0N/A contentStyle = null;
0N/A
1173N/A context = getContext(toolBar, Region.TOOL_BAR_DRAG_WINDOW,
1173N/A dragWindowStyle, ENABLED);
0N/A dragWindowStyle.uninstallDefaults(context);
0N/A context.dispose();
0N/A dragWindowStyle = null;
0N/A
0N/A toolBar.setLayout(null);
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1173N/A @Override
1173N/A protected void installComponents() {}
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1173N/A @Override
1173N/A protected void uninstallComponents() {}
0N/A
1999N/A /**
1999N/A * Creates a {@code LayoutManager} to use with the toolbar.
1999N/A *
1999N/A * @return a {@code LayoutManager} instance
1999N/A */
0N/A protected LayoutManager createLayout() {
0N/A return new SynthToolBarLayoutManager();
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A public SynthContext getContext(JComponent c) {
1999N/A return getContext(c, SynthLookAndFeel.getComponentState(c));
0N/A }
0N/A
0N/A private SynthContext getContext(JComponent c, int state) {
0N/A return SynthContext.getContext(SynthContext.class, c,
0N/A SynthLookAndFeel.getRegion(c), style, state);
0N/A }
0N/A
1173N/A private SynthContext getContext(JComponent c, Region region, SynthStyle style) {
1173N/A return SynthContext.getContext(SynthContext.class, c, region,
1173N/A style, getComponentState(c, region));
0N/A }
0N/A
1173N/A private SynthContext getContext(JComponent c, Region region,
1173N/A SynthStyle style, int state) {
0N/A return SynthContext.getContext(SynthContext.class, c, region,
1173N/A style, state);
0N/A }
0N/A
0N/A private int getComponentState(JComponent c, Region region) {
0N/A return SynthLookAndFeel.getComponentState(c);
0N/A }
0N/A
1999N/A /**
2146N/A * Notifies this UI delegate to repaint the specified component.
2146N/A * This method paints the component background, then calls
2146N/A * the {@link #paint(SynthContext,Graphics)} method.
2146N/A *
2146N/A * <p>In general, this method does not need to be overridden by subclasses.
2146N/A * All Look and Feel rendering code should reside in the {@code paint} method.
2146N/A *
2146N/A * @param g the {@code Graphics} object used for painting
2146N/A * @param c the component being painted
2146N/A * @see #paint(SynthContext,Graphics)
1999N/A */
1173N/A @Override
0N/A public void update(Graphics g, JComponent c) {
0N/A SynthContext context = getContext(c);
0N/A
0N/A SynthLookAndFeel.update(context, g);
0N/A context.getPainter().paintToolBarBackground(context,
0N/A g, 0, 0, c.getWidth(), c.getHeight(),
0N/A toolBar.getOrientation());
0N/A paint(context, g);
0N/A context.dispose();
0N/A }
0N/A
1999N/A /**
2146N/A * Paints the specified component according to the Look and Feel.
2146N/A * <p>This method is not used by Synth Look and Feel.
2146N/A * Painting is handled by the {@link #paint(SynthContext,Graphics)} method.
2146N/A *
2146N/A * @param g the {@code Graphics} object used for painting
2146N/A * @param c the component being painted
2146N/A * @see #paint(SynthContext,Graphics)
1999N/A */
1173N/A @Override
0N/A public void paint(Graphics g, JComponent c) {
0N/A SynthContext context = getContext(c);
0N/A
0N/A paint(context, g);
0N/A context.dispose();
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A public void paintBorder(SynthContext context, Graphics g, int x,
0N/A int y, int w, int h) {
0N/A context.getPainter().paintToolBarBorder(context, g, x, y, w, h,
0N/A toolBar.getOrientation());
0N/A }
0N/A
1999N/A /**
2174N/A * This implementation does nothing, because the {@code rollover}
2174N/A * property of the {@code JToolBar} class is not used
2174N/A * in the Synth Look and Feel.
1999N/A */
1173N/A @Override
0N/A protected void setBorderToNonRollover(Component c) {}
0N/A
1999N/A /**
2174N/A * This implementation does nothing, because the {@code rollover}
2174N/A * property of the {@code JToolBar} class is not used
2174N/A * in the Synth Look and Feel.
1999N/A */
1173N/A @Override
0N/A protected void setBorderToRollover(Component c) {}
0N/A
1999N/A /**
2174N/A * This implementation does nothing, because the {@code rollover}
2174N/A * property of the {@code JToolBar} class is not used
2174N/A * in the Synth Look and Feel.
1999N/A */
1173N/A @Override
0N/A protected void setBorderToNormal(Component c) {}
0N/A
1999N/A /**
1999N/A * Paints the toolbar.
1999N/A *
1999N/A * @param context context for the component being painted
2146N/A * @param g the {@code Graphics} object used for painting
2146N/A * @see #update(Graphics,JComponent)
1999N/A */
0N/A protected void paint(SynthContext context, Graphics g) {
0N/A if (handleIcon != null && toolBar.isFloatable()) {
0N/A int startX = toolBar.getComponentOrientation().isLeftToRight() ?
0N/A 0 : toolBar.getWidth() -
0N/A SynthIcon.getIconWidth(handleIcon, context);
0N/A SynthIcon.paintIcon(handleIcon, context, g, startX, 0,
0N/A SynthIcon.getIconWidth(handleIcon, context),
0N/A SynthIcon.getIconHeight(handleIcon, context));
0N/A }
0N/A
1173N/A SynthContext subcontext = getContext(
1173N/A toolBar, Region.TOOL_BAR_CONTENT, contentStyle);
0N/A paintContent(subcontext, g, contentRect);
0N/A subcontext.dispose();
0N/A }
0N/A
1999N/A /**
1999N/A * Paints the toolbar content.
1999N/A *
1999N/A * @param context context for the component being painted
1999N/A * @param g {@code Graphics} object used for painting
1999N/A * @param bounds bounding box for the toolbar
1999N/A */
1999N/A protected void paintContent(SynthContext context, Graphics g,
0N/A Rectangle bounds) {
0N/A SynthLookAndFeel.updateSubregion(context, g, bounds);
0N/A context.getPainter().paintToolBarContentBackground(context, g,
0N/A bounds.x, bounds.y, bounds.width, bounds.height,
0N/A toolBar.getOrientation());
0N/A context.getPainter().paintToolBarContentBorder(context, g,
0N/A bounds.x, bounds.y, bounds.width, bounds.height,
0N/A toolBar.getOrientation());
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1173N/A @Override
0N/A protected void paintDragWindow(Graphics g) {
0N/A int w = dragWindow.getWidth();
0N/A int h = dragWindow.getHeight();
1173N/A SynthContext context = getContext(
1173N/A toolBar, Region.TOOL_BAR_DRAG_WINDOW, dragWindowStyle);
1173N/A SynthLookAndFeel.updateSubregion(
1173N/A context, g, new Rectangle(0, 0, w, h));
0N/A context.getPainter().paintToolBarDragWindowBackground(context,
0N/A g, 0, 0, w, h,
0N/A dragWindow.getOrientation());
0N/A context.getPainter().paintToolBarDragWindowBorder(context, g, 0, 0, w, h,
0N/A dragWindow.getOrientation());
0N/A context.dispose();
0N/A }
0N/A
0N/A //
0N/A // PropertyChangeListener
0N/A //
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A public void propertyChange(PropertyChangeEvent e) {
0N/A if (SynthLookAndFeel.shouldUpdateStyle(e)) {
0N/A updateStyle((JToolBar)e.getSource());
0N/A }
0N/A }
0N/A
0N/A
0N/A class SynthToolBarLayoutManager implements LayoutManager {
0N/A public void addLayoutComponent(String name, Component comp) {}
0N/A
0N/A public void removeLayoutComponent(Component comp) {}
0N/A
0N/A public Dimension minimumLayoutSize(Container parent) {
0N/A JToolBar tb = (JToolBar)parent;
0N/A Insets insets = tb.getInsets();
0N/A Dimension dim = new Dimension();
0N/A SynthContext context = getContext(tb);
0N/A
0N/A if (tb.getOrientation() == JToolBar.HORIZONTAL) {
0N/A dim.width = tb.isFloatable() ?
0N/A SynthIcon.getIconWidth(handleIcon, context) : 0;
0N/A Dimension compDim;
0N/A for (int i = 0; i < tb.getComponentCount(); i++) {
2456N/A Component component = tb.getComponent(i);
2456N/A if (component.isVisible()) {
2456N/A compDim = component.getMinimumSize();
2456N/A dim.width += compDim.width;
2456N/A dim.height = Math.max(dim.height, compDim.height);
2456N/A }
0N/A }
0N/A } else {
0N/A dim.height = tb.isFloatable() ?
0N/A SynthIcon.getIconHeight(handleIcon, context) : 0;
0N/A Dimension compDim;
0N/A for (int i = 0; i < tb.getComponentCount(); i++) {
2456N/A Component component = tb.getComponent(i);
2456N/A if (component.isVisible()) {
2456N/A compDim = component.getMinimumSize();
2456N/A dim.width = Math.max(dim.width, compDim.width);
2456N/A dim.height += compDim.height;
2456N/A }
0N/A }
0N/A }
0N/A dim.width += insets.left + insets.right;
0N/A dim.height += insets.top + insets.bottom;
0N/A
0N/A context.dispose();
0N/A return dim;
0N/A }
0N/A
0N/A public Dimension preferredLayoutSize(Container parent) {
0N/A JToolBar tb = (JToolBar)parent;
0N/A Insets insets = tb.getInsets();
0N/A Dimension dim = new Dimension();
0N/A SynthContext context = getContext(tb);
0N/A
0N/A if (tb.getOrientation() == JToolBar.HORIZONTAL) {
0N/A dim.width = tb.isFloatable() ?
0N/A SynthIcon.getIconWidth(handleIcon, context) : 0;
0N/A Dimension compDim;
0N/A for (int i = 0; i < tb.getComponentCount(); i++) {
2456N/A Component component = tb.getComponent(i);
2456N/A if (component.isVisible()) {
2456N/A compDim = component.getPreferredSize();
2456N/A dim.width += compDim.width;
2456N/A dim.height = Math.max(dim.height, compDim.height);
2456N/A }
0N/A }
0N/A } else {
0N/A dim.height = tb.isFloatable() ?
0N/A SynthIcon.getIconHeight(handleIcon, context) : 0;
0N/A Dimension compDim;
0N/A for (int i = 0; i < tb.getComponentCount(); i++) {
2456N/A Component component = tb.getComponent(i);
2456N/A if (component.isVisible()) {
2456N/A compDim = component.getPreferredSize();
2456N/A dim.width = Math.max(dim.width, compDim.width);
2456N/A dim.height += compDim.height;
2456N/A }
0N/A }
0N/A }
0N/A dim.width += insets.left + insets.right;
0N/A dim.height += insets.top + insets.bottom;
0N/A
0N/A context.dispose();
0N/A return dim;
0N/A }
0N/A
0N/A public void layoutContainer(Container parent) {
0N/A JToolBar tb = (JToolBar)parent;
0N/A Insets insets = tb.getInsets();
0N/A boolean ltr = tb.getComponentOrientation().isLeftToRight();
0N/A SynthContext context = getContext(tb);
0N/A
0N/A Component c;
0N/A Dimension d;
1173N/A
1173N/A // JToolBar by default uses a somewhat modified BoxLayout as
1173N/A // its layout manager. For compatibility reasons, we want to
1173N/A // support Box "glue" as a way to move things around on the
1173N/A // toolbar. "glue" is represented in BoxLayout as a Box.Filler
1173N/A // with a minimum and preferred size of (0,0).
1173N/A // So what we do here is find the number of such glue fillers
1173N/A // and figure out how much space should be allocated to them.
1173N/A int glueCount = 0;
1173N/A for (int i=0; i<tb.getComponentCount(); i++) {
1173N/A if (isGlue(tb.getComponent(i))) glueCount++;
1173N/A }
1173N/A
0N/A if (tb.getOrientation() == JToolBar.HORIZONTAL) {
0N/A int handleWidth = tb.isFloatable() ?
0N/A SynthIcon.getIconWidth(handleIcon, context) : 0;
0N/A
0N/A // Note: contentRect does not take insets into account
0N/A // since it is used for determining the bounds that are
0N/A // passed to paintToolBarContentBackground().
0N/A contentRect.x = ltr ? handleWidth : 0;
0N/A contentRect.y = 0;
0N/A contentRect.width = tb.getWidth() - handleWidth;
0N/A contentRect.height = tb.getHeight();
0N/A
0N/A // However, we do take the insets into account here for
0N/A // the purposes of laying out the toolbar child components.
0N/A int x = ltr ?
0N/A handleWidth + insets.left :
0N/A tb.getWidth() - handleWidth - insets.right;
0N/A int baseY = insets.top;
0N/A int baseH = tb.getHeight() - insets.top - insets.bottom;
0N/A
1173N/A // we need to get the minimum width for laying things out
1173N/A // so that we can calculate how much empty space needs to
1173N/A // be distributed among the "glue", if any
1173N/A int extraSpacePerGlue = 0;
1173N/A if (glueCount > 0) {
1173N/A int minWidth = minimumLayoutSize(parent).width;
1173N/A extraSpacePerGlue = (tb.getWidth() - minWidth) / glueCount;
1173N/A if (extraSpacePerGlue < 0) extraSpacePerGlue = 0;
1173N/A }
1173N/A
0N/A for (int i = 0; i < tb.getComponentCount(); i++) {
0N/A c = tb.getComponent(i);
2456N/A if (c.isVisible()) {
2456N/A d = c.getPreferredSize();
2456N/A int y, h;
2456N/A if (d.height >= baseH || c instanceof JSeparator) {
2456N/A // Fill available height
2456N/A y = baseY;
2456N/A h = baseH;
2456N/A } else {
2456N/A // Center component vertically in the available space
2456N/A y = baseY + (baseH / 2) - (d.height / 2);
2456N/A h = d.height;
2456N/A }
2456N/A //if the component is a "glue" component then add to its
2456N/A //width the extraSpacePerGlue it is due
2456N/A if (isGlue(c)) d.width += extraSpacePerGlue;
2456N/A c.setBounds(ltr ? x : x - d.width, y, d.width, h);
2456N/A x = ltr ? x + d.width : x - d.width;
0N/A }
0N/A }
0N/A } else {
0N/A int handleHeight = tb.isFloatable() ?
0N/A SynthIcon.getIconHeight(handleIcon, context) : 0;
0N/A
0N/A // See notes above regarding the use of insets
0N/A contentRect.x = 0;
0N/A contentRect.y = handleHeight;
0N/A contentRect.width = tb.getWidth();
0N/A contentRect.height = tb.getHeight() - handleHeight;
0N/A
0N/A int baseX = insets.left;
0N/A int baseW = tb.getWidth() - insets.left - insets.right;
0N/A int y = handleHeight + insets.top;
0N/A
1173N/A // we need to get the minimum height for laying things out
1173N/A // so that we can calculate how much empty space needs to
1173N/A // be distributed among the "glue", if any
1173N/A int extraSpacePerGlue = 0;
1173N/A if (glueCount > 0) {
1173N/A int minHeight = minimumLayoutSize(parent).height;
1173N/A extraSpacePerGlue = (tb.getHeight() - minHeight) / glueCount;
1173N/A if (extraSpacePerGlue < 0) extraSpacePerGlue = 0;
1173N/A }
1173N/A
0N/A for (int i = 0; i < tb.getComponentCount(); i++) {
0N/A c = tb.getComponent(i);
2456N/A if (c.isVisible()) {
2456N/A d = c.getPreferredSize();
2456N/A int x, w;
2456N/A if (d.width >= baseW || c instanceof JSeparator) {
2456N/A // Fill available width
2456N/A x = baseX;
2456N/A w = baseW;
2456N/A } else {
2456N/A // Center component horizontally in the available space
2456N/A x = baseX + (baseW / 2) - (d.width / 2);
2456N/A w = d.width;
2456N/A }
2456N/A //if the component is a "glue" component then add to its
2456N/A //height the extraSpacePerGlue it is due
2456N/A if (isGlue(c)) d.height += extraSpacePerGlue;
2456N/A c.setBounds(x, y, w, d.height);
2456N/A y += d.height;
0N/A }
0N/A }
0N/A }
0N/A context.dispose();
0N/A }
1173N/A
1173N/A private boolean isGlue(Component c) {
2456N/A if (c.isVisible() && c instanceof Box.Filler) {
1173N/A Box.Filler f = (Box.Filler)c;
1173N/A Dimension min = f.getMinimumSize();
1173N/A Dimension pref = f.getPreferredSize();
1173N/A return min.width == 0 && min.height == 0 &&
1173N/A pref.width == 0 && pref.height == 0;
1173N/A }
1173N/A return false;
1173N/A }
0N/A }
0N/A}