PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package org.gjt.sp.jedit.gui.statusbar;
  25. //{{{ Imports
  26. import java.awt.Point;
  27. import java.awt.event.ActionEvent;
  28. import java.awt.event.ActionListener;
  29. import java.awt.event.MouseEvent;
  30. import java.text.DateFormat;
  31. import java.util.Date;
  32. import javax.swing.JComponent;
  33. import javax.swing.JLabel;
  34. import javax.swing.Timer;
  35. import javax.swing.ToolTipManager;
  36. import org.gjt.sp.jedit.View;
  37. import org.gjt.sp.jedit.jEdit;
  38. //}}}
  39. /**
  40. * @author Matthieu Casanova
  41. * @since jEdit 4.3pre14
  42. */
  43. public class ClockWidgetFactory implements StatusWidgetFactory
  44. {
  45. //{{{ getWidget() method
  46. public Widget getWidget(View view)
  47. {
  48. Widget clock = new ClockWidget();
  49. return clock;
  50. } //}}}
  51. //{{{ ClockWidget class
  52. private static class ClockWidget implements Widget
  53. {
  54. private final Clock clock;
  55. ClockWidget()
  56. {
  57. clock = new Clock();
  58. }
  59. public JComponent getComponent()
  60. {
  61. return clock;
  62. }
  63. public void update()
  64. {
  65. }
  66. public void propertiesChanged()
  67. {
  68. }
  69. } //}}}
  70. //{{{ Clock class
  71. private static class Clock extends JLabel implements ActionListener
  72. {
  73. //{{{ Clock constructor
  74. Clock()
  75. {
  76. setForeground(jEdit.getColorProperty("view.status.foreground"));
  77. setBackground(jEdit.getColorProperty("view.status.background"));
  78. } //}}}
  79. //{{{ addNotify() method
  80. @Override
  81. public void addNotify()
  82. {
  83. super.addNotify();
  84. update();
  85. int millisecondsPerMinute = 1000 * 60;
  86. timer = new Timer(millisecondsPerMinute,this);
  87. timer.setInitialDelay((int)(
  88. millisecondsPerMinute
  89. - System.currentTimeMillis()
  90. % millisecondsPerMinute) + 500);
  91. timer.start();
  92. ToolTipManager.sharedInstance().registerComponent(this);
  93. } //}}}
  94. //{{{ removeNotify() method
  95. @Override
  96. public void removeNotify()
  97. {
  98. timer.stop();
  99. ToolTipManager.sharedInstance().unregisterComponent(this);
  100. super.removeNotify();
  101. } //}}}
  102. //{{{ getToolTipText() method
  103. @Override
  104. public String getToolTipText()
  105. {
  106. return new Date().toString();
  107. } //}}}
  108. //{{{ getToolTipLocation() method
  109. @Override
  110. public Point getToolTipLocation(MouseEvent event)
  111. {
  112. return new Point(event.getX(),-20);
  113. } //}}}
  114. //{{{ actionPerformed() method
  115. public void actionPerformed(ActionEvent e)
  116. {
  117. update();
  118. } //}}}
  119. //{{{ Private members
  120. private Timer timer;
  121. //{{{ getTime() method
  122. private static String getTime()
  123. {
  124. return DateFormat.getTimeInstance(
  125. DateFormat.SHORT).format(new Date());
  126. } //}}}
  127. //{{{ update() method
  128. private void update()
  129. {
  130. setText(getTime());
  131. } //}}}
  132. //}}}
  133. } //}}}
  134. }