KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 package org.gjt.sp.jedit.menu;
24
25 //{{{ Imports
26
import javax.swing.*;
27 import java.awt.event.*;
28 import java.util.Vector JavaDoc;
29 import java.util.Collections JavaDoc;
30
31 import org.gjt.sp.jedit.browser.*;
32 import org.gjt.sp.jedit.gui.HistoryModel;
33 import org.gjt.sp.jedit.*;
34 //}}}
35

36 public class RecentDirectoriesProvider implements DynamicMenuProvider
37 {
38     //{{{ updateEveryTime() method
39
public boolean updateEveryTime()
40     {
41         return true;
42     } //}}}
43

44     //{{{ update() method
45
public void update(JMenu menu)
46     {
47         final View view = GUIUtilities.getView(menu);
48
49         //{{{ ActionListener...
50
ActionListener actionListener = new ActionListener()
51         {
52             public void actionPerformed(ActionEvent evt)
53             {
54                 VFSBrowser.browseDirectory(view,evt.getActionCommand());
55
56                 view.getStatus().setMessage(null);
57             }
58         }; //}}}
59

60         //{{{ MouseListener...
61
MouseListener mouseListener = new MouseAdapter()
62         {
63             public void mouseEntered(MouseEvent evt)
64             {
65                 view.getStatus().setMessage(
66                     ((JMenuItem)evt.getSource())
67                     .getActionCommand());
68             }
69
70             public void mouseExited(MouseEvent evt)
71             {
72                 view.getStatus().setMessage(null);
73             }
74         }; //}}}
75

76         HistoryModel model = HistoryModel.getModel("vfs.browser.path");
77         if(model.getSize() == 0)
78         {
79             JMenuItem menuItem = new JMenuItem(
80                 jEdit.getProperty("no-recent-dirs.label"));
81             menuItem.setEnabled(false);
82             menu.add(menuItem);
83             return;
84         }
85
86         boolean sort = jEdit.getBooleanProperty("sortRecent");
87
88         int maxItems = jEdit.getIntegerProperty("menu.spillover",20);
89
90         Vector JavaDoc menuItems = new Vector JavaDoc();
91
92         for(int i = 0; i < model.getSize(); i++)
93         {
94             String JavaDoc path = model.getItem(i);
95             JMenuItem menuItem = new JMenuItem(MiscUtilities.getFileName(path));
96             menuItem.setActionCommand(path);
97             menuItem.addActionListener(actionListener);
98             menuItem.addMouseListener(mouseListener);
99             menuItem.setIcon(FileCellRenderer.dirIcon);
100
101             if(sort)
102                 menuItems.addElement(menuItem);
103             else
104             {
105                 if(menu.getMenuComponentCount() >= maxItems
106                     && i != model.getSize() - 1)
107                 {
108                     JMenu newMenu = new JMenu(
109                         jEdit.getProperty("common.more"));
110                     menu.add(newMenu);
111                     menu = newMenu;
112                 }
113
114                 menu.add(menuItem);
115             }
116         }
117
118         if(sort)
119         {
120             Collections.sort(menuItems,
121                 new MiscUtilities.MenuItemCompare());
122             for(int i = 0; i < menuItems.size(); i++)
123             {
124                 if(menu.getMenuComponentCount() >= maxItems
125                     && i != 0)
126                 {
127                     JMenu newMenu = new JMenu(
128                         jEdit.getProperty("common.more"));
129                     menu.add(newMenu);
130                     menu = newMenu;
131                 }
132
133                 menu.add((JMenuItem)menuItems.elementAt(i));
134             }
135         }
136     } //}}}
137
}
138
Popular Tags