001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.forms.components.border;
031:
032: import java.awt.Dimension;
033: import java.awt.Font;
034: import java.awt.FontMetrics;
035: import java.awt.Graphics;
036:
037: import javax.swing.JComponent;
038: import javax.swing.UIManager;
039: import javax.swing.border.Border;
040: import javax.swing.plaf.ComponentUI;
041:
042: /**
043: * The UI for a component that simulates the left or right side of a
044: * TitledBorder. This component is useful when flattening layouts. It allows a
045: * titled border to be used to separate components, while allowing those
046: * components to stay in the same row or column of the form. This allows
047: * components to have the same x,y, width, or height, yet have differnt title
048: * borders. This is not possible when using standard TitledBorder.
049: *
050: * @author Jeff Tassin
051: */
052: public class TitledBorderSideUI extends ComponentUI {
053: /** the preferred width and height for this component */
054: private static final int PREF_WIDTH = 6;
055: private static final int PREF_HEIGHT = 24;
056:
057: /**
058: * Returns the component's preferred size for the current look and feel.
059: *
060: * @param c
061: * the component whose preferred size is being queried
062: * @return the preferred size
063: */
064: public Dimension getPreferredSize(JComponent c) {
065: return new Dimension(PREF_WIDTH, PREF_HEIGHT);
066: }
067:
068: /**
069: * Renders the border using the current look and feel.
070: *
071: * @param g
072: * the <code>Graphics</code> context in which to paint
073: * @param c
074: * the component being painted;
075: */
076: public void paint(Graphics g, JComponent c) {
077: Border border = UIManager.getBorder("TitledBorder.border");
078: if (c instanceof TitledBorderSide && border != null) {
079: TitledBorderSide tb = (TitledBorderSide) c;
080:
081: Font font = tb.getFont();
082: FontMetrics fm = c.getFontMetrics(font);
083:
084: int border_y = fm.getAscent() / 2
085: + TitledBorderLabelUI.VERTICAL_PADDING;
086: int border_height = c.getHeight() - border_y;
087:
088: if (tb.getOrientation() == TitledBorderSide.LEFT) {
089: /**
090: * render the border at an x-offset so that the right side is
091: * clipped by the component
092: */
093: int border_x = Math.max(0, c.getWidth() - PREF_WIDTH
094: / 2 - 2);
095: border.paintBorder(c, g, border_x, border_y, c
096: .getWidth(), border_height);
097: } else {
098: /**
099: * render the border at a negative x-offset so that the left
100: * side is clipped by the component
101: */
102: border.paintBorder(c, g, -PREF_WIDTH, border_y, c
103: .getWidth() + 1, border_height);
104: }
105: }
106: }
107:
108: }
|