001: package net.sourceforge.squirrel_sql.fw.gui;
002:
003: /*
004: * Copyright (C) 2002-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.awt.Dimension;
022: import java.awt.FontMetrics;
023: import java.awt.Insets;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.text.DateFormat;
027: import java.util.Calendar;
028:
029: import javax.swing.JLabel;
030: import javax.swing.Timer;
031: import javax.swing.border.Border;
032:
033: /**
034: * Time panel. This will show the current time.
035: * A timer to update the time is started when the component
036: * is added to its parent and stopped when removed from its parent.
037: *
038: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
039: */
040: public class TimePanel extends JLabel implements ActionListener {
041: /** Timer that updates time. */
042: private Timer _timer;
043:
044: /** Used to format the displayed date. */
045: private DateFormat _fmt = DateFormat
046: .getTimeInstance(DateFormat.LONG);
047: private Dimension _prefSize;
048: private Calendar _calendar = Calendar.getInstance();
049:
050: /**
051: * Default ctor.
052: */
053: public TimePanel() {
054: super ("", JLabel.CENTER);
055: }
056:
057: /**
058: * Add component to its parent. Start the timer for auto-update.
059: */
060: public void addNotify() {
061: super .addNotify();
062: _timer = new Timer(1000, this );
063: _timer.start();
064: }
065:
066: /**
067: * Remove component from its parent. Stop the timer.
068: */
069: public void removeNotify() {
070: super .removeNotify();
071: if (_timer != null) {
072: _timer.stop();
073: _timer = null;
074: }
075: }
076:
077: /**
078: * Update component with the current time.
079: *
080: * @param evt The current event.
081: */
082: public void actionPerformed(ActionEvent evt) {
083: _calendar.setTimeInMillis(System.currentTimeMillis());
084: setText(_fmt.format(_calendar.getTime()));
085: }
086:
087: /**
088: * Return the preferred size of this component.
089: *
090: * @return the preferred size of this component.
091: */
092: public Dimension getPreferredSize() {
093: if (null == _prefSize) {
094: // This was originaly done every time.
095: // and the count of instantiated objects was amazing
096: _prefSize = new Dimension();
097: _prefSize.height = 20;
098: FontMetrics fm = getFontMetrics(getFont());
099: Calendar cal = Calendar.getInstance();
100: cal.set(Calendar.HOUR_OF_DAY, 23);
101: cal.set(Calendar.MINUTE, 59);
102: cal.set(Calendar.SECOND, 59);
103: _prefSize.width = fm
104: .stringWidth(_fmt.format(cal.getTime()));
105: Border border = getBorder();
106: if (border != null) {
107: Insets ins = border.getBorderInsets(this );
108: if (ins != null) {
109: _prefSize.width += (ins.left + ins.right);
110: }
111: }
112: Insets ins = getInsets();
113: if (ins != null) {
114: _prefSize.width += (ins.left + ins.right) + 20;
115: }
116: }
117: return _prefSize;
118: }
119: }
|