001: /* {{{ MarkersSaveRequest.java - I/O request
002: * :tabSize=8:indentSize=8:noTabs=false:
003: * :folding=explicit:collapseFolds=1:
004: *
005: * based on jEdit.buffer.BufferSaveRequest (Copyright (C) 2000, 2005 Slava Pestov)
006: * Copyright (C) 2005 Martin Raspe
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: * }}} */
021:
022: package org.gjt.sp.jedit.bufferio;
023:
024: //{{{ Imports
025: import java.io.*;
026: import java.util.List;
027:
028: import org.gjt.sp.jedit.*;
029: import org.gjt.sp.jedit.io.*;
030: import org.gjt.sp.util.*;
031:
032: //}}}
033:
034: /**
035: * A save request for markers. Factored out from BufferSaveRequest.java
036: *
037: * @author Martin Raspe
038: * @created May 20, 2005
039: * @modified $Date: 2006/03/10 12:49:17 $ by $Author: hertzhaft $
040: */
041:
042: public class MarkersSaveRequest extends WorkRequest {
043: //{{{ Constants
044: public static final String ERROR_OCCURRED = "MarkersSaveRequest__error";
045:
046: //}}}
047:
048: //{{{ MarkersSaveRequest constructor
049: /**
050: * Creates a new I/O request for markers.
051: * @param view The view
052: * @param buffer The buffer
053: * @param session The VFS session
054: * @param vfs The VFS
055: * @param path The path
056: */
057: public MarkersSaveRequest(View view, Buffer buffer, Object session,
058: VFS vfs, String path) {
059: this .view = view;
060: this .buffer = buffer;
061: this .session = session;
062: this .vfs = vfs;
063: this .path = path;
064: this .markersPath = Buffer.getMarkersPath(vfs, path);
065:
066: } //}}}
067:
068: //{{{ run() method
069: public void run() {
070: OutputStream out = null;
071:
072: try {
073: // the entire save operation can be aborted...
074: setAbortable(true);
075: try {
076: // We only save markers to VFS's that support deletion.
077: // Otherwise, we will accumilate stale marks files.
078: if ((vfs.getCapabilities() & VFS.DELETE_CAP) != 0) {
079: if (buffer.getMarkers().isEmpty())
080: vfs._delete(session, markersPath, view);
081: else {
082: String[] args = { vfs.getFileName(path) };
083: setStatus(jEdit.getProperty(
084: "vfs.status.save-markers", args));
085: setValue(0);
086: out = vfs._createOutputStream(session,
087: markersPath, view);
088: if (out != null)
089: writeMarkers(out);
090: }
091: }
092: } catch (IOException io) {
093: Log.log(Log.ERROR, this , io);
094: buffer.setBooleanProperty(ERROR_OCCURRED, true);
095: }
096: } catch (WorkThread.Abort a) {
097: IOUtilities.closeQuietly(out);
098: buffer.setBooleanProperty(ERROR_OCCURRED, true);
099: }
100: } //}}}
101:
102: //{{{ writeMarkers() method
103: private void writeMarkers(OutputStream out) throws IOException {
104: Writer o = new BufferedWriter(new OutputStreamWriter(out));
105: try {
106: List<Marker> markers = buffer.getMarkers();
107: for (int i = 0; i < markers.size(); i++) {
108: Marker marker = markers.get(i);
109: o.write('!');
110: o.write(marker.getShortcut());
111: o.write(';');
112:
113: String pos = String.valueOf(marker.getPosition());
114: o.write(pos);
115: o.write(';');
116: o.write(pos);
117: o.write('\n');
118: }
119: } finally {
120: o.close();
121: }
122: } //}}}
123:
124: //{{{ Instance variables
125: protected View view;
126: protected Buffer buffer;
127: protected Object session;
128: protected VFS vfs;
129: protected String path;
130: protected String markersPath;
131: //}}}
132:
133: }
|