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                 final Color background = c.getBackground();
  77                 g.setColor(background);
  78                 g.fillRect(0, 0, w - 1, h - 1);
  79             }
  80 
  81             final Color oldColor = g.getColor();
  82 
  83             final JToolBar jtb = (JToolBar)c;
  84             final ComponentOrientation orient = jtb.getComponentOrientation();
  85             final boolean horizontal = jtb.getOrientation() == SwingConstants.HORIZONTAL;
  86 
  87             if (jtb.isFloatable()) {
  88                 if (horizontal) {
  89                     if (orient.isLeftToRight()) {
  90                         fillHandle(g, 2, 2, 10, h - 2, true);
  91                     } else {
  92                         fillHandle(g, w - 10, 2, w - 2, h - 2, true);
  93                     }
  94                 } else {
  95                     fillHandle(g, 2, 2, w - 2, 10, false);
  96                 }
  97             }
  98 
  99             g.setColor(oldColor);
 100 
 101             g.translate(-x, -y);
 102         }
 103 
 104         public Insets getBorderInsets(final java.awt.Component c) {
 105             final Insets borderInsets = new Insets(5, 5, 5, 5);
 106             return getBorderInsets(c, borderInsets);
 107         }
 108 
 109         public Insets getBorderInsets(final java.awt.Component c, final Insets borderInsets) {
 110             borderInsets.left = 4;
 111             borderInsets.right = 4;
 112             borderInsets.top = 2;
 113             borderInsets.bottom = 2;
 114 
 115             if (((JToolBar)c).isFloatable()) {
 116                 if (((JToolBar)c).getOrientation() == HORIZONTAL) {
 117                     borderInsets.left = 12;
 118                     // We don't have to adjust for right-to-left
 119                 } else { // vertical
 120                     borderInsets.top = 12;
 121                 }
 122             }
 123 
 124             final Insets margin = ((JToolBar)c).getMargin();
 125 
 126             if (margin != null) {
 127                 borderInsets.left += margin.left;
 128                 borderInsets.top += margin.top;
 129                 borderInsets.right += margin.right;
 130                 borderInsets.bottom += margin.bottom;
 131             }
 132 
 133             return borderInsets;
 134         }
 135 
 136         public boolean isBorderOpaque() {
 137             return true;
 138         }
 139     }
 140 }