Search in sources :

Example 1 with NSPopUpButton

use of ch.cyberduck.binding.application.NSPopUpButton in project cyberduck by iterate-ch.

the class MainController method upload.

private boolean upload(final List<Local> files) {
    // Selected bookmark
    Host open = null;
    Path workdir = null;
    for (BrowserController browser : MainController.getBrowsers()) {
        if (browser.isMounted()) {
            open = browser.getSession().getHost();
            workdir = browser.workdir();
            if (1 == MainController.getBrowsers().size()) {
                // If only one browser window upload to current working directory with no bookmark selection
                this.upload(open, files, workdir);
                return true;
            }
            break;
        }
    }
    final BookmarkCollection bookmarks = BookmarkCollection.defaultCollection();
    if (bookmarks.isEmpty()) {
        log.warn("No bookmark for upload");
        return false;
    }
    final NSPopUpButton bookmarksPopup = NSPopUpButton.buttonWithFrame(new NSRect(0, 26));
    bookmarksPopup.setToolTip(LocaleFactory.localizedString("Bookmarks", "Browser"));
    for (Host b : bookmarks) {
        String title = BookmarkNameProvider.toString(b);
        int i = 1;
        while (bookmarksPopup.itemWithTitle(title) != null) {
            title = BookmarkNameProvider.toString(b) + "-" + i;
            i++;
        }
        bookmarksPopup.addItemWithTitle(title);
        bookmarksPopup.lastItem().setImage(IconCacheFactory.<NSImage>get().iconNamed(b.getProtocol().icon(), 16));
        bookmarksPopup.lastItem().setRepresentedObject(b.getUuid());
        if (b.equals(open)) {
            bookmarksPopup.selectItemAtIndex(bookmarksPopup.indexOfItem(bookmarksPopup.lastItem()));
        }
    }
    if (null == open) {
        int i = 0;
        for (Host bookmark : bookmarks) {
            boolean found = false;
            // Pick the bookmark with the same download location
            for (Local file : files) {
                if (file.isChild(new DownloadDirectoryFinder().find(bookmark))) {
                    bookmarksPopup.selectItemAtIndex(new NSInteger(i));
                    found = true;
                    break;
                }
            }
            if (found) {
                break;
            }
            i++;
        }
    }
    if (-1 == bookmarksPopup.indexOfSelectedItem().intValue()) {
        // No bookmark for current browser found
        bookmarksPopup.selectItemAtIndex(new NSInteger(0));
    }
    final Host mount = open;
    final Path destination = workdir;
    final NSAlert alert = NSAlert.alert("Select Bookmark", MessageFormat.format("Upload {0} to the selected bookmark.", files.size() == 1 ? files.iterator().next().getName() : MessageFormat.format(LocaleFactory.localizedString("{0} Items"), String.valueOf(files.size()))), LocaleFactory.localizedString("Upload", "Transfer"), LocaleFactory.localizedString("Cancel"), null);
    alert.setAlertStyle(NSAlert.NSInformationalAlertStyle);
    final AlertController controller = new AlertController() {

        @Override
        public void loadBundle() {
            this.loadBundle(alert);
        }

        @Override
        public NSView getAccessoryView(final NSAlert alert) {
            return bookmarksPopup;
        }

        @Override
        public void callback(int returncode) {
            if (DEFAULT_OPTION == returncode) {
                final String selected = bookmarksPopup.selectedItem().representedObject();
                final Host bookmark = bookmarks.lookup(selected);
                if (bookmark.equals(mount)) {
                    // Use current working directory of browser for destination
                    upload(bookmark, files, destination);
                } else {
                    // No mounted browser
                    if (StringUtils.isNotBlank(bookmark.getDefaultPath())) {
                        upload(bookmark, files, new Path(PathNormalizer.normalize(bookmark.getDefaultPath()), EnumSet.of(Path.Type.directory)));
                    } else {
                        upload(bookmark, files, null == destination ? new Path(String.valueOf(Path.DELIMITER), EnumSet.of(Path.Type.directory)) : destination);
                    }
                }
            }
        }

        @Override
        public boolean validate() {
            return StringUtils.isNotEmpty(bookmarksPopup.selectedItem().representedObject());
        }
    };
    controller.beginSheet(TransferControllerFactory.get());
    return true;
}
Also used : NSAttributedString(ch.cyberduck.binding.foundation.NSAttributedString) DownloadDirectoryFinder(ch.cyberduck.ui.browser.DownloadDirectoryFinder) NSPopUpButton(ch.cyberduck.binding.application.NSPopUpButton) NSInteger(org.rococoa.cocoa.foundation.NSInteger) NSImage(ch.cyberduck.binding.application.NSImage) NSAlert(ch.cyberduck.binding.application.NSAlert) NSRect(org.rococoa.cocoa.foundation.NSRect) AlertController(ch.cyberduck.binding.AlertController)

Aggregations

AlertController (ch.cyberduck.binding.AlertController)1 NSAlert (ch.cyberduck.binding.application.NSAlert)1 NSImage (ch.cyberduck.binding.application.NSImage)1 NSPopUpButton (ch.cyberduck.binding.application.NSPopUpButton)1 NSAttributedString (ch.cyberduck.binding.foundation.NSAttributedString)1 DownloadDirectoryFinder (ch.cyberduck.ui.browser.DownloadDirectoryFinder)1 NSInteger (org.rococoa.cocoa.foundation.NSInteger)1 NSRect (org.rococoa.cocoa.foundation.NSRect)1