PageRenderTime 63ms CodeModel.GetById 40ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/org/gjt/sp/jedit/browser/BrowserIORequest.java

#
Java | 322 lines | 231 code | 30 blank | 61 comment | 7 complexity | dd2365a97bef57ffa395c2c5ba324d1c MD5 | raw file
  1/*
  2 * BrowserIORequest.java - VFS browser I/O request
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2000 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.browser;
 24
 25//{{{ Imports
 26import javax.swing.tree.DefaultMutableTreeNode;
 27import java.io.*;
 28import org.gjt.sp.jedit.io.*;
 29import org.gjt.sp.jedit.jEdit;
 30import org.gjt.sp.jedit.MiscUtilities;
 31import org.gjt.sp.util.WorkRequest;
 32import org.gjt.sp.util.WorkThread;
 33//}}}
 34
 35/**
 36 * A browser I/O request.
 37 * @author Slava Pestov
 38 * @version $Id: BrowserIORequest.java 3998 2002-01-28 04:20:54Z spestov $
 39 */
 40public class BrowserIORequest extends WorkRequest
 41{
 42	//{{{ Request types
 43	/**
 44	 * Directory listing I/O request.
 45	 */
 46	public static final int LIST_DIRECTORY = 0;
 47
 48	/**
 49	 * Delete file I/O request.
 50	 */
 51	public static final int DELETE = 1;
 52
 53	/**
 54	 * Rename file I/O request.
 55	 */
 56	public static final int RENAME = 2;
 57
 58	/**
 59	 * Make directory I/O request.
 60	 */
 61	public static final int MKDIR = 3;
 62	//}}}
 63
 64	//{{{ BrowserIORequest constructor
 65	/**
 66	 * Creates a new browser I/O request.
 67	 * @param type The request type
 68	 * @param browser The VFS browser instance
 69	 * @param path1 The first path name to operate on
 70	 * @param path2 The second path name to operate on
 71	 * @param node Only used for type == LIST_DIRECTORY
 72	 */
 73	public BrowserIORequest(int type, VFSBrowser browser,
 74		Object session, VFS vfs, String path1, String path2,
 75		DefaultMutableTreeNode node)
 76	{
 77		this.type = type;
 78		this.browser = browser;
 79		this.session = session;
 80		this.vfs = vfs;
 81		this.path1 = path1;
 82		this.path2 = path2;
 83		this.node = node;
 84	} //}}}
 85
 86	//{{{ run() method
 87	public void run()
 88	{
 89		switch(type)
 90		{
 91		case LIST_DIRECTORY:
 92			listDirectory();
 93			break;
 94		case DELETE:
 95			delete();
 96			break;
 97		case RENAME:
 98			rename();
 99			break;
100		case MKDIR:
101			mkdir();
102			break;
103		}
104
105		if(type != LIST_DIRECTORY)
106			browser.endRequest();
107	} //}}}
108
109	//{{{ toString() method
110	public String toString()
111	{
112		String typeString;
113		switch(type)
114		{
115		case LIST_DIRECTORY:
116			typeString = "LIST_DIRECTORY";
117			break;
118		case DELETE:
119			typeString = "DELETE";
120			break;
121		case RENAME:
122			typeString = "RENAME";
123			break;
124		case MKDIR:
125			typeString = "MKDIR";
126			break;
127		default:
128			typeString = "UNKNOWN!!!";
129			break;
130		}
131
132		return getClass().getName() + "[type=" + typeString
133			+ ",vfs=" + vfs + ",path1=" + path1
134			+ ",path2=" + path2 + "]";
135	} //}}}
136
137	//{{{ Private members
138
139	//{{{ Instance variables
140	private int type;
141	private VFSBrowser browser;
142	private Object session;
143	private VFS vfs;
144	private String path1;
145	private String path2;
146	private DefaultMutableTreeNode node;
147	//}}}
148
149	//{{{ listDirectory() method
150	private void listDirectory()
151	{
152		VFS.DirectoryEntry[] directory = null;
153		String[] args = { path1 };
154		setStatus(jEdit.getProperty("vfs.status.listing-directory",args));
155
156		String canonPath = null;
157
158		try
159		{
160			setAbortable(true);
161
162			canonPath = vfs._canonPath(session,path1,browser);
163
164			directory = vfs._listDirectory(session,canonPath,browser);
165		}
166		catch(IOException io)
167		{
168			setAbortable(false);
169			String[] pp = { io.toString() };
170			VFSManager.error(browser,path1,"ioerror.directory-error",pp);
171		}
172		catch(WorkThread.Abort a)
173		{
174		}
175		finally
176		{
177			try
178			{
179				vfs._endVFSSession(session,browser);
180			}
181			catch(IOException io)
182			{
183				setAbortable(false);
184				String[] pp = { io.toString() };
185				VFSManager.error(browser,path1,"ioerror.directory-error",pp);
186			}
187		}
188
189		setAbortable(false);
190		browser.directoryLoaded(node,canonPath,directory);
191	} //}}}
192
193	//{{{ delete() method
194	private void delete()
195	{
196		try
197		{
198			setAbortable(true);
199			String[] args = { path1 };
200			setStatus(jEdit.getProperty("vfs.status.deleting",args));
201
202			try
203			{
204				path1 = vfs._canonPath(session,path1,browser);
205
206
207				if(!vfs._delete(session,path1,browser))
208					VFSManager.error(browser,path1,"ioerror.delete-error",null);
209			}
210			catch(IOException io)
211			{
212				String[] pp = { io.toString() };
213				VFSManager.error(browser,path1,"ioerror.directory-error",pp);
214			}
215		}
216		catch(WorkThread.Abort a)
217		{
218		}
219		finally
220		{
221			try
222			{
223				vfs._endVFSSession(session,browser);
224			}
225			catch(IOException io)
226			{
227				String[] pp = { io.toString() };
228				VFSManager.error(browser,path1,"ioerror.directory-error",pp);
229			}
230		}
231	} //}}}
232
233	//{{{ rename() method
234	private void rename()
235	{
236		try
237		{
238			setAbortable(true);
239			String[] args = { path1, path2 };
240			setStatus(jEdit.getProperty("vfs.status.renaming",args));
241
242			try
243			{
244				path1 = vfs._canonPath(session,path1,browser);
245				path2 = vfs._canonPath(session,path2,browser);
246
247				VFS.DirectoryEntry file = vfs._getDirectoryEntry(
248					session,path2,browser);
249				if(file != null)
250					VFSManager.error(browser,path1,"ioerror.rename-exists",
251						new String[] { path2 });
252				else
253				{
254					if(!vfs._rename(session,path1,path2,browser))
255						VFSManager.error(browser,path1,"ioerror.rename-error",
256							new String[] { path2 });
257				}
258			}
259			catch(IOException io)
260			{
261				String[] pp = { io.toString() };
262				VFSManager.error(browser,path1,"ioerror.directory-error",pp);
263			}
264		}
265		catch(WorkThread.Abort a)
266		{
267		}
268		finally
269		{
270			try
271			{
272				vfs._endVFSSession(session,browser);
273			}
274			catch(IOException io)
275			{
276				String[] pp = { io.toString() };
277				VFSManager.error(browser,path1,"ioerror.directory-error",pp);
278			}
279		}
280	} //}}}
281
282	//{{{ mkdir() method
283	private void mkdir()
284	{
285		try
286		{
287			setAbortable(true);
288			String[] args = { path1 };
289			setStatus(jEdit.getProperty("vfs.status.mkdir",args));
290
291			try
292			{
293				path1 = vfs._canonPath(session,path1,browser);
294
295				if(!vfs._mkdir(session,path1,browser))
296					VFSManager.error(browser,path1,"ioerror.mkdir-error",null);
297			}
298			catch(IOException io)
299			{
300				args[0] = io.toString();
301				VFSManager.error(browser,path1,"ioerror",args);
302			}
303		}
304		catch(WorkThread.Abort a)
305		{
306		}
307		finally
308		{
309			try
310			{
311				vfs._endVFSSession(session,browser);
312			}
313			catch(IOException io)
314			{
315				String[] args = { io.toString() };
316				VFSManager.error(browser,path1,"ioerror",args);
317			}
318		}
319	} //}}}
320
321	//}}}
322}