1/* 2 * Copyright (c) 2011, 2012, 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 26package com.apple.laf; 27 28import java.awt.*; 29import java.awt.event.MouseEvent; 30 31import javax.swing.*; 32import javax.swing.plaf.ComponentUI; 33import javax.swing.plaf.basic.BasicPopupMenuUI; 34 35public class AquaPopupMenuUI extends BasicPopupMenuUI { 36 public static ComponentUI createUI(final JComponent x) { 37 return new AquaPopupMenuUI(); 38 } 39 40 public boolean isPopupTrigger(final MouseEvent e) { 41 // Use the awt popup trigger code since this only runs on our OS! 42 return e.isPopupTrigger(); 43 } 44 45 @Override 46 public void paint(final Graphics g, final JComponent c) { 47 if (!(g instanceof Graphics2D)) { 48 super.paint(g, c); 49 return; 50 } 51 52 if (!(PopupFactory.getSharedInstance() instanceof ScreenPopupFactory)) { 53 super.paint(g, c); 54 return; 55 } 56 57 // round off and put back edges in a new Graphics 58 final Graphics2D g2d = (Graphics2D)g.create(); 59 final Rectangle popupBounds = popupMenu.getBounds(); // NB: origin is still at 0,0 60 paintRoundRect(g2d, popupBounds); 61 clipEdges(g2d, popupBounds); 62 g2d.dispose(); 63 64 // if any subsequent drawing occurs over these corners, the window is square again 65 super.paint(g, c); 66 } 67 68 protected void paintRoundRect(final Graphics2D g2d, final Rectangle popupBounds) { 69 // setup the graphics context to blast alpha for every primitive we draw 70 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 71 g2d.setComposite(AlphaComposite.Clear); 72 73 // draw the 3px round-rect line around the outer bounds of the window, 74 // this gives the appearance of rounded corners 75 g2d.setStroke(new BasicStroke(3.0f)); 76 g2d.drawRoundRect(-2, -2, popupBounds.width + 3, popupBounds.height + 3, 12, 12); 77 } 78 79 static final int OVERLAP_SLACK = 10; 80 protected void clipEdges(final Graphics2D g2d, final Rectangle popupBounds) { 81 final Component invoker = popupMenu.getInvoker(); 82 if (!(invoker instanceof JMenu)) return; // only point corners originating from menu items 83 84 final Rectangle invokerBounds = invoker.getBounds(); 85 86 // only get location on screen when necessary 87 invokerBounds.setLocation(invoker.getLocationOnScreen()); 88 popupBounds.setLocation(popupMenu.getLocationOnScreen()); 89 90 final Point invokerCenter = new Point((int)invokerBounds.getCenterX(), (int)invokerBounds.getCenterY()); 91 if (popupBounds.contains(invokerCenter)) { 92 // invoker is "behind" the popup, no corners should be pointed 93 return; 94 } 95 96 // blast opaque background over the corners we want to "put back" 97 g2d.setComposite(AlphaComposite.SrcOver); 98 g2d.setColor(popupMenu.getBackground()); 99 100 final Point popupCenter = new Point((int)popupBounds.getCenterX(), (int)popupBounds.getCenterY()); 101 final boolean invokerMidpointAbovePopupMidpoint = invokerCenter.y <= popupCenter.y; 102 103 if (invokerBounds.x + invokerBounds.width < popupBounds.x + OVERLAP_SLACK) { 104 // popup is far right of invoker 105 if (invokerMidpointAbovePopupMidpoint) { 106 // point upper left corner, most common case 107 g2d.fillRect(-2, -2, 8, 8); 108 return; 109 } 110 // point lower left corner 111 g2d.fillRect(-2, popupBounds.height - 6, 8, 8); 112 return; 113 } 114 115 if (popupBounds.x + popupBounds.width < invokerBounds.x + OVERLAP_SLACK) { 116 // popup is far left of invoker 117 if (invokerMidpointAbovePopupMidpoint) { 118 // point upper right corner 119 g2d.fillRect(popupBounds.width - 6, -2, 8, 8); 120 return; 121 } 122 // point lower right corner 123 g2d.fillRect(popupBounds.width - 6, popupBounds.height - 6, 8, 8); 124 return; 125 } 126 127 // popup is neither "far right" or "far left" of it's invoker 128 if (invokerBounds.y + invokerBounds.height < popupBounds.y + OVERLAP_SLACK) { 129 // popup is "middle" below it's invoker, 130 // this is probably the "connected" case where both upper corners should touch 131 g2d.fillRect(-2, -2, popupBounds.width + 4, 8); 132 return; 133 } 134 135 // if none of these cases match...don't make any corners pointed 136 } 137} 138