PageRenderTime 47ms CodeModel.GetById 37ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/gui/RecentFilesMenu.java

#
Java | 131 lines | 83 code | 16 blank | 32 comment | 9 complexity | fbde589b86e1a89523de03132cdd7143 MD5 | raw file
  1/*
  2 * RecentFilesMenu.java - Recent file list menu
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2000, 2001 Slava Pestov
  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 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.gui;
 24
 25//{{{ Imports
 26import javax.swing.*;
 27import java.awt.event.*;
 28import java.util.Vector;
 29import org.gjt.sp.jedit.browser.FileCellRenderer;
 30import org.gjt.sp.jedit.io.VFSManager;
 31import org.gjt.sp.jedit.io.VFS;
 32import org.gjt.sp.jedit.*;
 33//}}}
 34
 35public class RecentFilesMenu extends EnhancedMenu
 36{
 37	//{{{ RecentFilesMenu constructor
 38	public RecentFilesMenu()
 39	{
 40		super("recent-files");
 41	} //}}}
 42
 43	//{{{ setPopupMenuVisible() method
 44	public void setPopupMenuVisible(boolean b)
 45	{
 46		if(b)
 47		{
 48			final View view = GUIUtilities.getView(this);
 49
 50			if(getMenuComponentCount() != 0)
 51				removeAll();
 52
 53			//{{{ ActionListener...
 54			ActionListener actionListener = new ActionListener()
 55			{
 56				public void actionPerformed(ActionEvent evt)
 57				{
 58					jEdit.openFile(view,evt.getActionCommand());
 59					view.getStatus().setMessage(null);
 60				}
 61			}; //}}}
 62
 63			//{{{ MouseListener...
 64			MouseListener mouseListener = new MouseAdapter()
 65			{
 66				public void mouseEntered(MouseEvent evt)
 67				{
 68					view.getStatus().setMessage(
 69						((JMenuItem)evt.getSource())
 70						.getActionCommand());
 71				}
 72
 73				public void mouseExited(MouseEvent evt)
 74				{
 75					view.getStatus().setMessage(null);
 76				}
 77			}; //}}}
 78
 79			Vector recentVector = BufferHistory.getBufferHistory();
 80
 81			if(recentVector.size() == 0)
 82			{
 83				add(GUIUtilities.loadMenuItem("no-recent-files"));
 84				super.setPopupMenuVisible(b);
 85				return;
 86			}
 87
 88			Vector menuItems = new Vector();
 89			boolean sort = jEdit.getBooleanProperty("sortRecent");
 90
 91			/*
 92			 * While recentVector has 50 entries or so, we only display
 93			 * a few of those in the menu (otherwise it will be way too
 94			 * long)
 95			 */
 96			int recentFileCount = Math.min(recentVector.size(),
 97				jEdit.getIntegerProperty("history",25));
 98
 99			for(int i = recentVector.size() - 1;
100				i >= recentVector.size() - recentFileCount;
101				i--)
102			{
103				String path = ((BufferHistory.Entry)recentVector
104					.elementAt(i)).path;
105				VFS vfs = VFSManager.getVFSForPath(path);
106				JMenuItem menuItem = new JMenuItem(vfs.getFileName(path));
107				menuItem.setActionCommand(path);
108				menuItem.addActionListener(actionListener);
109				menuItem.addMouseListener(mouseListener);
110				menuItem.setIcon(FileCellRenderer.fileIcon);
111
112				if(sort)
113					menuItems.addElement(menuItem);
114				else
115					add(menuItem);
116			}
117
118			if(sort)
119			{
120				MiscUtilities.quicksort(menuItems,
121					new MiscUtilities.MenuItemCompare());
122				for(int i = 0; i < menuItems.size(); i++)
123				{
124					add((JMenuItem)menuItems.elementAt(i));
125				}
126			}
127		}
128
129		super.setPopupMenuVisible(b);
130	} //}}}
131}