Fix transcript in transfer window.
[cyberduck.git] / source / ch / cyberduck / ui / cocoa / CommandController.java
blobfeeb22858d08d8e8eca136182680acbccb723ec3
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.NSButton;
22 import ch.cyberduck.binding.application.NSImage;
23 import ch.cyberduck.binding.application.NSImageView;
24 import ch.cyberduck.binding.application.NSLayoutManager;
25 import ch.cyberduck.binding.application.NSProgressIndicator;
26 import ch.cyberduck.binding.application.NSTextField;
27 import ch.cyberduck.binding.application.NSTextView;
28 import ch.cyberduck.binding.application.NSWindow;
29 import ch.cyberduck.binding.foundation.NSAttributedString;
30 import ch.cyberduck.binding.foundation.NSObject;
31 import ch.cyberduck.binding.foundation.NSRange;
32 import ch.cyberduck.core.PathCache;
33 import ch.cyberduck.core.Session;
34 import ch.cyberduck.core.TranscriptListener;
35 import ch.cyberduck.core.exception.BackgroundException;
36 import ch.cyberduck.core.features.Command;
37 import ch.cyberduck.core.local.Application;
38 import ch.cyberduck.core.resources.IconCacheFactory;
39 import ch.cyberduck.core.threading.ControllerBackgroundAction;
40 import ch.cyberduck.ui.cocoa.threading.WindowMainAction;
42 import org.apache.commons.lang3.StringUtils;
43 import org.rococoa.cocoa.foundation.NSUInteger;
45 /**
46 * @version $Id$
48 public class CommandController extends SheetController implements TranscriptListener, NSLayoutManager.Delegate {
50 @Outlet
51 private NSTextField inputField;
52 @Outlet
53 private NSTextView responseField;
54 @Outlet
55 private NSProgressIndicator progress;
56 @Outlet
57 private NSImageView image;
59 @Override
60 public void setWindow(final NSWindow window) {
61 window.setContentMinSize(window.frame().size);
62 super.setWindow(window);
65 public void setInputField(NSTextField inputField) {
66 this.inputField = inputField;
69 public void setResponseField(NSTextView responseField) {
70 this.responseField = responseField;
71 this.responseField.setEditable(false);
72 this.responseField.setSelectable(true);
73 this.responseField.setUsesFontPanel(false);
74 this.responseField.setRichText(false);
75 this.responseField.layoutManager().setDelegate(this.id());
78 public void setProgress(NSProgressIndicator progress) {
79 this.progress = progress;
80 this.progress.setDisplayedWhenStopped(false);
83 public void setImage(NSImageView image) {
84 this.image = image;
85 this.image.setImage(IconCacheFactory.<NSImage>get().applicationIcon(new Application("com.apple.Terminal"), 128));
88 @Override
89 public void layoutManager_didCompleteLayoutForTextContainer_atEnd(NSLayoutManager layoutManager,
90 NSObject textContainer,
91 boolean finished) {
92 if(finished && this.responseField.window().isVisible()) {
93 this.responseField.scrollRangeToVisible(
94 NSRange.NSMakeRange(this.responseField.textStorage().length(), new NSUInteger(0))
99 private Session<?> session;
101 public CommandController(final WindowController parent, final Session session) {
102 super(parent);
103 this.session = session;
106 @Override
107 protected String getBundleName() {
108 return "Command";
111 @Action
112 public void sendButtonClicked(final NSButton sender) {
113 final String command = this.inputField.stringValue();
114 if(StringUtils.isNotBlank(command)) {
115 progress.startAnimation(null);
116 sender.setEnabled(false);
117 parent.background(new ControllerBackgroundAction<Void>(this, session, PathCache.empty()) {
118 @Override
119 public boolean alert() {
120 return false;
123 @Override
124 public Void run() throws BackgroundException {
125 final Command feature = session.getFeature(Command.class);
126 feature.send(command, this, CommandController.this);
127 return null;
130 @Override
131 public void cleanup() {
132 super.cleanup();
133 progress.stopAnimation(null);
134 sender.setEnabled(true);
137 @Override
138 public String getActivity() {
139 return command;
145 @Override
146 public void log(boolean request, final String message) {
147 invoke(new WindowMainAction(this) {
148 @Override
149 public void run() {
150 responseField.textStorage().replaceCharactersInRange_withAttributedString(
151 NSRange.NSMakeRange(responseField.textStorage().length(), new NSUInteger(0)),
152 NSAttributedString.attributedStringWithAttributes(message + "\n", FIXED_WITH_FONT_ATTRIBUTES));
157 @Override
158 protected boolean validateInput() {
159 return true;
162 @Override
163 public void callback(final int returncode) {
167 @Override
168 public void invalidate() {
169 responseField.layoutManager().setDelegate(null);
170 super.invalidate();