Lines 38-44
Link Here
|
38 |
* Version 2 license, then the option applies only if the new code is |
38 |
* Version 2 license, then the option applies only if the new code is |
39 |
* made subject to such option by the copyright holder. |
39 |
* made subject to such option by the copyright holder. |
40 |
*/ |
40 |
*/ |
41 |
|
|
|
42 |
package org.netbeans.modules.openfile; |
41 |
package org.netbeans.modules.openfile; |
43 |
|
42 |
|
44 |
import java.beans.PropertyChangeEvent; |
43 |
import java.beans.PropertyChangeEvent; |
Lines 48-54
Link Here
|
48 |
import org.openide.windows.CloneableTopComponent; |
47 |
import org.openide.windows.CloneableTopComponent; |
49 |
import org.openide.windows.TopComponent; |
48 |
import org.openide.windows.TopComponent; |
50 |
import java.beans.PropertyChangeListener; |
49 |
import java.beans.PropertyChangeListener; |
51 |
import java.net.URL; |
50 |
import java.io.File; |
52 |
import java.util.ArrayList; |
51 |
import java.util.ArrayList; |
53 |
import java.util.Collections; |
52 |
import java.util.Collections; |
54 |
import java.util.Iterator; |
53 |
import java.util.Iterator; |
Lines 57-63
Link Here
|
57 |
import java.util.logging.Logger; |
56 |
import java.util.logging.Logger; |
58 |
import java.util.prefs.Preferences; |
57 |
import java.util.prefs.Preferences; |
59 |
import org.openide.filesystems.FileObject; |
58 |
import org.openide.filesystems.FileObject; |
60 |
import org.openide.filesystems.URLMapper; |
59 |
import org.openide.filesystems.FileUtil; |
61 |
import org.openide.util.NbPreferences; |
60 |
import org.openide.util.NbPreferences; |
62 |
import org.openide.windows.WindowManager; |
61 |
import org.openide.windows.WindowManager; |
63 |
|
62 |
|
Lines 70-99
Link Here
|
70 |
|
69 |
|
71 |
/** List of recently closed files */ |
70 |
/** List of recently closed files */ |
72 |
private static List<HistoryItem> history = new ArrayList<HistoryItem>(); |
71 |
private static List<HistoryItem> history = new ArrayList<HistoryItem>(); |
73 |
|
|
|
74 |
/** Preferences node for storing history info */ |
72 |
/** Preferences node for storing history info */ |
75 |
private static Preferences prefs; |
73 |
private static Preferences prefs; |
76 |
|
|
|
77 |
private static final Object HISTORY_LOCK = new Object(); |
74 |
private static final Object HISTORY_LOCK = new Object(); |
78 |
|
|
|
79 |
/** Name of preferences node where we persist history */ |
75 |
/** Name of preferences node where we persist history */ |
80 |
private static final String PREFS_NODE = "RecentFilesHistory"; //NOI18N |
76 |
private static final String PREFS_NODE = "RecentFilesHistory"; //NOI18N |
81 |
|
|
|
82 |
/** Prefix of property for recent file URL*/ |
77 |
/** Prefix of property for recent file URL*/ |
83 |
private static final String PROP_URL_PREFIX = "RecentFilesURL."; //NOI18N |
78 |
private static final String PROP_URL_PREFIX = "RecentFilesURL."; //NOI18N |
84 |
|
|
|
85 |
/** Separator to encode file path and time into one string in preferences */ |
86 |
private static final String SEPARATOR = "; time="; |
87 |
|
88 |
/** Boundary for items count in history */ |
79 |
/** Boundary for items count in history */ |
89 |
static final int MAX_HISTORY_ITEMS = 15; |
80 |
static final int MAX_HISTORY_ITEMS = 15; |
90 |
|
81 |
|
91 |
private RecentFiles () { |
82 |
private RecentFiles() { |
92 |
} |
83 |
} |
93 |
|
84 |
|
94 |
/** Starts to listen for recently closed files */ |
85 |
/** Starts to listen for recently closed files */ |
95 |
public static void init () { |
86 |
public static void init() { |
96 |
WindowManager.getDefault().invokeWhenUIReady(new Runnable() { |
87 |
WindowManager.getDefault().invokeWhenUIReady(new Runnable() { |
|
|
88 |
|
97 |
public void run() { |
89 |
public void run() { |
98 |
List<HistoryItem> loaded = load(); |
90 |
List<HistoryItem> loaded = load(); |
99 |
synchronized (HISTORY_LOCK) { |
91 |
synchronized (HISTORY_LOCK) { |
Lines 105-116
Link Here
|
105 |
} |
97 |
} |
106 |
|
98 |
|
107 |
/** Returns read-only list of recently closed files */ |
99 |
/** Returns read-only list of recently closed files */ |
108 |
public static List<HistoryItem> getRecentFiles () { |
100 |
public static List<HistoryItem> getRecentFiles() { |
109 |
synchronized (HISTORY_LOCK) { |
101 |
synchronized (HISTORY_LOCK) { |
110 |
checkHistory(false); |
102 |
checkHistory(); |
111 |
return Collections.unmodifiableList(history); |
103 |
return Collections.unmodifiableList(history); |
112 |
} |
104 |
} |
113 |
} |
105 |
} |
|
|
106 |
private static volatile boolean historyProbablyValid; |
114 |
|
107 |
|
115 |
/** |
108 |
/** |
116 |
* True if there are probably some recently closed files. |
109 |
* True if there are probably some recently closed files. |
Lines 118-152
Link Here
|
118 |
* but this is much faster than calling {@link #getRecentFiles}. |
111 |
* but this is much faster than calling {@link #getRecentFiles}. |
119 |
*/ |
112 |
*/ |
120 |
public static boolean hasRecentFiles() { |
113 |
public static boolean hasRecentFiles() { |
121 |
synchronized (HISTORY_LOCK) { |
114 |
if (!historyProbablyValid) { |
122 |
checkHistory(true); |
115 |
synchronized (HISTORY_LOCK) { |
123 |
return !history.isEmpty(); |
116 |
checkHistory(); |
|
|
117 |
return !history.isEmpty(); |
118 |
} |
124 |
} |
119 |
} |
|
|
120 |
return historyProbablyValid; |
125 |
} |
121 |
} |
126 |
|
122 |
|
127 |
/** Loads list of recent files stored in previous system sessions. |
123 |
/** Loads list of recent files stored in previous system sessions. |
128 |
* @return list of stored recent files |
124 |
* @return list of stored recent files |
129 |
*/ |
125 |
*/ |
130 |
static List<HistoryItem> load () { |
126 |
static List<HistoryItem> load() { |
131 |
String[] keys; |
127 |
String[] keys; |
132 |
Preferences _prefs = getPrefs(); |
128 |
Preferences _prefs = getPrefs(); |
133 |
try { |
129 |
try { |
134 |
keys = _prefs.keys(); |
130 |
keys = _prefs.keys(); |
135 |
} |
131 |
} catch (BackingStoreException ex) { |
136 |
catch (BackingStoreException ex) { |
|
|
137 |
Logger.getLogger(RecentFiles.class.getName()).log(Level.FINE, ex.getMessage(), ex); |
132 |
Logger.getLogger(RecentFiles.class.getName()).log(Level.FINE, ex.getMessage(), ex); |
138 |
return Collections.emptyList(); |
133 |
return Collections.emptyList(); |
139 |
} |
134 |
} |
140 |
|
135 |
|
141 |
List<HistoryItem> result = new ArrayList<HistoryItem>(); |
136 |
List<HistoryItem> result = new ArrayList<HistoryItem>(); |
142 |
for (String curKey : keys) { |
137 |
for (String curKey : keys) { |
143 |
String value = _prefs.get(curKey, null); |
138 |
String value = _prefs.get(curKey, null); |
144 |
if (curKey.startsWith(PROP_URL_PREFIX) && (value != null)){ |
139 |
if (value != null) { |
145 |
try { |
140 |
try { |
146 |
int id = new Integer(curKey.substring(PROP_URL_PREFIX.length())).intValue(); |
141 |
int id = new Integer(curKey.substring(PROP_URL_PREFIX.length())).intValue(); |
147 |
HistoryItem hItem = new HistoryItem(id, new URL(value)); |
142 |
HistoryItem hItem = new HistoryItem(id, value); |
148 |
int ind = result.indexOf(hItem); |
143 |
int ind = result.indexOf(hItem); |
149 |
if (ind == -1){ |
144 |
if (ind == -1) { |
150 |
result.add(hItem); |
145 |
result.add(hItem); |
151 |
} else { |
146 |
} else { |
152 |
_prefs.remove(PROP_URL_PREFIX + Math.max(result.get(ind).id, id)); |
147 |
_prefs.remove(PROP_URL_PREFIX + Math.max(result.get(ind).id, id)); |
Lines 163-230
Link Here
|
163 |
} |
158 |
} |
164 |
Collections.sort(result); |
159 |
Collections.sort(result); |
165 |
store(result); |
160 |
store(result); |
166 |
|
161 |
|
167 |
return result; |
162 |
return result; |
168 |
} |
163 |
} |
169 |
|
164 |
|
170 |
static void store () { |
165 |
static void store() { |
171 |
store(history); |
166 |
store(history); |
172 |
} |
167 |
} |
173 |
|
168 |
|
174 |
static void store (List<HistoryItem> history) { |
169 |
static void store(List<HistoryItem> history) { |
175 |
Preferences _prefs = getPrefs(); |
170 |
Preferences _prefs = getPrefs(); |
176 |
for(int i = 0; i < history.size(); i++){ |
171 |
for (int i = 0; i < history.size(); i++) { |
177 |
HistoryItem hi = history.get(i); |
172 |
HistoryItem hi = history.get(i); |
178 |
if ((hi.id != i) && (hi.id >= history.size())){ |
173 |
if ((hi.id != i) && (hi.id >= history.size())) { |
179 |
_prefs.remove(PROP_URL_PREFIX + hi.id); |
174 |
_prefs.remove(PROP_URL_PREFIX + hi.id); |
180 |
} |
175 |
} |
181 |
hi.id = i; |
176 |
hi.id = i; |
182 |
_prefs.put(PROP_URL_PREFIX + i, hi.getURL().toExternalForm()); |
177 |
_prefs.put(PROP_URL_PREFIX + i, hi.getPath()); |
183 |
} |
178 |
} |
184 |
} |
179 |
} |
185 |
|
180 |
|
186 |
static Preferences getPrefs () { |
181 |
static Preferences getPrefs() { |
187 |
if (prefs == null) { |
182 |
if (prefs == null) { |
188 |
prefs = NbPreferences.forModule(RecentFiles.class).node(PREFS_NODE); |
183 |
prefs = NbPreferences.forModule(RecentFiles.class).node(PREFS_NODE); |
189 |
} |
184 |
} |
190 |
return prefs; |
185 |
return prefs; |
191 |
} |
186 |
} |
192 |
|
187 |
|
193 |
/** Adds file represented by given TopComponent to the list, |
188 |
/** Adds file represented by given TopComponent to the list, |
194 |
* if conditions are met. |
189 |
* if conditions are met. |
195 |
*/ |
190 |
*/ |
196 |
private static void addFile (TopComponent tc) { |
191 |
private static void addFile(TopComponent tc) { |
197 |
if (tc instanceof CloneableTopComponent) { |
192 |
if (tc instanceof CloneableTopComponent) { |
198 |
addFile(obtainURL(tc)); |
193 |
addFile(obtainPath(tc)); |
199 |
} |
194 |
} |
200 |
} |
195 |
} |
201 |
|
196 |
|
202 |
static void addFile (URL fileURL){ |
197 |
static void addFile(String path) { |
203 |
if (fileURL != null) { |
198 |
if (path != null) { |
204 |
synchronized (HISTORY_LOCK) { |
199 |
historyProbablyValid = false; |
205 |
// avoid duplicates |
200 |
synchronized (HISTORY_LOCK) { |
206 |
HistoryItem hItem = null; |
201 |
// avoid duplicates |
207 |
do{ |
202 |
HistoryItem hItem = null; |
208 |
hItem = findHistoryItem(fileURL); |
203 |
do { |
209 |
}while(history.remove(hItem)); |
204 |
hItem = findHistoryItem(path); |
|
|
205 |
} while (history.remove(hItem)); |
210 |
|
206 |
|
211 |
hItem = new HistoryItem(0, fileURL); |
207 |
hItem = new HistoryItem(0, path); |
212 |
history.add(0, hItem); |
208 |
history.add(0, hItem); |
213 |
for(int i = MAX_HISTORY_ITEMS; i<history.size(); i++){ |
209 |
for (int i = MAX_HISTORY_ITEMS; i < history.size(); i++) { |
214 |
history.remove(i); |
210 |
history.remove(i); |
215 |
} |
|
|
216 |
store(); |
217 |
} |
211 |
} |
|
|
212 |
store(); |
218 |
} |
213 |
} |
|
|
214 |
} |
219 |
} |
215 |
} |
220 |
|
216 |
|
221 |
/** Removes file represented by given TopComponent from the list */ |
217 |
/** Removes file represented by given TopComponent from the list */ |
222 |
private static void removeFile (TopComponent tc) { |
218 |
private static void removeFile(TopComponent tc) { |
|
|
219 |
historyProbablyValid = false; |
223 |
if (tc instanceof CloneableTopComponent) { |
220 |
if (tc instanceof CloneableTopComponent) { |
224 |
URL fileURL = obtainURL(tc); |
221 |
String path = obtainPath(tc); |
225 |
if (fileURL != null) { |
222 |
if (path != null) { |
226 |
synchronized (HISTORY_LOCK) { |
223 |
synchronized (HISTORY_LOCK) { |
227 |
HistoryItem hItem = findHistoryItem(fileURL); |
224 |
HistoryItem hItem = findHistoryItem(path); |
228 |
if (hItem != null) { |
225 |
if (hItem != null) { |
229 |
history.remove(hItem); |
226 |
history.remove(hItem); |
230 |
} |
227 |
} |
Lines 233-316
Link Here
|
233 |
} |
230 |
} |
234 |
} |
231 |
} |
235 |
} |
232 |
} |
236 |
|
233 |
|
237 |
private static URL obtainURL (TopComponent tc) { |
234 |
private static String obtainPath(TopComponent tc) { |
238 |
DataObject dObj = tc.getLookup().lookup(DataObject.class); |
235 |
DataObject dObj = tc.getLookup().lookup(DataObject.class); |
239 |
if (dObj != null) { |
236 |
if (dObj != null) { |
240 |
FileObject fo = dObj.getPrimaryFile(); |
237 |
FileObject fo = dObj.getPrimaryFile(); |
241 |
if (fo != null) { |
238 |
if (fo != null) { |
242 |
return convertFile2URL(fo); |
239 |
return convertFile2Path(fo); |
243 |
} |
240 |
} |
244 |
} |
241 |
} |
245 |
return null; |
242 |
return null; |
246 |
} |
243 |
} |
247 |
|
244 |
|
248 |
private static HistoryItem findHistoryItem (URL url) { |
245 |
private static HistoryItem findHistoryItem(String path) { |
249 |
for (HistoryItem hItem : history) { |
246 |
for (HistoryItem hItem : history) { |
250 |
if (url.equals(hItem.getURL())) { |
247 |
if (path.equals(hItem.getPath())) { |
251 |
return hItem; |
248 |
return hItem; |
252 |
} |
249 |
} |
253 |
} |
250 |
} |
254 |
return null; |
251 |
return null; |
255 |
} |
252 |
} |
256 |
|
253 |
|
257 |
static URL convertFile2URL (FileObject fo) { |
254 |
static String convertFile2Path(FileObject fo) { |
258 |
URL url = URLMapper.findURL(fo, URLMapper.EXTERNAL); |
255 |
File f = FileUtil.toFile(fo); |
259 |
if (url == null) { |
256 |
return f == null ? null : f.getPath(); |
260 |
Logger.getLogger(RecentFiles.class.getName()).log(Level.FINE, |
|
|
261 |
"convertFile2URL: URL can't be found for FileObject " + fo); // NOI18N |
262 |
} |
263 |
return url; |
264 |
} |
257 |
} |
265 |
|
258 |
|
266 |
static FileObject convertURL2File (URL url) { |
259 |
static FileObject convertPath2File(String path) { |
267 |
FileObject fo = URLMapper.findFileObject(url); |
260 |
File f = new File(path); |
268 |
if (fo == null) { |
261 |
f = FileUtil.normalizeFile(f); |
269 |
Logger.getLogger(RecentFiles.class.getName()).log(Level.FINE, |
262 |
return f == null ? null : FileUtil.toFileObject(f); |
270 |
"convertURL2File: File can't be found for URL " + url); // NOI18N |
|
|
271 |
} |
272 |
return fo; |
273 |
} |
263 |
} |
274 |
|
264 |
|
275 |
/** Checks recent files history and removes non-valid entries */ |
265 |
/** Checks recent files history and removes non-valid entries */ |
276 |
private static void checkHistory(boolean checkOnlyForFirstValid) { |
266 |
private static void checkHistory() { |
277 |
assert Thread.holdsLock(HISTORY_LOCK); |
267 |
assert Thread.holdsLock(HISTORY_LOCK); |
278 |
Iterator<HistoryItem> it = history.iterator(); |
268 |
historyProbablyValid = !history.isEmpty(); |
279 |
while (it.hasNext()) { |
269 |
} |
280 |
HistoryItem historyItem = it.next(); |
270 |
|
281 |
FileObject fo = convertURL2File(historyItem.getURL()); |
271 |
static void pruneHistory() { |
282 |
if (fo == null || !fo.isValid()) { |
272 |
synchronized (HISTORY_LOCK) { |
283 |
it.remove(); |
273 |
Iterator<HistoryItem> it = history.iterator(); |
284 |
} else if (checkOnlyForFirstValid) { |
274 |
while (it.hasNext()) { |
285 |
break; |
275 |
HistoryItem historyItem = it.next(); |
|
|
276 |
File f = new File(historyItem.getPath()); |
277 |
if (!f.exists()) { |
278 |
it.remove(); |
279 |
} |
286 |
} |
280 |
} |
287 |
} |
281 |
} |
288 |
} |
282 |
} |
289 |
|
283 |
|
290 |
/** One item of the recently closed files history |
284 |
/** One item of the recently closed files history |
291 |
* Comparable by the time field, ascending from most recent to older items. |
285 |
* Comparable by the time field, ascending from most recent to older items. |
292 |
*/ |
286 |
*/ |
293 |
public static final class HistoryItem implements Comparable<HistoryItem> { |
287 |
|
|
|
288 |
static final class HistoryItem implements Comparable<HistoryItem> { |
289 |
|
294 |
private int id; |
290 |
private int id; |
295 |
private URL fileURL; |
291 |
private String path; |
296 |
private String fileName; |
292 |
private String fileName; |
297 |
|
293 |
|
298 |
HistoryItem (int id, URL fileURL) { |
294 |
HistoryItem(int id, String path) { |
299 |
this.fileURL = fileURL; |
295 |
this.path = path; |
300 |
this.id = id; |
296 |
this.id = id; |
301 |
} |
297 |
} |
302 |
|
298 |
|
303 |
public URL getURL () { |
299 |
public String getPath() { |
304 |
return fileURL; |
300 |
return path; |
305 |
} |
301 |
} |
306 |
|
302 |
|
307 |
public String getFileName () { |
303 |
public String getFileName() { |
308 |
if (fileName == null){ |
304 |
if (fileName == null) { |
309 |
int pos = fileURL.getFile().lastIndexOf('/'); |
305 |
int pos = path.lastIndexOf(File.separatorChar); |
310 |
if ((pos != -1) && (pos < fileURL.getFile().length())){ |
306 |
if ((pos != -1) && (pos < path.length())) { |
311 |
fileName = fileURL.getFile().substring(pos+1); |
307 |
fileName = path.substring(pos + 1); |
312 |
}else{ |
308 |
} else { |
313 |
fileName = fileURL.getFile(); |
309 |
fileName = path; |
314 |
} |
310 |
} |
315 |
} |
311 |
} |
316 |
return fileName; |
312 |
return fileName; |
Lines 322-338
Link Here
|
322 |
|
318 |
|
323 |
@Override |
319 |
@Override |
324 |
public boolean equals(Object obj) { |
320 |
public boolean equals(Object obj) { |
325 |
if (obj instanceof HistoryItem){ |
321 |
if (obj instanceof HistoryItem) { |
326 |
return ((HistoryItem)obj).getURL().equals(fileURL); |
322 |
return ((HistoryItem) obj).getPath().equals(path); |
327 |
} |
323 |
} |
328 |
return false; |
324 |
return false; |
329 |
} |
325 |
} |
|
|
326 |
|
327 |
@Override |
328 |
public int hashCode() { |
329 |
int hash = 7; |
330 |
hash = 17 * hash + (this.path != null ? this.path.hashCode() : 0); |
331 |
return hash; |
332 |
} |
330 |
} |
333 |
} |
331 |
|
334 |
|
332 |
/** Receives info about opened and closed TopComponents from window system. |
335 |
/** Receives info about opened and closed TopComponents from window system. |
333 |
*/ |
336 |
*/ |
334 |
private static class WindowRegistryL implements PropertyChangeListener { |
337 |
private static class WindowRegistryL implements PropertyChangeListener { |
335 |
|
338 |
|
336 |
public void propertyChange(PropertyChangeEvent evt) { |
339 |
public void propertyChange(PropertyChangeEvent evt) { |
337 |
if (TopComponent.Registry.PROP_TC_CLOSED.equals(evt.getPropertyName())) { |
340 |
if (TopComponent.Registry.PROP_TC_CLOSED.equals(evt.getPropertyName())) { |
338 |
addFile((TopComponent) evt.getNewValue()); |
341 |
addFile((TopComponent) evt.getNewValue()); |
Lines 341-347
Link Here
|
341 |
removeFile((TopComponent) evt.getNewValue()); |
344 |
removeFile((TopComponent) evt.getNewValue()); |
342 |
} |
345 |
} |
343 |
} |
346 |
} |
344 |
|
|
|
345 |
} |
347 |
} |
346 |
|
|
|
347 |
} |
348 |
} |