PageRenderTime 39ms CodeModel.GetById 34ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/statusbar/ClockWidgetFactory.java

#
Java | 153 lines | 92 code | 19 blank | 42 comment | 0 complexity | 6919a6b5f2bdf522fcf03acae4808292 MD5 | raw file
  1/*
  2 * ClockWidgetFactory.java - The clock widget service
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2008 Matthieu Casanova
  7 * Portions Copyright (C) 2001, 2004 Slava Pestov
  8 * Portions copyright (C) 2001 Mike Dillon
  9 *
 10 * This program is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU General Public License
 12 * as published by the Free Software Foundation; either version 2
 13 * of the License, or any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 23 */
 24
 25package org.gjt.sp.jedit.gui.statusbar;
 26
 27//{{{ Imports
 28import java.awt.Point;
 29import java.awt.event.ActionEvent;
 30import java.awt.event.ActionListener;
 31import java.awt.event.MouseEvent;
 32import java.text.DateFormat;
 33import java.util.Date;
 34import javax.swing.JComponent;
 35import javax.swing.JLabel;
 36import javax.swing.Timer;
 37import javax.swing.ToolTipManager;
 38import org.gjt.sp.jedit.View;
 39import org.gjt.sp.jedit.jEdit;
 40//}}}
 41
 42/**
 43 * @author Matthieu Casanova
 44 * @since jEdit 4.3pre14 
 45 */
 46public class ClockWidgetFactory implements StatusWidgetFactory
 47{
 48	//{{{ getWidget() method
 49	public Widget getWidget(View view) 
 50	{
 51		Widget clock = new ClockWidget();
 52		return clock;
 53	} //}}}
 54
 55	//{{{ ClockWidget class
 56	private static class ClockWidget implements Widget
 57	{
 58		private final Clock clock;
 59		ClockWidget()
 60		{
 61			clock = new Clock();
 62		}
 63		
 64		public JComponent getComponent() 
 65		{
 66			return clock;
 67		}
 68		
 69		public void update() 
 70		{
 71		}
 72		
 73		public void propertiesChanged()
 74		{
 75		}
 76	} //}}}
 77
 78	//{{{ Clock class
 79	private static class Clock extends JLabel implements ActionListener
 80	{
 81		//{{{ Clock constructor
 82		Clock()
 83		{
 84			setForeground(jEdit.getColorProperty("view.status.foreground"));
 85			setBackground(jEdit.getColorProperty("view.status.background"));
 86		} //}}}
 87
 88		//{{{ addNotify() method
 89		@Override
 90		public void addNotify()
 91		{
 92			super.addNotify();
 93			update();
 94
 95			int millisecondsPerMinute = 1000 * 60;
 96
 97			timer = new Timer(millisecondsPerMinute,this);
 98			timer.setInitialDelay((int)(
 99				millisecondsPerMinute
100				- System.currentTimeMillis()
101				% millisecondsPerMinute) + 500);
102			timer.start();
103			ToolTipManager.sharedInstance().registerComponent(this);
104		} //}}}
105
106		//{{{ removeNotify() method
107		@Override
108		public void removeNotify()
109		{
110			timer.stop();
111			ToolTipManager.sharedInstance().unregisterComponent(this);
112			super.removeNotify();
113		} //}}}
114
115		//{{{ getToolTipText() method
116		@Override
117		public String getToolTipText()
118		{
119			return new Date().toString();
120		} //}}}
121
122		//{{{ getToolTipLocation() method
123		@Override
124		public Point getToolTipLocation(MouseEvent event)
125		{
126			return new Point(event.getX(),-20);
127		} //}}}
128
129		//{{{ actionPerformed() method
130		public void actionPerformed(ActionEvent e)
131		{
132			update();
133		} //}}}
134
135		//{{{ Private members
136		private Timer timer;
137
138		//{{{ getTime() method
139		private static String getTime()
140		{
141			return DateFormat.getTimeInstance(
142				DateFormat.SHORT).format(new Date());
143		} //}}}
144
145		//{{{ update() method
146		private void update()
147		{
148			setText(getTime());
149		} //}}}
150
151		//}}}
152	} //}}}
153}