PageRenderTime 47ms CodeModel.GetById 29ms app.highlight 15ms RepoModel.GetById 0ms app.codeStats 1ms

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