001: /*
002: * RecentFilesProvider.java - Recent file 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:
027: import org.gjt.sp.jedit.*;
028: import org.gjt.sp.jedit.browser.FileCellRenderer;
029:
030: import javax.swing.*;
031: import javax.swing.event.ChangeEvent;
032: import javax.swing.event.ChangeListener;
033: import java.awt.event.ActionEvent;
034: import java.awt.event.ActionListener;
035: import java.awt.event.KeyAdapter;
036: import java.awt.event.KeyEvent;
037: import java.util.ArrayList;
038: import java.util.Collections;
039: import java.util.Iterator;
040: import java.util.List;
041:
042: //}}}
043:
044: public class RecentFilesProvider implements DynamicMenuProvider {
045: //{{{ updateEveryTime() method
046: public boolean updateEveryTime() {
047: return false;
048: } //}}}
049:
050: //{{{ update() method
051: public void update(JMenu menu) {
052: final View view = GUIUtilities.getView(menu);
053:
054: //{{{ ActionListener...
055: ActionListener actionListener = new ActionListener() {
056: public void actionPerformed(ActionEvent evt) {
057: jEdit.openFile(view, evt.getActionCommand());
058: view.getStatus().setMessage(null);
059: }
060: }; //}}}
061:
062: //{{{ MouseListener...
063: /*
064: MouseListener mouseListener = new MouseAdapter()
065: {
066: public void mouseEntered(MouseEvent evt)
067: {
068: view.getStatus().setMessage(
069: ((JMenuItem)evt.getSource())
070: .getActionCommand());
071: }
072:
073: public void mouseExited(MouseEvent evt)
074: {
075: view.getStatus().setMessage(null);
076: }
077: };
078: */
079: //}}}
080: //{{{ ChangeListener...
081: ChangeListener changeListener = new ChangeListener() {
082: public void stateChanged(ChangeEvent e) {
083: JMenuItem menuItem = (JMenuItem) e.getSource();
084:
085: view.getStatus().setMessage(
086: menuItem.isArmed() ? menuItem
087: .getActionCommand() : null);
088: }
089: }; //}}}
090:
091: List<BufferHistory.Entry> recentVector = BufferHistory
092: .getHistory();
093:
094: if (recentVector.isEmpty()) {
095: JMenuItem menuItem = new JMenuItem(jEdit
096: .getProperty("no-recent-files.label"));
097: menuItem.setEnabled(false);
098: menu.add(menuItem);
099: return;
100: }
101:
102: final List<JMenuItem> menuItems = new ArrayList<JMenuItem>();
103: final JTextField text = new JTextField();
104: text.setToolTipText(jEdit
105: .getProperty("recent-files.textfield.tooltip"));
106: menu.add(text);
107: text.addKeyListener(new KeyAdapter() {
108: public void keyReleased(KeyEvent e) {
109: String typedText = text.getText();
110: for (JMenuItem tempMenuItem : menuItems) {
111: if (typedText.length() == 0) {
112: tempMenuItem.setEnabled(true);
113: } else {
114: String fileName = tempMenuItem.getText();
115: boolean matchesStart = fileName.toLowerCase()
116: .startsWith(typedText.toLowerCase());
117: tempMenuItem.setEnabled(matchesStart);
118: }
119: }
120: }
121: });
122:
123: boolean sort = jEdit.getBooleanProperty("sortRecent");
124:
125: int maxItems = jEdit.getIntegerProperty("menu.spillover", 20);
126:
127: Iterator<BufferHistory.Entry> iter = recentVector.iterator();
128: while (iter.hasNext()) {
129: String path = iter.next().path;
130: JMenuItem menuItem = new JMenuItem(MiscUtilities
131: .getFileName(path));
132: menuItem.setActionCommand(path);
133: menuItem.addActionListener(actionListener);
134: // menuItem.addMouseListener(mouseListener);
135: menuItem.addChangeListener(changeListener);
136:
137: menuItem.setIcon(FileCellRenderer.fileIcon);
138:
139: menuItems.add(menuItem);
140: if (!sort) {
141: if (menu.getMenuComponentCount() >= maxItems
142: && iter.hasNext()) {
143: JMenu newMenu = new JMenu(jEdit
144: .getProperty("common.more"));
145: menu.add(newMenu);
146: menu = newMenu;
147: }
148:
149: menu.add(menuItem);
150: }
151: }
152:
153: if (sort) {
154: Collections.sort(menuItems,
155: new MiscUtilities.MenuItemCompare());
156: for (int i = 0; i < menuItems.size(); i++) {
157: if (menu.getMenuComponentCount() >= maxItems && i != 0) {
158: JMenu newMenu = new JMenu(jEdit
159: .getProperty("common.more"));
160: menu.add(newMenu);
161: menu = newMenu;
162: }
163:
164: menu.add(menuItems.get(i));
165: }
166: }
167: JMenuItem menuItem = new JMenuItem(jEdit
168: .getProperty("clear-recent-files.label"));
169: menuItem.addActionListener(new ActionListener() {
170: public void actionPerformed(ActionEvent e) {
171: BufferHistory.clear();
172: }
173: });
174: menu.addSeparator();
175: menu.add(menuItem);
176: } //}}}
177: }
|