KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > Autosave


1 /*
2  * Autosave.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: Autosave.java,v 1.2 2002/10/11 14:06:18 piso Exp $
6  *
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 (at your option) 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.armedbear.j;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 public final class Autosave implements Constants
31 {
32     private static final String JavaDoc CATALOG_NAME = "catalog";
33     private static Properties JavaDoc catalog;
34     private static File catalogFile;
35
36     private static boolean autosaveEnabled = true;
37     private static File autosaveDirectory;
38     private static boolean initialized;
39
40     public static synchronized File getAutosaveDirectory()
41     {
42         if (!initialized) {
43             autosaveDirectory =
44                 File.getInstance(Directories.getEditorDirectory(), "autosave");
45             if (autosaveDirectory != null) {
46                 if (!autosaveDirectory.isDirectory()) {
47                     autosaveDirectory.mkdirs();
48                     if (!autosaveDirectory.isDirectory()) {
49                         autosaveDirectory = null;
50                         autosaveEnabled = false;
51                     }
52                 }
53             }
54             initialized = true;
55         }
56         return autosaveDirectory;
57     }
58
59     public static synchronized final boolean isAutosaveEnabled()
60     {
61         return autosaveEnabled;
62     }
63
64     public static synchronized void put(String JavaDoc netPath, String JavaDoc alias)
65     {
66         if (catalog == null)
67             catalog = new Properties JavaDoc();
68         catalog.put(netPath, alias);
69     }
70
71     // Update catalog file when a buffer is renamed.
72
public static synchronized void rename(String JavaDoc oldName, String JavaDoc newName)
73     {
74         if (catalog != null && oldName != null) {
75             String JavaDoc alias = (String JavaDoc) catalog.remove(oldName);
76             if (alias != null) {
77                 catalog.put(newName, alias);
78                 flush();
79             }
80         }
81     }
82
83     public static synchronized void flush()
84     {
85         if (catalogFile == null) {
86             if (getAutosaveDirectory() == null)
87                 return;
88             catalogFile = File.getInstance(getAutosaveDirectory(), CATALOG_NAME);
89         }
90         try {
91             OutputStream JavaDoc out = catalogFile.getOutputStream();
92             catalog.store(out, null);
93             out.flush();
94             out.close();
95         }
96         catch (IOException JavaDoc e) {
97             Log.error(e);
98         }
99     }
100
101     public static synchronized void deleteCatalogFile()
102     {
103         if (catalogFile == null) {
104             if (getAutosaveDirectory() == null)
105                 return;
106             catalogFile = File.getInstance(getAutosaveDirectory(), CATALOG_NAME);
107         }
108         catalogFile.delete();
109     }
110
111     public static synchronized void recover()
112     {
113         if (catalogFile == null) {
114             if (getAutosaveDirectory() == null)
115                 return;
116             catalogFile = File.getInstance(getAutosaveDirectory(), CATALOG_NAME);
117         }
118         if (!catalogFile.exists())
119             return; // No catalog file.
120
if (catalog == null)
121             catalog = new Properties JavaDoc();
122         try {
123             InputStream JavaDoc in = catalogFile.getInputStream();
124             catalog.load(in);
125             in.close();
126         }
127         catch (IOException JavaDoc e) {
128             Log.error(e);
129         }
130         Enumeration JavaDoc e = catalog.propertyNames();
131         while (e.hasMoreElements()) {
132             String JavaDoc netPath = (String JavaDoc) e.nextElement();
133             String JavaDoc alias = catalog.getProperty(netPath);
134             if (alias != null)
135                 queryRecoverFile(netPath, alias);
136         }
137         catalogFile.delete();
138     }
139
140     private static void queryRecoverFile(String JavaDoc netPath, String JavaDoc alias)
141     {
142         File autosaveFile = File.getInstance(getAutosaveDirectory(), alias);
143         if (!autosaveFile.exists()) {
144             // Nothing we can do.
145
catalog.remove(netPath);
146             return;
147         }
148         final File file = File.getInstance(netPath);
149         if (file == null)
150             return; // Shouldn't happen.
151
boolean confirmed = false;
152         File dir = file.getParentFile();
153         if (dir != null && dir.equals(Directories.getDraftsFolder()))
154             confirmed = true;
155         if (!confirmed) {
156             String JavaDoc prompt = "Recover " + netPath + " from autosave file?";
157             int response = ConfirmDialog.showConfirmDialog(null, prompt, "Autosave");
158             if (response == RESPONSE_YES)
159                 confirmed = true;
160         }
161         if (confirmed) {
162             if (file.isRemote()) {
163                 String JavaDoc recoverPath = file.getHostName() + '/' + file.canonicalPath();
164                 Log.debug("recoverPath = |" + recoverPath + "|");
165                 File recoverFile = File.getInstance(getRecoverDirectory(), recoverPath);
166                 Log.debug("recoverFile = |" + recoverFile.netPath() + "|");
167                 File parentDir = recoverFile.getParentFile();
168                 if (!parentDir.isDirectory())
169                     parentDir.mkdirs();
170                 if (parentDir.isDirectory()) {
171                     recoverFile.delete();
172                     // BUG! Error handling! What if rename and copy both fail?
173
if (autosaveFile.renameTo(recoverFile) || Utilities.copyFile(autosaveFile, recoverFile)) {
174                         autosaveFile.delete();
175                         catalog.remove(netPath);
176                         String JavaDoc message = "File has been saved as " + recoverFile.canonicalPath() + ".";
177                         MessageDialog.showMessageDialog(message, "Autosave");
178                     }
179                 }
180             } else {
181                 // BUG! Should back up old file first!
182
file.delete();
183                 // BUG! Error handling! What if rename and copy both fail?
184
if (autosaveFile.renameTo(file) || Utilities.copyFile(autosaveFile, file)) {
185                     autosaveFile.delete();
186                     catalog.remove(netPath);
187                 }
188             }
189         } else {
190             String JavaDoc prompt = "Delete autosave file for " + netPath + "?";
191             int response = ConfirmDialog.showConfirmDialog(null, prompt, "Autosave");
192             if (response == RESPONSE_YES) {
193                 autosaveFile.delete();
194                 catalog.remove(netPath);
195             }
196         }
197     }
198
199     private static final File getRecoverDirectory()
200     {
201         return File.getInstance(Directories.getEditorDirectory(), "recover");
202     }
203 }
204
Popular Tags