KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > bufferio > MarkersSaveRequest


1 /* {{{ MarkersSaveRequest.java - I/O request
2  * :tabSize=8:indentSize=8:noTabs=false:
3  * :folding=explicit:collapseFolds=1:
4  *
5  * based on jEdit.buffer.BufferSaveRequest (Copyright (C) 2000, 2005 Slava Pestov)
6  * Copyright (C) 2005 Martin Raspe
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  * }}} */

21
22 package org.gjt.sp.jedit.bufferio;
23
24 //{{{ Imports
25
import java.io.*;
26 import java.util.Vector JavaDoc;
27 import org.gjt.sp.jedit.*;
28 import org.gjt.sp.jedit.io.*;
29 import org.gjt.sp.util.*;
30 //}}}
31

32 /**
33  * A save request for markers. Factored out from BufferSaveRequest.java
34  *
35  * @author Martin Raspe
36  * @created May 20, 2005
37  * @modified $Date: 2006/03/10 12:49:17 $ by $Author: hertzhaft $
38  */

39
40 public class MarkersSaveRequest extends WorkRequest
41 {
42     //{{{ Constants
43
public static final String JavaDoc ERROR_OCCURRED = "MarkersSaveRequest__error";
44     //}}}
45

46     //{{{ MarkersSaveRequest constructor
47
/**
48      * Creates a new I/O request for markers.
49      * @param view The view
50      * @param buffer The buffer
51      * @param session The VFS session
52      * @param vfs The VFS
53      * @param path The path
54      */

55     public MarkersSaveRequest(View view, Buffer buffer,
56         Object JavaDoc session, VFS vfs, String JavaDoc path)
57     {
58         this.view = view;
59         this.buffer = buffer;
60         this.session = session;
61         this.vfs = vfs;
62         this.path = path;
63         this.markersPath = buffer.getMarkersPath(vfs);
64
65     } //}}}
66

67     //{{{ run() method
68
public void run()
69     {
70         OutputStream out = null;
71
72         try
73         {
74             // the entire save operation can be aborted...
75
setAbortable(true);
76             try
77             {
78                 // We only save markers to VFS's that support deletion.
79
// Otherwise, we will accumilate stale marks files.
80
if((vfs.getCapabilities() & VFS.DELETE_CAP) != 0)
81                 {
82                     if(buffer.getMarkers().isEmpty())
83                         vfs._delete(session,markersPath,view);
84                     else
85                     {
86                         String JavaDoc[] args = { vfs.getFileName(path) };
87                         setStatus(jEdit.getProperty("vfs.status.save-markers",args));
88                         setValue(0);
89                         out = vfs._createOutputStream(session,markersPath,view);
90                         if(out != null)
91                             writeMarkers(out);
92                         }
93                 }
94             }
95             catch(IOException io)
96             {
97                 Log.log(Log.ERROR,this,io);
98                 buffer.setBooleanProperty(ERROR_OCCURRED,true);
99             }
100         }
101         catch(WorkThread.Abort a)
102         {
103             if(out != null)
104             {
105                 try
106                 {
107                     out.close();
108                 }
109                 catch(IOException io)
110                 {
111                 }
112             }
113
114             buffer.setBooleanProperty(ERROR_OCCURRED,true);
115         }
116     } //}}}
117

118     //{{{ writeMarkers() method
119
private void writeMarkers(OutputStream out)
120         throws IOException
121     {
122         Writer o = new BufferedWriter(new OutputStreamWriter(out));
123         try
124         {
125             Vector JavaDoc markers = buffer.getMarkers();
126             for(int i = 0; i < markers.size(); i++)
127             {
128                 Marker marker = (Marker)markers.elementAt(i);
129                 o.write('!');
130                 o.write(marker.getShortcut());
131                 o.write(';');
132
133                 String JavaDoc pos = String.valueOf(marker.getPosition());
134                 o.write(pos);
135                 o.write(';');
136                 o.write(pos);
137                 o.write('\n');
138             }
139         }
140         finally
141         {
142             o.close();
143         }
144     } //}}}
145

146     //{{{ Instance variables
147
protected View view;
148     protected Buffer buffer;
149     protected Object JavaDoc session;
150     protected VFS vfs;
151     protected String JavaDoc path;
152     protected String JavaDoc markersPath;
153     //}}}
154

155 }
156
Popular Tags