use of com.mucommander.commons.file.AbstractFile in project mucommander by mucommander.
the class AppearancePanel method exportTheme.
/**
* Exports the specified theme.
*
* @param theme theme to export.
*/
private void exportTheme(Theme theme) {
JFileChooser chooser;
AbstractFile file;
chooser = createFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.addChoosableFileFilter(new ExtensionFileFilter("xml", Translator.get("prefs_dialog.xml_file")));
chooser.setDialogTitle(Translator.get("prefs_dialog.export_theme", theme.getName()));
if (chooser.showDialog(parent, Translator.get("prefs_dialog.export")) == JFileChooser.APPROVE_OPTION) {
file = FileFactory.getFile(chooser.getSelectedFile().getAbsolutePath());
lastSelectedFolder = file.getParent();
// Makes sure the file's extension is .xml.
try {
if (// Note: getExtension() may return null if no extension
!"xml".equalsIgnoreCase(file.getExtension()))
file = lastSelectedFolder.getDirectChild(file.getName() + ".xml");
int collision = FileCollisionChecker.checkForCollision(null, file);
if (collision != FileCollisionChecker.NO_COLLISION) {
// Do not offer the multiple files mode options such as 'skip' and 'apply to all'
DialogAction action = new FileCollisionDialog(parent, parent, collision, null, file, false, false).getActionValue();
// User chose to overwrite the file
if (action == FileCollisionDialog.FileCollisionAction.OVERWRITE) {
// Simply continue and file will be overwritten
} else // User chose to cancel or closed the dialog
{
return;
}
}
// Exports the theme.
ThemeManager.exportTheme(theme, (java.io.File) file.getUnderlyingFileObject());
// changes.
if (lastSelectedFolder.equals(ThemeManager.getCustomThemesFolder()))
populateThemes(theme);
}// Notifies users of errors.
catch (Exception exception) {
InformationDialog.showErrorDialog(this, Translator.get("write_error"), Translator.get("cannot_write_file", file.getName()));
}
}
}
use of com.mucommander.commons.file.AbstractFile in project mucommander by mucommander.
the class DrivePopupButton method updateButton.
/**
* Updates the button's label and icon to reflect the current folder and match one of the current volumes: <
* <ul>
* <li>If the specified folder corresponds to a bookmark, the bookmark's name will be displayed
* <li>If the specified folder corresponds to a local file, the enclosing volume's name will be displayed
* <li>If the specified folder corresponds to a remote file, the protocol's name will be displayed
* </ul>
* The button's icon will be the current folder's one.
*/
private void updateButton() {
AbstractFile currentFolder = folderPanel.getCurrentFolder();
String currentPath = currentFolder.getAbsolutePath();
FileURL currentURL = currentFolder.getURL();
// First try to find a bookmark matching the specified folder
for (Bookmark bookmark : BookmarkManager.getBookmarks()) {
if (currentPath.equals(bookmark.getLocation())) {
// Note: if several bookmarks match current folder, the first one will be used
setText(bookmark.getName());
setIcon(IconManager.getIcon(IconManager.FILE_ICON_SET, CustomFileIconProvider.BOOKMARK_ICON_NAME));
return;
}
}
// If no bookmark matched current folder
String protocol = currentURL.getScheme();
switch(protocol) {
// Local file, use volume's name
case LocalFile.SCHEMA:
String newLabel = null;
// display 'SMB' which is the underlying protocol
if (OsFamily.WINDOWS.isCurrent() && !FileURL.LOCALHOST.equals(currentURL.getHost())) {
newLabel = "SMB";
} else {
if (OsFamily.WINDOWS.isCurrent())
currentPath = currentFolder.getAbsolutePath(false).toLowerCase();
else
currentPath = currentFolder.getCanonicalPath(false).toLowerCase();
int bestLength = -1;
int bestIndex = 0;
String temp;
int len;
for (int i = 0; i < volumes.length; i++) {
if (OsFamily.WINDOWS.isCurrent())
temp = volumes[i].getAbsolutePath(false).toLowerCase();
else
temp = volumes[i].getCanonicalPath(false).toLowerCase();
len = temp.length();
if (currentPath.startsWith(temp) && len > bestLength) {
bestIndex = i;
bestLength = len;
}
}
newLabel = volumes[bestIndex].getName();
// Not used because the call to FileSystemView is slow
// if(fileSystemView!=null)
// newToolTip = getWindowsExtendedDriveName(volumes[bestIndex]);
}
setText(newLabel);
setIcon(FileIcons.getFileIcon(currentFolder));
break;
case BookmarkProtocolProvider.BOOKMARK:
String currentFolderName = currentFolder.getName();
setText(currentFolderName.isEmpty() ? Translator.get("bookmarks_menu") : currentFolderName);
setIcon(IconManager.getIcon(IconManager.FILE_ICON_SET, CustomFileIconProvider.BOOKMARKS_ICON_NAME));
break;
case SearchFile.SCHEMA:
setText(Translator.get("find"));
setIcon(IconManager.getIcon(IconManager.FILE_ICON_SET, CustomFileIconProvider.FIND_RESULT_ICON_NAME));
break;
case "gdrive":
setText(Translator.get("gdrive"));
setIcon(IconManager.getIcon(IconManager.FILE_ICON_SET, CustomFileIconProvider.GOOGLE_DRIVE_ICON_NAME));
break;
case "dropbox":
setText(Translator.get("dropbox"));
setIcon(IconManager.getIcon(IconManager.FILE_ICON_SET, CustomFileIconProvider.DROPBOX_ICON_NAME));
break;
default:
// Remote file, use the protocol's name
setText(protocol.toUpperCase());
setIcon(FileIcons.getFileIcon(currentFolder));
}
}
use of com.mucommander.commons.file.AbstractFile in project mucommander by mucommander.
the class FileCollisionDialog method addFileDetails.
private void addFileDetails(XAlignedComponentPanel panel, AbstractFile file, String nameLabel) {
addFileDetailsRow(panel, nameLabel + ":", new FileLabel(file, false), 0);
AbstractFile parent = file.getParent();
addFileDetailsRow(panel, Translator.get("location") + ":", new FileLabel(parent == null ? file : parent, true), 0);
addFileDetailsRow(panel, Translator.get("size") + ":", new JLabel(SizeFormat.format(file.getSize(), SizeFormat.DIGITS_FULL | SizeFormat.UNIT_LONG | SizeFormat.INCLUDE_SPACE)), 0);
addFileDetailsRow(panel, Translator.get("date") + ":", new JLabel(CustomDateFormat.format(new Date(file.getDate()))), 0);
addFileDetailsRow(panel, Translator.get("permissions") + ":", new JLabel(file.getPermissionsString()), 10);
}
use of com.mucommander.commons.file.AbstractFile in project mucommander by mucommander.
the class PackDialog method computeInitialPath.
// ////////////////////////////////////////////
// TransferDestinationDialog implementation //
// ////////////////////////////////////////////
@Override
protected PathFieldContent computeInitialPath(FileSet files) {
String initialPath = mainFrame.getInactivePanel().getCurrentFolder().getAbsolutePath(true);
AbstractFile file;
String fileName;
// - if it contains more than one file, uses the FileSet's parent folder's name.
if (files.size() == 1) {
file = files.elementAt(0);
fileName = file.isDirectory() && !DesktopManager.isApplication(file) ? file.getName() : file.getNameWithoutExtension();
} else {
file = files.getBaseFolder();
fileName = file.isRoot() ? "" : DesktopManager.isApplication(file) ? file.getNameWithoutExtension() : file.getName();
}
return new PathFieldContent(initialPath + fileName + "." + Archiver.getFormatExtension(lastFormat), initialPath.length(), initialPath.length() + fileName.length());
}
use of com.mucommander.commons.file.AbstractFile in project mucommander by mucommander.
the class BrowseLocationThread method run.
@Override
public void run() {
LOGGER.debug("starting folder change...");
boolean folderChangedSuccessfully = false;
// Show some progress in the progress bar to give hope
folderPanel.setProgressValue(10);
boolean userCancelled = false;
CredentialsMapping newCredentialsMapping = null;
// True if Guest authentication was selected in the authentication dialog (guest credentials must not be
// added to CredentialsManager)
boolean guestCredentialsSelected = false;
AuthenticationType authenticationType = folderURL.getAuthenticationType();
if (credentialsMapping != null) {
newCredentialsMapping = credentialsMapping;
CredentialsManager.authenticate(folderURL, newCredentialsMapping);
} else // avoid waiting for an AuthException to be thrown.
if (!folderURL.containsCredentials() && ((authenticationType == AuthenticationType.AUTHENTICATION_REQUIRED) || (authenticationType == AuthenticationType.AUTHENTICATION_OPTIONAL && CredentialsManager.getMatchingCredentials(folderURL).length > 0))) {
AuthDialog authDialog = popAuthDialog(folderURL, false, null);
newCredentialsMapping = authDialog.getCredentialsMapping();
guestCredentialsSelected = authDialog.guestCredentialsSelected();
// User cancelled the authentication dialog, stop
if (newCredentialsMapping == null)
userCancelled = true;
else // Use the provided credentials and invalidate the folder AbstractFile instance (if any) so that
// it gets recreated with the new credentials
{
CredentialsManager.authenticate(folderURL, newCredentialsMapping);
folder = null;
}
}
if (!userCancelled) {
boolean canonicalPathFollowed = false;
do {
// Set cursor to hourglass/wait
mainFrame.setCursor(new Cursor(Cursor.WAIT_CURSOR));
// Render all actions inactive while changing folder
mainFrame.setNoEventsMode(true);
try {
// Thread was created using a FileURL
if (folder == null) {
AbstractFile file = FileFactory.getFile(folderURL, true);
synchronized (KILL_LOCK) {
if (killed) {
LOGGER.debug("this thread has been killed, returning");
break;
}
}
// File resolved -> 25% complete
folderPanel.setProgressValue(25);
// or doesn't exist
if (file == null || !file.exists()) {
// Restore default cursor
mainFrame.setCursor(Cursor.getDefaultCursor());
locationChanger.showFolderDoesNotExistDialog();
break;
}
if (!file.canRead()) {
// Restore default cursor
mainFrame.setCursor(Cursor.getDefaultCursor());
showFailedToReadFolderDialog();
break;
}
// File is a regular directory, all good
if (file.isDirectory()) {
// Just continue
} else // File is a browsable file (Zip archive for instance) but not a directory : Browse or Download ? => ask the user
if (file.isBrowsable()) {
// of the OpenAction (enter pressed on the file). This works well enough in practice.
if (!globalHistory.contains(folderURL) && !file.equals(folderPanel.getFileTable().getSelectedFile())) {
// Restore default cursor
mainFrame.setCursor(Cursor.getDefaultCursor());
// Download or browse file ?
QuestionDialog dialog = new QuestionDialog(mainFrame, null, Translator.get("table.download_or_browse"), mainFrame, Arrays.asList(BrowseLocationThreadAction.BROWSE, BrowseLocationThreadAction.DOWNLOAD, BrowseLocationThreadAction.CANCEL), 0);
DialogAction ret = dialog.getActionValue();
if (ret == DIALOG_DISPOSED_ACTION || ret == BrowseLocationThreadAction.CANCEL)
break;
// Download file
if (ret == BrowseLocationThreadAction.DOWNLOAD) {
showDownloadDialog(file);
break;
}
// Continue if BROWSE
// Set cursor to hourglass/wait
mainFrame.setCursor(new Cursor(Cursor.WAIT_CURSOR));
}
// else just continue and browse file's contents
} else // File is a regular file: show download dialog which allows to download (copy) the file
// to a directory specified by the user
{
showDownloadDialog(file);
break;
}
this.folder = file;
} else // Thread was created using an AbstractFile instance, check file existence
if (!folder.exists()) {
// Find a 'workable' folder if the requested folder doesn't exist anymore
if (findWorkableFolder) {
AbstractFile newFolder = locationChanger.getWorkableFolder(folder);
if (newFolder.equals(folder)) {
// If we've already tried the returned folder, give up (avoids a potentially endless loop)
locationChanger.showFolderDoesNotExistDialog();
break;
}
// Try again with the new folder
folder = newFolder;
folderURL = folder.getURL();
// Discard the file to select, if any
fileToSelect = null;
continue;
} else {
locationChanger.showFolderDoesNotExistDialog();
break;
}
} else if (!folder.canRead()) {
showFailedToReadFolderDialog();
break;
}
// resolved again.
if (!canonicalPathFollowed && followCanonicalPath(folder)) {
try {
// Recreate the FileURL using the file's canonical path
FileURL newURL = FileURL.getFileURL(folder.getCanonicalPath());
// Keep the credentials and properties (if any)
newURL.setCredentials(folderURL.getCredentials());
newURL.importProperties(folderURL);
this.folderURL = newURL;
// Invalidate the AbstractFile instance
this.folder = null;
// There won't be any further attempts after this one
canonicalPathFollowed = true;
// Loop the resolve the file
continue;
} catch (MalformedURLException e) {
// In the unlikely event of the canonical path being malformed, the AbstractFile
// and FileURL instances are left untouched
}
}
synchronized (KILL_LOCK) {
if (killed) {
LOGGER.debug("this thread has been killed, returning");
break;
}
}
// File tested -> 50% complete
folderPanel.setProgressValue(50);
synchronized (KILL_LOCK) {
if (killed) {
LOGGER.debug("this thread has been killed, returning");
break;
}
// From now on, thread cannot be killed (would comprise table integrity)
doNotKill = true;
}
// files listed -> 75% complete
folderPanel.setProgressValue(75);
LOGGER.trace("calling setCurrentFolder");
// Change the file table's current folder and select the specified file (if any)
locationChanger.setCurrentFolder(folder, fileToSelect, changeLockedTab, true);
// folder set -> 95% complete
folderPanel.setProgressValue(95);
// Do not add the credentials if guest credentials were selected by the user.
if (newCredentialsMapping != null && !guestCredentialsSelected)
CredentialsManager.addCredentials(newCredentialsMapping);
// All good !
folderChangedSuccessfully = true;
break;
} catch (Exception e) {
LOGGER.debug("Caught exception", e);
if (killed) {
// If #tryKill() called #interrupt(), the exception we just caught was most likely
// thrown as a result of the thread being interrupted.
//
// The exception can be a java.lang.InterruptedException (Thread throws those),
// a java.nio.channels.ClosedByInterruptException (InterruptibleChannel throws those)
// or any other exception thrown by some code that swallowed the original exception
// and threw a new one.
LOGGER.debug("Thread was interrupted, ignoring exception");
break;
}
// Restore default cursor
mainFrame.setCursor(Cursor.getDefaultCursor());
if (e instanceof AuthException) {
AuthException authException = (AuthException) e;
// Retry (loop) if user provided new credentials, if not stop
AuthDialog authDialog = popAuthDialog(authException.getURL(), true, authException.getMessage());
newCredentialsMapping = authDialog.getCredentialsMapping();
guestCredentialsSelected = authDialog.guestCredentialsSelected();
if (newCredentialsMapping != null) {
// Invalidate the existing AbstractFile instance
folder = null;
// Use the provided credentials
CredentialsManager.authenticate(folderURL, newCredentialsMapping);
continue;
}
} else {
// Find a 'workable' folder if the requested folder doesn't exist anymore
if (findWorkableFolder) {
AbstractFile newFolder = locationChanger.getWorkableFolder(folder);
if (newFolder.equals(folder)) {
// If we've already tried the returned folder, give up (avoids a potentially endless loop)
locationChanger.showFolderDoesNotExistDialog();
break;
}
// Try again with the new folder
folder = newFolder;
folderURL = folder.getURL();
// Discard the file to select, if any
fileToSelect = null;
continue;
}
showAccessErrorDialog(e);
}
// Stop looping!
break;
}
} while (true);
}
synchronized (KILL_LOCK) {
// Clean things up
cleanup(folderChangedSuccessfully);
}
}
Aggregations