001: /*
002: * RecentDirectoriesProvider.java - Recent directory list menu
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2000, 2003 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.menu;
024:
025: //{{{ Imports
026: import javax.swing.*;
027: import java.awt.event.*;
028: import java.util.Vector;
029: import java.util.Collections;
030:
031: import org.gjt.sp.jedit.browser.*;
032: import org.gjt.sp.jedit.gui.HistoryModel;
033: import org.gjt.sp.jedit.*;
034:
035: //}}}
036:
037: public class RecentDirectoriesProvider implements DynamicMenuProvider {
038: //{{{ updateEveryTime() method
039: public boolean updateEveryTime() {
040: return true;
041: } //}}}
042:
043: //{{{ update() method
044: public void update(JMenu menu) {
045: final View view = GUIUtilities.getView(menu);
046:
047: //{{{ ActionListener...
048: ActionListener actionListener = new ActionListener() {
049: public void actionPerformed(ActionEvent evt) {
050: VFSBrowser
051: .browseDirectory(view, evt.getActionCommand());
052:
053: view.getStatus().setMessage(null);
054: }
055: }; //}}}
056:
057: //{{{ MouseListener...
058: MouseListener mouseListener = new MouseAdapter() {
059: public void mouseEntered(MouseEvent evt) {
060: view.getStatus().setMessage(
061: ((JMenuItem) evt.getSource())
062: .getActionCommand());
063: }
064:
065: public void mouseExited(MouseEvent evt) {
066: view.getStatus().setMessage(null);
067: }
068: }; //}}}
069:
070: HistoryModel model = HistoryModel.getModel("vfs.browser.path");
071: if (model.getSize() == 0) {
072: JMenuItem menuItem = new JMenuItem(jEdit
073: .getProperty("no-recent-dirs.label"));
074: menuItem.setEnabled(false);
075: menu.add(menuItem);
076: return;
077: }
078:
079: boolean sort = jEdit.getBooleanProperty("sortRecent");
080:
081: int maxItems = jEdit.getIntegerProperty("menu.spillover", 20);
082:
083: Vector menuItems = new Vector();
084:
085: for (int i = 0; i < model.getSize(); i++) {
086: String path = model.getItem(i);
087: JMenuItem menuItem = new JMenuItem(MiscUtilities
088: .getFileName(path));
089: menuItem.setActionCommand(path);
090: menuItem.addActionListener(actionListener);
091: menuItem.addMouseListener(mouseListener);
092: menuItem.setIcon(FileCellRenderer.dirIcon);
093:
094: if (sort)
095: menuItems.addElement(menuItem);
096: else {
097: if (menu.getMenuComponentCount() >= maxItems
098: && i != model.getSize() - 1) {
099: JMenu newMenu = new JMenu(jEdit
100: .getProperty("common.more"));
101: menu.add(newMenu);
102: menu = newMenu;
103: }
104:
105: menu.add(menuItem);
106: }
107: }
108:
109: if (sort) {
110: Collections.sort(menuItems,
111: new MiscUtilities.MenuItemCompare());
112: for (int i = 0; i < menuItems.size(); i++) {
113: if (menu.getMenuComponentCount() >= maxItems && i != 0) {
114: JMenu newMenu = new JMenu(jEdit
115: .getProperty("common.more"));
116: menu.add(newMenu);
117: menu = newMenu;
118: }
119:
120: menu.add((JMenuItem) menuItems.elementAt(i));
121: }
122: }
123: } //}}}
124: }
|