1 21 22 package org.armedbear.j; 23 24 import javax.swing.SwingUtilities ; 25 26 public final class RemoteBuffer extends Buffer implements Constants 27 { 28 private FtpSession session; 29 private Buffer buffer; 30 private final ProgressNotifier progressNotifier; 31 private String ref; 32 private boolean render; 33 private HttpLoadProcess httpLoadProcess; 34 private FtpLoadProcess ftpLoadProcess; 35 private SshLoadProcess sshLoadProcess; 36 37 public RemoteBuffer(File file) 38 { 39 super(); 40 initializeUndo(); 41 setFile(file); 42 type = TYPE_NORMAL; 43 autosaveEnabled = false; 44 readOnly = true; 45 progressNotifier = new StatusBarProgressNotifier(this); 46 setProperty(Property.VERTICAL_RULE, 0); 47 setProperty(Property.SHOW_LINE_NUMBERS, false); 48 } 49 50 public RemoteBuffer(HttpFile file, String ref) 51 { 52 this(file); 53 this.ref = ref; 54 render = true; 55 } 56 57 public RemoteBuffer(FtpFile file, FtpSession session) 58 { 59 this(file); 60 this.session = session; 61 } 62 63 private int initialLineNumber; 64 private int initialOffset; 65 66 public void setInitialDotPos(int lineNumber, int offset) 67 { 68 initialLineNumber = lineNumber; 71 initialOffset = offset; 72 } 73 74 public int load() 75 { 76 setLoaded(true); 77 mode = Editor.getModeList().getMode(PLAIN_TEXT_MODE); 78 formatter = mode.getFormatter(this); 79 80 final File file = getFile(); 81 if (file instanceof FtpFile) { 82 if (session == null) { 83 Debug.bug("RemoteBuffer.load session is null"); 84 session = FtpSession.getSession((FtpFile) file); 85 if (session == null) 86 return LOAD_FAILED; } 88 setBusy(true); 89 ftpLoadProcess = new FtpLoadProcess(this, (FtpFile)file, session); 90 ftpLoadProcess.setProgressNotifier(progressNotifier); 91 ftpLoadProcess.setSuccessRunnable(ftpLoadSuccessRunnable); 92 ftpLoadProcess.setErrorRunnable(ftpLoadErrorRunnable); 93 ftpLoadProcess.start(); 94 return LOAD_PENDING; 95 } else if (file instanceof HttpFile) { 96 setBusy(true); 97 httpLoadProcess = new HttpLoadProcess(this, (HttpFile)file); 98 httpLoadProcess.setProgressNotifier(progressNotifier); 99 httpLoadProcess.setSuccessRunnable(httpLoadSuccessRunnable); 100 httpLoadProcess.setErrorRunnable(httpLoadErrorRunnable); 101 httpLoadProcess.start(); 102 } else if (file instanceof SshFile) { 103 setBusy(true); 104 sshLoadProcess = new SshLoadProcess(this, (SshFile)file); 105 sshLoadProcess.setSuccessRunnable(sshLoadSuccessRunnable); 106 sshLoadProcess.setErrorRunnable(sshLoadErrorRunnable); 107 sshLoadProcess.start(); 108 } 109 return LOAD_COMPLETED; 110 } 111 112 private Runnable ftpLoadSuccessRunnable = new Runnable () { 113 public void run() 114 { 115 File file = ftpLoadProcess.getFile(); 116 String listing = ftpLoadProcess.getListing(); 117 if (ftpLoadProcess.fileIsDirectory()) { 118 buffer = new Directory(file, listing); 119 buffer.load(); 120 } else { 121 File cache = ftpLoadProcess.getCache(); 122 if (cache == null) 123 return; 124 buffer = createBuffer(file, cache, listing); 125 } 126 replaceBufferRunnable.run(); 128 } 129 }; 130 131 private ErrorRunnable ftpLoadErrorRunnable = new ErrorRunnable("Load failed") { 132 public void run() 133 { 134 kill(); 135 super.run(); 136 } 137 }; 138 139 private Runnable httpLoadSuccessRunnable = new Runnable () 140 { 141 public void run() 142 { 143 File cache = httpLoadProcess.getCache(); 144 if (cache != null) { 145 final File file = httpLoadProcess.getFile(); 148 setFile(file); 149 if (render) { 150 buffer = WebBuffer.createWebBuffer(file, cache, ref); 151 buffer.load(); 152 } else { 153 buffer = createBuffer(file, cache, getListing()); 154 ModeList modeList = Editor.getModeList(); 156 String contentType = httpLoadProcess.getContentType(); 157 Mode mode; 158 if (contentType != null && contentType.toLowerCase().startsWith("text/html")) 159 mode = modeList.getMode(HTML_MODE); 160 else { 161 mode = modeList.getModeForFileName(file.getName()); 162 if (mode == null) 163 mode = modeList.getMode(PLAIN_TEXT_MODE); 164 } 165 buffer.setMode(mode); 166 buffer.load(); 167 } 168 replaceBufferRunnable.run(); 169 } 170 } 171 }; 172 173 private ErrorRunnable httpLoadErrorRunnable = new ErrorRunnable("Load failed") { 174 public void run() 175 { 176 Log.debug("httpLoadErrorRunnable.run"); 177 Editor editor = Editor.currentEditor(); 178 if (editor.getBuffer() == RemoteBuffer.this) { 179 editor.status(""); 180 editor.setDefaultCursor(); 181 } 182 super.run(); 183 kill(); 184 } 185 }; 186 187 private Runnable sshLoadSuccessRunnable = new Runnable () { 188 public void run() 189 { 190 File file = sshLoadProcess.getFile(); 191 String listing = sshLoadProcess.getListing(); 192 if (sshLoadProcess.fileIsDirectory()) { 193 buffer = new Directory(file, listing); 194 buffer.load(); 195 } else { 196 File cache = sshLoadProcess.getCache(); 197 buffer = createBuffer(file, cache, listing); 198 } 199 replaceBufferRunnable.run(); 201 } 202 }; 203 204 private Runnable replaceBufferRunnable = new Runnable () { 205 public void run() 206 { 207 if (Editor.getBufferList().contains(RemoteBuffer.this)) { 208 int result; 209 try { 210 if (!buffer.initialized()) 211 buffer.initialize(); 212 result = buffer.load(); 213 } 214 catch (OutOfMemoryError e) { 215 buffer.kill(); 216 RemoteBuffer.this.kill(); 217 Runnable r = new Runnable () { 218 public void run() 219 { 220 MessageDialog.showMessageDialog( 221 Editor.currentEditor(), 222 "Insufficient memory to load buffer", 223 "Error"); 224 } 225 }; 226 SwingUtilities.invokeLater(r); 227 result = LOAD_FAILED; 228 } 229 if (result == LOAD_COMPLETED) { 230 buffer.setInitialDotPos(initialLineNumber, initialOffset); 231 Editor.getBufferList().replace(RemoteBuffer.this, buffer); 232 } 233 } else 234 buffer.kill(); 235 } 236 }; 237 238 private ErrorRunnable sshLoadErrorRunnable = new ErrorRunnable("Load failed") { 239 public void run() 240 { 241 if (Editor.getBufferList().contains(RemoteBuffer.this)) { 242 if (isEmpty()) 243 kill(); 244 else 245 setBusy(false); 246 } 247 if (sshLoadProcess.cancelled()) { 248 for (EditorIterator it = new EditorIterator(); it.hasNext();) 249 it.nextEditor().updateDisplay(); 250 Editor.currentEditor().status("Cancelled"); 251 } else 252 super.run(); 253 } 254 }; 255 256 public void dispose() 257 { 258 if (progressNotifier != null) 259 progressNotifier.cancel(); 260 super.dispose(); 261 } 262 263 public String getTitle() 264 { 265 return getFile().getHostName(); 266 } 267 268 public String toString() 270 { 271 return getFile().getHostName(); 272 } 273 274 public File getCurrentDirectory() 275 { 276 return Directories.getUserHomeDirectory(); 277 } 278 } 279 | Popular Tags |