PageRenderTime 25ms CodeModel.GetById 12ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/menu/RecentFilesProvider.java

#
Java | 153 lines | 88 code | 19 blank | 46 comment | 9 complexity | debe5349ea09e15c1ccd7bde0cfac548 MD5 | raw file
  1/*
  2 * RecentFilesProvider.java - Recent file list menu
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2000, 2003 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.menu;
 24
 25//{{{ Imports
 26import java.awt.event.*;
 27import javax.swing.*;
 28import javax.swing.event.*;
 29import java.util.*;
 30import org.gjt.sp.jedit.browser.FileCellRenderer;
 31import org.gjt.sp.jedit.*;
 32//}}}
 33
 34public class RecentFilesProvider implements DynamicMenuProvider
 35{
 36	//{{{ updateEveryTime() method
 37	public boolean updateEveryTime()
 38	{
 39		return false;
 40	} //}}}
 41
 42	//{{{ update() method
 43	public void update(JMenu menu)
 44	{
 45		final View view = GUIUtilities.getView(menu);
 46
 47		//{{{ ActionListener...
 48		ActionListener actionListener = new ActionListener()
 49		{
 50			public void actionPerformed(ActionEvent evt)
 51			{
 52				jEdit.openFile(view,evt.getActionCommand());
 53				view.getStatus().setMessage(null);
 54			}
 55		}; //}}}
 56
 57		//{{{ MouseListener...
 58		/*
 59		MouseListener mouseListener = new MouseAdapter()
 60		{
 61			public void mouseEntered(MouseEvent evt)
 62			{
 63				view.getStatus().setMessage(
 64					((JMenuItem)evt.getSource())
 65					.getActionCommand());
 66			}
 67
 68			public void mouseExited(MouseEvent evt)
 69			{
 70				view.getStatus().setMessage(null);
 71			}
 72		};
 73		*/
 74		//}}}
 75		
 76		//{{{ ChangeListener...
 77		ChangeListener changeListener = new ChangeListener()
 78		{
 79			public void stateChanged(ChangeEvent e)
 80			{
 81				JMenuItem menuItem = (JMenuItem) e.getSource();
 82				
 83				view.getStatus().setMessage(menuItem.isArmed()?menuItem.getActionCommand():null);
 84			} 
 85		}; //}}}
 86
 87		List recentVector = BufferHistory.getHistory();
 88
 89		if(recentVector.isEmpty())
 90		{
 91			JMenuItem menuItem = new JMenuItem(
 92				jEdit.getProperty("no-recent-files.label"));
 93			menuItem.setEnabled(false);
 94			menu.add(menuItem);
 95			return;
 96		}
 97
 98		Vector menuItems = new Vector();
 99
100		boolean sort = jEdit.getBooleanProperty("sortRecent");
101
102		int maxItems = jEdit.getIntegerProperty("menu.spillover",20);
103
104		Iterator iter = recentVector.iterator();
105		while(iter.hasNext())
106		{
107			String path = ((BufferHistory.Entry)iter.next()).path;
108			JMenuItem menuItem = new JMenuItem(MiscUtilities
109				.getFileName(path));
110			menuItem.setActionCommand(path);
111			menuItem.addActionListener(actionListener);
112//			menuItem.addMouseListener(mouseListener);
113			menuItem.addChangeListener(changeListener);
114			
115			menuItem.setIcon(FileCellRenderer.fileIcon);
116
117			if(sort)
118				menuItems.addElement(menuItem);
119			else
120			{
121				if(menu.getMenuComponentCount() >= maxItems
122					&& iter.hasNext())
123				{
124					JMenu newMenu = new JMenu(
125						jEdit.getProperty("common.more"));
126					menu.add(newMenu);
127					menu = newMenu;
128				}
129
130				menu.add(menuItem);
131			}
132		}
133
134		if(sort)
135		{
136			Collections.sort(menuItems,
137				new MiscUtilities.MenuItemCompare());
138			for(int i = 0; i < menuItems.size(); i++)
139			{
140				if(menu.getMenuComponentCount() >= maxItems
141					&& i != 0)
142				{
143					JMenu newMenu = new JMenu(
144						jEdit.getProperty("common.more"));
145					menu.add(newMenu);
146					menu = newMenu;
147				}
148
149				menu.add((JMenuItem)menuItems.elementAt(i));
150			}
151		}
152	} //}}}
153}