PageRenderTime 99ms CodeModel.GetById 87ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/menu/RecentDirectoriesProvider.java

#
Java | 136 lines | 92 code | 17 blank | 27 comment | 12 complexity | 2abe72f8a4d6705f8b4ad0135cbf26f1 MD5 | raw file
  1/*
  2 * RecentDirectoriesProvider.java - Recent directory 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 javax.swing.event.*;
 27import javax.swing.*;
 28import java.awt.event.*;
 29import java.util.Vector;
 30import org.gjt.sp.jedit.browser.*;
 31import org.gjt.sp.jedit.gui.HistoryModel;
 32import org.gjt.sp.jedit.*;
 33//}}}
 34
 35public class RecentDirectoriesProvider implements DynamicMenuProvider
 36{
 37	//{{{ updateEveryTime() method
 38	public boolean updateEveryTime()
 39	{
 40		return true;
 41	} //}}}
 42
 43	//{{{ update() method
 44	public void update(JMenu menu)
 45	{
 46		final View view = GUIUtilities.getView(menu);
 47
 48		//{{{ ActionListener...
 49		ActionListener actionListener = new ActionListener()
 50		{
 51			public void actionPerformed(ActionEvent evt)
 52			{
 53				VFSBrowser.browseDirectory(view,evt.getActionCommand());
 54
 55				view.getStatus().setMessage(null);
 56			}
 57		}; //}}}
 58
 59		//{{{ MouseListener...
 60		MouseListener mouseListener = new MouseAdapter()
 61		{
 62			public void mouseEntered(MouseEvent evt)
 63			{
 64				view.getStatus().setMessage(
 65					((JMenuItem)evt.getSource())
 66					.getActionCommand());
 67			}
 68
 69			public void mouseExited(MouseEvent evt)
 70			{
 71				view.getStatus().setMessage(null);
 72			}
 73		}; //}}}
 74
 75		HistoryModel model = HistoryModel.getModel("vfs.browser.path");
 76		if(model.getSize() == 0)
 77		{
 78			JMenuItem menuItem = new JMenuItem(
 79				jEdit.getProperty("no-recent-dirs.label"));
 80			menuItem.setEnabled(false);
 81			menu.add(menuItem);
 82			return;
 83		}
 84
 85		boolean sort = jEdit.getBooleanProperty("sortRecent");
 86
 87		int maxItems = jEdit.getIntegerProperty("menu.spillover",20);
 88
 89		Vector menuItems = new Vector();
 90
 91		for(int i = 0; i < model.getSize(); i++)
 92		{
 93			String path = model.getItem(i);
 94			JMenuItem menuItem = new JMenuItem(MiscUtilities.getFileName(path));
 95			menuItem.setActionCommand(path);
 96			menuItem.addActionListener(actionListener);
 97			menuItem.addMouseListener(mouseListener);
 98			menuItem.setIcon(FileCellRenderer.dirIcon);
 99
100			if(sort)
101				menuItems.addElement(menuItem);
102			else
103			{
104				if(menu.getMenuComponentCount() >= maxItems
105					&& i != model.getSize() - 1)
106				{
107					JMenu newMenu = new JMenu(
108						jEdit.getProperty("common.more"));
109					menu.add(newMenu);
110					menu = newMenu;
111				}
112
113				menu.add(menuItem);
114			}
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				if(menu.getMenuComponentCount() >= maxItems
124					&& i != 0)
125				{
126					JMenu newMenu = new JMenu(
127						jEdit.getProperty("common.more"));
128					menu.add(newMenu);
129					menu = newMenu;
130				}
131
132				menu.add((JMenuItem)menuItems.elementAt(i));
133			}
134		}
135	} //}}}
136}