PageRenderTime 140ms CodeModel.GetById 128ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 348 lines | 252 code | 31 blank | 65 comment | 8 complexity | 039f1ea7b3050d46f223c6396d847481 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 4842 2003-08-04 00:23:07Z 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 node Only used for type == LIST_DIRECTORY
 69	 * @param loadInfo A two-element array filled out by the request;
 70	 * element 1 is the canonical path, element 2 is the file list.
 71	 */
 72	BrowserIORequest(int type, VFSBrowser browser,
 73		Object session, VFS vfs, String path1, String path2,
 74		Object node, Object[] loadInfo)
 75	{
 76		this.type = type;
 77		this.browser = browser;
 78		this.session = session;
 79		this.vfs = vfs;
 80		this.path1 = path1;
 81		this.path2 = path2;
 82		this.node = node;
 83		this.loadInfo = loadInfo;
 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
106	//{{{ toString() method
107	public String toString()
108	{
109		String typeString;
110		switch(type)
111		{
112		case LIST_DIRECTORY:
113			typeString = "LIST_DIRECTORY";
114			break;
115		case DELETE:
116			typeString = "DELETE";
117			break;
118		case RENAME:
119			typeString = "RENAME";
120			break;
121		case MKDIR:
122			typeString = "MKDIR";
123			break;
124		default:
125			typeString = "UNKNOWN!!!";
126			break;
127		}
128
129		return getClass().getName() + "[type=" + typeString
130			+ ",vfs=" + vfs + ",path1=" + path1
131			+ ",path2=" + path2 + "]";
132	} //}}}
133
134	//{{{ Private members
135
136	//{{{ Instance variables
137	private int type;
138	private VFSBrowser browser;
139	private Object session;
140	private VFS vfs;
141	private String path1;
142	private String path2;
143	private Object node;
144	private Object[] loadInfo;
145	//}}}
146
147	//{{{ listDirectory() method
148	private void listDirectory()
149	{
150		VFS.DirectoryEntry[] directory = null;
151
152		String[] args = { path1 };
153		setStatus(jEdit.getProperty("vfs.status.listing-directory",args));
154
155		String canonPath = path1;
156
157		try
158		{
159			setAbortable(true);
160
161			canonPath = vfs._canonPath(session,path1,browser);
162			directory = vfs._listDirectory(session,canonPath,browser);
163		}
164		catch(IOException io)
165		{
166			setAbortable(false);
167			Log.log(Log.ERROR,this,io);
168			String[] pp = { io.toString() };
169			VFSManager.error(browser,path1,"ioerror.directory-error",pp);
170		}
171		catch(WorkThread.Abort a)
172		{
173		}
174		finally
175		{
176			try
177			{
178				vfs._endVFSSession(session,browser);
179			}
180			catch(IOException io)
181			{
182				setAbortable(false);
183				Log.log(Log.ERROR,this,io);
184				String[] pp = { io.toString() };
185				VFSManager.error(browser,path1,"ioerror.directory-error",pp);
186			}
187		}
188
189		setAbortable(false);
190
191		loadInfo[0] = canonPath;
192		loadInfo[1] = directory;
193	} //}}}
194
195	//{{{ delete() method
196	private void delete()
197	{
198		try
199		{
200			setAbortable(true);
201			String[] args = { path1 };
202			setStatus(jEdit.getProperty("vfs.status.deleting",args));
203
204			try
205			{
206				path1 = vfs._canonPath(session,path1,browser);
207
208
209				if(!vfs._delete(session,path1,browser))
210					VFSManager.error(browser,path1,"ioerror.delete-error",null);
211			}
212			catch(IOException io)
213			{
214				setAbortable(false);
215				Log.log(Log.ERROR,this,io);
216				String[] pp = { io.toString() };
217				VFSManager.error(browser,path1,"ioerror.directory-error",pp);
218			}
219		}
220		catch(WorkThread.Abort a)
221		{
222		}
223		finally
224		{
225			try
226			{
227				vfs._endVFSSession(session,browser);
228			}
229			catch(IOException io)
230			{
231				setAbortable(false);
232				Log.log(Log.ERROR,this,io);
233				String[] pp = { io.toString() };
234				VFSManager.error(browser,path1,"ioerror.directory-error",pp);
235			}
236		}
237	} //}}}
238
239	//{{{ rename() method
240	private void rename()
241	{
242		try
243		{
244			setAbortable(true);
245			String[] args = { path1, path2 };
246			setStatus(jEdit.getProperty("vfs.status.renaming",args));
247
248			try
249			{
250				path1 = vfs._canonPath(session,path1,browser);
251				path2 = vfs._canonPath(session,path2,browser);
252
253				VFS.DirectoryEntry file = vfs._getDirectoryEntry(
254					session,path2,browser);
255				if(file != null)
256				{
257					if((OperatingSystem.isDOSDerived()
258						|| OperatingSystem.isMacOS())
259						&& path1.equalsIgnoreCase(path2))
260					{
261						// allow user to change name
262						// case
263					}
264					else
265					{
266						VFSManager.error(browser,path1,
267							"ioerror.rename-exists",
268							new String[] { path2 });
269						return;
270					}
271				}
272
273				if(!vfs._rename(session,path1,path2,browser))
274					VFSManager.error(browser,path1,"ioerror.rename-error",
275						new String[] { path2 });
276			}
277			catch(IOException io)
278			{
279				setAbortable(false);
280				Log.log(Log.ERROR,this,io);
281				String[] pp = { io.toString() };
282				VFSManager.error(browser,path1,"ioerror.directory-error",pp);
283			}
284		}
285		catch(WorkThread.Abort a)
286		{
287		}
288		finally
289		{
290			try
291			{
292				vfs._endVFSSession(session,browser);
293			}
294			catch(IOException io)
295			{
296				setAbortable(false);
297				Log.log(Log.ERROR,this,io);
298				String[] pp = { io.toString() };
299				VFSManager.error(browser,path1,"ioerror.directory-error",pp);
300			}
301		}
302	} //}}}
303
304	//{{{ mkdir() method
305	private void mkdir()
306	{
307		try
308		{
309			setAbortable(true);
310			String[] args = { path1 };
311			setStatus(jEdit.getProperty("vfs.status.mkdir",args));
312
313			try
314			{
315				path1 = vfs._canonPath(session,path1,browser);
316
317				if(!vfs._mkdir(session,path1,browser))
318					VFSManager.error(browser,path1,"ioerror.mkdir-error",null);
319			}
320			catch(IOException io)
321			{
322				setAbortable(false);
323				Log.log(Log.ERROR,this,io);
324				args[0] = io.toString();
325				VFSManager.error(browser,path1,"ioerror",args);
326			}
327		}
328		catch(WorkThread.Abort a)
329		{
330		}
331		finally
332		{
333			try
334			{
335				vfs._endVFSSession(session,browser);
336			}
337			catch(IOException io)
338			{
339				setAbortable(false);
340				Log.log(Log.ERROR,this,io);
341				String[] args = { io.toString() };
342				VFSManager.error(browser,path1,"ioerror",args);
343			}
344		}
345	} //}}}
346
347	//}}}
348}