KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * RemoteBuffer.java
3  *
4  * Copyright (C) 2000-2003 Peter Graves
5  * $Id: RemoteBuffer.java,v 1.8 2003/12/01 00:01:26 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 final class RemoteBuffer extends Buffer implements Constants
27 {
28     private FtpSession session;
29     private Buffer buffer;
30     private final ProgressNotifier progressNotifier;
31     private String JavaDoc 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 JavaDoc 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         // We just want to store the paraemters we're called with here, so we
69
// can call setInitialDotPos() on the "real" buffer later.
70
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; // Report error!
87
}
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 JavaDoc ftpLoadSuccessRunnable = new Runnable JavaDoc() {
113         public void run()
114         {
115             File file = ftpLoadProcess.getFile();
116             String JavaDoc 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             // Success.
127
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 JavaDoc httpLoadSuccessRunnable = new Runnable JavaDoc()
140     {
141         public void run()
142         {
143             File cache = httpLoadProcess.getCache();
144             if (cache != null) {
145                 // Success.
146
// We may have followed a redirect.
147
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                     // Set the buffer's mode.
155
ModeList modeList = Editor.getModeList();
156                     String JavaDoc 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 JavaDoc sshLoadSuccessRunnable = new Runnable JavaDoc() {
188         public void run()
189         {
190             File file = sshLoadProcess.getFile();
191             String JavaDoc 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             // Success.
200
replaceBufferRunnable.run();
201         }
202     };
203
204     private Runnable JavaDoc replaceBufferRunnable = new Runnable JavaDoc() {
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 JavaDoc e) {
215                     buffer.kill();
216                     RemoteBuffer.this.kill();
217                     Runnable JavaDoc r = new Runnable JavaDoc() {
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 JavaDoc getTitle()
264     {
265         return getFile().getHostName();
266     }
267
268     // For the buffer list.
269
public String JavaDoc toString()
270     {
271         return getFile().getHostName();
272     }
273
274     public File getCurrentDirectory()
275     {
276         return Directories.getUserHomeDirectory();
277     }
278 }
279
Popular Tags