PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/tray/JTrayIcon.java

#
Java | 135 lines | 88 code | 15 blank | 32 comment | 9 complexity | b7098b41706bd1148c2df15d98ebd70e 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. * jEdit - Programmer's Text Editor
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright Š 2011 Matthieu Casanova
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package org.gjt.sp.jedit.gui.tray;
  22. //{{{ Imports
  23. import java.awt.Frame;
  24. import java.awt.Image;
  25. import java.awt.TrayIcon;
  26. import java.awt.event.MouseAdapter;
  27. import java.awt.event.MouseEvent;
  28. import java.awt.event.MouseListener;
  29. import javax.swing.JDialog;
  30. import javax.swing.JPopupMenu;
  31. import javax.swing.event.PopupMenuEvent;
  32. import javax.swing.event.PopupMenuListener;
  33. import org.gjt.sp.jedit.GUIUtilities;
  34. //}}}
  35. /**
  36. * A TrayIcon that accepts Swing JPopupMenu.
  37. * @author Matthieu Casanova
  38. * @since jEdit 4.5pre1
  39. */
  40. public class JTrayIcon extends TrayIcon
  41. {
  42. private JDialog parent;
  43. private JPopupMenu menu;
  44. private MouseListener mouseListener;
  45. private PopupMenuListener popupMenuListener;
  46. //{{{ JTrayIcon constructor
  47. public JTrayIcon(Image image, String tooltip)
  48. {
  49. super(image, tooltip, null);
  50. } //}}}
  51. //{{{ getMenu() method
  52. public JPopupMenu getMenu()
  53. {
  54. return menu;
  55. } //}}}
  56. //{{{ setMenu() method
  57. public void setMenu(JPopupMenu menu)
  58. {
  59. if (menu == null)
  60. {
  61. if (mouseListener != null)
  62. {
  63. removeMouseListener(mouseListener);
  64. mouseListener = null;
  65. }
  66. if (popupMenuListener != null)
  67. {
  68. this.menu.removePopupMenuListener(popupMenuListener);
  69. popupMenuListener = null;
  70. }
  71. parent = null;
  72. }
  73. else
  74. {
  75. parent = new JDialog((Frame) null);
  76. parent.setUndecorated(true);
  77. parent.setAlwaysOnTop(true);
  78. if (mouseListener == null)
  79. {
  80. mouseListener = new MyMouseListener();
  81. addMouseListener(mouseListener);
  82. }
  83. popupMenuListener = new MyPopupMenuListener();
  84. menu.addPopupMenuListener(popupMenuListener);
  85. }
  86. this.menu = menu;
  87. } //}}}
  88. //{{{ MyMouseListener class
  89. private class MyMouseListener extends MouseAdapter
  90. {
  91. @Override
  92. public void mouseClicked(MouseEvent e)
  93. {
  94. if (GUIUtilities.isPopupTrigger(e))
  95. {
  96. parent.setLocation(e.getX(), e.getY() - menu.getPreferredSize().height);
  97. parent.setVisible(true);
  98. menu.show(parent, 0, 0);
  99. }
  100. }
  101. } //}}}
  102. //{{{ MyPopupMenuListener class
  103. private class MyPopupMenuListener implements PopupMenuListener
  104. {
  105. @Override
  106. public void popupMenuWillBecomeVisible(PopupMenuEvent e)
  107. {
  108. }
  109. @Override
  110. public void popupMenuWillBecomeInvisible(PopupMenuEvent e)
  111. {
  112. parent.setVisible(false);
  113. }
  114. @Override
  115. public void popupMenuCanceled(PopupMenuEvent e)
  116. {
  117. parent.setVisible(false);
  118. }
  119. } //}}}
  120. }