PageRenderTime 46ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/gui/RecentFilesMenu.java

#
Java | 131 lines | 82 code | 17 blank | 32 comment | 8 complexity | ba3f33c65ac0f49b8f4a9e93dbdbe788 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.event.*;
 27import javax.swing.*;
 28import java.awt.event.*;
 29import java.util.Vector;
 30import org.gjt.sp.jedit.browser.FileCellRenderer;
 31import org.gjt.sp.jedit.io.VFSManager;
 32import org.gjt.sp.jedit.io.VFS;
 33import org.gjt.sp.jedit.*;
 34//}}}
 35
 36public class RecentFilesMenu extends EnhancedMenu implements MenuListener
 37{
 38	//{{{ RecentFilesMenu constructor
 39	public RecentFilesMenu()
 40	{
 41		super("recent-files");
 42		addMenuListener(this);
 43	} //}}}
 44
 45	//{{{ menuSelected() method
 46	public void menuSelected(MenuEvent evt)
 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			return;
 85		}
 86
 87		Vector menuItems = new Vector();
 88		boolean sort = jEdit.getBooleanProperty("sortRecent");
 89
 90		/*
 91		 * While recentVector has 50 entries or so, we only display
 92		 * a few of those in the menu (otherwise it will be way too
 93		 * long)
 94		 */
 95		int recentFileCount = Math.min(recentVector.size(),
 96			jEdit.getIntegerProperty("history",25));
 97
 98		for(int i = recentVector.size() - 1;
 99			i >= recentVector.size() - recentFileCount;
100			i--)
101		{
102			String path = ((BufferHistory.Entry)recentVector
103				.elementAt(i)).path;
104			VFS vfs = VFSManager.getVFSForPath(path);
105			JMenuItem menuItem = new JMenuItem(vfs.getFileName(path));
106			menuItem.setActionCommand(path);
107			menuItem.addActionListener(actionListener);
108			menuItem.addMouseListener(mouseListener);
109			menuItem.setIcon(FileCellRenderer.fileIcon);
110
111			if(sort)
112				menuItems.addElement(menuItem);
113			else
114				add(menuItem);
115		}
116
117		if(sort)
118		{
119			MiscUtilities.quicksort(menuItems,
120				new MiscUtilities.MenuItemCompare());
121			for(int i = 0; i < menuItems.size(); i++)
122			{
123				add((JMenuItem)menuItems.elementAt(i));
124			}
125		}
126	} //}}}
127
128	public void menuDeselected(MenuEvent e) {}
129
130	public void menuCanceled(MenuEvent e) {}
131}