001: /*
002: * RecentFiles.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: RecentFiles.java,v 1.3 2003/06/29 00:19:34 piso Exp $
006: *
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 (at your option) 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.armedbear.j;
023:
024: import java.io.BufferedReader;
025: import java.io.BufferedWriter;
026: import java.io.IOException;
027: import java.io.InputStreamReader;
028: import java.io.OutputStreamWriter;
029: import java.util.ArrayList;
030: import java.util.List;
031:
032: public final class RecentFiles implements Constants {
033: private static final int MAX_ENTRIES = 100;
034:
035: // Singleton.
036: private static RecentFiles instance;
037:
038: private final ArrayList entries = new ArrayList();
039: private final File file;
040: private int version;
041: private boolean changed;
042:
043: private RecentFiles() {
044: file = File.getInstance(Directories.getEditorDirectory(),
045: "recent");
046: if (file != null && file.isFile())
047: load();
048: }
049:
050: public static synchronized RecentFiles getInstance() {
051: if (instance == null)
052: Editor.protect(instance = new RecentFiles());
053: return instance;
054: }
055:
056: public synchronized final List getEntries() {
057: return entries;
058: }
059:
060: private RecentFilesEntry findEntry(File file) {
061: if (file != null) {
062: final int limit = entries.size();
063: for (int i = 0; i < limit; i++) {
064: RecentFilesEntry entry = (RecentFilesEntry) entries
065: .get(i);
066: if (entry.matches(file))
067: return entry;
068: }
069: }
070: return null;
071: }
072:
073: private static boolean excluded(Buffer buffer) {
074: if (buffer.isTransient())
075: return true;
076: if (buffer instanceof Directory)
077: return true;
078: if (buffer instanceof RemoteBuffer)
079: return true;
080: if (buffer.getModeId() == MAN_MODE)
081: return true;
082: if (buffer instanceof ImageBuffer)
083: return true;
084: if (buffer.getModeId() == SEND_MAIL_MODE)
085: return true;
086: if (buffer.isUntitled())
087: return true;
088: File file = buffer.getFile();
089: if (file == null)
090: return true;
091: File parent = file.getParentFile();
092: if (parent != null
093: && parent.equals(Directories.getTempDirectory()))
094: return true;
095: return false;
096: }
097:
098: public synchronized void bufferActivated(Buffer buffer) {
099: if (excluded(buffer))
100: return;
101: File file = buffer.getFile();
102: if (file != null) {
103: long now = System.currentTimeMillis();
104: RecentFilesEntry entry = findEntry(file);
105: if (entry == null)
106: entry = new RecentFilesEntry(file);
107: if (entry.firstVisit == 0)
108: entry.firstVisit = now;
109: entry.lastVisit = now;
110: store(entry);
111: changed = true;
112: }
113: }
114:
115: public synchronized void bufferDeactivated(Buffer buffer,
116: Position dot) {
117: if (dot == null)
118: return;
119: if (excluded(buffer))
120: return;
121: File file = buffer.getFile();
122: if (file != null) {
123: RecentFilesEntry entry = findEntry(file);
124: if (entry != null) {
125: entry.lineNumber = dot.lineNumber();
126: entry.offs = dot.getOffset();
127: store(entry);
128: changed = true;
129: }
130: }
131: }
132:
133: private void store(RecentFilesEntry newEntry) {
134: Debug.assertTrue(newEntry != null);
135: final int limit = entries.size();
136: for (int i = 0; i < limit; i++) {
137: RecentFilesEntry entry = (RecentFilesEntry) entries.get(i);
138: if (entry.equals(newEntry)) {
139: if (i == 0) {
140: entries.set(0, newEntry);
141: return;
142: }
143: entries.remove(i);
144: break;
145: }
146: }
147: // Add new entry.
148: entries.add(0, newEntry);
149: }
150:
151: private void load() {
152: if (file == null)
153: return;
154: if (!file.isFile())
155: return;
156: Debug.assertTrue(entries.size() == 0);
157: try {
158: BufferedReader reader = new BufferedReader(
159: new InputStreamReader(file.getInputStream()));
160: String s = reader.readLine();
161: if (s != null) {
162: try {
163: version = Integer.parseInt(s);
164: } catch (NumberFormatException e) {
165: }
166: if (version == RecentFilesEntry.getVersion()) {
167: while ((s = reader.readLine()) != null) {
168: try {
169: // Constructor will throw exception if string is invalid.
170: RecentFilesEntry entry = new RecentFilesEntry(
171: s);
172: entries.add(entry);
173: } catch (Exception e) {
174: Log.error("malformed recent files entry");
175: }
176: }
177: }
178: }
179: reader.close();
180: } catch (IOException e) {
181: Log.error(e);
182: }
183: if (entries.size() == 0)
184: file.delete();
185: }
186:
187: public synchronized void save() {
188: if (!changed)
189: return;
190: if (file == null)
191: return;
192: if (entries.size() == 0)
193: return;
194: try {
195: BufferedWriter writer = new BufferedWriter(
196: new OutputStreamWriter(file.getOutputStream()));
197: writer.write(String.valueOf(RecentFilesEntry.getVersion()));
198: writer.newLine();
199: int limit = entries.size();
200: if (limit > MAX_ENTRIES)
201: limit = MAX_ENTRIES;
202: for (int i = 0; i < limit; i++) {
203: RecentFilesEntry entry = (RecentFilesEntry) entries
204: .get(i);
205: writer.write(entry.toString());
206: writer.newLine();
207: }
208: writer.flush();
209: writer.close();
210: changed = false;
211: } catch (Exception e) {
212: Log.error(e);
213: }
214: }
215: }
|