1 /*
   2  * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.apple.laf;
  27 
  28 import java.awt.*;
  29 
  30 import javax.swing.*;
  31 import javax.swing.border.*;
  32 import javax.swing.plaf.*;
  33 import javax.swing.plaf.basic.BasicToolBarUI;
  34 
  35 import com.apple.laf.AquaUtils.*;
  36 
  37 public class AquaToolBarUI extends BasicToolBarUI implements SwingConstants {
  38     private static RecyclableSingleton<ToolBarBorder> toolBarBorder = new RecyclableSingletonFromDefaultConstructor<ToolBarBorder>(ToolBarBorder.class);
  39     public static Border getToolBarBorder() {
  40         return toolBarBorder.get();
  41     }
  42 
  43     public static ComponentUI createUI(final JComponent c) {
  44         return new AquaToolBarUI();
  45     }
  46 
  47     protected void setBorderToNonRollover(final Component c) { }
  48     protected void setBorderToNormal(final Component c) { }
  49     protected void setBorderToRollover(final Component c) { }
  50 
  51     protected RootPaneContainer createFloatingWindow(final JToolBar toolbar) {
  52         final RootPaneContainer window = super.createFloatingWindow(toolbar);
  53         window.getRootPane().putClientProperty("Window.style", "small");
  54         return window;
  55     }
  56 
  57     /* ToolBarBorder and drag-off handle, based loosly on MetalBumps */
  58     @SuppressWarnings("serial") // Superclass is not serializable across versions
  59     static class ToolBarBorder extends AbstractBorder implements UIResource, javax.swing.SwingConstants {
  60         protected void fillHandle(final Graphics g, final int x1, final int y1, final int x2, final int y2, final boolean horizontal) {
  61             g.setColor(UIManager.getColor("ToolBar.borderHandleColor"));
  62             if (horizontal) {
  63                 final int h = y2 - y1 - 2;
  64                 g.fillRect(x1 + 2, y1 + 1, 1, h);
  65                 g.fillRect(x1 + 5, y1 + 1, 1, h);
  66             } else {
  67                 final int w = x2 - x1 - 2;
  68                 g.fillRect(x1 + 1, y1 + 2, w, 1);
  69                 g.fillRect(x1 + 1, y1 + 5, w, 1);
  70             }
  71         }
  72 
  73         public void paintBorder(final java.awt.Component c, final Graphics g, int x, int y, final int w, final int h) {
  74             g.translate(x, y);
  75 
  76             if (c.isOpaque()) {
  77                 AquaUtils.fillRect(g, c, c.getBackground(), 0, 0, w - 1, h - 1);
  78             }
  79 
  80             final Color oldColor = g.getColor();
  81 
  82             final JToolBar jtb = (JToolBar)c;
  83             final ComponentOrientation orient = jtb.getComponentOrientation();
  84             final boolean horizontal = jtb.getOrientation() == SwingConstants.HORIZONTAL;
  85 
  86             if (jtb.isFloatable()) {
  87                 if (horizontal) {
  88                     if (orient.isLeftToRight()) {
  89                         fillHandle(g, 2, 2, 10, h - 2, true);
  90                     } else {
  91                         fillHandle(g, w - 10, 2, w - 2, h - 2, true);
  92                     }
  93                 } else {
  94                     fillHandle(g, 2, 2, w - 2, 10, false);
  95                 }
  96             }
  97 
  98             g.setColor(oldColor);
  99 
 100             g.translate(-x, -y);
 101         }
 102 
 103         public Insets getBorderInsets(final java.awt.Component c) {
 104             final Insets borderInsets = new Insets(5, 5, 5, 5);
 105             return getBorderInsets(c, borderInsets);
 106         }
 107 
 108         public Insets getBorderInsets(final java.awt.Component c, final Insets borderInsets) {
 109             borderInsets.left = 4;
 110             borderInsets.right = 4;
 111             borderInsets.top = 2;
 112             borderInsets.bottom = 2;
 113 
 114             if (((JToolBar)c).isFloatable()) {
 115                 if (((JToolBar)c).getOrientation() == HORIZONTAL) {
 116                     borderInsets.left = 12;
 117                     // We don't have to adjust for right-to-left
 118                 } else { // vertical
 119                     borderInsets.top = 12;
 120                 }
 121             }
 122 
 123             final Insets margin = ((JToolBar)c).getMargin();
 124 
 125             if (margin != null) {
 126                 borderInsets.left += margin.left;
 127                 borderInsets.top += margin.top;
 128                 borderInsets.right += margin.right;
 129                 borderInsets.bottom += margin.bottom;
 130             }
 131 
 132             return borderInsets;
 133         }
 134 
 135         public boolean isBorderOpaque() {
 136             return true;
 137         }
 138     }
 139 
 140     @Override
 141     public final void update(final Graphics g, final JComponent c) {
 142         if (c.isOpaque()) {
 143             AquaUtils.fillRect(g, c);
 144         }
 145         paint(g, c);
 146     }
 147 }