001: /*
002: * ComponentTitledBorder.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing;
023:
024: import java.awt.Color;
025: import java.awt.Component;
026: import java.awt.Dimension;
027: import java.awt.Graphics;
028: import java.awt.Insets;
029: import java.awt.Rectangle;
030:
031: import javax.swing.JComponent;
032: import javax.swing.border.Border;
033: import javax.swing.border.TitledBorder;
034:
035: /* ----------------------------------------------------------
036: * CVS NOTE: Changes to the CVS repository prior to the
037: * release of version 3.0.0beta1 has meant a
038: * resetting of CVS revision numbers.
039: * ----------------------------------------------------------
040: */
041:
042: /** <p>This class provides a titled border with a component
043: * as the title the border.<br>
044: * Modified example from
045: * http://www2.gol.com/users/tame/swing/examples/BorderExamples1.html
046: */
047: /**
048: *
049: * @author Takis Diakoumis
050: * @version $Revision: 1.5 $
051: * @date $Date: 2006/06/14 15:07:18 $
052: */
053: public class ComponentTitledBorder extends TitledBorder {
054:
055: protected JComponent component;
056:
057: public ComponentTitledBorder(JComponent component) {
058: this (null, component, LEFT, TOP);
059: }
060:
061: public ComponentTitledBorder(Border border) {
062: this (border, null, LEFT, TOP);
063: }
064:
065: public ComponentTitledBorder(Border border, JComponent component) {
066: this (border, component, LEFT, TOP);
067: }
068:
069: public ComponentTitledBorder(Border border, JComponent component,
070: int titleJustification, int titlePosition) {
071: super (border, null, titleJustification, titlePosition, null,
072: null);
073: this .component = component;
074: if (border == null) {
075: this .border = super .getBorder();
076: }
077: }
078:
079: public void paintBorder(Component c, Graphics g, int x, int y,
080: int width, int height) {
081: Rectangle borderR = new Rectangle(x + EDGE_SPACING, y
082: + EDGE_SPACING, width - (EDGE_SPACING * 2), height
083: - (EDGE_SPACING * 2));
084: Insets borderInsets;
085: if (border != null) {
086: borderInsets = border.getBorderInsets(c);
087: } else {
088: borderInsets = new Insets(0, 0, 0, 0);
089: }
090:
091: Rectangle rect = new Rectangle(x, y, width, height);
092: Insets insets = getBorderInsets(c);
093: Rectangle compR = getComponentRect(rect, insets);
094: int diff;
095: switch (titlePosition) {
096: case ABOVE_TOP:
097: diff = compR.height + TEXT_SPACING;
098: borderR.y += diff;
099: borderR.height -= diff;
100: break;
101: case TOP:
102: case DEFAULT_POSITION:
103: diff = insets.top / 2 - borderInsets.top - EDGE_SPACING;
104: borderR.y += diff;
105: borderR.height -= diff;
106: break;
107: case BELOW_TOP:
108: case ABOVE_BOTTOM:
109: break;
110: case BOTTOM:
111: diff = insets.bottom / 2 - borderInsets.bottom
112: - EDGE_SPACING;
113: borderR.height -= diff;
114: break;
115: case BELOW_BOTTOM:
116: diff = compR.height + TEXT_SPACING;
117: borderR.height -= diff;
118: break;
119: }
120: border.paintBorder(c, g, borderR.x, borderR.y, borderR.width,
121: borderR.height);
122: Color col = g.getColor();
123: g.setColor(c.getBackground());
124: g.fillRect(compR.x, compR.y, compR.width, compR.height);
125: g.setColor(col);
126: component.repaint();
127: }
128:
129: public Insets getBorderInsets(Component c, Insets insets) {
130: Insets borderInsets;
131: if (border != null) {
132: borderInsets = border.getBorderInsets(c);
133: } else {
134: borderInsets = new Insets(0, 0, 0, 0);
135: }
136: insets.top = EDGE_SPACING + TEXT_SPACING + borderInsets.top;
137: insets.right = EDGE_SPACING + TEXT_SPACING + borderInsets.right;
138: insets.bottom = EDGE_SPACING + TEXT_SPACING
139: + borderInsets.bottom;
140: insets.left = EDGE_SPACING + TEXT_SPACING + borderInsets.left;
141:
142: if (c == null || component == null) {
143: return insets;
144: }
145:
146: int compHeight = 0;
147: if (component != null) {
148: compHeight = component.getPreferredSize().height;
149: }
150:
151: switch (titlePosition) {
152: case ABOVE_TOP:
153: insets.top += compHeight + TEXT_SPACING;
154: break;
155: case TOP:
156: case DEFAULT_POSITION:
157: insets.top += Math.max(compHeight, borderInsets.top)
158: - borderInsets.top;
159: break;
160: case BELOW_TOP:
161: insets.top += compHeight + TEXT_SPACING;
162: break;
163: case ABOVE_BOTTOM:
164: insets.bottom += compHeight + TEXT_SPACING;
165: break;
166: case BOTTOM:
167: insets.bottom += Math.max(compHeight, borderInsets.bottom)
168: - borderInsets.bottom;
169: break;
170: case BELOW_BOTTOM:
171: insets.bottom += compHeight + TEXT_SPACING;
172: break;
173: }
174: return insets;
175: }
176:
177: public JComponent getTitleComponent() {
178: return component;
179: }
180:
181: public void setTitleComponent(JComponent component) {
182: this .component = component;
183: }
184:
185: public Rectangle getComponentRect(Rectangle rect,
186: Insets borderInsets) {
187: Dimension compD = component.getPreferredSize();
188: Rectangle compR = new Rectangle(0, 0, compD.width, compD.height);
189: switch (titlePosition) {
190: case ABOVE_TOP:
191: compR.y = EDGE_SPACING;
192: break;
193: case TOP:
194: case DEFAULT_POSITION:
195: compR.y = EDGE_SPACING
196: + (borderInsets.top - EDGE_SPACING - TEXT_SPACING - compD.height)
197: / 2;
198: break;
199: case BELOW_TOP:
200: compR.y = borderInsets.top - compD.height - TEXT_SPACING;
201: break;
202: case ABOVE_BOTTOM:
203: compR.y = rect.height - borderInsets.bottom + TEXT_SPACING;
204: break;
205: case BOTTOM:
206: compR.y = rect.height
207: - borderInsets.bottom
208: + TEXT_SPACING
209: + (borderInsets.bottom - EDGE_SPACING
210: - TEXT_SPACING - compD.height) / 2;
211: break;
212: case BELOW_BOTTOM:
213: compR.y = rect.height - compD.height - EDGE_SPACING;
214: break;
215: }
216: switch (titleJustification) {
217: case LEFT:
218: case DEFAULT_JUSTIFICATION:
219: compR.x = TEXT_INSET_H + borderInsets.left;
220: break;
221: case RIGHT:
222: compR.x = rect.width - borderInsets.right - TEXT_INSET_H
223: - compR.width;
224: break;
225: case CENTER:
226: compR.x = (rect.width - compR.width) / 2;
227: break;
228: }
229: return compR;
230: }
231:
232: }
|