PageRenderTime 19ms CodeModel.GetById 13ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bufferio/BufferInsertRequest.java

#
Java | 125 lines | 74 code | 11 blank | 40 comment | 4 complexity | 933038c566b42da3efb19c69b8670637 MD5 | raw file
  1/*
  2 * BufferInsertRequest.java - I/O request
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2000, 2005 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.bufferio;
 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 buffer insert request.
 34 * @author Slava Pestov
 35 * @version $Id: BufferInsertRequest.java 12504 2008-04-22 23:12:43Z ezust $
 36 */
 37public class BufferInsertRequest extends BufferIORequest
 38{
 39	//{{{ BufferInsertRequest constructor
 40	/**
 41	 * Creates a new buffer I/O request.
 42	 * @param view The view
 43	 * @param buffer The buffer
 44	 * @param session The VFS session
 45	 * @param vfs The VFS
 46	 * @param path The path
 47	 */
 48	public BufferInsertRequest(View view, Buffer buffer,
 49		Object session, VFS vfs, String path)
 50	{
 51		super(view,buffer,session,vfs,path);
 52	} //}}}
 53
 54	//{{{ run() method
 55	public void run()
 56	{
 57		InputStream in = null;
 58		try
 59		{
 60			String[] args = { vfs.getFileName(path) };
 61			setStatus(jEdit.getProperty("vfs.status.load",args));
 62			setAbortable(true);
 63
 64			path = vfs._canonPath(session,path,view);
 65
 66			VFSFile entry = vfs._getFile(
 67				session,path,view);
 68			long length;
 69			if(entry != null)
 70				length = entry.getLength();
 71			else
 72				length = 0L;
 73
 74			in = vfs._createInputStream(session,path,false,view);
 75			if(in == null)
 76				return;
 77
 78			final SegmentBuffer seg = read(
 79				autodetect(in),length,true);
 80
 81			/* we don't do this in Buffer.insert() so that
 82			   we can insert multiple files at once */
 83			VFSManager.runInAWTThread(new Runnable()
 84			{
 85				public void run()
 86				{
 87					view.getTextArea().setSelectedText(
 88						seg.toString());
 89				}
 90			});
 91		}
 92		catch(Exception e)
 93		{
 94			Log.log(Log.ERROR,this,e);
 95			String[] pp = { e.toString() };
 96			VFSManager.error(view,path,"ioerror.read-error",pp);
 97
 98			buffer.setBooleanProperty(ERROR_OCCURRED,true);
 99		}
100		catch(WorkThread.Abort a)
101		{
102			buffer.setBooleanProperty(ERROR_OCCURRED,true);
103		}
104		finally
105		{
106			IOUtilities.closeQuietly(in);
107			try
108			{
109				vfs._endVFSSession(session,view);
110			}
111			catch(Exception e)
112			{
113				Log.log(Log.ERROR,this,e);
114				String[] pp = { e.toString() };
115				VFSManager.error(view,path,"ioerror.read-error",pp);
116
117				buffer.setBooleanProperty(ERROR_OCCURRED,true);
118			}
119			catch(WorkThread.Abort a)
120			{
121				buffer.setBooleanProperty(ERROR_OCCURRED,true);
122			}
123		}
124	} //}}}
125}