1 /*
   2  * Copyright (c) 2011, 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     static class ToolBarBorder extends AbstractBorder implements UIResource, javax.swing.SwingConstants {
  59         protected void fillHandle(final Graphics g, final int x1, final int y1, final int x2, final int y2, final boolean horizontal) {
  60             g.setColor(UIManager.getColor("ToolBar.borderHandleColor"));
  61             if (horizontal) {
  62                 final int h = y2 - y1 - 2;
  63                 g.fillRect(x1 + 2, y1 + 1, 1, h);
  64                 g.fillRect(x1 + 5, y1 + 1, 1, h);
  65             } else {
  66                 final int w = x2 - x1 - 2;
  67                 g.fillRect(x1 + 1, y1 + 2, w, 1);
  68                 g.fillRect(x1 + 1, y1 + 5, w, 1);
  69             }
  70         }
  71 
  72         public void paintBorder(final java.awt.Component c, final Graphics g, int x, int y, final int w, final int h) {
  73             g.translate(x, y);
  74 
  75             if (c.isOpaque()) {
  76                 AquaUtils.fillRect(g, c, c.getBackground(), 0, 0, w - 1, h - 1);
  77             }
  78 
  79             final Color oldColor = g.getColor();
  80 
  81             final JToolBar jtb = (JToolBar)c;
  82             final ComponentOrientation orient = jtb.getComponentOrientation();
  83             final boolean horizontal = jtb.getOrientation() == SwingConstants.HORIZONTAL;
  84 
  85             if (jtb.isFloatable()) {
  86                 if (horizontal) {
  87                     if (orient.isLeftToRight()) {
  88                         fillHandle(g, 2, 2, 10, h - 2, true);
  89                     } else {
  90                         fillHandle(g, w - 10, 2, w - 2, h - 2, true);
  91                     }
  92                 } else {
  93                     fillHandle(g, 2, 2, w - 2, 10, false);
  94                 }
  95             }
  96 
  97             g.setColor(oldColor);
  98 
  99             g.translate(-x, -y);
 100         }
 101 
 102         public Insets getBorderInsets(final java.awt.Component c) {
 103             final Insets borderInsets = new Insets(5, 5, 5, 5);
 104             return getBorderInsets(c, borderInsets);
 105         }
 106 
 107         public Insets getBorderInsets(final java.awt.Component c, final Insets borderInsets) {
 108             borderInsets.left = 4;
 109             borderInsets.right = 4;
 110             borderInsets.top = 2;
 111             borderInsets.bottom = 2;
 112 
 113             if (((JToolBar)c).isFloatable()) {
 114                 if (((JToolBar)c).getOrientation() == HORIZONTAL) {
 115                     borderInsets.left = 12;
 116                     // We don't have to adjust for right-to-left
 117                 } else { // vertical
 118                     borderInsets.top = 12;
 119                 }
 120             }
 121 
 122             final Insets margin = ((JToolBar)c).getMargin();
 123 
 124             if (margin != null) {
 125                 borderInsets.left += margin.left;
 126                 borderInsets.top += margin.top;
 127                 borderInsets.right += margin.right;
 128                 borderInsets.bottom += margin.bottom;
 129             }
 130 
 131             return borderInsets;
 132         }
 133 
 134         public boolean isBorderOpaque() {
 135             return true;
 136         }
 137     }
 138 
 139     @Override
 140     public final void update(final Graphics g, final JComponent c) {
 141         if (c.isOpaque()) {
 142             AquaUtils.fillRect(g, c);
 143         }
 144         paint(g, c);
 145     }
 146 }