use of com.mucommander.ui.dialog.QuestionDialog in project mucommander by mucommander.
the class EditorRegistrar method registerFileEditors.
/**
* Registers all available editor services for the given file type.
*
* @param file the file that will be displayed by the returned FileEditor
* @param presenter file editor presenter to register to
* @param frame the frame in which the FileEditor is shown
* @return number of editor services registered
* @throws UserCancelledException if the user has been asked to confirm the
* operation and canceled
*/
public static int registerFileEditors(AbstractFile file, FileEditorPresenter presenter, EditorFrame frame) throws UserCancelledException {
int counter = 0;
boolean editorCanceled = false;
List<FileEditorService> editorServices = FileEditorServiceTracker.getEditorServices();
for (FileEditorService service : editorServices) {
try {
if (service.canEditFile(file)) {
presenter.addEditorService(service);
counter++;
}
} catch (WarnUserException e) {
QuestionDialog dialog = new QuestionDialog((Frame) null, Translator.get("warning"), Translator.get(e.getMessage()), frame.getMainFrame(), Arrays.asList(EditorRegistrarAction.OPEN_ANYWAY, EditorRegistrarAction.CANCEL), 0);
DialogAction ret = dialog.getActionValue();
if (ret == EditorRegistrarAction.CANCEL || ret == DIALOG_DISPOSED_ACTION) {
// User canceled the operation
editorCanceled = true;
} else {
// User confirmed the operation
presenter.addEditorService(service);
counter++;
}
}
}
if (counter == 0 && editorCanceled) {
throw new UserCancelledException();
}
return counter;
}
use of com.mucommander.ui.dialog.QuestionDialog in project mucommander by mucommander.
the class ViewerRegistrar method registerFileViewers.
/**
* Registers all available viewer services for the given file type.
*
* @param file the file that will be displayed by the returned FileViewer
* @param presenter file viewer presenter to register to
* @return number of viewer services registered
* @throws UserCancelledException if the user has been asked to confirm the
* operation and canceled
*/
public static int registerFileViewers(AbstractFile file, FileViewerPresenter presenter, ViewerFrame viewerFrame) throws UserCancelledException {
int counter = 0;
boolean viewerCanceled = false;
List<FileViewerService> viewerServices = FileViewerServiceTracker.getViewerServices();
for (FileViewerService service : viewerServices) {
try {
if (service.canViewFile(file)) {
presenter.addViewerService(service);
counter++;
}
} catch (WarnUserException e) {
// TODO: question the user how does he want to open the file (as image, text..)
// Todo: display a proper warning dialog with the appropriate icon
QuestionDialog dialog = new QuestionDialog((Frame) null, Translator.get("warning"), Translator.get(e.getMessage()), viewerFrame.getMainFrame(), Arrays.asList(ViewerRegistrarAction.OPEN_ANYWAY, ViewerRegistrarAction.CANCEL), 0);
DialogAction ret = dialog.getActionValue();
if (ret == ViewerRegistrarAction.CANCEL || ret == DIALOG_DISPOSED_ACTION) {
// User canceled the operation
viewerCanceled = true;
} else {
// User confirmed the operation
presenter.addViewerService(service);
counter++;
}
}
}
if (counter == 0 && viewerCanceled) {
throw new UserCancelledException();
}
return counter;
}
use of com.mucommander.ui.dialog.QuestionDialog 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);
}
}
use of com.mucommander.ui.dialog.QuestionDialog in project mucommander by mucommander.
the class FileJob method showErrorDialog.
/**
* Displays an error dialog with the specified title and message and returns the selection action's value.
*/
protected DialogAction showErrorDialog(String title, String message, List<DialogAction> actionChoices) {
// Return SKIP_ACTION if 'skip all' has previously been selected and 'skip' is in the list of actions.
if (autoSkipErrors) {
for (DialogAction action : actionChoices) if (action == FileJobAction.SKIP)
return FileJobAction.SKIP;
}
// Send a system notification if a notifier is available and enabled
if (NotifierProvider.isAvailable() && NotifierProvider.getNotifier().isEnabled())
NotifierProvider.displayBackgroundNotification(NotificationType.JOB_ERROR, title, message);
QuestionDialog dialog;
if (getProgressDialog() == null)
dialog = new QuestionDialog(getMainFrame(), title, message, getMainFrame(), actionChoices, 0);
else
dialog = new QuestionDialog(getProgressDialog(), title, message, getMainFrame(), actionChoices, 0);
// Cancel or close dialog stops this job
DialogAction userChoice = waitForUserResponse(dialog);
if (userChoice == QuestionDialog.DIALOG_DISPOSED_ACTION || userChoice == FileJobAction.CANCEL)
interrupt();
else // Keep 'skip all' choice for further error and return SKIP
if (userChoice == FileJobAction.SKIP_ALL) {
autoSkipErrors = true;
return FileJobAction.SKIP;
}
return userChoice;
}
use of com.mucommander.ui.dialog.QuestionDialog in project mucommander by mucommander.
the class FileJob method showErrorDialog.
/**
* Displays an error dialog with the specified title and message and returns the selection action's value.
*/
protected int showErrorDialog(String title, String message, String[] actionTexts, int[] actionValues) {
// Return SKIP_ACTION if 'skip all' has previously been selected and 'skip' is in the list of actions.
if (autoSkipErrors) {
for (int actionValue : actionValues) if (actionValue == FileJobAction.SKIP)
return FileJobAction.SKIP;
}
// Send a system notification if a notifier is available and enabled
if (NotifierProvider.isAvailable() && NotifierProvider.getNotifier().isEnabled())
NotifierProvider.displayBackgroundNotification(NotificationType.JOB_ERROR, title, message);
QuestionDialog dialog;
if (getProgressDialog() == null)
dialog = new QuestionDialog(getMainFrame(), title, message, getMainFrame(), actionTexts, actionValues, 0);
else
dialog = new QuestionDialog(getProgressDialog(), title, message, getMainFrame(), actionTexts, actionValues, 0);
// Cancel or close dialog stops this job
int userChoice = waitForUserResponse(dialog);
if (userChoice == -1 || userChoice == FileJobAction.CANCEL)
interrupt();
else // Keep 'skip all' choice for further error and return SKIP_ACTION
if (userChoice == FileJobAction.SKIP_ALL) {
autoSkipErrors = true;
return FileJobAction.SKIP;
}
return userChoice;
}
Aggregations