Path: blob/master/src/java.desktop/macosx/classes/com/apple/laf/AquaOptionPaneUI.java
41154 views
/*1* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.apple.laf;2627import java.awt.*;2829import javax.swing.*;30import javax.swing.plaf.ComponentUI;31import javax.swing.plaf.basic.BasicOptionPaneUI;3233public class AquaOptionPaneUI extends BasicOptionPaneUI {34private static final int kOKCancelButtonWidth = 79;35private static final int kButtonHeight = 23;3637private static final int kDialogSmallPadding = 4;38private static final int kDialogLargePadding = 23;3940/**41* Creates a new BasicOptionPaneUI instance.42*/43public static ComponentUI createUI(final JComponent x) {44return new AquaOptionPaneUI();45}4647/**48* Creates and returns a Container containin the buttons. The buttons49* are created by calling {@code getButtons}.50*/51protected Container createButtonArea() {52final Container bottom = super.createButtonArea();53// Now replace the Layout54bottom.setLayout(new AquaButtonAreaLayout(true, kDialogSmallPadding));55return bottom;56}5758/**59* Messaged from installComponents to create a Container containing the60* body of the message.61* The icon and body should be aligned on their top edges62*/63protected Container createMessageArea() {64final JPanel top = new JPanel();65top.setBorder(UIManager.getBorder("OptionPane.messageAreaBorder"));66top.setLayout(new BoxLayout(top, BoxLayout.X_AXIS));6768/* Fill the body. */69final Container body = new JPanel();7071final Icon sideIcon = getIcon();7273if (sideIcon != null) {74final JLabel iconLabel = new JLabel(sideIcon);75iconLabel.setVerticalAlignment(SwingConstants.TOP);7677final JPanel iconPanel = new JPanel();78iconPanel.add(iconLabel);79top.add(iconPanel);80top.add(Box.createHorizontalStrut(kDialogLargePadding));81}8283body.setLayout(new GridBagLayout());84final GridBagConstraints cons = new GridBagConstraints();85cons.gridx = cons.gridy = 0;86cons.gridwidth = GridBagConstraints.REMAINDER;87cons.gridheight = 1;88cons.anchor = GridBagConstraints.WEST;89cons.insets = new Insets(0, 0, 3, 0);9091addMessageComponents(body, cons, getMessage(), getMaxCharactersPerLineCount(), false);92top.add(body);9394return top;95}9697/**98* AquaButtonAreaLayout lays out all99* components according to the HI Guidelines:100* The most important button is always on the far right101* The group of buttons is on the right for left-to-right,102* left for right-to-left103* The widths of each component will be set to the largest preferred size width.104*105*106* This inner class is marked "public" due to a compiler bug.107* This class should be treated as a "protected" inner class.108* Instantiate it only within subclasses of BasicOptionPaneUI.109*110* BasicOptionPaneUI expects that its buttons are layed out with111* a subclass of ButtonAreaLayout112*/113public static class AquaButtonAreaLayout extends ButtonAreaLayout {114public AquaButtonAreaLayout(final boolean syncAllWidths, final int padding) {115super(true, padding);116}117118public void layoutContainer(final Container container) {119final Component[] children = container.getComponents();120if (children == null || 0 >= children.length) return;121122final int numChildren = children.length;123final int yLocation = container.getInsets().top;124125// Always syncAllWidths - and heights!126final Dimension maxSize = new Dimension(kOKCancelButtonWidth, kButtonHeight);127for (int i = 0; i < numChildren; i++) {128final Dimension sizes = children[i].getPreferredSize();129maxSize.width = Math.max(maxSize.width, sizes.width);130maxSize.height = Math.max(maxSize.height, sizes.height);131}132133// ignore getCentersChildren, because we don't134int xLocation = container.getSize().width - (maxSize.width * numChildren + (numChildren - 1) * padding);135final int xOffset = maxSize.width + padding;136137// most important button (button zero) on far right138for (int i = numChildren - 1; i >= 0; i--) {139children[i].setBounds(xLocation, yLocation, maxSize.width, maxSize.height);140xLocation += xOffset;141}142}143144@Override145public Dimension minimumLayoutSize(Container c) {146if (c != null) {147Component[] children = c.getComponents();148if (children != null && children.length > 0) {149int numChildren = children.length;150Insets cInsets = c.getInsets();151int extraHeight = cInsets.top + cInsets.bottom;152int extraWidth = cInsets.left + cInsets.right;153int okCancelButtonWidth = extraWidth154+ (kOKCancelButtonWidth * numChildren)155+ (numChildren - 1) * padding;156int okbuttonHeight = extraHeight + kButtonHeight;157Dimension minSize = super.minimumLayoutSize(c);158return new Dimension(Math.max(minSize.width,159okCancelButtonWidth),160Math.max(minSize.height, okbuttonHeight));161}162}163return new Dimension(0, 0);164}165}166}167168169