* otherwise create a sub menu.
*/
public static void fillAttachmentsMenu(IMenuManager manager, final IStructuredSelection selection, final IShellProvider shellProvider, boolean directMenu) {
final List<Pair<IAttachment, URI>> attachments = ModelUtils.getAttachmentLinks(selection);
if (!attachments.isEmpty()) {
manager.add(new Separator("attachments")); //$NON-NLS-1$
/* Either as direct Menu or Submenu */
IMenuManager attachmentMenu;
if (directMenu)
attachmentMenu = manager;
else {
attachmentMenu = new MenuManager(Messages.ApplicationActionBarAdvisor_ATTACHMENTS, "attachments"); //$NON-NLS-1$
manager.add(attachmentMenu);
}
final IPreferenceScope preferences = Owl.getPreferenceService().getGlobalScope();
/* Offer to Download All */
if (attachments.size() > 1) {
int sumBytes = 0;
for (Pair<IAttachment, URI> attachment : attachments) {
if (attachment.getFirst().getLength() > 0) {
sumBytes += attachment.getFirst().getLength();
} else {
sumBytes = 0;
break;
}
}
String sumSize = OwlUI.getSize(sumBytes);
Action downloadAllAction = new Action(sumSize != null ? (NLS.bind(Messages.ApplicationActionBarAdvisor_DOWNLOAD_ALL_WITH_SIZE, sumSize)) : (Messages.ApplicationActionBarAdvisor_DOWNLOAD_ALL)) {
@Override
public void run() {
DirectoryDialog dialog = new DirectoryDialog(shellProvider.getShell(), SWT.None);
dialog.setText(Messages.ApplicationActionBarAdvisor_SELECT_FOLDER_FOR_DOWNLOADS);
String downloadFolder = preferences.getString(DefaultPreferences.DOWNLOAD_FOLDER);
if (StringUtils.isSet(downloadFolder) && new File(downloadFolder).exists())
dialog.setFilterPath(downloadFolder);
String folder = dialog.open();
if (StringUtils.isSet(folder)) {
for (Pair<IAttachment, URI> attachment : attachments) {
Controller.getDefault().getDownloadService().download(attachment.getFirst(), attachment.getSecond(), new File(folder), true);
}
/* Remember Download Folder in Settings */
preferences.putString(DefaultPreferences.DOWNLOAD_FOLDER, folder);
}
}
};
downloadAllAction.setImageDescriptor(OwlUI.getImageDescriptor("icons/elcl16/save_all.gif")); //$NON-NLS-1$
attachmentMenu.add(downloadAllAction);
attachmentMenu.add(new Separator());
}
/* Collect openable Attachments that have already been downloaded */
List<Action> openActions = new ArrayList<Action>(1);
Set<String> downloadLocations = getDownloadLocations();
/* Offer Download Action for each */
for (final Pair<IAttachment, URI> attachmentPair : attachments) {
IAttachment attachment = attachmentPair.getFirst();
final String fileName = URIUtils.getFile(attachmentPair.getSecond(), OwlUI.getExtensionForMime(attachment.getType()));
String size = OwlUI.getSize(attachment.getLength());
Action action = new Action(size != null ? (NLS.bind(Messages.ApplicationActionBarAdvisor_FILE_SIZE, fileName, size)) : (fileName)) {
@Override
public void run() {
FileDialog dialog = new FileDialog(shellProvider.getShell(), SWT.SAVE);
dialog.setText(Messages.ApplicationActionBarAdvisor_SELECT_FILE_FOR_DOWNLOAD);
dialog.setFileName(Application.IS_WINDOWS ? CoreUtils.getSafeFileNameForWindows(fileName) : fileName);
dialog.setOverwrite(true);
String downloadFolder = preferences.getString(DefaultPreferences.DOWNLOAD_FOLDER);
if (StringUtils.isSet(downloadFolder) && new File(downloadFolder).exists())
dialog.setFilterPath(downloadFolder);
String selectedFileName = dialog.open();
if (StringUtils.isSet(selectedFileName)) {
File file = new File(selectedFileName);
Controller.getDefault().getDownloadService().download(attachmentPair.getFirst(), attachmentPair.getSecond(), file.getName(), file.getParentFile(), true);
/* Remember Download Folder in Settings */
preferences.putString(DefaultPreferences.DOWNLOAD_FOLDER, file.getParentFile().toString());
}
}
};
action.setImageDescriptor(OwlUI.getImageDescriptor("icons/etool16/save_as.gif")); //$NON-NLS-1$
attachmentMenu.add(action);
/* Check if Attachment already exists and offer Open Action then */
String usedFileName = Application.IS_WINDOWS ? CoreUtils.getSafeFileNameForWindows(fileName) : fileName;
if (shouldOfferOpenAction(usedFileName)) {
for (String downloadLocation : downloadLocations) {
final File downloadedFile = new File(downloadLocation, usedFileName);
if (downloadedFile.exists()) {
Action openAction = new Action(NLS.bind(Messages.ApplicationActionBarAdvisor_OPEN_FILE, fileName)) {
@Override
public void run() {
Program.launch(downloadedFile.toString());
}
};
openAction.setImageDescriptor(OwlUI.getAttachmentImage(fileName, attachmentPair.getFirst().getType()));
openActions.add(openAction);
break;
}
}
}
}
/* Offer Open Action for each downloaded */
if (!openActions.isEmpty()) {
attachmentMenu.add(new Separator());
for (Action openAction : openActions) {
attachmentMenu.add(openAction);
}
}
/* Offer to Automize Downloading */
attachmentMenu.add(new Separator());
attachmentMenu.add(new AutomateFilterAction(PresetAction.DOWNLOAD, selection));
}
}