/
ProgressController.java
365 lines (319 loc) · 13.5 KB
/
ProgressController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package ch.cyberduck.ui.cocoa;
/*
* Copyright (c) 2005 David Kocher. All rights reserved.
* http://cyberduck.ch/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Bug fixes, suggestions and comments should be sent to:
* dkocher@cyberduck.ch
*/
import ch.cyberduck.binding.Action;
import ch.cyberduck.binding.Delegate;
import ch.cyberduck.binding.Outlet;
import ch.cyberduck.binding.application.NSCell;
import ch.cyberduck.binding.application.NSColor;
import ch.cyberduck.binding.application.NSFont;
import ch.cyberduck.binding.application.NSImage;
import ch.cyberduck.binding.application.NSImageView;
import ch.cyberduck.binding.application.NSMenuItem;
import ch.cyberduck.binding.application.NSPopUpButton;
import ch.cyberduck.binding.application.NSProgressIndicator;
import ch.cyberduck.binding.application.NSTextField;
import ch.cyberduck.binding.application.NSView;
import ch.cyberduck.binding.foundation.NSArray;
import ch.cyberduck.binding.foundation.NSAttributedString;
import ch.cyberduck.binding.foundation.NSDictionary;
import ch.cyberduck.binding.foundation.NSNotification;
import ch.cyberduck.binding.foundation.NSNotificationCenter;
import ch.cyberduck.core.LocaleFactory;
import ch.cyberduck.core.ProgressListener;
import ch.cyberduck.core.UserDateFormatterFactory;
import ch.cyberduck.core.formatter.SizeFormatter;
import ch.cyberduck.core.formatter.SizeFormatterFactory;
import ch.cyberduck.core.resources.IconCacheFactory;
import ch.cyberduck.core.threading.DefaultMainAction;
import ch.cyberduck.core.transfer.Transfer;
import ch.cyberduck.core.transfer.TransferItem;
import ch.cyberduck.core.transfer.TransferListener;
import ch.cyberduck.core.transfer.TransferProgress;
import ch.cyberduck.ui.cocoa.delegate.AbstractMenuDelegate;
import ch.cyberduck.ui.cocoa.delegate.TransferMenuDelegate;
import org.apache.commons.lang3.StringUtils;
import org.rococoa.Foundation;
import org.rococoa.cocoa.foundation.NSInteger;
import java.text.MessageFormat;
import java.util.Date;
import java.util.List;
/**
* @version $Id$
*/
public class ProgressController extends BundleController implements TransferListener, ProgressListener {
private final NSNotificationCenter notificationCenter = NSNotificationCenter.defaultCenter();
private Transfer transfer;
/**
* Formatter for file size
*/
private SizeFormatter sizeFormatter = SizeFormatterFactory.get();
public ProgressController(final Transfer transfer) {
this.transfer = transfer;
this.loadBundle();
}
/**
* Resets both the progress and status field
*/
@Override
public void awakeFromNib() {
this.progress(MessageFormat.format(LocaleFactory.localizedString("{0} of {1}"),
sizeFormatter.format(transfer.getTransferred()),
sizeFormatter.format(transfer.getSize())));
this.message(StringUtils.EMPTY);
this.status(LocaleFactory.localizedString(transfer.isComplete() ?
String.format("%s complete", StringUtils.capitalize(transfer.getType().name())) : "Transfer incomplete", "Status"));
super.awakeFromNib();
}
@Override
protected String getBundleName() {
return "Progress.nib";
}
@Override
public void invalidate() {
filesPopup.menu().setDelegate(null);
notificationCenter.removeObserver(this.id());
super.invalidate();
}
@Override
public void start(final Transfer transfer) {
invoke(new DefaultMainAction() {
@Override
public void run() {
progressBar.setHidden(false);
progressBar.setIndeterminate(true);
progressBar.startAnimation(null);
statusIconView.setImage(YELLOW_ICON);
progress(StringUtils.EMPTY);
status(StringUtils.EMPTY);
}
});
}
@Override
public void stop(final Transfer transfer) {
invoke(new DefaultMainAction() {
@Override
public void run() {
progressBar.stopAnimation(null);
progressBar.setIndeterminate(true);
progressBar.setHidden(true);
message(StringUtils.EMPTY);
progress(MessageFormat.format(LocaleFactory.localizedString("{0} of {1}"),
sizeFormatter.format(transfer.getTransferred()),
sizeFormatter.format(transfer.getSize())));
status(LocaleFactory.localizedString(LocaleFactory.localizedString(transfer.isComplete() ?
String.format("%s complete", StringUtils.capitalize(transfer.getType().name())) : "Transfer incomplete", "Status"), "Status"));
statusIconView.setImage(transfer.isComplete() ? GREEN_ICON : RED_ICON);
}
});
}
@Override
public void progress(final TransferProgress progress) {
this.progress(progress.getProgress());
final double transferred = progress.getTransferred();
final double size = progress.getSize();
invoke(new DefaultMainAction() {
@Override
public void run() {
if(transferred > 0 && size > 0) {
progressBar.setIndeterminate(false);
progressBar.setMaxValue(size);
progressBar.setDoubleValue(transferred);
}
else {
progressBar.setIndeterminate(true);
}
}
});
}
private void progress(final String message) {
this.invoke(new DefaultMainAction() {
@Override
public void run() {
progressField.setAttributedStringValue(NSAttributedString.attributedStringWithAttributes(
message, TRUNCATE_MIDDLE_ATTRIBUTES));
}
});
}
@Override
public void message(final String message) {
final String text;
if(StringUtils.isBlank(message)) {
// Do not display any progress text when transfer is stopped
final Date timestamp = transfer.getTimestamp();
if(null != timestamp) {
text = UserDateFormatterFactory.get().getLongFormat(timestamp.getTime(), false);
}
else {
text = StringUtils.EMPTY;
}
}
else {
text = message;
}
messageField.setAttributedStringValue(NSAttributedString.attributedStringWithAttributes(
text, TRUNCATE_MIDDLE_ATTRIBUTES));
}
private void status(final String status) {
statusField.setAttributedStringValue(NSAttributedString.attributedStringWithAttributes(status,
TRUNCATE_MIDDLE_ATTRIBUTES));
}
private static final NSDictionary NORMAL_FONT_ATTRIBUTES = NSDictionary.dictionaryWithObjectsForKeys(
NSArray.arrayWithObjects(
NSFont.systemFontOfSize(NSFont.smallSystemFontSize()),
TableCellAttributes.PARAGRAPH_STYLE_LEFT_ALIGNMENT_TRUNCATE_TAIL),
NSArray.arrayWithObjects(
NSAttributedString.FontAttributeName,
NSAttributedString.ParagraphStyleAttributeName)
);
private static final NSDictionary HIGHLIGHTED_FONT_ATTRIBUTES = NSDictionary.dictionaryWithObjectsForKeys(
NSArray.arrayWithObjects(
NSFont.systemFontOfSize(NSFont.smallSystemFontSize()),
NSColor.whiteColor(),
TableCellAttributes.PARAGRAPH_STYLE_LEFT_ALIGNMENT_TRUNCATE_TAIL),
NSArray.arrayWithObjects(
NSAttributedString.FontAttributeName,
NSAttributedString.ForegroundColorAttributeName,
NSAttributedString.ParagraphStyleAttributeName)
);
private boolean highlighted;
public boolean isHighlighted() {
return highlighted;
}
public void setHighlighted(final boolean h) {
highlighted = h;
statusField.setTextColor(h ? NSColor.whiteColor() : NSColor.textColor());
progressField.setTextColor(h ? NSColor.whiteColor() : NSColor.darkGrayColor());
messageField.setTextColor(h ? NSColor.whiteColor() : NSColor.darkGrayColor());
this.setMenuHighlighted(h);
}
private void setMenuHighlighted(boolean highlighted) {
for(int i = 0; i < filesPopup.numberOfItems().intValue(); i++) {
filesPopup.itemAtIndex(new NSInteger(i)).setAttributedTitle(
NSAttributedString.attributedStringWithAttributes(filesPopup.itemAtIndex(new NSInteger(i)).title(),
highlighted ? HIGHLIGHTED_FONT_ATTRIBUTES : NORMAL_FONT_ATTRIBUTES)
);
}
}
// ----------------------------------------------------------
// Outlets
// ----------------------------------------------------------
@Outlet
private NSPopUpButton filesPopup;
@Delegate
private AbstractMenuDelegate filesPopupMenuDelegate;
public void setFilesPopup(final NSPopUpButton p) {
this.filesPopup = p;
this.filesPopup.setTarget(this.id());
this.filesPopup.removeAllItems();
final List<TransferItem> items = transfer.getRoots();
for(int i = 0; i < items.size(); i++) {
final TransferItem entry = items.get(i);
final NSMenuItem item = this.filesPopup.menu().addItemWithTitle_action_keyEquivalent(entry.remote.getName(), null, StringUtils.EMPTY);
if(i == 0) {
if(items.size() > 1) {
item.setTitle(String.format("%s (%d more)", entry.remote.getName(), items.size() - 1));
}
else {
item.setTitle(entry.remote.getName());
}
}
else {
item.setTitle(entry.remote.getName());
}
}
this.filesPopupMenuDelegate = new TransferMenuDelegate(transfer);
this.filesPopup.menu().setDelegate(this.filesPopupMenuDelegate.id());
notificationCenter.addObserver(this.id(),
Foundation.selector("filesPopupWillShow:"),
NSPopUpButton.PopUpButtonWillPopUpNotification,
this.filesPopup);
notificationCenter.addObserver(this.id(),
Foundation.selector("filesPopupWillHide:"),
"NSMenuDidEndTrackingNotification",
this.filesPopup.menu());
}
@Action
public void filesPopupWillShow(final NSNotification sender) {
this.setMenuHighlighted(false);
}
@Action
public void filesPopupWillHide(final NSNotification sender) {
this.setMenuHighlighted(highlighted);
}
@Outlet
private NSTextField progressField;
public void setProgressField(final NSTextField f) {
this.progressField = f;
this.progressField.setEditable(false);
this.progressField.setSelectable(false);
this.progressField.setTextColor(NSColor.darkGrayColor());
}
@Outlet
private NSTextField statusField;
public void setStatusField(final NSTextField f) {
this.statusField = f;
this.statusField.setEditable(false);
this.statusField.setSelectable(false);
this.statusField.setTextColor(NSColor.darkGrayColor());
}
@Outlet
private NSTextField messageField;
public void setMessageField(final NSTextField f) {
this.messageField = f;
this.messageField.setEditable(false);
this.messageField.setSelectable(false);
this.messageField.setTextColor(NSColor.darkGrayColor());
}
@Outlet
private NSProgressIndicator progressBar;
public void setProgressBar(final NSProgressIndicator p) {
this.progressBar = p;
this.progressBar.setDisplayedWhenStopped(false);
this.progressBar.setUsesThreadedAnimation(true);
this.progressBar.setControlSize(NSCell.NSSmallControlSize);
this.progressBar.setStyle(NSProgressIndicator.NSProgressIndicatorBarStyle);
this.progressBar.setMinValue(0);
}
@Outlet
private NSImageView statusIconView;
private static final NSImage RED_ICON = IconCacheFactory.<NSImage>get().iconNamed("statusRed.tiff");
private static final NSImage GREEN_ICON = IconCacheFactory.<NSImage>get().iconNamed("statusGreen.tiff");
private static final NSImage YELLOW_ICON = IconCacheFactory.<NSImage>get().iconNamed("statusYellow.tiff");
public void setStatusIconView(final NSImageView statusIconView) {
this.statusIconView = statusIconView;
this.statusIconView.setImage(transfer.isComplete() ? GREEN_ICON : RED_ICON);
}
@Outlet
private NSImageView iconImageView;
public void setIconImageView(final NSImageView iconImageView) {
this.iconImageView = iconImageView;
this.iconImageView.setImage(IconCacheFactory.<NSImage>get().iconNamed(String.format("transfer-%s.tiff", transfer.getType().name()), 32));
}
/**
* The view drawn in the table cell
*/
private NSView progressView;
public void setProgressView(final NSView v) {
this.progressView = v;
}
@Override
public NSView view() {
return this.progressView;
}
}