PageRenderTime 124ms CodeModel.GetById 81ms app.highlight 22ms RepoModel.GetById 18ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/browser/BrowserIORequest.java

#
Java | 345 lines | 250 code | 31 blank | 64 comment | 8 complexity | 2bb462ecb964f85627c4a813525bd794 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, 2003 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 java.io.*;
 27import org.gjt.sp.jedit.io.*;
 28import org.gjt.sp.jedit.*;
 29import org.gjt.sp.util.*;
 30//}}}
 31
 32/**
 33 * A browser I/O request.
 34 * @author Slava Pestov
 35 * @version $Id: BrowserIORequest.java 5053 2004-05-29 01:55:26Z spestov $
 36 */
 37class BrowserIORequest extends WorkRequest
 38{
 39	//{{{ Request types
 40	/**
 41	 * Directory listing I/O request.
 42	 */
 43	public static final int LIST_DIRECTORY = 0;
 44
 45	/**
 46	 * Delete file I/O request.
 47	 */
 48	public static final int DELETE = 1;
 49
 50	/**
 51	 * Rename file I/O request.
 52	 */
 53	public static final int RENAME = 2;
 54
 55	/**
 56	 * Make directory I/O request.
 57	 */
 58	public static final int MKDIR = 3;
 59	//}}}
 60
 61	//{{{ BrowserIORequest constructor
 62	/**
 63	 * Creates a new browser I/O request.
 64	 * @param type The request type
 65	 * @param browser The VFS browser instance
 66	 * @param path1 The first path name to operate on
 67	 * @param path2 The second path name to operate on
 68	 * @param loadInfo A two-element array filled out by the request;
 69	 * element 1 is the canonical path, element 2 is the file list.
 70	 */
 71	BrowserIORequest(int type, VFSBrowser browser,
 72		Object session, VFS vfs, String path1, String path2,
 73		Object[] loadInfo)
 74	{
 75		this.type = type;
 76		this.browser = browser;
 77		this.session = session;
 78		this.vfs = vfs;
 79		this.path1 = path1;
 80		this.path2 = path2;
 81		this.loadInfo = loadInfo;
 82	} //}}}
 83
 84	//{{{ run() method
 85	public void run()
 86	{
 87		switch(type)
 88		{
 89		case LIST_DIRECTORY:
 90			listDirectory();
 91			break;
 92		case DELETE:
 93			delete();
 94			break;
 95		case RENAME:
 96			rename();
 97			break;
 98		case MKDIR:
 99			mkdir();
100			break;
101		}
102	} //}}}
103
104	//{{{ toString() method
105	public String toString()
106	{
107		String typeString;
108		switch(type)
109		{
110		case LIST_DIRECTORY:
111			typeString = "LIST_DIRECTORY";
112			break;
113		case DELETE:
114			typeString = "DELETE";
115			break;
116		case RENAME:
117			typeString = "RENAME";
118			break;
119		case MKDIR:
120			typeString = "MKDIR";
121			break;
122		default:
123			typeString = "UNKNOWN!!!";
124			break;
125		}
126
127		return getClass().getName() + "[type=" + typeString
128			+ ",vfs=" + vfs + ",path1=" + path1
129			+ ",path2=" + path2 + "]";
130	} //}}}
131
132	//{{{ Private members
133
134	//{{{ Instance variables
135	private int type;
136	private VFSBrowser browser;
137	private Object session;
138	private VFS vfs;
139	private String path1;
140	private String path2;
141	private Object[] loadInfo;
142	//}}}
143
144	//{{{ listDirectory() method
145	private void listDirectory()
146	{
147		VFS.DirectoryEntry[] directory = null;
148
149		String[] args = { path1 };
150		setStatus(jEdit.getProperty("vfs.status.listing-directory",args));
151
152		String canonPath = path1;
153
154		try
155		{
156			setAbortable(true);
157
158			canonPath = vfs._canonPath(session,path1,browser);
159			directory = vfs._listDirectory(session,canonPath,browser);
160		}
161		catch(IOException io)
162		{
163			setAbortable(false);
164			Log.log(Log.ERROR,this,io);
165			String[] pp = { io.toString() };
166			VFSManager.error(browser,path1,"ioerror.directory-error",pp);
167		}
168		catch(WorkThread.Abort a)
169		{
170		}
171		finally
172		{
173			try
174			{
175				vfs._endVFSSession(session,browser);
176			}
177			catch(IOException io)
178			{
179				setAbortable(false);
180				Log.log(Log.ERROR,this,io);
181				String[] pp = { io.toString() };
182				VFSManager.error(browser,path1,"ioerror.directory-error",pp);
183			}
184		}
185
186		setAbortable(false);
187
188		loadInfo[0] = canonPath;
189		loadInfo[1] = directory;
190	} //}}}
191
192	//{{{ delete() method
193	private void delete()
194	{
195		try
196		{
197			setAbortable(true);
198			String[] args = { path1 };
199			setStatus(jEdit.getProperty("vfs.status.deleting",args));
200
201			try
202			{
203				path1 = vfs._canonPath(session,path1,browser);
204
205
206				if(!vfs._delete(session,path1,browser))
207					VFSManager.error(browser,path1,"ioerror.delete-error",null);
208			}
209			catch(IOException io)
210			{
211				setAbortable(false);
212				Log.log(Log.ERROR,this,io);
213				String[] pp = { io.toString() };
214				VFSManager.error(browser,path1,"ioerror.directory-error",pp);
215			}
216		}
217		catch(WorkThread.Abort a)
218		{
219		}
220		finally
221		{
222			try
223			{
224				vfs._endVFSSession(session,browser);
225			}
226			catch(IOException io)
227			{
228				setAbortable(false);
229				Log.log(Log.ERROR,this,io);
230				String[] pp = { io.toString() };
231				VFSManager.error(browser,path1,"ioerror.directory-error",pp);
232			}
233		}
234	} //}}}
235
236	//{{{ rename() method
237	private void rename()
238	{
239		try
240		{
241			setAbortable(true);
242			String[] args = { path1, path2 };
243			setStatus(jEdit.getProperty("vfs.status.renaming",args));
244
245			try
246			{
247				path1 = vfs._canonPath(session,path1,browser);
248				path2 = vfs._canonPath(session,path2,browser);
249
250				VFS.DirectoryEntry file = vfs._getDirectoryEntry(
251					session,path2,browser);
252				if(file != null)
253				{
254					if((OperatingSystem.isDOSDerived()
255						|| OperatingSystem.isMacOS())
256						&& path1.equalsIgnoreCase(path2))
257					{
258						// allow user to change name
259						// case
260					}
261					else
262					{
263						VFSManager.error(browser,path1,
264							"ioerror.rename-exists",
265							new String[] { path2 });
266						return;
267					}
268				}
269
270				if(!vfs._rename(session,path1,path2,browser))
271					VFSManager.error(browser,path1,"ioerror.rename-error",
272						new String[] { path2 });
273			}
274			catch(IOException io)
275			{
276				setAbortable(false);
277				Log.log(Log.ERROR,this,io);
278				String[] pp = { io.toString() };
279				VFSManager.error(browser,path1,"ioerror.directory-error",pp);
280			}
281		}
282		catch(WorkThread.Abort a)
283		{
284		}
285		finally
286		{
287			try
288			{
289				vfs._endVFSSession(session,browser);
290			}
291			catch(IOException io)
292			{
293				setAbortable(false);
294				Log.log(Log.ERROR,this,io);
295				String[] pp = { io.toString() };
296				VFSManager.error(browser,path1,"ioerror.directory-error",pp);
297			}
298		}
299	} //}}}
300
301	//{{{ mkdir() method
302	private void mkdir()
303	{
304		try
305		{
306			setAbortable(true);
307			String[] args = { path1 };
308			setStatus(jEdit.getProperty("vfs.status.mkdir",args));
309
310			try
311			{
312				path1 = vfs._canonPath(session,path1,browser);
313
314				if(!vfs._mkdir(session,path1,browser))
315					VFSManager.error(browser,path1,"ioerror.mkdir-error",null);
316			}
317			catch(IOException io)
318			{
319				setAbortable(false);
320				Log.log(Log.ERROR,this,io);
321				args[0] = io.toString();
322				VFSManager.error(browser,path1,"ioerror",args);
323			}
324		}
325		catch(WorkThread.Abort a)
326		{
327		}
328		finally
329		{
330			try
331			{
332				vfs._endVFSSession(session,browser);
333			}
334			catch(IOException io)
335			{
336				setAbortable(false);
337				Log.log(Log.ERROR,this,io);
338				String[] args = { io.toString() };
339				VFSManager.error(browser,path1,"ioerror",args);
340			}
341		}
342	} //}}}
343
344	//}}}
345}