1 package ch
.cyberduck
.core
.threading
;
4 * Copyright (c) 2002-2013 David Kocher. All rights reserved.
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 * feedback@cyberduck.ch
21 import ch
.cyberduck
.core
.LocaleFactory
;
22 import ch
.cyberduck
.core
.Path
;
23 import ch
.cyberduck
.core
.exception
.BackgroundException
;
24 import ch
.cyberduck
.core
.exception
.ConnectionCanceledException
;
26 import org
.apache
.log4j
.Logger
;
28 import java
.util
.HashSet
;
29 import java
.util
.List
;
35 public abstract class AbstractBackgroundAction
<T
> implements BackgroundAction
<T
> {
36 private static final Logger log
= Logger
.getLogger(AbstractBackgroundAction
.class);
40 protected final Set
<BackgroundActionListener
> listeners
41 = new HashSet
<BackgroundActionListener
>();
55 public void cancel() {
56 if(log
.isDebugEnabled()) {
57 log
.debug(String
.format("Cancel background task %s", this));
59 final BackgroundActionListener
[] l
= listeners
.toArray(
60 new BackgroundActionListener
[listeners
.size()]);
61 for(BackgroundActionListener listener
: l
) {
62 listener
.cancel(this);
64 state
= State
.canceled
;
68 * To be overridden by a concrete subclass. Returns false by default for actions
69 * not connected to a graphical user interface
71 * @return True if the user canceled this action
74 public boolean isCanceled() {
75 return state
== State
.canceled
;
79 public boolean isRunning() {
80 return state
== State
.running
;
84 public void prepare() throws ConnectionCanceledException
{
85 if(log
.isDebugEnabled()) {
86 log
.debug(String
.format("Prepare background task %s", this));
88 final BackgroundActionListener
[] l
= listeners
.toArray(
89 new BackgroundActionListener
[listeners
.size()]);
90 for(BackgroundActionListener listener
: l
) {
93 state
= State
.running
;
97 public void finish() {
98 if(log
.isDebugEnabled()) {
99 log
.debug(String
.format("Finish background task %s", this));
101 final BackgroundActionListener
[] l
= listeners
.toArray(
102 new BackgroundActionListener
[listeners
.size()]);
103 for(BackgroundActionListener listener
: l
) {
106 state
= State
.stopped
;
110 public boolean alert() {
115 public T
call() throws BackgroundException
{
116 if(log
.isDebugEnabled()) {
117 log
.debug(String
.format("Run background task %s", this));
123 public void cleanup() {
127 protected String
toString(final List
<Path
> files
) {
128 final StringBuilder name
= new StringBuilder();
129 name
.append(files
.get(0).getName());
130 if(files
.size() > 1) {
133 return name
.toString();
137 public void addListener(final BackgroundActionListener listener
) {
138 listeners
.add(listener
);
142 public void removeListener(final BackgroundActionListener listener
) {
143 listeners
.remove(listener
);
147 public String
getActivity() {
148 return LocaleFactory
.localizedString("Unknown");
152 public String
getName() {
153 return LocaleFactory
.localizedString("Unknown");
157 public Object
lock() {
158 // No synchronization with other tasks by default