001: /*
002: * DirectoryProvider.java - 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: import javax.swing.*;
027: import java.awt.event.*;
028: import java.io.File;
029: import java.util.Arrays;
030:
031: import org.gjt.sp.jedit.browser.*;
032: import org.gjt.sp.jedit.io.FileVFS;
033: import org.gjt.sp.jedit.*;
034:
035: //}}}
036:
037: public class DirectoryProvider implements DynamicMenuProvider {
038: //{{{ DirectoryProvider constructor
039: public DirectoryProvider(String dir) {
040: this .dir = dir;
041: } //}}}
042:
043: //{{{ updateEveryTime() method
044: public boolean updateEveryTime() {
045: return true;
046: } //}}}
047:
048: //{{{ update() method
049: public void update(JMenu menu) {
050: final View view = GUIUtilities.getView(menu);
051:
052: final String path;
053: if (dir == null) {
054: path = view.getBuffer().getDirectory();
055: } else
056: path = dir;
057:
058: JMenuItem mi = new JMenuItem(path + ":");
059: mi.setActionCommand(path);
060: mi.setIcon(FileCellRenderer.openDirIcon);
061:
062: //{{{ ActionListeners
063: ActionListener fileListener = new ActionListener() {
064: public void actionPerformed(ActionEvent evt) {
065: jEdit.openFile(view, evt.getActionCommand());
066: }
067: };
068:
069: ActionListener dirListener = new ActionListener() {
070: public void actionPerformed(ActionEvent evt) {
071: VFSBrowser
072: .browseDirectory(view, evt.getActionCommand());
073: }
074: }; //}}}
075:
076: mi.addActionListener(dirListener);
077:
078: menu.add(mi);
079: menu.addSeparator();
080:
081: if (dir == null
082: && !(view.getBuffer().getVFS() instanceof FileVFS)) {
083: mi = new JMenuItem(jEdit.getProperty("directory.not-local"));
084: mi.setEnabled(false);
085: menu.add(mi);
086: return;
087: }
088:
089: File directory = new File(path);
090:
091: JMenu current = menu;
092:
093: // for filtering out backups
094: String backupPrefix = jEdit.getProperty("backup.prefix");
095: String backupSuffix = jEdit.getProperty("backup.suffix");
096:
097: File[] list = directory.listFiles();
098: if (list == null || list.length == 0) {
099: mi = new JMenuItem(jEdit.getProperty("directory.no-files"));
100: mi.setEnabled(false);
101: menu.add(mi);
102: } else {
103: int maxItems = jEdit.getIntegerProperty("menu.spillover",
104: 20);
105:
106: Arrays.sort(list, new MiscUtilities.StringICaseCompare());
107: for (int i = 0; i < list.length; i++) {
108: File file = list[i];
109:
110: String name = file.getName();
111:
112: // skip marker files
113: if (name.endsWith(".marks"))
114: continue;
115:
116: // skip autosave files
117: if (name.startsWith("#") && name.endsWith("#"))
118: continue;
119:
120: // skip backup files
121: if ((backupPrefix.length() != 0 && name
122: .startsWith(backupPrefix))
123: || (backupSuffix.length() != 0 && name
124: .endsWith(backupSuffix)))
125: continue;
126:
127: // skip directories
128: //if(file.isDirectory())
129: // continue;
130:
131: mi = new JMenuItem(name);
132: mi.setActionCommand(file.getPath());
133: mi.addActionListener(file.isDirectory() ? dirListener
134: : fileListener);
135: mi
136: .setIcon(file.isDirectory() ? FileCellRenderer.dirIcon
137: : FileCellRenderer.fileIcon);
138:
139: if (current.getItemCount() >= maxItems
140: && i != list.length - 1) {
141: //current.addSeparator();
142: JMenu newCurrent = new JMenu(jEdit
143: .getProperty("common.more"));
144: current.add(newCurrent);
145: current = newCurrent;
146: }
147: current.add(mi);
148: }
149: }
150: } //}}}
151:
152: //{{{ Private members
153: private String dir;
154: //}}}
155: }
|