1 21 22 package org.armedbear.j; 23 24 import javax.swing.SwingUtilities ; 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 private boolean confirmOverwrite; 35 36 private boolean confirmIfDestinationChanged; 38 39 private String title; 41 42 private Runnable successRunnable; 43 private ProgressNotifier progressNotifier; 44 private String 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 s) 72 { 73 title = s; 74 } 75 76 public final void setSuccessRunnable(Runnable r) 77 { 78 successRunnable = r; 79 } 80 81 public final String 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 (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 hostName = destination.getHostName(); 130 progressNotifier = new StatusBarProgressNotifier(buffer); 131 session.setProgressNotifier(progressNotifier); 132 if (!session.verifyConnected()) { 133 String 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 if (session.exists(destination.canonicalPath())) { 148 SwingUtilities.invokeLater(confirmOverwriteRunnable); 151 return; 152 } 153 } 154 if (confirmIfDestinationChanged && destinationExists) { 155 String s = session.getDirectoryListingForFile(destination.canonicalPath()); 157 if (!s.equals(buffer.getListing())) { 158 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 buffer.setBusy(false); 179 if (errorRunnable != null) { 180 errorRunnable.setMessage(session.getErrorText()); 181 SwingUtilities.invokeLater(errorRunnable); 182 } 183 } 184 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 private final Runnable confirmOverwriteRunnable = new Runnable () { 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 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 private final Runnable confirmDestinationChangedRunnable = new Runnable () { 221 public void run() 222 { 223 Debug.assertTrue(SwingUtilities.isEventDispatchThread()); 224 final Editor editor = Editor.currentEditor(); 225 editor.setDefaultCursor(); 226 String 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 cancelRunnable = new Runnable () { 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 |