PageRenderTime 147ms CodeModel.GetById 133ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/gui/CurrentDirectoryMenu.java

#
Java | 152 lines | 99 code | 22 blank | 31 comment | 21 complexity | 509672eff97ea99e59a06ff030eafb04 MD5 | raw file
  1/*
  2 * CurrentDirectoryMenu.java - File list menu
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2000, 2001 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.gui;
 24
 25import javax.swing.*;
 26import java.awt.event.*;
 27import java.io.File;
 28import org.gjt.sp.jedit.browser.*;
 29import org.gjt.sp.jedit.io.VFSManager;
 30import org.gjt.sp.jedit.*;
 31
 32public class CurrentDirectoryMenu extends EnhancedMenu
 33{
 34	//{{{ CurrentDirectoryMenu constructor
 35	public CurrentDirectoryMenu()
 36	{
 37		super("current-directory");
 38	} //}}}
 39
 40	//{{{ setPopupMenuVisible() method
 41	public void setPopupMenuVisible(boolean b)
 42	{
 43		if(b)
 44		{
 45			final View view = GUIUtilities.getView(this);
 46
 47			if(getMenuComponentCount() != 0)
 48				removeAll();
 49
 50			final String path = MiscUtilities.getParentOfPath(
 51				view.getBuffer().getPath());
 52			JMenuItem mi = new JMenuItem(path);
 53			mi.setIcon(FileCellRenderer.openDirIcon);
 54
 55			//{{{ Directory action listener...
 56			mi.addActionListener(new ActionListener()
 57			{
 58				public void actionPerformed(ActionEvent evt)
 59				{
 60					VFSBrowser.browseDirectory(view,path);
 61				}
 62			}); //}}}
 63
 64			add(mi);
 65			addSeparator();
 66
 67			if(view.getBuffer().getFile() == null)
 68			{
 69				mi = new JMenuItem(jEdit.getProperty(
 70					"current-directory.not-local"));
 71				mi.setEnabled(false);
 72				add(mi);
 73				super.setPopupMenuVisible(b);
 74				return;
 75			}
 76
 77			File dir = new File(path);
 78
 79			JMenu current = this;
 80
 81			//{{{ ActionListener class
 82			ActionListener listener = new ActionListener()
 83			{
 84				public void actionPerformed(ActionEvent evt)
 85				{
 86					jEdit.openFile(view,evt.getActionCommand());
 87				}
 88			}; //}}}
 89
 90			// for filtering out backups
 91			String backupPrefix = jEdit.getProperty("backup.prefix");
 92			String backupSuffix = jEdit.getProperty("backup.suffix");
 93
 94			String[] list = dir.list();
 95			if(list == null || list.length == 0)
 96			{
 97				mi = new JMenuItem(jEdit.getProperty(
 98					"current-directory.no-files"));
 99				mi.setEnabled(false);
100				add(mi);
101			}
102			else
103			{
104				MiscUtilities.quicksort(list,
105					new MiscUtilities.StringICaseCompare());
106				for(int i = 0; i < list.length; i++)
107				{
108					String name = list[i];
109
110					// skip marker files
111					if(name.endsWith(".marks"))
112						continue;
113
114					// skip autosave files
115					if(name.startsWith("#") && name.endsWith("#"))
116						continue;
117
118					// skip backup files
119					if((backupPrefix.length() != 0
120						&& name.startsWith(backupPrefix))
121						|| (backupSuffix.length() != 0
122						&& name.endsWith(backupSuffix)))
123						continue;
124
125					// skip directories
126					File file = new File(path,name);
127					if(file.isDirectory())
128						continue;
129
130					mi = new JMenuItem(name);
131					mi.setActionCommand(file.getPath());
132					mi.addActionListener(listener);
133					mi.setIcon(FileCellRenderer.fileIcon);
134
135					if(current.getItemCount() >= 20)
136					{
137						//current.addSeparator();
138						JMenu newCurrent = new JMenu(
139							jEdit.getProperty(
140							"common.more"));
141						current.add(newCurrent);
142						current = newCurrent;
143					}
144					current.add(mi);
145				}
146			}
147
148		}
149
150		super.setPopupMenuVisible(b);
151	} //}}}
152}