001: /*
002: * RemoteBuffer.java
003: *
004: * Copyright (C) 2000-2003 Peter Graves
005: * $Id: RemoteBuffer.java,v 1.8 2003/12/01 00:01:26 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import javax.swing.SwingUtilities;
025:
026: public final class RemoteBuffer extends Buffer implements Constants {
027: private FtpSession session;
028: private Buffer buffer;
029: private final ProgressNotifier progressNotifier;
030: private String ref;
031: private boolean render;
032: private HttpLoadProcess httpLoadProcess;
033: private FtpLoadProcess ftpLoadProcess;
034: private SshLoadProcess sshLoadProcess;
035:
036: public RemoteBuffer(File file) {
037: super ();
038: initializeUndo();
039: setFile(file);
040: type = TYPE_NORMAL;
041: autosaveEnabled = false;
042: readOnly = true;
043: progressNotifier = new StatusBarProgressNotifier(this );
044: setProperty(Property.VERTICAL_RULE, 0);
045: setProperty(Property.SHOW_LINE_NUMBERS, false);
046: }
047:
048: public RemoteBuffer(HttpFile file, String ref) {
049: this (file);
050: this .ref = ref;
051: render = true;
052: }
053:
054: public RemoteBuffer(FtpFile file, FtpSession session) {
055: this (file);
056: this .session = session;
057: }
058:
059: private int initialLineNumber;
060: private int initialOffset;
061:
062: public void setInitialDotPos(int lineNumber, int offset) {
063: // We just want to store the paraemters we're called with here, so we
064: // can call setInitialDotPos() on the "real" buffer later.
065: initialLineNumber = lineNumber;
066: initialOffset = offset;
067: }
068:
069: public int load() {
070: setLoaded(true);
071: mode = Editor.getModeList().getMode(PLAIN_TEXT_MODE);
072: formatter = mode.getFormatter(this );
073:
074: final File file = getFile();
075: if (file instanceof FtpFile) {
076: if (session == null) {
077: Debug.bug("RemoteBuffer.load session is null");
078: session = FtpSession.getSession((FtpFile) file);
079: if (session == null)
080: return LOAD_FAILED; // Report error!
081: }
082: setBusy(true);
083: ftpLoadProcess = new FtpLoadProcess(this , (FtpFile) file,
084: session);
085: ftpLoadProcess.setProgressNotifier(progressNotifier);
086: ftpLoadProcess.setSuccessRunnable(ftpLoadSuccessRunnable);
087: ftpLoadProcess.setErrorRunnable(ftpLoadErrorRunnable);
088: ftpLoadProcess.start();
089: return LOAD_PENDING;
090: } else if (file instanceof HttpFile) {
091: setBusy(true);
092: httpLoadProcess = new HttpLoadProcess(this , (HttpFile) file);
093: httpLoadProcess.setProgressNotifier(progressNotifier);
094: httpLoadProcess.setSuccessRunnable(httpLoadSuccessRunnable);
095: httpLoadProcess.setErrorRunnable(httpLoadErrorRunnable);
096: httpLoadProcess.start();
097: } else if (file instanceof SshFile) {
098: setBusy(true);
099: sshLoadProcess = new SshLoadProcess(this , (SshFile) file);
100: sshLoadProcess.setSuccessRunnable(sshLoadSuccessRunnable);
101: sshLoadProcess.setErrorRunnable(sshLoadErrorRunnable);
102: sshLoadProcess.start();
103: }
104: return LOAD_COMPLETED;
105: }
106:
107: private Runnable ftpLoadSuccessRunnable = new Runnable() {
108: public void run() {
109: File file = ftpLoadProcess.getFile();
110: String listing = ftpLoadProcess.getListing();
111: if (ftpLoadProcess.fileIsDirectory()) {
112: buffer = new Directory(file, listing);
113: buffer.load();
114: } else {
115: File cache = ftpLoadProcess.getCache();
116: if (cache == null)
117: return;
118: buffer = createBuffer(file, cache, listing);
119: }
120: // Success.
121: replaceBufferRunnable.run();
122: }
123: };
124:
125: private ErrorRunnable ftpLoadErrorRunnable = new ErrorRunnable(
126: "Load failed") {
127: public void run() {
128: kill();
129: super .run();
130: }
131: };
132:
133: private Runnable httpLoadSuccessRunnable = new Runnable() {
134: public void run() {
135: File cache = httpLoadProcess.getCache();
136: if (cache != null) {
137: // Success.
138: // We may have followed a redirect.
139: final File file = httpLoadProcess.getFile();
140: setFile(file);
141: if (render) {
142: buffer = WebBuffer
143: .createWebBuffer(file, cache, ref);
144: buffer.load();
145: } else {
146: buffer = createBuffer(file, cache, getListing());
147: // Set the buffer's mode.
148: ModeList modeList = Editor.getModeList();
149: String contentType = httpLoadProcess
150: .getContentType();
151: Mode mode;
152: if (contentType != null
153: && contentType.toLowerCase().startsWith(
154: "text/html"))
155: mode = modeList.getMode(HTML_MODE);
156: else {
157: mode = modeList.getModeForFileName(file
158: .getName());
159: if (mode == null)
160: mode = modeList.getMode(PLAIN_TEXT_MODE);
161: }
162: buffer.setMode(mode);
163: buffer.load();
164: }
165: replaceBufferRunnable.run();
166: }
167: }
168: };
169:
170: private ErrorRunnable httpLoadErrorRunnable = new ErrorRunnable(
171: "Load failed") {
172: public void run() {
173: Log.debug("httpLoadErrorRunnable.run");
174: Editor editor = Editor.currentEditor();
175: if (editor.getBuffer() == RemoteBuffer.this ) {
176: editor.status("");
177: editor.setDefaultCursor();
178: }
179: super .run();
180: kill();
181: }
182: };
183:
184: private Runnable sshLoadSuccessRunnable = new Runnable() {
185: public void run() {
186: File file = sshLoadProcess.getFile();
187: String listing = sshLoadProcess.getListing();
188: if (sshLoadProcess.fileIsDirectory()) {
189: buffer = new Directory(file, listing);
190: buffer.load();
191: } else {
192: File cache = sshLoadProcess.getCache();
193: buffer = createBuffer(file, cache, listing);
194: }
195: // Success.
196: replaceBufferRunnable.run();
197: }
198: };
199:
200: private Runnable replaceBufferRunnable = new Runnable() {
201: public void run() {
202: if (Editor.getBufferList().contains(RemoteBuffer.this )) {
203: int result;
204: try {
205: if (!buffer.initialized())
206: buffer.initialize();
207: result = buffer.load();
208: } catch (OutOfMemoryError e) {
209: buffer.kill();
210: RemoteBuffer.this .kill();
211: Runnable r = new Runnable() {
212: public void run() {
213: MessageDialog
214: .showMessageDialog(
215: Editor.currentEditor(),
216: "Insufficient memory to load buffer",
217: "Error");
218: }
219: };
220: SwingUtilities.invokeLater(r);
221: result = LOAD_FAILED;
222: }
223: if (result == LOAD_COMPLETED) {
224: buffer.setInitialDotPos(initialLineNumber,
225: initialOffset);
226: Editor.getBufferList().replace(RemoteBuffer.this ,
227: buffer);
228: }
229: } else
230: buffer.kill();
231: }
232: };
233:
234: private ErrorRunnable sshLoadErrorRunnable = new ErrorRunnable(
235: "Load failed") {
236: public void run() {
237: if (Editor.getBufferList().contains(RemoteBuffer.this )) {
238: if (isEmpty())
239: kill();
240: else
241: setBusy(false);
242: }
243: if (sshLoadProcess.cancelled()) {
244: for (EditorIterator it = new EditorIterator(); it
245: .hasNext();)
246: it.nextEditor().updateDisplay();
247: Editor.currentEditor().status("Cancelled");
248: } else
249: super .run();
250: }
251: };
252:
253: public void dispose() {
254: if (progressNotifier != null)
255: progressNotifier.cancel();
256: super .dispose();
257: }
258:
259: public String getTitle() {
260: return getFile().getHostName();
261: }
262:
263: // For the buffer list.
264: public String toString() {
265: return getFile().getHostName();
266: }
267:
268: public File getCurrentDirectory() {
269: return Directories.getUserHomeDirectory();
270: }
271: }
|