Merge pull request #64 in ITERATE/cyberduck from feature/windows/9074 to master
[cyberduck.git] / source / ch / cyberduck / ui / cocoa / DownloadController.java
blob50128bd8949b807e81b3f7ab8175141dbf65fa4f
1 package ch.cyberduck.ui.cocoa;
3 /*
4 * Copyright (c) 2005 David Kocher. All rights reserved.
5 * http://cyberduck.ch/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to:
18 * dkocher@cyberduck.ch
21 import ch.cyberduck.binding.application.NSAlert;
22 import ch.cyberduck.binding.application.NSTextField;
23 import ch.cyberduck.binding.application.NSWindow;
24 import ch.cyberduck.core.DefaultPathKindDetector;
25 import ch.cyberduck.core.Host;
26 import ch.cyberduck.core.HostParser;
27 import ch.cyberduck.core.LocalFactory;
28 import ch.cyberduck.core.LocaleFactory;
29 import ch.cyberduck.core.Path;
30 import ch.cyberduck.core.PathKindDetector;
31 import ch.cyberduck.core.PathNormalizer;
32 import ch.cyberduck.core.local.BrowserLauncherFactory;
33 import ch.cyberduck.core.preferences.PreferencesFactory;
34 import ch.cyberduck.core.transfer.DownloadTransfer;
35 import ch.cyberduck.core.transfer.Transfer;
37 import org.apache.commons.lang3.StringUtils;
38 import org.rococoa.cocoa.foundation.NSRect;
40 import java.util.EnumSet;
42 /**
43 * @version $Id$
45 public class DownloadController extends AlertController {
47 protected NSTextField urlField
48 = NSTextField.textfieldWithFrame(new NSRect(0, 22));
50 private PathKindDetector detector = new DefaultPathKindDetector();
52 @Override
53 protected void beginSheet(final NSWindow window) {
54 this.setAccessoryView(urlField);
55 this.updateField(urlField, url);
56 alert.setShowsHelp(true);
57 super.beginSheet(window);
60 private String url;
62 public DownloadController(final WindowController parent) {
63 this(parent, StringUtils.EMPTY);
66 public DownloadController(final WindowController parent, final String url) {
67 super(parent, NSAlert.alert(
68 LocaleFactory.localizedString("New Download", "Download"),
69 LocaleFactory.localizedString("URL", "Download"),
70 LocaleFactory.localizedString("Download", "Download"),
71 null,
72 LocaleFactory.localizedString("Cancel", "Download")
73 ), NSAlert.NSInformationalAlertStyle);
74 this.url = url;
77 @Override
78 public void callback(final int returncode) {
79 if(returncode == DEFAULT_OPTION) {
80 final Host host = HostParser.parse(urlField.stringValue());
81 final Path file = new Path(PathNormalizer.normalize(host.getDefaultPath(), true),
82 EnumSet.of(detector.detect(host.getDefaultPath())));
83 host.setDefaultPath(file.getParent().getAbsolute());
84 final Transfer transfer = new DownloadTransfer(host, file,
85 LocalFactory.get(PreferencesFactory.get().getProperty("queue.download.folder"), file.getName()));
86 TransferControllerFactory.get().start(transfer);
90 @Override
91 protected void focus() {
92 // Focus accessory view.
93 urlField.selectText(null);
94 window.makeFirstResponder(urlField);
97 @Override
98 protected boolean validateInput() {
99 Host host = HostParser.parse(urlField.stringValue());
100 return StringUtils.isNotBlank(host.getDefaultPath());
103 @Override
104 protected void help() {
105 StringBuilder site = new StringBuilder(PreferencesFactory.get().getProperty("website.help"));
106 site.append("/howto/download");
107 BrowserLauncherFactory.get().open(site.toString());