|
What this is
Other links
The source code
/*
* VFSBrowser.java - VFS browser
* :tabSize=8:indentSize=8:noTabs=false:
* :folding=explicit:collapseFolds=1:
*
* Copyright (C) 2000, 2003 Slava Pestov
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.gjt.sp.jedit.browser;
//{{{ Imports
import bsh.*;
import gnu.regexp.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.File;
import java.util.*;
import org.gjt.sp.jedit.io.*;
import org.gjt.sp.jedit.gui.*;
import org.gjt.sp.jedit.msg.*;
import org.gjt.sp.jedit.search.*;
import org.gjt.sp.jedit.*;
import org.gjt.sp.util.Log;
//}}}
/**
* The main class of the VFS browser.
* @author Slava Pestov
* @version $Id: VFSBrowser.java,v 1.110 2004/07/14 05:18:50 spestov Exp $
*/
public class VFSBrowser extends JPanel implements EBComponent, DefaultFocusComponent
{
public static final String NAME = "vfs.browser";
//{{{ Browser types
/**
* Open file dialog mode. Equals JFileChooser.OPEN_DIALOG for
* backwards compatibility.
*/
public static final int OPEN_DIALOG = 0;
/**
* Save file dialog mode. Equals JFileChooser.SAVE_DIALOG for
* backwards compatibility.
*/
public static final int SAVE_DIALOG = 1;
/**
* Choose directory dialog mode.
*/
public static final int BROWSER_DIALOG = 4;
/**
* Choose directory dialog mode.
*/
public static final int CHOOSE_DIRECTORY_DIALOG = 3;
/**
* Stand-alone browser mode.
*/
public static final int BROWSER = 2;
//}}}
//{{{ browseDirectoryInNewWindow() method
/**
* Opens the specified directory in a new, floating, file system browser.
* @param view The view
* @param path The directory's path
* @since jEdit 4.1pre2
*/
public static void browseDirectoryInNewWindow(View view, String path)
{
DockableWindowManager wm = view.getDockableWindowManager();
if(path != null)
{
// this is such a bad way of doing it, but oh well...
jEdit.setTemporaryProperty("vfs.browser.path.tmp",path);
}
wm.floatDockableWindow("vfs.browser");
jEdit.unsetProperty("vfs.browser.path.tmp");
} //}}}
//{{{ browseDirectory() method
/**
* Opens the specified directory in a file system browser.
* @param view The view
* @param path The directory's path
* @since jEdit 4.0pre3
*/
public static void browseDirectory(View view, String path)
{
DockableWindowManager wm = view.getDockableWindowManager();
VFSBrowser browser = (VFSBrowser)wm.getDockable(NAME);
if(browser != null)
{
wm.showDockableWindow(NAME);
browser.setDirectory(path);
}
else
{
if(path != null)
{
// this is such a bad way of doing it, but oh well...
jEdit.setTemporaryProperty("vfs.browser.path.tmp",path);
}
wm.addDockableWindow("vfs.browser");
jEdit.unsetProperty("vfs.browser.path.tmp");
}
} //}}}
//{{{ getActionContext() method
/**
* Returns the browser action context.
* @since jEdit 4.2pre1
*/
public static ActionContext getActionContext()
{
return actionContext;
} //}}}
//{{{ VFSBrowser constructor
/**
* Creates a new VFS browser.
* @param view The view to open buffers in by default
*/
public VFSBrowser(View view, String position)
{
this(view,null,BROWSER,true,position);
} //}}}
//{{{ VFSBrowser constructor
/**
* Creates a new VFS browser.
* @param view The view to open buffers in by default
* @param path The path to display
* @param mode The browser mode
* @param multipleSelection True if multiple selection should be allowed
* @param position Where the browser is located
* @since jEdit 4.2pre1
*/
public VFSBrowser(View view, String path, int mode,
boolean multipleSelection, String position)
{
super(new BorderLayout());
listenerList = new EventListenerList();
this.mode = mode;
this.multipleSelection = multipleSelection;
this.view = view;
currentEncoding = jEdit.getProperty("buffer.encoding",
System.getProperty("file.encoding"));
autoDetectEncoding = jEdit.getBooleanProperty(
"buffer.encodingAutodetect");
ActionHandler actionHandler = new ActionHandler();
Box topBox = new Box(BoxLayout.Y_AXIS);
horizontalLayout = (mode != BROWSER
|| DockableWindowManager.TOP.equals(position)
|| DockableWindowManager.BOTTOM.equals(position));
toolbarBox = new Box(horizontalLayout
? BoxLayout.X_AXIS
: BoxLayout.Y_AXIS);
topBox.add(toolbarBox);
GridBagLayout layout = new GridBagLayout();
JPanel pathAndFilterPanel = new JPanel(layout);
GridBagConstraints cons = new GridBagConstraints();
cons.gridwidth = cons.gridheight = 1;
cons.gridx = cons.gridy = 0;
cons.fill = GridBagConstraints.BOTH;
cons.anchor = GridBagConstraints.EAST;
JLabel label = new JLabel(jEdit.getProperty("vfs.browser.path"),
SwingConstants.RIGHT);
label.setBorder(new EmptyBorder(0,0,0,12));
layout.setConstraints(label,cons);
pathAndFilterPanel.add(label);
pathField = new HistoryTextField("vfs.browser.path");
pathField.setInstantPopups(true);
pathField.setEnterAddsToHistory(false);
pathField.setSelectAllOnFocus(true);
// because its preferred size can be quite wide, we
// don't want it to make the browser way too big,
// so set the preferred width to 0.
Dimension prefSize = pathField.getPreferredSize();
prefSize.width = 0;
pathField.setPreferredSize(prefSize);
pathField.addActionListener(actionHandler);
cons.gridx = 1;
cons.weightx = 1.0f;
layout.setConstraints(pathField,cons);
pathAndFilterPanel.add(pathField);
filterCheckbox = new JCheckBox(jEdit.getProperty("vfs.browser.filter"));
filterCheckbox.setMargin(new Insets(0,0,0,0));
filterCheckbox.setRequestFocusEnabled(false);
filterCheckbox.setBorder(new EmptyBorder(0,0,0,12));
filterCheckbox.setSelected(jEdit.getBooleanProperty(
"vfs.browser.filter-enabled"));
filterCheckbox.addActionListener(actionHandler);
if(mode != CHOOSE_DIRECTORY_DIALOG)
{
cons.gridx = 0;
cons.weightx = 0.0f;
cons.gridy = 1;
layout.setConstraints(filterCheckbox,cons);
pathAndFilterPanel.add(filterCheckbox);
}
filterField = new HistoryTextField("vfs.browser.filter");
filterField.setInstantPopups(true);
filterField.setSelectAllOnFocus(true);
filterField.addActionListener(actionHandler);
if(mode != CHOOSE_DIRECTORY_DIALOG)
{
cons.gridx = 1;
cons.weightx = 1.0f;
layout.setConstraints(filterField,cons);
pathAndFilterPanel.add(filterField);
}
topBox.add(pathAndFilterPanel);
add(BorderLayout.NORTH,topBox);
add(BorderLayout.CENTER,browserView = new BrowserView(this));
propertiesChanged();
String filter;
if(mode == BROWSER || !jEdit.getBooleanProperty(
"vfs.browser.currentBufferFilter"))
{
filter = jEdit.getProperty("vfs.browser.last-filter");
if(filter == null)
filter = jEdit.getProperty("vfs.browser.default-filter");
}
else
{
String ext = MiscUtilities.getFileExtension(
view.getBuffer().getName());
if(ext.length() == 0)
filter = jEdit.getProperty("vfs.browser.default-filter");
else
filter = "*" + ext;
}
filterField.setText(filter);
filterField.addCurrentToHistory();
updateFilterEnabled();
// see VFSBrowser.browseDirectory()
if(path == null)
path = jEdit.getProperty("vfs.browser.path.tmp");
if(path == null || path.length() == 0)
{
String userHome = System.getProperty("user.home");
String defaultPath = jEdit.getProperty("vfs.browser.defaultPath");
if(defaultPath.equals("home"))
path = userHome;
else if(defaultPath.equals("working"))
path = System.getProperty("user.dir");
else if(defaultPath.equals("buffer"))
{
if(view != null)
{
Buffer buffer = view.getBuffer();
path = buffer.getDirectory();
}
else
path = userHome;
}
else if(defaultPath.equals("last"))
{
HistoryModel pathModel = HistoryModel.getModel("vfs.browser.path");
if(pathModel.getSize() == 0)
path = "~";
else
path = pathModel.getItem(0);
}
else if(defaultPath.equals("favorites"))
path = "favorites:";
else
{
// unknown value??!!!
path = userHome;
}
}
final String _path = path;
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
setDirectory(_path);
}
});
} //}}}
//{{{ focusOnDefaultComponent() method
public void focusOnDefaultComponent()
{
browserView.focusOnFileView();
} //}}}
//{{{ addNotify() method
public void addNotify()
{
super.addNotify();
EditBus.addToBus(this);
} //}}}
//{{{ removeNotify() method
public void removeNotify()
{
super.removeNotify();
jEdit.setBooleanProperty("vfs.browser.filter-enabled",
filterCheckbox.isSelected());
if(mode == BROWSER || !jEdit.getBooleanProperty(
"vfs.browser.currentBufferFilter"))
{
jEdit.setProperty("vfs.browser.last-filter",
filterField.getText());
}
EditBus.removeFromBus(this);
} //}}}
//{{{ handleMessage() method
public void handleMessage(EBMessage msg)
{
if(msg instanceof PropertiesChanged)
propertiesChanged();
else if(msg instanceof BufferUpdate)
{
BufferUpdate bmsg = (BufferUpdate)msg;
if(bmsg.getWhat() == BufferUpdate.CREATED
|| bmsg.getWhat() == BufferUpdate.CLOSED)
browserView.updateFileView();
// hacked BufferIORequest to send VFSUpdates in case
// two stage save is off now...
/* else if(bmsg.getWhat() == BufferUpdate.SAVED)
{
maybeReloadDirectory(MiscUtilities.getParentOfPath(
bmsg.getBuffer().getPath()));
} */
}
else if(msg instanceof PluginUpdate)
{
PluginUpdate pmsg = (PluginUpdate)msg;
if(pmsg.getWhat() == PluginUpdate.LOADED
|| pmsg.getWhat() == PluginUpdate.UNLOADED)
{
plugins.updatePopupMenu();
}
}
else if(msg instanceof VFSUpdate)
{
maybeReloadDirectory(((VFSUpdate)msg).getPath());
}
} //}}}
//{{{ getView() method
public View getView()
{
return view;
} //}}}
//{{{ getMode() method
public int getMode()
{
return mode;
} //}}}
//{{{ isMultipleSelectionEnabled() method
public boolean isMultipleSelectionEnabled()
{
return multipleSelection;
} //}}}
//{{{ isHorizontalLayout() method
public boolean isHorizontalLayout()
{
return horizontalLayout;
} //}}}
//{{{ getShowHiddenFiles() method
public boolean getShowHiddenFiles()
{
return showHiddenFiles;
} //}}}
//{{{ setShowHiddenFiles() method
public void setShowHiddenFiles(boolean showHiddenFiles)
{
this.showHiddenFiles = showHiddenFiles;
} //}}}
//{{{ getFilenameFilter() method
/**
* Returns the file name filter glob.
* @since jEdit 3.2pre2
*/
public String getFilenameFilter()
{
if(filterCheckbox.isSelected())
{
String filter = filterField.getText();
if(filter.length() == 0)
return "*";
else
return filter;
}
else
return "*";
} //}}}
//{{{ setFilenameFilter() method
public void setFilenameFilter(String filter)
{
if(filter == null || filter.length() == 0 || filter.equals("*"))
filterCheckbox.setSelected(false);
else
{
filterCheckbox.setSelected(true);
filterField.setText(filter);
}
} //}}}
//{{{ getDirectoryField() method
public HistoryTextField getDirectoryField()
{
return pathField;
} //}}}
//{{{ getDirectory() method
public String getDirectory()
{
return path;
} //}}}
//{{{ setDirectory() method
public void setDirectory(String path)
{
if(path.startsWith("file:"))
path = path.substring(5);
pathField.setText(path);
if(!startRequest())
return;
updateFilenameFilter();
browserView.saveExpansionState();
browserView.loadDirectory(null,path);
this.path = path;
VFSManager.runInAWTThread(new Runnable()
{
public void run()
{
endRequest();
}
});
} //}}}
//{{{ rootDirectory() method
/**
* Goes to the local drives directory.
* @since jEdit 4.0pre4
*/
public void rootDirectory()
{
if(OperatingSystem.isMacOS() || OperatingSystem.isDOSDerived())
setDirectory(FileRootsVFS.PROTOCOL + ":");
else
setDirectory("/");
} //}}}
//{{{ reloadDirectory() method
public void reloadDirectory()
{
// used by FTP plugin to clear directory cache
VFSManager.getVFSForPath(path).reloadDirectory(path);
updateFilenameFilter();
browserView.saveExpansionState();
browserView.loadDirectory(null,path);
} //}}}
//{{{ delete() method
/**
* Note that all files must be on the same VFS.
*/
public void delete(VFS.DirectoryEntry[] files)
{
String dialogType;
if(MiscUtilities.isURL(files[0].deletePath)
&& FavoritesVFS.PROTOCOL.equals(
MiscUtilities.getProtocolOfURL(files[0].deletePath)))
{
dialogType = "vfs.browser.delete-favorites";
}
else
{
dialogType = "vfs.browser.delete-confirm";
}
StringBuffer buf = new StringBuffer();
for(int i = 0; i < files.length; i++)
{
buf.append(files[i].path);
buf.append('\n');
}
Object[] args = { buf.toString() };
int result = GUIUtilities.confirm(this,dialogType,args,
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if(result != JOptionPane.YES_OPTION)
return;
VFS vfs = VFSManager.getVFSForPath(files[0].deletePath);
if(!startRequest())
return;
for(int i = 0; i < files.length; i++)
{
Object session = vfs.createVFSSession(files[i].deletePath,this);
if(session == null)
continue;
VFSManager.runInWorkThread(new BrowserIORequest(
BrowserIORequest.DELETE,this,
session,vfs,files[i].deletePath,
null,null));
}
VFSManager.runInAWTThread(new Runnable()
{
public void run()
{
endRequest();
}
});
} //}}}
//{{{ rename() method
public void rename(String from)
{
VFS vfs = VFSManager.getVFSForPath(from);
String filename = vfs.getFileName(from);
String[] args = { filename };
String to = GUIUtilities.input(this,"vfs.browser.rename",
args,filename);
if(to == null)
return;
to = MiscUtilities.constructPath(vfs.getParentOfPath(from),to);
Object session = vfs.createVFSSession(from,this);
if(session == null)
return;
if(!startRequest())
return;
VFSManager.runInWorkThread(new BrowserIORequest(
BrowserIORequest.RENAME,this,
session,vfs,from,to,null));
VFSManager.runInAWTThread(new Runnable()
{
public void run()
{
endRequest();
}
});
} //}}}
//{{{ mkdir() method
public void mkdir()
{
String newDirectory = GUIUtilities.input(this,"vfs.browser.mkdir",null);
if(newDirectory == null)
return;
// if a directory is selected, create new dir in there.
// if a file is selected, create new dir inside its parent.
VFS.DirectoryEntry[] selected = getSelectedFiles();
String parent;
if(selected.length == 0)
parent = path;
else if(selected[0].type == VFS.DirectoryEntry.FILE)
{
parent = selected[0].path;
parent = VFSManager.getVFSForPath(parent)
.getParentOfPath(parent);
}
else
parent = selected[0].path;
VFS vfs = VFSManager.getVFSForPath(parent);
// path is the currently viewed directory in the browser
newDirectory = MiscUtilities.constructPath(parent,newDirectory);
Object session = vfs.createVFSSession(newDirectory,this);
if(session == null)
return;
if(!startRequest())
return;
VFSManager.runInWorkThread(new BrowserIORequest(
BrowserIORequest.MKDIR,this,
session,vfs,newDirectory,null,null));
VFSManager.runInAWTThread(new Runnable()
{
public void run()
{
endRequest();
}
});
} //}}}
//{{{ newFile() method
/**
* Creates a new file in the current directory.
* @since jEdit 4.0pre2
*/
public void newFile()
{
VFS.DirectoryEntry[] selected = getSelectedFiles();
if(selected.length >= 1)
{
VFS.DirectoryEntry file = selected[0];
if(file.type == VFS.DirectoryEntry.DIRECTORY)
jEdit.newFile(view,file.path);
else
{
VFS vfs = VFSManager.getVFSForPath(file.path);
jEdit.newFile(view,vfs.getParentOfPath(file.path));
}
}
else
jEdit.newFile(view,path);
} //}}}
//{{{ searchInDirectory() method
/**
* Opens a directory search in the current directory.
* @since jEdit 4.0pre2
*/
public void searchInDirectory()
{
VFS.DirectoryEntry[] selected = getSelectedFiles();
if(selected.length >= 1)
{
VFS.DirectoryEntry file = selected[0];
searchInDirectory(file.path,file.type != VFS.DirectoryEntry.FILE);
}
else
{
searchInDirectory(this.path,true);
}
} //}}}
//{{{ searchInDirectory() method
/**
* Opens a directory search in the specified directory.
* @param path The path name
* @param directory True if the path is a directory, false if it is a file
* @since jEdit 4.2pre1
*/
public void searchInDirectory(String path, boolean directory)
{
String filter;
if(directory)
{
filter = getFilenameFilter();
}
else
{
String name = MiscUtilities.getFileName(path);
String ext = MiscUtilities.getFileExtension(name);
filter = (ext == null || ext.length() == 0
? getFilenameFilter()
: "*" + ext);
path = MiscUtilities.getParentOfPath(path);
}
SearchAndReplace.setSearchFileSet(new DirectoryListSet(
path,filter,true));
SearchDialog.showSearchDialog(view,null,SearchDialog.DIRECTORY);
} //}}}
//{{{ getBrowserView() method
public BrowserView getBrowserView()
{
return browserView;
} //}}}
//{{{ getSelectedFiles() method
public VFS.DirectoryEntry[] getSelectedFiles()
{
return browserView.getSelectedFiles();
} //}}}
//{{{ locateFile() method
/**
* Goes to the given file's directory and selects the file in the list.
* @param path The file
* @since jEdit 4.2pre2
*/
public void locateFile(final String path)
{
if(!filenameFilter.isMatch(MiscUtilities.getFileName(path)))
setFilenameFilter(null);
setDirectory(MiscUtilities.getParentOfPath(path));
VFSManager.runInAWTThread(new Runnable()
{
public void run()
{
browserView.getTable().selectFile(path);
}
});
} //}}}
//{{{ addBrowserListener() method
public void addBrowserListener(BrowserListener l)
{
listenerList.add(BrowserListener.class,l);
} //}}}
//{{{ removeBrowserListener() method
public void removeBrowserListener(BrowserListener l)
{
listenerList.remove(BrowserListener.class,l);
} //}}}
//{{{ filesActivated() method
// canDoubleClickClose set to false when ENTER pressed
public static final int M_OPEN = 0;
public static final int M_OPEN_NEW_VIEW = 1;
public static final int M_OPEN_NEW_PLAIN_VIEW = 2;
public static final int M_OPEN_NEW_SPLIT = 3;
public static final int M_INSERT = 4;
/**
* This method does the "double-click" handling. It is public so that
*
|
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.