KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > FtpSaveProcess


1 /*
2  * FtpSaveProcess.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: FtpSaveProcess.java,v 1.2 2003/07/05 17:42:59 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
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.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import javax.swing.SwingUtilities JavaDoc;
25
26 public class FtpSaveProcess implements BackgroundProcess, Constants
27 {
28     private final Buffer buffer;
29     private final File source;
30     private FtpFile destination;
31     private final FtpSession session;
32
33     // Option for saveAs(), saveCopy().
34
private boolean confirmOverwrite;
35
36     // Option for save().
37
private boolean confirmIfDestinationChanged;
38
39     // Title for message dialogs.
40
private String JavaDoc title;
41
42     private Runnable JavaDoc successRunnable;
43     private ProgressNotifier progressNotifier;
44     private String JavaDoc listing;
45     private boolean force;
46
47     public FtpSaveProcess(Buffer buffer, File source, FtpFile destination,
48                           FtpSession session)
49     {
50         this.buffer = buffer;
51         Debug.assertTrue(buffer != null);
52         this.source = source;
53         Debug.assertTrue(source != null);
54         this.destination = destination;
55         Debug.assertTrue(destination != null);
56         this.session = session;
57         Debug.assertTrue(session != null);
58         Debug.assertTrue(session.isLocked());
59     }
60
61     public final void setConfirmOverwrite(boolean b)
62     {
63         confirmOverwrite = b;
64     }
65
66     public final void setConfirmIfDestinationChanged(boolean b)
67     {
68         confirmIfDestinationChanged = b;
69     }
70
71     public final void setTitle(String JavaDoc s)
72     {
73         title = s;
74     }
75
76     public final void setSuccessRunnable(Runnable JavaDoc r)
77     {
78         successRunnable = r;
79     }
80
81     public final String JavaDoc getListing()
82     {
83         return listing;
84     }
85
86     public void start()
87     {
88         Debug.assertTrue(SwingUtilities.isEventDispatchThread());
89         if (!buffer.isLocked()) {
90             Log.debug("start() buffer is not locked");
91             if (!buffer.lock()) {
92                 MessageDialog.showMessageDialog("Buffer is busy", buffer.getFile().netPath());
93                 session.unlock();
94                 return;
95             }
96             Log.debug("start() buffer locked OK");
97         }
98         Debug.assertTrue(buffer.isLocked());
99         buffer.setBusy(true);
100         for (EditorIterator it = new EditorIterator(); it.hasNext();) {
101             Editor ed = it.nextEditor();
102             if (ed.getBuffer() == buffer)
103                 ed.setWaitCursor();
104         }
105         new Thread JavaDoc(this).start();
106     }
107
108     public void run()
109     {
110         Debug.assertTrue(buffer.isLocked());
111         try {
112             buffer.setBackgroundProcess(this);
113             if (source.isFile())
114                 doSave();
115             buffer.setBackgroundProcess(null);
116         }
117         finally {
118             buffer.unlock();
119         }
120     }
121
122     public void doSave()
123     {
124         Log.debug("doSave force = " + force);
125         Debug.assertTrue(buffer != null);
126         Debug.assertTrue(source != null);
127         Debug.assertTrue(!SwingUtilities.isEventDispatchThread());
128
129         final String JavaDoc hostName = destination.getHostName();
130         progressNotifier = new StatusBarProgressNotifier(buffer);
131         session.setProgressNotifier(progressNotifier);
132         if (!session.verifyConnected()) {
133             String JavaDoc message = session.getErrorText();
134             if (message == null)
135                 message = "Unable to connect to " + hostName + " on port " + destination.getPort();
136             errorRunnable.setMessage(message);
137             buffer.setBusy(false);
138             SwingUtilities.invokeLater(errorRunnable);
139             session.unlock();
140             return;
141         }
142         final boolean destinationExists = session.exists(destination.canonicalPath());
143         int permissions = destinationExists ? session.getPermissions(destination) : 0;
144         if (!force) {
145             if (confirmOverwrite) {
146                 // Check to see if destination file exists on remote host.
147
if (session.exists(destination.canonicalPath())) {
148                     // Confirm before overwriting. Leave the session
149
// locked for now.
150
SwingUtilities.invokeLater(confirmOverwriteRunnable);
151                     return;
152                 }
153             }
154             if (confirmIfDestinationChanged && destinationExists) {
155                 // Check to see if destination file has changed on remote host.
156
String JavaDoc s = session.getDirectoryListingForFile(destination.canonicalPath());
157                 if (!s.equals(buffer.getListing())) {
158                     // Destination file has changed since it was loaded. Leave
159
// the session locked for now.
160
SwingUtilities.invokeLater(confirmDestinationChangedRunnable);
161                     return;
162                 }
163             }
164         }
165         final boolean saveInPlace = buffer.getBooleanProperty(Property.SAVE_IN_PLACE);
166         int result = session.put(source, destination, source.length(), saveInPlace);
167         if (result == SUCCESS) {
168             if (permissions != 0 && !saveInPlace)
169                 session.chmod(destination, permissions);
170             listing = session.getDirectoryListingForFile(destination.canonicalPath());
171             buffer.setBusy(false);
172             if (successRunnable != null)
173                 SwingUtilities.invokeLater(successRunnable);
174         } else if (result == CANCELLED) {
175             SwingUtilities.invokeLater(cancelRunnable);
176         } else {
177             // Error.
178
buffer.setBusy(false);
179             if (errorRunnable != null) {
180                 errorRunnable.setMessage(session.getErrorText());
181                 SwingUtilities.invokeLater(errorRunnable);
182             }
183         }
184         // And in any case...
185
session.unlock();
186     }
187
188     public synchronized void cancel()
189     {
190         if (progressNotifier != null) {
191             progressNotifier.cancel();
192             progressNotifier.progressStop();
193             progressNotifier.setText("Transfer cancelled, cleaning up...");
194         }
195     }
196
197     // Confirm overwrite of existing destination file for saveAs() and
198
// saveCopy().
199
private final Runnable JavaDoc confirmOverwriteRunnable = new Runnable JavaDoc() {
200         public void run()
201         {
202             Debug.assertTrue(SwingUtilities.isEventDispatchThread());
203             Debug.assertTrue(session.isLocked());
204             final Editor editor = Editor.currentEditor();
205             editor.setDefaultCursor();
206             String JavaDoc text = "Overwrite existing file " + destination.getName() + " on " + destination.getHostName() + "?";
207             final boolean confirmed = editor.confirm(title, text);
208             if (confirmed) {
209                 force = true;
210                 start();
211             } else {
212                 buffer.setBusy(false);
213                 session.unlock();
214             }
215         }
216     };
217
218     // Confirm save if destination file has changed on the remote host since
219
// it was loaded.
220
private final Runnable JavaDoc confirmDestinationChangedRunnable = new Runnable JavaDoc() {
221         public void run()
222         {
223             Debug.assertTrue(SwingUtilities.isEventDispatchThread());
224             final Editor editor = Editor.currentEditor();
225             editor.setDefaultCursor();
226             String JavaDoc text = destination.getName() + " has changed on " + destination.getHostName() + ". Save anyway?";
227             final boolean confirmed = editor.confirm(title, text);
228             if (confirmed) {
229                 force = true;
230                 start();
231             } else {
232                 buffer.setBusy(false);
233                 session.unlock();
234             }
235         }
236     };
237
238     private final Runnable JavaDoc cancelRunnable = new Runnable JavaDoc() {
239         public void run()
240         {
241             buffer.setBusy(false);
242             for (EditorIterator it = new EditorIterator(); it.hasNext();) {
243                 Editor ed = it.nextEditor();
244                 if (ed.getBuffer() == buffer) {
245                     ed.status("Transfer cancelled");
246                     ed.setDefaultCursor();
247                 }
248             }
249             MessageDialog.showMessageDialog("Transfer cancelled", title);
250         }
251     };
252
253     private final ErrorRunnable errorRunnable = new ErrorRunnable("Save failed");
254 }
255
Popular Tags