KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > menu > RecentFilesProvider


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 javax.swing.event.*;
29 import java.util.*;
30 import org.gjt.sp.jedit.browser.FileCellRenderer;
31 import org.gjt.sp.jedit.*;
32 //}}}
33

34 public 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     final List<JMenuItem> menuItems = new ArrayList<JMenuItem>();
99     final JTextField text = new JTextField();
100     text.setToolTipText(jEdit.getProperty("recent-files.textfield.tooltip"));
101     menu.add(text);
102         text.addKeyListener(new KeyAdapter()
103         {
104             public void keyReleased(KeyEvent e)
105             {
106                 String JavaDoc typedText = text.getText();
107                 for (JMenuItem tempMenuItem : menuItems)
108                 {
109                     if (typedText.length() == 0)
110                     {
111                         tempMenuItem.setEnabled(true);
112                     }
113                     else
114                     {
115                         String JavaDoc fileName = tempMenuItem.getText();
116                         boolean matchesStart = fileName.toLowerCase().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 iter = recentVector.iterator();
128         while(iter.hasNext())
129         {
130             String JavaDoc path = ((BufferHistory.Entry)iter.next()).path;
131             JMenuItem menuItem = new JMenuItem(MiscUtilities
132                 .getFileName(path));
133             menuItem.setActionCommand(path);
134             menuItem.addActionListener(actionListener);
135 // menuItem.addMouseListener(mouseListener);
136
menuItem.addChangeListener(changeListener);
137             
138             menuItem.setIcon(FileCellRenderer.fileIcon);
139
140             menuItems.add(menuItem);
141             if(!sort)
142             {
143                 if(menu.getMenuComponentCount() >= maxItems
144                     && iter.hasNext())
145                 {
146                     JMenu newMenu = new JMenu(
147                         jEdit.getProperty("common.more"));
148                     menu.add(newMenu);
149                     menu = newMenu;
150                 }
151
152                 menu.add(menuItem);
153             }
154         }
155
156         if(sort)
157         {
158             Collections.sort(menuItems,
159                 new MiscUtilities.MenuItemCompare());
160             for(int i = 0; i < menuItems.size(); i++)
161             {
162                 if(menu.getMenuComponentCount() >= maxItems
163                     && i != 0)
164                 {
165                     JMenu newMenu = new JMenu(
166                         jEdit.getProperty("common.more"));
167                     menu.add(newMenu);
168                     menu = newMenu;
169                 }
170
171                 menu.add((JMenuItem)menuItems.get(i));
172             }
173         }
174         JMenuItem menuItem = new JMenuItem(jEdit.getProperty("clear-recent-files.label"));
175         menuItem.addActionListener(new ActionListener()
176         {
177             public void actionPerformed(ActionEvent e)
178             {
179                 BufferHistory.clear();
180             }
181         });
182         menu.addSeparator();
183         menu.add(menuItem);
184     } //}}}
185
}
186
Popular Tags