Source code: org/gjt/sp/jedit/menu/RecentFilesProvider.java
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
23 package org.gjt.sp.jedit.menu;
24
25 //{{{ Imports
26 import java.awt.event.*;
27 import javax.swing.*;
28 import java.util.*;
29 import org.gjt.sp.jedit.browser.FileCellRenderer;
30 import org.gjt.sp.jedit.*;
31 //}}}
32
33 public class RecentFilesProvider implements DynamicMenuProvider
34 {
35 //{{{ updateEveryTime() method
36 public boolean updateEveryTime()
37 {
38 return false;
39 } //}}}
40
41 //{{{ update() method
42 public void update(JMenu menu)
43 {
44 final View view = GUIUtilities.getView(menu);
45
46 //{{{ ActionListener...
47 ActionListener actionListener = new ActionListener()
48 {
49 public void actionPerformed(ActionEvent evt)
50 {
51 jEdit.openFile(view,evt.getActionCommand());
52 view.getStatus().setMessage(null);
53 }
54 }; //}}}
55
56 //{{{ MouseListener...
57 MouseListener mouseListener = new MouseAdapter()
58 {
59 public void mouseEntered(MouseEvent evt)
60 {
61 view.getStatus().setMessage(
62 ((JMenuItem)evt.getSource())
63 .getActionCommand());
64 }
65
66 public void mouseExited(MouseEvent evt)
67 {
68 view.getStatus().setMessage(null);
69 }
70 }; //}}}
71
72 List recentVector = BufferHistory.getHistory();
73
74 if(recentVector.size() == 0)
75 {
76 JMenuItem menuItem = new JMenuItem(
77 jEdit.getProperty("no-recent-files.label"));
78 menuItem.setEnabled(false);
79 menu.add(menuItem);
80 return;
81 }
82
83 Vector menuItems = new Vector();
84
85 boolean sort = jEdit.getBooleanProperty("sortRecent");
86
87 int maxItems = jEdit.getIntegerProperty("menu.spillover",20);
88
89 Iterator iter = recentVector.iterator();
90 while(iter.hasNext())
91 {
92 String path = ((BufferHistory.Entry)iter.next()).path;
93 JMenuItem menuItem = new JMenuItem(MiscUtilities
94 .getFileName(path));
95 menuItem.setActionCommand(path);
96 menuItem.addActionListener(actionListener);
97 menuItem.addMouseListener(mouseListener);
98 menuItem.setIcon(FileCellRenderer.fileIcon);
99
100 if(sort)
101 menuItems.addElement(menuItem);
102 else
103 {
104 if(menu.getMenuComponentCount() >= maxItems
105 && iter.hasNext())
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 }