1 /*
   2  * Copyright (c) 2004, 2006, 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.*;
  31 import javax.swing.plaf.basic.BasicPopupMenuSeparatorUI;
  32 import javax.swing.plaf.ComponentUI;
  33 
  34 import com.sun.java.swing.plaf.windows.TMSchema.Part;
  35 import com.sun.java.swing.plaf.windows.TMSchema.State;
  36 import com.sun.java.swing.plaf.windows.XPStyle.Skin;
  37 
  38 /**
  39  * Windows L&F implementation of PopupMenuSeparatorUI.
  40  *
  41  * @author Leif Samuelsson
  42  * @author Igor Kushnirskiy
  43  */
  44 
  45 public class WindowsPopupMenuSeparatorUI extends BasicPopupMenuSeparatorUI {
  46 
  47     public static ComponentUI createUI(JComponent c) {
  48         return new WindowsPopupMenuSeparatorUI();
  49     }
  50 
  51     public void paint(Graphics g, JComponent c) {
  52         Dimension s = c.getSize();
  53         if (WindowsMenuItemUI.isVistaPainting()) {
  54             int x = 1;
  55             Component parent = c.getParent();
  56             if (parent instanceof JComponent) {
  57                 Object gutterOffsetObject =
  58                     ((JComponent) parent).getClientProperty(
  59                         WindowsPopupMenuUI.GUTTER_OFFSET_KEY);
  60                 if (gutterOffsetObject instanceof Integer) {
  61                     /*
  62                      * gutter offset is in parent's coordinates.
  63                      * See comment in
  64                      * WindowsPopupMenuUI.getTextOffset(JComponent)
  65                      */
  66                     x = ((Integer) gutterOffsetObject).intValue() - c.getX();
  67                     x += WindowsPopupMenuUI.getGutterWidth();
  68                 }
  69             }
  70             Skin skin = XPStyle.getXP().getSkin(c, Part.MP_POPUPSEPARATOR);
  71             int skinHeight = skin.getHeight();
  72             int y = (s.height - skinHeight) / 2;
  73             skin.paintSkin(g, x, y, s.width - x - 1, skinHeight, State.NORMAL);
  74         } else {
  75             int y = s.height / 2;
  76             g.setColor(c.getForeground());
  77             g.drawLine(1, y - 1, s.width - 2, y - 1);
  78 
  79             g.setColor(c.getBackground());
  80             g.drawLine(1, y,     s.width - 2, y);
  81         }
  82     }
  83 
  84     public Dimension getPreferredSize(JComponent c) {
  85         int fontHeight = 0;
  86         Font font = c.getFont();
  87         if (font != null) {
  88             fontHeight = c.getFontMetrics(font).getHeight();
  89         }
  90 
  91         return new Dimension(0, fontHeight/2 + 2);
  92     }
  93 
  94 }