1   /**
2    * JHylaFax - A java client for HylaFAX.
3    *
4    * Copyright (C) 2005 by Steffen Pingel <steffenp@gmx.de>
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation; either version 2 of the License, or
9    * (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19   */
20  package net.sf.jhylafax;
21  
22  import static net.sf.jhylafax.JHylaFAX.i18n;
23  import gnu.inet.ftp.TransferListener;
24  import org.xnap.commons.io.ProgressMonitor;
25  import org.xnap.commons.io.SubTaskProgressMonitor;
26  import org.xnap.commons.io.UserAbortException;
27  
28  /**
29   * A mediator that links a {@link gnu.inet.ftp.TransferListener} and
30   * a {@link org.xnap.commons.io.ProgressMonitor} object.
31   * 
32   * @author Steffen Pingel
33   */
34  public class TransferMonitor implements TransferListener {
35  
36  	private SubTaskProgressMonitor monitor;
37  
38  	public TransferMonitor(ProgressMonitor monitor, int amount, long totalSize) {
39  		this.monitor = new SubTaskProgressMonitor(monitor, amount, totalSize);
40  	}
41  
42  	public void transferStarted()
43  	{
44  	}
45  
46  	public void transferCompleted()
47  	{
48  		try {
49  			monitor.done();
50  		}
51  		catch (UserAbortException e) {
52  			// user cancelled transfer, will be handled elsewhere
53  		}
54  	}
55  
56  	public void transfered(long value)
57  	{
58  		try {
59  			monitor.setValue(value);
60  		}
61  		catch (UserAbortException e) {
62  			// may not throw UserAbortException as this will break the server
63  			// communication: this means no way to abort a transfer, somewhat 
64  			// ugly but there is no way to call Putter.cancel() because the 
65  			// Putter object is not accessible
66  			monitor.setText(i18n.tr("Aborting transfer, please wait"));
67  		}
68  	}
69  
70  	public void transferFailed()
71  	{
72  		try {
73  			monitor.done();
74  		}
75  		catch (UserAbortException e) {
76  			// user cancelled transfer, will be handled elsewhere
77  		}
78  	}
79  
80  }