PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 196 lines | 139 code | 20 blank | 37 comment | 28 complexity | 1156e17e64be45ece4de7e9d35b069a5 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 jEdit contributors
  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.Window;
  25. import java.awt.event.ActionEvent;
  26. import java.awt.event.ActionListener;
  27. import java.awt.event.MouseAdapter;
  28. import java.awt.event.MouseEvent;
  29. import java.util.HashMap;
  30. import java.util.Map;
  31. import javax.swing.JMenuItem;
  32. import javax.swing.JPopupMenu;
  33. import org.gjt.sp.jedit.EBComponent;
  34. import org.gjt.sp.jedit.EBMessage;
  35. import org.gjt.sp.jedit.EditServer;
  36. import org.gjt.sp.jedit.GUIUtilities;
  37. import org.gjt.sp.jedit.View;
  38. import org.gjt.sp.jedit.jEdit;
  39. import org.gjt.sp.jedit.msg.EditPaneUpdate;
  40. import org.gjt.sp.util.StringList;
  41. //}}}
  42. /**
  43. * @author Matthieu Casanova
  44. * @since jEdit 4.5pre1
  45. */
  46. public class JEditSwingTrayIcon extends JEditTrayIcon implements EBComponent
  47. {
  48. private boolean restore;
  49. private String userDir;
  50. private String[] args;
  51. //{{{ JEditSwingTrayIcon() constructor
  52. public JEditSwingTrayIcon()
  53. {
  54. super(GUIUtilities.getEditorIcon(), "jEdit");
  55. setImageAutoSize(true);
  56. JPopupMenu popup = new JPopupMenu();
  57. JMenuItem newViewItem = new JMenuItem(jEdit.getProperty("tray.newView.label"));
  58. JMenuItem newPlainViewItem = new JMenuItem(jEdit.getProperty("tray.newPlainView.label"));
  59. JMenuItem exitItem = new JMenuItem(jEdit.getProperty("tray.exit.label"));
  60. popup.add(newViewItem);
  61. popup.add(newPlainViewItem);
  62. popup.addSeparator();
  63. popup.add(exitItem);
  64. ActionListener actionListener = new MyActionListener(newViewItem, newPlainViewItem, exitItem);
  65. newViewItem.addActionListener(actionListener);
  66. newPlainViewItem.addActionListener(actionListener);
  67. exitItem.addActionListener(actionListener);
  68. setMenu(popup);
  69. addMouseListener(new MyMouseAdapter());
  70. } //}}}
  71. @Override
  72. /** Update tooltip to reflect the window titles currently available. */
  73. public void handleMessage(EBMessage message)
  74. {
  75. if (message instanceof EditPaneUpdate &&
  76. (((EditPaneUpdate)message).getWhat() == EditPaneUpdate.BUFFER_CHANGED)) {
  77. StringList sl = new StringList();
  78. for (View v: jEdit.getViews())
  79. sl.add(v.getTitle());
  80. setToolTip(sl.join(" | "));
  81. }
  82. }
  83. //{{{ setTrayIconArgs() method
  84. @Override
  85. void setTrayIconArgs(boolean restore, String userDir, String[] args)
  86. {
  87. this.restore = restore;
  88. this.userDir = userDir;
  89. this.args = args;
  90. } //}}}
  91. //{{{ MyMouseAdapter class
  92. private class MyMouseAdapter extends MouseAdapter
  93. {
  94. private final Map<Window,Boolean> windowState = new HashMap<Window, Boolean>();
  95. @Override
  96. public void mouseClicked(MouseEvent e)
  97. {
  98. if (e.getButton() != MouseEvent.BUTTON1)
  99. return;
  100. if (jEdit.getViewCount() == 0)
  101. {
  102. EditServer.handleClient(restore, true, false, userDir, args);
  103. }
  104. else
  105. {
  106. boolean newVisibilityState = !jEdit.getActiveView().isVisible();
  107. if (newVisibilityState)
  108. {
  109. for (Window window : Window.getOwnerlessWindows())
  110. {
  111. if (skipWindow(window))
  112. continue;
  113. Boolean previousState = windowState.get(window);
  114. if (previousState == null)
  115. window.setVisible(true);
  116. else if (previousState)
  117. window.setVisible(previousState);
  118. }
  119. windowState.clear();
  120. if (jEdit.getActiveView().getState() == Frame.ICONIFIED)
  121. jEdit.getActiveView().setState(Frame.NORMAL);
  122. jEdit.getActiveView().toFront();
  123. }
  124. else
  125. {
  126. for (Window window : Window.getOwnerlessWindows())
  127. {
  128. if (skipWindow(window))
  129. continue;
  130. windowState.put(window, window.isVisible());
  131. window.setVisible(false);
  132. }
  133. }
  134. }
  135. }
  136. //{{{ skipWindow method
  137. /**
  138. * Check if a window is not top level or systray icon
  139. * @param window the checked window
  140. * @return true if it is not toplevel or systray icon
  141. */
  142. private boolean skipWindow(Window window)
  143. {
  144. if (window.getClass().getName().contains("Tray"))
  145. return true;
  146. return false;
  147. } //}}}
  148. } //}}}
  149. //{{{ MyActionListener class
  150. private static class MyActionListener implements ActionListener
  151. {
  152. private final JMenuItem newViewItem;
  153. private final JMenuItem newPlainViewItem;
  154. private final JMenuItem exitItem;
  155. MyActionListener(JMenuItem newViewItem, JMenuItem newPlainViewItem, JMenuItem exitItem)
  156. {
  157. this.newViewItem = newViewItem;
  158. this.newPlainViewItem = newPlainViewItem;
  159. this.exitItem = exitItem;
  160. }
  161. @Override
  162. public void actionPerformed(ActionEvent e)
  163. {
  164. if (e.getSource() == newViewItem)
  165. {
  166. jEdit.newView(null);
  167. } else if (e.getSource() == newPlainViewItem)
  168. {
  169. jEdit.newView(null, null, true);
  170. } else if (e.getSource() == exitItem)
  171. {
  172. jEdit.exit(null, true);
  173. }
  174. }
  175. } //}}}
  176. }