1 /*
   2  * Copyright (c) 1997, 2005, 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.sun.java.swing.plaf.windows;
  27 
  28 import java.awt.*;
  29 
  30 import javax.swing.plaf.ComponentUI;
  31 import javax.swing.plaf.basic.*;
  32 import javax.swing.*;
  33 
  34 import static com.sun.java.swing.plaf.windows.TMSchema.Part;
  35 import static com.sun.java.swing.plaf.windows.XPStyle.Skin;
  36 
  37 
  38 /**
  39  * Draws Windows toolbar separators.
  40  * <p>
  41  *
  42  * @author Mark Davidson
  43  */
  44 public class WindowsToolBarSeparatorUI extends BasicToolBarSeparatorUI {
  45 
  46     public static ComponentUI createUI( JComponent c ) {
  47         return new WindowsToolBarSeparatorUI();
  48     }
  49 
  50     public Dimension getPreferredSize(JComponent c) {
  51         Dimension size = ((JToolBar.Separator)c).getSeparatorSize();
  52 
  53         if (size != null) {
  54             size = size.getSize();
  55         } else {
  56             size = new Dimension(6, 6);
  57             XPStyle xp = XPStyle.getXP();
  58             if (xp != null) {
  59                 boolean vertical = ((JSeparator)c).getOrientation() == SwingConstants.VERTICAL;
  60                 Part part = vertical ? Part.TP_SEPARATOR : Part.TP_SEPARATORVERT;
  61                 Skin skin = xp.getSkin(c, part);
  62                 size.width = skin.getWidth();
  63                 size.height = skin.getHeight();
  64             }
  65 
  66             if (((JSeparator)c).getOrientation() == SwingConstants.VERTICAL) {
  67                 size.height = 0;
  68             } else {
  69                 size.width = 0;
  70             }
  71         }
  72         return size;
  73     }
  74 
  75     public Dimension getMaximumSize(JComponent c) {
  76         Dimension pref = getPreferredSize(c);
  77         if (((JSeparator)c).getOrientation() == SwingConstants.VERTICAL) {
  78             return new Dimension(pref.width, Short.MAX_VALUE);
  79         } else {
  80             return new Dimension(Short.MAX_VALUE, pref.height);
  81         }
  82     }
  83 
  84     public void paint( Graphics g, JComponent c ) {
  85         boolean vertical = ((JSeparator)c).getOrientation() == SwingConstants.VERTICAL;
  86         Dimension size = c.getSize();
  87 
  88         XPStyle xp = XPStyle.getXP();
  89         if (xp != null) {
  90             Part part = vertical ? Part.TP_SEPARATOR : Part.TP_SEPARATORVERT;
  91             Skin skin = xp.getSkin(c, part);
  92 
  93             int dx = vertical ? (size.width - skin.getWidth()) / 2 : 0;
  94             int dy = vertical ? 0 : (size.height - skin.getHeight()) / 2;
  95             int dw = vertical ? skin.getWidth() : size.width;
  96             int dh = vertical ? size.height : skin.getHeight();
  97             skin.paintSkin(g, dx, dy, dw, dh, null);
  98         } else {
  99 
 100         Color temp = g.getColor();
 101 
 102         UIDefaults table = UIManager.getLookAndFeelDefaults();
 103 
 104         Color shadow = table.getColor("ToolBar.shadow");
 105         Color highlight = table.getColor("ToolBar.highlight");
 106 
 107         if (vertical) {
 108             int x = (size.width / 2) - 1;
 109             g.setColor(shadow);
 110             g.drawLine(x, 2, x, size.height - 2);
 111 
 112             g.setColor(highlight);
 113             g.drawLine(x + 1, 2, x + 1, size.height - 2);
 114         } else {
 115             int y = (size.height / 2) - 1;
 116             g.setColor(shadow);
 117             g.drawLine(2, y, size.width - 2, y);
 118             g.setColor(highlight);
 119             g.drawLine(2, y + 1, size.width - 2, y + 1);
 120         }
 121         g.setColor(temp);
 122         }
 123     }
 124 }