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/A
4632N/Aimport javax.swing.*;
4632N/Aimport javax.swing.border.*;
4632N/Aimport javax.swing.plaf.UIResource;
4632N/A
4632N/Aimport apple.laf.JRSUIState;
4632N/Aimport apple.laf.JRSUIConstants.*;
4632N/A
4632N/Aimport com.apple.laf.AquaUtils.RecyclableSingleton;
4632N/A
4632N/Apublic class AquaTableHeaderBorder extends AbstractBorder {
4632N/A protected static final int SORT_NONE = 0;
4632N/A protected static final int SORT_ASCENDING = 1;
4632N/A protected static final int SORT_DECENDING = -1;
4632N/A
4632N/A protected final Insets editorBorderInsets = new Insets(1, 3, 1, 3);
4632N/A protected final AquaPainter<JRSUIState> painter = AquaPainter.create(JRSUIState.getInstance());
4632N/A
4632N/A protected static AquaTableHeaderBorder getListHeaderBorder() {
4632N/A // we don't want to share this, because the .setSelected() state
4632N/A // would persist to all other JTable instances
4632N/A return new AquaTableHeaderBorder();
4632N/A }
4632N/A
4632N/A protected AquaTableHeaderBorder() {
4632N/A painter.state.set(AlignmentHorizontal.LEFT);
4632N/A painter.state.set(AlignmentVertical.TOP);
4632N/A }
4632N/A
4632N/A /**
4632N/A * Paints the border for the specified component with the specified
4632N/A * position and size.
4632N/A * @param c the component for which this border is being painted
4632N/A * @param g the paint graphics
4632N/A * @param x the x position of the painted border
4632N/A * @param y the y position of the painted border
4632N/A * @param width the width of the painted border
4632N/A * @param height the height of the painted border
4632N/A */
4632N/A protected boolean doPaint = true;
4632N/A public void paintBorder(final Component c, final Graphics g, final int x, final int y, final int width, final int height) {
4632N/A if (!doPaint) return;
4632N/A final JComponent jc = (JComponent)c;
4632N/A
4632N/A // if the developer wants to set their own color, we should
4632N/A // interpret this as "get out of the way", and don't draw aqua.
4632N/A final Color componentBackground = jc.getBackground();
4632N/A if (!(componentBackground instanceof UIResource)) {
4632N/A doPaint = false;
4632N/A jc.paint(g);
4632N/A getAlternateBorder().paintBorder(jc, g, x, y, width, height);
4632N/A doPaint = true;
4632N/A return;
4632N/A }
4632N/A
4632N/A final State state = getState(jc);
4632N/A painter.state.set(state);
4632N/A painter.state.set(jc.hasFocus() ? Focused.YES : Focused.NO);
4632N/A painter.state.set(height > 16 ? Widget.BUTTON_BEVEL : Widget.BUTTON_LIST_HEADER);
4632N/A painter.state.set(selected ? BooleanValue.YES : BooleanValue.NO);
4632N/A
4632N/A switch (sortOrder) {
4632N/A case SORT_ASCENDING:
4632N/A painter.state.set(Direction.UP);
4632N/A break;
4632N/A case SORT_DECENDING:
4632N/A painter.state.set(Direction.DOWN);
4632N/A break;
4632N/A default:
4632N/A painter.state.set(Direction.NONE);
4632N/A break;
4632N/A }
4632N/A
4632N/A final int newX = x;
4632N/A final int newY = y;
4632N/A final int newWidth = width;
4632N/A final int newHeight = height;
4632N/A
4632N/A painter.paint(g, c, newX - 1, newY - 1, newWidth + 1, newHeight);
4632N/A
4632N/A // Draw the header
4632N/A g.clipRect(newX, y, newWidth, height);
4632N/A g.translate(fHorizontalShift, -1);
4632N/A doPaint = false;
4632N/A jc.paint(g);
4632N/A doPaint = true;
4632N/A }
4632N/A
4632N/A protected State getState(final JComponent jc) {
4632N/A if (!jc.isEnabled()) return State.DISABLED;
4632N/A
4632N/A final JRootPane rootPane = jc.getRootPane();
4632N/A if (rootPane == null) return State.ACTIVE;
4632N/A
4632N/A if (!AquaFocusHandler.isActive(rootPane)) return State.INACTIVE;
4632N/A
4632N/A return State.ACTIVE;
4632N/A }
4632N/A
4632N/A static final RecyclableSingleton<Border> alternateBorder = new RecyclableSingleton<Border>() {
4632N/A @Override
4632N/A protected Border getInstance() {
4632N/A return BorderFactory.createRaisedBevelBorder();
4632N/A }
4632N/A };
4632N/A protected static Border getAlternateBorder() {
4632N/A return alternateBorder.get();
4632N/A }
4632N/A
4632N/A /**
4632N/A * Returns the insets of the border.
4632N/A * @param c the component for which this border insets value applies
4632N/A */
4632N/A public Insets getBorderInsets(final Component c) {
4632N/A // bad to create new one each time. For debugging only.
4632N/A return editorBorderInsets;
4632N/A }
4632N/A
4632N/A public Insets getBorderInsets(final Component c, final Insets insets) {
4632N/A insets.left = editorBorderInsets.left;
4632N/A insets.top = editorBorderInsets.top;
4632N/A insets.right = editorBorderInsets.right;
4632N/A insets.bottom = editorBorderInsets.bottom;
4632N/A return insets;
4632N/A }
4632N/A
4632N/A /**
4632N/A * Returns whether or not the border is opaque. If the border
4632N/A * is opaque, it is responsible for filling in it's own
4632N/A * background when painting.
4632N/A */
4632N/A public boolean isBorderOpaque() {
4632N/A return false;
4632N/A }
4632N/A
4632N/A /**
4632N/A * Sets whether or not this instance of Border draws selected or not. Used by AquaFileChooserUI
4632N/A */
4632N/A private boolean selected = false;
4632N/A protected void setSelected(final boolean inSelected) {
4632N/A selected = inSelected;
4632N/A }
4632N/A
4632N/A /**
4632N/A * Sets an amount to shift the position of the labels. Used by AquaFileChooserUI
4632N/A */
4632N/A private int fHorizontalShift = 0;
4632N/A protected void setHorizontalShift(final int inShift) {
4632N/A fHorizontalShift = inShift;
4632N/A }
4632N/A
4632N/A private int sortOrder = SORT_NONE;
4632N/A protected void setSortOrder(final int inSortOrder) {
4632N/A if (inSortOrder < SORT_DECENDING || inSortOrder > SORT_ASCENDING) {
4632N/A throw new IllegalArgumentException("Invalid sort order constant: " + inSortOrder);
4632N/A }
4632N/A
4632N/A sortOrder = inSortOrder;
4632N/A }
4632N/A}