01: /*
02: * Copyright (C) 2005 Jeff Tassin
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18:
19: package com.jeta.swingbuilder.gui.components;
20:
21: import java.awt.Dimension;
22: import java.awt.Font;
23: import java.awt.FontMetrics;
24: import java.awt.Insets;
25:
26: import javax.swing.BorderFactory;
27: import javax.swing.JLabel;
28: import javax.swing.SwingConstants;
29: import javax.swing.border.BevelBorder;
30: import javax.swing.border.Border;
31:
32: /**
33: * A cell is used in a TSStatusBar. It represents a single cell on the bar. A
34: * bar is made up as follows: [cell1][cell2]...[cellN]
35: *
36: * @author Jeff Tassin
37: */
38: public class TSCell extends JLabel {
39: String m_mask; // this is a mask that represents the maximum size for this
40: // cell
41: // for example, if this cell represents a time, you could set the mask to
42: // 00:00:00
43: Dimension m_maxsize;
44:
45: /**
46: * flag indicating if this is the main cell. The cell increases size with
47: * the status bar. But, it will never go below its mask size
48: */
49: private boolean m_main;
50:
51: public TSCell(String name, String mask) {
52: setName(name);
53: Border b = BorderFactory.createCompoundBorder(BorderFactory
54: .createBevelBorder(BevelBorder.LOWERED), BorderFactory
55: .createEmptyBorder(0, 2, 0, 2));
56:
57: setBorder(b);
58: setOpaque(true);
59: m_mask = mask;
60: setHorizontalAlignment(SwingConstants.CENTER);
61: }
62:
63: public Dimension getPreferredSize() {
64: if (m_maxsize == null)
65: m_maxsize = new Dimension();
66: Font f = getFont();
67: if (f != null) {
68: Border b = getBorder();
69: if (b != null) {
70: Insets insets = b.getBorderInsets(this );
71: FontMetrics metrics = getFontMetrics(f);
72: m_maxsize.height = metrics.getHeight() + insets.top
73: + insets.bottom;
74: m_maxsize.width = metrics.stringWidth(m_mask)
75: + insets.left + insets.right;
76: }
77: }
78:
79: return m_maxsize;
80: }
81:
82: /**
83: * @return a flag if this cell is the main cell
84: */
85: public boolean isMain() {
86: return m_main;
87: }
88:
89: /**
90: * Sets this cell as the main cell on the status bar. Only one cell can be
91: * the main cell. Setting more than one cell as main will lead to undefined
92: * behavior.
93: */
94: public void setMain(boolean bMain) {
95: m_main = bMain;
96: }
97: }
|