You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StatusBar.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.ui.components;
  23. import java.awt.Component;
  24. import java.awt.Dimension;
  25. import java.awt.Point;
  26. import java.awt.event.ActionEvent;
  27. import java.awt.event.ActionListener;
  28. import java.awt.event.MouseEvent;
  29. import java.awt.event.MouseListener;
  30. import java.util.ArrayList;
  31. import java.util.Date;
  32. import java.util.List;
  33. import java.util.Timer;
  34. import java.util.TimerTask;
  35. import javax.swing.BorderFactory;
  36. import javax.swing.Icon;
  37. import javax.swing.ImageIcon;
  38. import javax.swing.JLabel;
  39. import javax.swing.JMenuItem;
  40. import javax.swing.JPanel;
  41. import javax.swing.JPopupMenu;
  42. import javax.swing.Spring;
  43. import javax.swing.SpringLayout;
  44. import com.dmdirc.Config;
  45. import com.dmdirc.Error;
  46. import com.dmdirc.ui.interfaces.StatusErrorNotifier;
  47. import com.dmdirc.ui.interfaces.StatusMessageNotifier;
  48. import static com.dmdirc.ui.UIUtilities.SMALL_BORDER;
  49. /**
  50. * Status bar, shows message and info on the gui.
  51. */
  52. public final class StatusBar extends JPanel implements MouseListener,
  53. ActionListener {
  54. /**
  55. * A version number for this class. It should be changed whenever the class
  56. * structure is changed (or anything else that would prevent serialized
  57. * objects being unserialized with the new class).
  58. */
  59. private static final long serialVersionUID = 1;
  60. /** message label. */
  61. private final JLabel messageLabel;
  62. /** icon label. */
  63. private final JLabel iconLabel;
  64. /** current status bar message notifier. */
  65. private StatusMessageNotifier messageNotifier;
  66. /** current status bar error notifier. */
  67. private StatusErrorNotifier errorNotifier;
  68. /** non error state image icon. */
  69. private final ImageIcon normalIcon;
  70. /** Error history storage. */
  71. private final List<Error> errors;
  72. /** Timer to clear the error. */
  73. private TimerTask errorTimer;
  74. /** Timer to clear the message. */
  75. private TimerTask messageTimer;
  76. /** Popupmenu for this frame. */
  77. private final JPopupMenu popup;
  78. /** Clear error menu menu item. */
  79. private final JMenuItem clearErrors;
  80. /** No errors menu menu item. */
  81. private final JMenuItem noErrors;
  82. /** Creates a new instance of StatusBar. */
  83. public StatusBar() {
  84. super();
  85. final SpringLayout layout = new SpringLayout();
  86. errors = new ArrayList<Error>();
  87. popup = new JPopupMenu();
  88. noErrors = new JMenuItem("No errors");
  89. noErrors.addActionListener(this);
  90. noErrors.setActionCommand("None");
  91. popup.add(noErrors);
  92. clearErrors = new JMenuItem("Clear errors");
  93. clearErrors.addActionListener(this);
  94. clearErrors.setActionCommand("Clear");
  95. setBorder(BorderFactory.createEmptyBorder(0, SMALL_BORDER, SMALL_BORDER,
  96. SMALL_BORDER));
  97. messageLabel = new JLabel("Ready");
  98. iconLabel = new JLabel();
  99. messageLabel.setBorder(BorderFactory.createEtchedBorder());
  100. iconLabel.setBorder(BorderFactory.createEtchedBorder());
  101. messageLabel.addMouseListener(this);
  102. iconLabel.addMouseListener(this);
  103. this.setLayout(layout);
  104. add(messageLabel);
  105. add(iconLabel);
  106. setPreferredSize(new Dimension(Short.MAX_VALUE, 25));
  107. setMaximumSize(new Dimension(Short.MAX_VALUE, 25));
  108. normalIcon = new ImageIcon(this.getClass()
  109. .getClassLoader().getResource("uk/org/ownage/dmdirc/res/normal.png"));
  110. clearMessage();
  111. clearError();
  112. layoutBar();
  113. }
  114. /**
  115. * Sets the message in the status bar with a specified callback event
  116. * using the default timeout.
  117. *
  118. * @param newMessage Message to display
  119. * @param newNotifier status message notifier to be notified for events on
  120. * this message
  121. */
  122. public void setMessage(final String newMessage,
  123. final StatusMessageNotifier newNotifier) {
  124. final int timeout = Config.getOptionInt("statusBar", "messageDisplayLength", 5);
  125. setMessage(newMessage, newNotifier, timeout);
  126. }
  127. /**
  128. * Sets the message in the status bar with a specified callback event for
  129. * a specified time.
  130. *
  131. * @param newMessage Message to display
  132. * @param newNotifier status message notifier to be notified for events on
  133. * this message
  134. * @param timeout message timeout in seconds
  135. */
  136. public synchronized void setMessage(final String newMessage,
  137. final StatusMessageNotifier newNotifier, final int timeout) {
  138. messageLabel.setText(newMessage);
  139. messageNotifier = newNotifier;
  140. if (messageTimer != null && (System.currentTimeMillis()
  141. - messageTimer.scheduledExecutionTime()) <= 0) {
  142. messageTimer.cancel();
  143. }
  144. messageTimer = new TimerTask() {
  145. public void run() {
  146. clearMessage();
  147. }
  148. };
  149. new Timer().schedule(messageTimer,
  150. new Date(System.currentTimeMillis() + 250 + timeout * 1000L));
  151. }
  152. /**
  153. * sets the message in the status bar.
  154. *
  155. * @param newMessage Message to display
  156. */
  157. public void setMessage(final String newMessage) {
  158. setMessage(newMessage, null);
  159. }
  160. /**
  161. * Removes the message from the status bar.
  162. */
  163. public void clearMessage() {
  164. setMessage("Ready.");
  165. }
  166. /**
  167. * sets the icon in the status bar with a specified callback event.
  168. *
  169. * @param newIcon Icon to display
  170. * @param newNotifier status error notifier to be notified for events on
  171. * this error
  172. */
  173. public synchronized void setError(final ImageIcon newIcon,
  174. final StatusErrorNotifier newNotifier) {
  175. addToHistory(newIcon, newNotifier);
  176. iconLabel.setIcon(newIcon);
  177. errorNotifier = newNotifier;
  178. if (errorTimer != null && (System.currentTimeMillis()
  179. - errorTimer.scheduledExecutionTime()) <= 0) {
  180. errorTimer.cancel();
  181. }
  182. final int displayLength = Config.getOptionInt("statusBar", "errorDisplayLength", 10000);
  183. errorTimer = new TimerTask() {
  184. public void run() {
  185. clearError();
  186. }
  187. };
  188. new Timer().schedule(errorTimer,
  189. new Date(System.currentTimeMillis() + 250 + displayLength));
  190. }
  191. /**
  192. * sets the icon in the status bar.
  193. *
  194. * @param newIcon Icon to display
  195. */
  196. public void setError(final ImageIcon newIcon) {
  197. setError(newIcon, null);
  198. }
  199. /**
  200. * Removes the error state from the status bar.
  201. */
  202. public void clearError() {
  203. setError(normalIcon);
  204. }
  205. /**
  206. * Invoked when a mouse button has been pressed on a component.
  207. *
  208. * @param mouseEvent mouse event
  209. */
  210. public void mousePressed(final MouseEvent mouseEvent) {
  211. processMouseEvent(mouseEvent);
  212. }
  213. /**
  214. * Invoked when a mouse button has been released on a component.
  215. *
  216. * @param mouseEvent mouse event
  217. */
  218. public void mouseReleased(final MouseEvent mouseEvent) {
  219. processMouseEvent(mouseEvent);
  220. }
  221. /**
  222. * Invoked when the mouse enters a component.
  223. *
  224. * @param mouseEvent mouse event
  225. */
  226. public void mouseEntered(final MouseEvent mouseEvent) {
  227. //Ignore.
  228. }
  229. /**
  230. * Invoked when the mouse exits a component.
  231. *
  232. * @param mouseEvent mouse event
  233. */
  234. public void mouseExited(final MouseEvent mouseEvent) {
  235. //Ignore.
  236. }
  237. /**
  238. * Invoked when the mouse button has been clicked (pressed and released)
  239. * on a component.
  240. *
  241. * @param mouseEvent mouse event
  242. */
  243. public void mouseClicked(final MouseEvent mouseEvent) {
  244. if (mouseEvent.getSource() == messageLabel && messageNotifier != null) {
  245. messageNotifier.clickReceived(mouseEvent);
  246. } else if (mouseEvent.getSource() == iconLabel && errorNotifier != null) {
  247. errorNotifier.clickReceived();
  248. }
  249. processMouseEvent(mouseEvent);
  250. }
  251. /**
  252. * Processes every mouse button event to check for a popup trigger.
  253. * @param e mouse event
  254. */
  255. public void processMouseEvent(final MouseEvent e) {
  256. if (e.isPopupTrigger() && e.getSource() == iconLabel) {
  257. final Point point = this.getMousePosition();
  258. popup.show(this, (int) point.getX(), (int) point.getY());
  259. } else {
  260. super.processMouseEvent(e);
  261. }
  262. }
  263. /**
  264. * Adds the error to the history.
  265. * @param icon error icon
  266. * @param notifier error notifier
  267. */
  268. private void addToHistory(final Icon icon, final StatusErrorNotifier notifier) {
  269. if (icon != null && notifier != null) {
  270. JMenuItem mi;
  271. final Error error = new Error(icon, notifier);
  272. final int errorHistory = Config.getOptionInt("statusBar", "errorHistory", 10);
  273. errors.add(error);
  274. popup.removeAll();
  275. while (errors.size() >= errorHistory) {
  276. errors.remove(0);
  277. }
  278. for (Error thisError : errors) {
  279. mi = new JMenuItem(thisError.getDate().toString(), icon);
  280. mi.addActionListener(this);
  281. mi.setActionCommand(String.valueOf(errors.indexOf(thisError)));
  282. popup.add(mi);
  283. }
  284. popup.addSeparator();
  285. popup.add(clearErrors);
  286. }
  287. }
  288. /**
  289. * Shows the error from the history. {@inheritDoc}
  290. */
  291. public void actionPerformed(final ActionEvent e) {
  292. if ("Clear".equals(e.getActionCommand())) {
  293. errors.clear();
  294. popup.removeAll();
  295. popup.add(noErrors);
  296. clearError();
  297. } else if ("None".equals(e.getActionCommand())) {
  298. //Ignore
  299. } else {
  300. errors.get(Integer.valueOf(e.getActionCommand())).getNotifier().clickReceived();
  301. }
  302. }
  303. /**
  304. * Adds a component to the status bar.
  305. *
  306. * @param component component to add
  307. */
  308. public void addComponent(final Component component) {
  309. add(component, this.getComponentCount() - 1);
  310. layoutBar();
  311. }
  312. /**
  313. * Removes a component to the status bar.
  314. *
  315. * @param component component to add
  316. */
  317. public void removeComponent(final Component component) {
  318. remove(component);
  319. layoutBar();
  320. }
  321. /** Layout the status bar. */
  322. private void layoutBar() {
  323. this.setVisible(false);
  324. final SpringLayout layout = (SpringLayout) this.getLayout();
  325. final int numComponents = this.getComponentCount() - 1;
  326. SpringLayout.Constraints constraints;
  327. layout.putConstraint(SpringLayout.WEST, getComponent(0),
  328. Spring.constant(0), SpringLayout.WEST, this);
  329. layout.putConstraint(SpringLayout.EAST, getComponent(0),
  330. Spring.constant(-SMALL_BORDER), SpringLayout.WEST, getComponent(1));
  331. constraints = layout.getConstraints(getComponent(0));
  332. constraints.setHeight(Spring.constant(20));
  333. if (numComponents > 1) {
  334. for (int i = 1; i < numComponents; i++) {
  335. layout.putConstraint(SpringLayout.EAST, getComponent(i),
  336. Spring.constant(-SMALL_BORDER), SpringLayout.WEST,
  337. getComponent(i + 1));
  338. constraints = layout.getConstraints(getComponent(i));
  339. constraints.setHeight(Spring.constant(20));
  340. constraints.setWidth(constraints.getWidth());
  341. }
  342. }
  343. layout.putConstraint(SpringLayout.EAST, getComponent(numComponents),
  344. Spring.constant(0), SpringLayout.EAST, this);
  345. constraints = layout.getConstraints(getComponent(numComponents));
  346. constraints.setHeight(Spring.constant(20));
  347. constraints.setWidth(constraints.getWidth());
  348. this.setVisible(true);
  349. }
  350. }