001: /*
002: * BufferInsertRequest.java - I/O request
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2000, 2005 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.bufferio;
024:
025: //{{{ Imports
026: import java.io.*;
027: import org.gjt.sp.jedit.io.*;
028: import org.gjt.sp.jedit.*;
029: import org.gjt.sp.util.*;
030:
031: //}}}
032:
033: /**
034: * A buffer insert request.
035: * @author Slava Pestov
036: * @version $Id: BufferInsertRequest.java 9499 2007-05-06 13:03:35Z k_satoda $
037: */
038: public class BufferInsertRequest extends BufferIORequest {
039: //{{{ BufferInsertRequest constructor
040: /**
041: * Creates a new buffer I/O request.
042: * @param view The view
043: * @param buffer The buffer
044: * @param session The VFS session
045: * @param vfs The VFS
046: * @param path The path
047: */
048: public BufferInsertRequest(View view, Buffer buffer,
049: Object session, VFS vfs, String path) {
050: super (view, buffer, session, vfs, path);
051: } //}}}
052:
053: //{{{ run() method
054: public void run() {
055: InputStream in = null;
056: try {
057: String[] args = { vfs.getFileName(path) };
058: setStatus(jEdit.getProperty("vfs.status.load", args));
059: setAbortable(true);
060:
061: path = vfs._canonPath(session, path, view);
062:
063: VFSFile entry = vfs._getFile(session, path, view);
064: long length;
065: if (entry != null)
066: length = entry.getLength();
067: else
068: length = 0L;
069:
070: in = vfs._createInputStream(session, path, false, view);
071: if (in == null)
072: return;
073:
074: final SegmentBuffer seg = read(autodetect(in), length, true);
075:
076: /* we don't do this in Buffer.insert() so that
077: we can insert multiple files at once */
078: VFSManager.runInAWTThread(new Runnable() {
079: public void run() {
080: view.getTextArea().setSelectedText(seg.toString());
081: }
082: });
083: } catch (Exception e) {
084: Log.log(Log.ERROR, this , e);
085: String[] pp = { e.toString() };
086: VFSManager.error(view, path, "ioerror.read-error", pp);
087:
088: buffer.setBooleanProperty(ERROR_OCCURRED, true);
089: } catch (WorkThread.Abort a) {
090: buffer.setBooleanProperty(ERROR_OCCURRED, true);
091: } finally {
092: IOUtilities.closeQuietly(in);
093: try {
094: vfs._endVFSSession(session, view);
095: } catch (Exception e) {
096: Log.log(Log.ERROR, this , e);
097: String[] pp = { e.toString() };
098: VFSManager.error(view, path, "ioerror.read-error", pp);
099:
100: buffer.setBooleanProperty(ERROR_OCCURRED, true);
101: } catch (WorkThread.Abort a) {
102: buffer.setBooleanProperty(ERROR_OCCURRED, true);
103: }
104: }
105: } //}}}
106: }
|