PageRenderTime 106ms CodeModel.GetById 94ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/menu/DirectoryProvider.java

#
Java | 171 lines | 109 code | 25 blank | 37 comment | 22 complexity | 54a4eba24bb3baba8b8bb651f0a795a3 MD5 | raw file
  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
 23package org.gjt.sp.jedit.menu;
 24
 25//{{{ Imports
 26import javax.swing.event.*;
 27import javax.swing.*;
 28import java.awt.event.*;
 29import java.io.File;
 30import org.gjt.sp.jedit.browser.*;
 31import org.gjt.sp.jedit.io.FileVFS;
 32import org.gjt.sp.jedit.*;
 33//}}}
 34
 35public class DirectoryProvider implements DynamicMenuProvider
 36{
 37	//{{{ DirectoryProvider constructor
 38	public DirectoryProvider(String dir)
 39	{
 40		this.dir = dir;
 41	} //}}}
 42
 43	//{{{ updateEveryTime() method
 44	public boolean updateEveryTime()
 45	{
 46		return true;
 47	} //}}}
 48
 49	//{{{ update() method
 50	public void update(JMenu menu)
 51	{
 52		final View view = GUIUtilities.getView(menu);
 53
 54		final String path;
 55		if(dir == null)
 56		{
 57			path = view.getBuffer().getDirectory();
 58		}
 59		else
 60			path = dir;
 61
 62		JMenuItem mi = new JMenuItem(path + ":");
 63		mi.setActionCommand(path);
 64		mi.setIcon(FileCellRenderer.openDirIcon);
 65
 66		//{{{ ActionListeners
 67		ActionListener fileListener = new ActionListener()
 68		{
 69			public void actionPerformed(ActionEvent evt)
 70			{
 71				jEdit.openFile(view,evt.getActionCommand());
 72			}
 73		};
 74
 75		ActionListener dirListener = new ActionListener()
 76		{
 77			public void actionPerformed(ActionEvent evt)
 78			{
 79				VFSBrowser.browseDirectory(view,
 80					evt.getActionCommand());
 81			}
 82		}; //}}}
 83
 84		mi.addActionListener(dirListener);
 85
 86		menu.add(mi);
 87		menu.addSeparator();
 88
 89		if(dir == null && !(view.getBuffer().getVFS() instanceof FileVFS))
 90		{
 91			mi = new JMenuItem(jEdit.getProperty(
 92				"directory.not-local"));
 93			mi.setEnabled(false);
 94			menu.add(mi);
 95			return;
 96		}
 97
 98		File directory = new File(path);
 99
100		JMenu current = menu;
101
102		// for filtering out backups
103		String backupPrefix = jEdit.getProperty("backup.prefix");
104		String backupSuffix = jEdit.getProperty("backup.suffix");
105
106		File[] list = directory.listFiles();
107		if(list == null || list.length == 0)
108		{
109			mi = new JMenuItem(jEdit.getProperty(
110				"directory.no-files"));
111			mi.setEnabled(false);
112			menu.add(mi);
113		}
114		else
115		{
116			int maxItems = jEdit.getIntegerProperty("menu.spillover",20);
117
118			MiscUtilities.quicksort(list,
119				new MiscUtilities.StringICaseCompare());
120			for(int i = 0; i < list.length; i++)
121			{
122				File file = list[i];
123
124				String name = file.getName();
125
126				// skip marker files
127				if(name.endsWith(".marks"))
128					continue;
129
130				// skip autosave files
131				if(name.startsWith("#") && name.endsWith("#"))
132					continue;
133
134				// skip backup files
135				if((backupPrefix.length() != 0
136					&& name.startsWith(backupPrefix))
137					|| (backupSuffix.length() != 0
138					&& name.endsWith(backupSuffix)))
139					continue;
140
141				// skip directories
142				//if(file.isDirectory())
143				//	continue;
144
145				mi = new JMenuItem(name);
146				mi.setActionCommand(file.getPath());
147				mi.addActionListener(file.isDirectory()
148					? dirListener
149					: fileListener);
150				mi.setIcon(file.isDirectory()
151					? FileCellRenderer.dirIcon
152					: FileCellRenderer.fileIcon);
153
154				if(current.getItemCount() >= maxItems && i != list.length - 1)
155				{
156					//current.addSeparator();
157					JMenu newCurrent = new JMenu(
158						jEdit.getProperty(
159						"common.more"));
160					current.add(newCurrent);
161					current = newCurrent;
162				}
163				current.add(mi);
164			}
165		}
166	} //}}}
167
168	//{{{ Private members
169	private String dir;
170	//}}}
171}