KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * DirectoryProvider.java - 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 javax.swing.*;
27 import java.awt.event.*;
28 import java.io.File JavaDoc;
29 import java.util.Arrays JavaDoc;
30
31 import org.gjt.sp.jedit.browser.*;
32 import org.gjt.sp.jedit.io.FileVFS;
33 import org.gjt.sp.jedit.*;
34 //}}}
35

36 public class DirectoryProvider implements DynamicMenuProvider
37 {
38     //{{{ DirectoryProvider constructor
39
public DirectoryProvider(String JavaDoc dir)
40     {
41         this.dir = dir;
42     } //}}}
43

44     //{{{ updateEveryTime() method
45
public boolean updateEveryTime()
46     {
47         return true;
48     } //}}}
49

50     //{{{ update() method
51
public void update(JMenu menu)
52     {
53         final View view = GUIUtilities.getView(menu);
54
55         final String JavaDoc path;
56         if(dir == null)
57         {
58             path = view.getBuffer().getDirectory();
59         }
60         else
61             path = dir;
62
63         JMenuItem mi = new JMenuItem(path + ":");
64         mi.setActionCommand(path);
65         mi.setIcon(FileCellRenderer.openDirIcon);
66
67         //{{{ ActionListeners
68
ActionListener fileListener = new ActionListener()
69         {
70             public void actionPerformed(ActionEvent evt)
71             {
72                 jEdit.openFile(view,evt.getActionCommand());
73             }
74         };
75
76         ActionListener dirListener = new ActionListener()
77         {
78             public void actionPerformed(ActionEvent evt)
79             {
80                 VFSBrowser.browseDirectory(view,
81                     evt.getActionCommand());
82             }
83         }; //}}}
84

85         mi.addActionListener(dirListener);
86
87         menu.add(mi);
88         menu.addSeparator();
89
90         if(dir == null && !(view.getBuffer().getVFS() instanceof FileVFS))
91         {
92             mi = new JMenuItem(jEdit.getProperty(
93                 "directory.not-local"));
94             mi.setEnabled(false);
95             menu.add(mi);
96             return;
97         }
98
99         File JavaDoc directory = new File JavaDoc(path);
100
101         JMenu current = menu;
102
103         // for filtering out backups
104
String JavaDoc backupPrefix = jEdit.getProperty("backup.prefix");
105         String JavaDoc backupSuffix = jEdit.getProperty("backup.suffix");
106
107         File JavaDoc[] list = directory.listFiles();
108         if(list == null || list.length == 0)
109         {
110             mi = new JMenuItem(jEdit.getProperty(
111                 "directory.no-files"));
112             mi.setEnabled(false);
113             menu.add(mi);
114         }
115         else
116         {
117             int maxItems = jEdit.getIntegerProperty("menu.spillover",20);
118
119             Arrays.sort(list,
120                 new MiscUtilities.StringICaseCompare());
121             for(int i = 0; i < list.length; i++)
122             {
123                 File JavaDoc file = list[i];
124
125                 String JavaDoc name = file.getName();
126
127                 // skip marker files
128
if(name.endsWith(".marks"))
129                     continue;
130
131                 // skip autosave files
132
if(name.startsWith("#") && name.endsWith("#"))
133                     continue;
134
135                 // skip backup files
136
if((backupPrefix.length() != 0
137                     && name.startsWith(backupPrefix))
138                     || (backupSuffix.length() != 0
139                     && name.endsWith(backupSuffix)))
140                     continue;
141
142                 // skip directories
143
//if(file.isDirectory())
144
// continue;
145

146                 mi = new JMenuItem(name);
147                 mi.setActionCommand(file.getPath());
148                 mi.addActionListener(file.isDirectory()
149                     ? dirListener
150                     : fileListener);
151                 mi.setIcon(file.isDirectory()
152                     ? FileCellRenderer.dirIcon
153                     : FileCellRenderer.fileIcon);
154
155                 if(current.getItemCount() >= maxItems && i != list.length - 1)
156                 {
157                     //current.addSeparator();
158
JMenu newCurrent = new JMenu(
159                         jEdit.getProperty(
160                         "common.more"));
161                     current.add(newCurrent);
162                     current = newCurrent;
163                 }
164                 current.add(mi);
165             }
166         }
167     } //}}}
168

169     //{{{ Private members
170
private String JavaDoc dir;
171     //}}}
172
}
173
Popular Tags