4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/Apackage com.apple.laf;
4632N/A
4632N/Aimport java.awt.*;
4632N/Aimport java.awt.event.MouseEvent;
4632N/A
4632N/Aimport javax.swing.*;
4632N/Aimport javax.swing.plaf.ComponentUI;
4632N/Aimport javax.swing.plaf.basic.BasicPopupMenuUI;
4632N/A
4632N/Apublic class AquaPopupMenuUI extends BasicPopupMenuUI {
4632N/A public static ComponentUI createUI(final JComponent x) {
4632N/A return new AquaPopupMenuUI();
4632N/A }
4632N/A
4632N/A public boolean isPopupTrigger(final MouseEvent e) {
4632N/A // Use the awt popup trigger code since this only runs on our OS!
4632N/A return e.isPopupTrigger();
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void paint(final Graphics g, final JComponent c) {
4632N/A if (!(g instanceof Graphics2D)) {
4632N/A super.paint(g, c);
4632N/A return;
4632N/A }
4632N/A
4632N/A if (!(PopupFactory.getSharedInstance() instanceof ScreenPopupFactory)) {
4632N/A super.paint(g, c);
4632N/A return;
4632N/A }
4632N/A
4632N/A // round off and put back edges in a new Graphics
4632N/A final Graphics2D g2d = (Graphics2D)g.create();
4632N/A final Rectangle popupBounds = popupMenu.getBounds(); // NB: origin is still at 0,0
4632N/A paintRoundRect(g2d, popupBounds);
4632N/A clipEdges(g2d, popupBounds);
4632N/A g2d.dispose();
4632N/A
4632N/A // if any subsequent drawing occurs over these corners, the window is square again
4632N/A super.paint(g, c);
4632N/A }
4632N/A
4632N/A protected void paintRoundRect(final Graphics2D g2d, final Rectangle popupBounds) {
4632N/A // setup the graphics context to blast alpha for every primitive we draw
4632N/A g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
4632N/A g2d.setComposite(AlphaComposite.Clear);
4632N/A
4632N/A // draw the 3px round-rect line around the outer bounds of the window,
4632N/A // this gives the appearance of rounded corners
4632N/A g2d.setStroke(new BasicStroke(3.0f));
4632N/A g2d.drawRoundRect(-2, -2, popupBounds.width + 3, popupBounds.height + 3, 12, 12);
4632N/A }
4632N/A
4632N/A static final int OVERLAP_SLACK = 10;
4632N/A protected void clipEdges(final Graphics2D g2d, final Rectangle popupBounds) {
4632N/A final Component invoker = popupMenu.getInvoker();
4632N/A if (!(invoker instanceof JMenu)) return; // only point corners originating from menu items
4632N/A
4632N/A final Rectangle invokerBounds = invoker.getBounds();
4632N/A
4632N/A // only get location on screen when necessary
4632N/A invokerBounds.setLocation(invoker.getLocationOnScreen());
4632N/A popupBounds.setLocation(popupMenu.getLocationOnScreen());
4632N/A
4632N/A final Point invokerCenter = new Point((int)invokerBounds.getCenterX(), (int)invokerBounds.getCenterY());
4632N/A if (popupBounds.contains(invokerCenter)) {
4632N/A // invoker is "behind" the popup, no corners should be pointed
4632N/A return;
4632N/A }
4632N/A
4632N/A // blast opaque background over the corners we want to "put back"
4632N/A g2d.setComposite(AlphaComposite.SrcOver);
4632N/A g2d.setColor(popupMenu.getBackground());
4632N/A
4632N/A final Point popupCenter = new Point((int)popupBounds.getCenterX(), (int)popupBounds.getCenterY());
4632N/A final boolean invokerMidpointAbovePopupMidpoint = invokerCenter.y <= popupCenter.y;
4632N/A
4632N/A if (invokerBounds.x + invokerBounds.width < popupBounds.x + OVERLAP_SLACK) {
4632N/A // popup is far right of invoker
4632N/A if (invokerMidpointAbovePopupMidpoint) {
4632N/A // point upper left corner, most common case
4632N/A g2d.fillRect(-2, -2, 8, 8);
4632N/A return;
4632N/A }
4632N/A // point lower left corner
4632N/A g2d.fillRect(-2, popupBounds.height - 6, 8, 8);
4632N/A return;
4632N/A }
4632N/A
4632N/A if (popupBounds.x + popupBounds.width < invokerBounds.x + OVERLAP_SLACK) {
4632N/A // popup is far left of invoker
4632N/A if (invokerMidpointAbovePopupMidpoint) {
4632N/A // point upper right corner
4632N/A g2d.fillRect(popupBounds.width - 6, -2, 8, 8);
4632N/A return;
4632N/A }
4632N/A // point lower right corner
4632N/A g2d.fillRect(popupBounds.width - 6, popupBounds.height - 6, 8, 8);
4632N/A return;
4632N/A }
4632N/A
4632N/A // popup is neither "far right" or "far left" of it's invoker
4632N/A if (invokerBounds.y + invokerBounds.height < popupBounds.y + OVERLAP_SLACK) {
4632N/A // popup is "middle" below it's invoker,
4632N/A // this is probably the "connected" case where both upper corners should touch
4632N/A g2d.fillRect(-2, -2, popupBounds.width + 4, 8);
4632N/A return;
4632N/A }
4632N/A
4632N/A // if none of these cases match...don't make any corners pointed
4632N/A }
4632N/A}