Merge pull request #64 in ITERATE/cyberduck from feature/windows/9074 to master
[cyberduck.git] / source / ch / cyberduck / ui / cocoa / CreateFileController.java
blob326d76673952591903f098339234c6fd78427881
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.NSImage;
23 import ch.cyberduck.core.Cache;
24 import ch.cyberduck.core.LocaleFactory;
25 import ch.cyberduck.core.Path;
26 import ch.cyberduck.core.editor.EditorFactory;
27 import ch.cyberduck.core.local.Application;
28 import ch.cyberduck.core.resources.IconCacheFactory;
29 import ch.cyberduck.core.threading.WorkerBackgroundAction;
30 import ch.cyberduck.core.worker.TouchWorker;
31 import ch.cyberduck.ui.browser.UploadTargetFinder;
33 import java.util.Collections;
34 import java.util.EnumSet;
36 /**
37 * @version $Id$
39 public class CreateFileController extends FileController {
41 public CreateFileController(final BrowserController parent, final Cache<Path> cache) {
42 super(parent, cache, NSAlert.alert(
43 LocaleFactory.localizedString("Create new file", "File"),
44 LocaleFactory.localizedString("Enter the name for the new file:", "File"),
45 LocaleFactory.localizedString("Create", "File"),
46 EditorFactory.instance().getDefaultEditor() != Application.notfound ? LocaleFactory.localizedString("Edit", "File") : null,
47 LocaleFactory.localizedString("Cancel", "File")
48 ));
49 alert.setIcon(IconCacheFactory.<NSImage>get().documentIcon(null, 64));
52 @Override
53 public void callback(final int returncode) {
54 final Path parent = new UploadTargetFinder(this.getWorkdir()).find(this.getSelected());
55 if(returncode == DEFAULT_OPTION) {
56 this.run(parent, inputField.stringValue(), false);
58 else if(returncode == ALTERNATE_OPTION) {
59 this.run(parent, inputField.stringValue(), true);
63 private void run(final Path directory, final String filename, final boolean edit) {
64 final Path file = new Path(directory, filename, EnumSet.of(Path.Type.file));
65 parent.background(new WorkerBackgroundAction<Boolean>(parent, parent.getSession(), parent.getCache(),
66 new TouchWorker(file) {
67 @Override
68 public void cleanup(final Boolean done) {
69 if(done) {
70 if(filename.charAt(0) == '.') {
71 parent.setShowHiddenFiles(true);
73 parent.reload(parent.workdir(), Collections.singletonList(file), Collections.singletonList(file));
74 if(edit) {
75 file.attributes().setSize(0L);
76 parent.edit(file);
80 }));