Lines 41-139
Link Here
|
41 |
* Version 2 license, then the option applies only if the new code is |
41 |
* Version 2 license, then the option applies only if the new code is |
42 |
* made subject to such option by the copyright holder. |
42 |
* made subject to such option by the copyright holder. |
43 |
*/ |
43 |
*/ |
44 |
|
|
|
45 |
package org.netbeans.modules.refactoring.spi.impl; |
44 |
package org.netbeans.modules.refactoring.spi.impl; |
46 |
|
45 |
|
47 |
import java.beans.PropertyChangeEvent; |
|
|
48 |
import java.beans.PropertyChangeListener; |
49 |
import java.beans.PropertyChangeSupport; |
50 |
import java.lang.reflect.InvocationTargetException; |
51 |
import java.util.Arrays; |
52 |
import java.util.Collection; |
53 |
import java.util.HashMap; |
54 |
import java.util.HashSet; |
55 |
import java.util.IdentityHashMap; |
46 |
import java.util.IdentityHashMap; |
56 |
import java.util.Iterator; |
47 |
import java.util.Iterator; |
57 |
import java.util.LinkedList; |
48 |
import java.util.LinkedList; |
58 |
import java.util.Map; |
49 |
import javax.swing.JOptionPane; |
59 |
import java.util.Set; |
50 |
import javax.swing.event.ChangeListener; |
60 |
import javax.swing.SwingUtilities; |
51 |
import javax.swing.undo.CannotRedoException; |
61 |
import javax.swing.event.DocumentEvent; |
52 |
import javax.swing.undo.CannotUndoException; |
62 |
import javax.swing.event.DocumentListener; |
|
|
63 |
import javax.swing.text.AbstractDocument; |
64 |
import javax.swing.text.Document; |
65 |
import javax.swing.text.StyledDocument; |
66 |
import org.netbeans.api.project.FileOwnerQuery; |
67 |
import org.netbeans.api.project.Project; |
68 |
import org.netbeans.api.project.ui.OpenProjects; |
69 |
import org.netbeans.modules.refactoring.spi.BackupFacility; |
70 |
import org.netbeans.modules.refactoring.api.ProgressEvent; |
53 |
import org.netbeans.modules.refactoring.api.ProgressEvent; |
71 |
import org.netbeans.modules.refactoring.api.ProgressListener; |
54 |
import org.netbeans.modules.refactoring.api.ProgressListener; |
72 |
import org.netbeans.modules.refactoring.api.RefactoringSession; |
55 |
import org.netbeans.modules.refactoring.api.RefactoringSession; |
73 |
import org.openide.LifecycleManager; |
56 |
import org.netbeans.modules.refactoring.spi.BackupFacility; |
74 |
import org.openide.cookies.EditorCookie; |
57 |
import org.openide.util.ChangeSupport; |
75 |
import org.openide.filesystems.FileChangeAdapter; |
58 |
import org.openide.util.NbBundle; |
76 |
import org.openide.filesystems.FileEvent; |
59 |
import org.openide.windows.WindowManager; |
77 |
import org.openide.filesystems.FileObject; |
|
|
78 |
import org.openide.filesystems.FileRenameEvent; |
79 |
import org.openide.loaders.DataObject; |
80 |
import org.openide.text.CloneableEditorSupport; |
81 |
import org.openide.text.NbDocument; |
82 |
import org.openide.util.Exceptions; |
83 |
|
60 |
|
84 |
/** |
61 |
/** |
85 |
* |
62 |
* |
86 |
* @author Jan Becicka |
63 |
* @author Jan Becicka |
87 |
*/ |
64 |
*/ |
88 |
public final class UndoManager extends FileChangeAdapter implements DocumentListener, PropertyChangeListener /*, GlobalPathRegistryListener */{ |
65 |
public final class UndoManager { |
89 |
|
66 |
|
90 |
/** stack of undo items */ |
67 |
/** |
|
|
68 |
* stack of undo items |
69 |
*/ |
91 |
private LinkedList<LinkedList<UndoItem>> undoList; |
70 |
private LinkedList<LinkedList<UndoItem>> undoList; |
92 |
|
71 |
/** |
93 |
/** stack of redo items */ |
72 |
* stack of redo items |
|
|
73 |
*/ |
94 |
private LinkedList<LinkedList<UndoItem>> redoList; |
74 |
private LinkedList<LinkedList<UndoItem>> redoList; |
95 |
|
75 |
|
96 |
/** set of all CloneableEditorSupports */ |
76 |
private final ChangeSupport changeSupport = new ChangeSupport(this); |
97 |
private final Set<CloneableEditorSupport> allCES = new HashSet<CloneableEditorSupport>(); |
|
|
98 |
|
99 |
private final Map<FileObject, CloneableEditorSupport> fileObjectToCES = new HashMap<FileObject, CloneableEditorSupport>(); |
100 |
|
101 |
/** map document -> CloneableEditorSupport */ |
102 |
private final Map<Document, CloneableEditorSupport> documentToCES = new HashMap<Document, CloneableEditorSupport>(); |
103 |
|
104 |
/** map listener -> CloneableEditorSupport */ |
105 |
private final Map<InvalidationListener, Collection<? extends CloneableEditorSupport>> listenerToCES = new HashMap<InvalidationListener, Collection<? extends CloneableEditorSupport>>(); |
106 |
private boolean listenersRegistered = false; |
107 |
|
108 |
public static final String PROP_STATE = "state"; //NOI18N |
109 |
|
110 |
private final PropertyChangeSupport pcs = new PropertyChangeSupport(this); |
111 |
|
112 |
private boolean wasUndo = false; |
77 |
private boolean wasUndo = false; |
113 |
private boolean wasRedo = false; |
78 |
private boolean wasRedo = false; |
114 |
private boolean transactionStart; |
79 |
private boolean transactionStart; |
115 |
private boolean dontDeleteUndo = false; |
|
|
116 |
|
117 |
private IdentityHashMap<LinkedList, String> descriptionMap; |
80 |
private IdentityHashMap<LinkedList, String> descriptionMap; |
118 |
private String description; |
81 |
private String description; |
119 |
private ProgressListener progress; |
82 |
private ProgressListener progress; |
120 |
|
|
|
121 |
private static UndoManager instance; |
83 |
private static UndoManager instance; |
122 |
private Set<Project> projects; |
|
|
123 |
|
84 |
|
124 |
|
85 |
/** |
125 |
public static UndoManager getDefault() { |
86 |
* Singleton instance |
126 |
if (instance==null) { |
87 |
* @return |
|
|
88 |
*/ |
89 |
public static synchronized UndoManager getDefault() { |
90 |
if (instance == null) { |
127 |
instance = new UndoManager(); |
91 |
instance = new UndoManager(); |
128 |
} |
92 |
} |
129 |
return instance; |
93 |
return instance; |
130 |
} |
94 |
} |
131 |
/** Creates a new instance of UndoManager */ |
95 |
|
|
|
96 |
/** |
97 |
* Creates a new instance of UndoManager |
98 |
*/ |
132 |
private UndoManager() { |
99 |
private UndoManager() { |
133 |
undoList = new LinkedList<LinkedList<UndoItem>>(); |
100 |
undoList = new LinkedList<LinkedList<UndoItem>>(); |
134 |
redoList = new LinkedList<LinkedList<UndoItem>>(); |
101 |
redoList = new LinkedList<LinkedList<UndoItem>>(); |
135 |
descriptionMap = new IdentityHashMap<LinkedList, String>(); |
102 |
descriptionMap = new IdentityHashMap<LinkedList, String>(); |
136 |
projects = new HashSet<Project>(); |
|
|
137 |
} |
103 |
} |
138 |
|
104 |
|
139 |
private UndoManager(ProgressListener progress) { |
105 |
private UndoManager(ProgressListener progress) { |
Lines 141-221
Link Here
|
141 |
this.progress = progress; |
107 |
this.progress = progress; |
142 |
} |
108 |
} |
143 |
|
109 |
|
|
|
110 |
/** |
111 |
* Setter for undo description. For instance "Rename" |
112 |
* @param desc |
113 |
*/ |
144 |
public void setUndoDescription(String desc) { |
114 |
public void setUndoDescription(String desc) { |
145 |
description = desc; |
115 |
description = desc; |
146 |
} |
116 |
} |
147 |
|
117 |
|
|
|
118 |
/** |
119 |
* Getter for undo description. |
120 |
* @return |
121 |
*/ |
148 |
public String getUndoDescription() { |
122 |
public String getUndoDescription() { |
149 |
if (undoList.isEmpty()) return null; |
123 |
if (undoList.isEmpty()) { |
|
|
124 |
return null; |
125 |
} |
150 |
return descriptionMap.get(undoList.getFirst()); |
126 |
return descriptionMap.get(undoList.getFirst()); |
151 |
} |
127 |
} |
152 |
|
128 |
|
|
|
129 |
/** |
130 |
* Getter for Redo description. |
131 |
* @return |
132 |
*/ |
153 |
public String getRedoDescription() { |
133 |
public String getRedoDescription() { |
154 |
if (redoList.isEmpty()) return null; |
134 |
if (redoList.isEmpty()) { |
|
|
135 |
return null; |
136 |
} |
155 |
return descriptionMap.get(redoList.getFirst()); |
137 |
return descriptionMap.get(redoList.getFirst()); |
156 |
} |
138 |
} |
157 |
|
139 |
|
158 |
/** called to mark transaction start |
140 |
/** |
|
|
141 |
* called to mark transaction start |
159 |
*/ |
142 |
*/ |
160 |
public void transactionStarted() { |
143 |
public void transactionStarted() { |
161 |
transactionStart = true; |
144 |
transactionStart = true; |
162 |
unregisterListeners(); |
|
|
163 |
//RepositoryUpdater.getDefault().setListenOnChanges(false); |
164 |
} |
145 |
} |
165 |
|
146 |
|
166 |
/** |
147 |
/** |
167 |
* called to mark end of transaction |
148 |
* called to mark end of transaction |
168 |
*/ |
149 |
*/ |
169 |
public void transactionEnded(boolean fail) { |
150 |
public void transactionEnded(boolean fail) { |
170 |
try { |
|
|
171 |
description = null; |
151 |
description = null; |
172 |
dontDeleteUndo = true; |
152 |
if (fail && !undoList.isEmpty()) { |
173 |
if (fail && !undoList.isEmpty()) |
153 |
//XXX todo |
174 |
undoList.removeFirst(); |
154 |
//undoList.removeFirst(); |
175 |
else { |
155 |
} else { |
176 |
// [TODO] (jb) this code disables undos for changes using org.openide.src |
156 |
// [TODO] (jb) this code disables undos for changes using org.openide.src |
177 |
if (isUndoAvailable() && getUndoDescription() == null) { |
157 |
if (isUndoAvailable() && getUndoDescription() == null) { |
178 |
descriptionMap.remove(undoList.removeFirst()); |
158 |
descriptionMap.remove(undoList.removeFirst()); |
179 |
dontDeleteUndo = false; |
|
|
180 |
} |
159 |
} |
181 |
|
|
|
182 |
} |
160 |
} |
183 |
|
161 |
fireChange(); |
184 |
invalidate(null); |
|
|
185 |
dontDeleteUndo = false; |
186 |
} finally { |
187 |
if (SwingUtilities.isEventDispatchThread()) { |
188 |
registerListeners(); |
189 |
} else { |
190 |
try { |
191 |
SwingUtilities.invokeAndWait(new Runnable() { |
192 |
|
193 |
@Override |
194 |
public void run() { |
195 |
registerListeners(); |
196 |
} |
162 |
} |
197 |
}); |
|
|
198 |
} catch (InterruptedException ex) { |
199 |
Exceptions.printStackTrace(ex); |
200 |
} catch (InvocationTargetException ex) { |
201 |
Exceptions.printStackTrace(ex); |
202 |
} |
203 |
} |
204 |
fireStateChange(); |
205 |
} |
206 |
} |
207 |
|
163 |
|
208 |
/** undo last transaction */ |
164 |
/** |
|
|
165 |
* undo last transaction |
166 |
*/ |
209 |
public void undo() { |
167 |
public void undo() { |
210 |
//System.out.println("************* Starting UNDO"); |
168 |
//System.out.println("************* Starting UNDO"); |
211 |
if (isUndoAvailable()) { |
169 |
if (isUndoAvailable()) { |
|
|
170 |
if (JOptionPane.showConfirmDialog( |
171 |
WindowManager.getDefault().getMainWindow(), |
172 |
NbBundle.getMessage(UndoManager.class, "MSG_ReallyUndo", getUndoDescription()), |
173 |
NbBundle.getMessage(UndoManager.class, "MSG_ConfirmUndo"), |
174 |
JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) { |
175 |
throw new CannotUndoException(); |
176 |
} |
177 |
|
178 |
Runnable run = new Runnable() { |
179 |
|
180 |
public void run() { |
212 |
boolean fail = true; |
181 |
boolean fail = true; |
213 |
try { |
182 |
try { |
214 |
transactionStarted(); |
183 |
transactionStarted(); |
215 |
wasUndo = true; |
184 |
wasUndo = true; |
216 |
LinkedList undo = (LinkedList) undoList.getFirst(); |
185 |
LinkedList undo = (LinkedList) undoList.getFirst(); |
217 |
fireProgressListenerStart(0, undo.size()); |
186 |
fireProgressListenerStart(0, undo.size()); |
218 |
undoList.removeFirst(); |
|
|
219 |
Iterator undoIterator = undo.iterator(); |
187 |
Iterator undoIterator = undo.iterator(); |
220 |
UndoItem item; |
188 |
UndoItem item; |
221 |
redoList.addFirst(new LinkedList<UndoItem>()); |
189 |
redoList.addFirst(new LinkedList<UndoItem>()); |
Lines 228-233
Link Here
|
228 |
addItem(item); |
196 |
addItem(item); |
229 |
} |
197 |
} |
230 |
} |
198 |
} |
|
|
199 |
undoList.removeFirst(); |
231 |
fail = false; |
200 |
fail = false; |
232 |
} finally { |
201 |
} finally { |
233 |
try { |
202 |
try { |
Lines 235-258
Link Here
|
235 |
transactionEnded(fail); |
204 |
transactionEnded(fail); |
236 |
} finally { |
205 |
} finally { |
237 |
fireProgressListenerStop(); |
206 |
fireProgressListenerStop(); |
238 |
fireStateChange(); |
207 |
fireChange(); |
239 |
} |
208 |
} |
240 |
} |
209 |
} |
|
|
210 |
|
241 |
} |
211 |
} |
|
|
212 |
}; |
213 |
|
214 |
// if (SwingUtilities.isEventDispatchThread()) { |
215 |
// ProgressUtils.runOffEventDispatchThread(run, |
216 |
// "Undoing... ", |
217 |
// new AtomicBoolean(), |
218 |
// false); |
219 |
// } else { |
220 |
run.run(); |
221 |
// } |
242 |
} |
222 |
} |
|
|
223 |
} |
243 |
|
224 |
|
244 |
/** redo last undo |
225 |
/** |
|
|
226 |
* redo last undo |
245 |
*/ |
227 |
*/ |
246 |
public void redo() { |
228 |
public void redo() { |
247 |
//System.out.println("************* Starting REDO"); |
229 |
//System.out.println("************* Starting REDO"); |
248 |
if (isRedoAvailable()) { |
230 |
if (isRedoAvailable()) { |
|
|
231 |
if (JOptionPane.showConfirmDialog( |
232 |
WindowManager.getDefault().getMainWindow(), |
233 |
NbBundle.getMessage(UndoManager.class, "MSG_ReallyRedo", getRedoDescription()), |
234 |
NbBundle.getMessage(UndoManager.class, "MSG_ConfirmRedo"), |
235 |
JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) { |
236 |
throw new CannotRedoException(); |
237 |
} |
238 |
Runnable run = new Runnable() { |
239 |
|
240 |
public void run() { |
249 |
boolean fail = true; |
241 |
boolean fail = true; |
250 |
try { |
242 |
try { |
251 |
transactionStarted(); |
243 |
transactionStarted(); |
252 |
wasRedo = true; |
244 |
wasRedo = true; |
253 |
LinkedList<UndoItem> redo = redoList.getFirst(); |
245 |
LinkedList<UndoItem> redo = redoList.getFirst(); |
254 |
fireProgressListenerStart(1, redo.size()); |
246 |
fireProgressListenerStart(1, redo.size()); |
255 |
redoList.removeFirst(); |
|
|
256 |
Iterator<UndoItem> redoIterator = redo.iterator(); |
247 |
Iterator<UndoItem> redoIterator = redo.iterator(); |
257 |
UndoItem item; |
248 |
UndoItem item; |
258 |
description = descriptionMap.remove(redo); |
249 |
description = descriptionMap.remove(redo); |
Lines 264-269
Link Here
|
264 |
addItem(item); |
255 |
addItem(item); |
265 |
} |
256 |
} |
266 |
} |
257 |
} |
|
|
258 |
redoList.removeFirst(); |
267 |
fail = false; |
259 |
fail = false; |
268 |
} finally { |
260 |
} finally { |
269 |
try { |
261 |
try { |
Lines 271-296
Link Here
|
271 |
transactionEnded(fail); |
263 |
transactionEnded(fail); |
272 |
} finally { |
264 |
} finally { |
273 |
fireProgressListenerStop(); |
265 |
fireProgressListenerStop(); |
274 |
fireStateChange(); |
266 |
fireChange(); |
275 |
} |
267 |
} |
276 |
} |
268 |
} |
277 |
} |
269 |
} |
|
|
270 |
}; |
271 |
|
272 |
// if (SwingUtilities.isEventDispatchThread()) { |
273 |
// ProgressUtils.runOffEventDispatchThread(run, |
274 |
// "Redoing... ", |
275 |
// new AtomicBoolean(), |
276 |
// false); |
277 |
// } else { |
278 |
run.run(); |
279 |
// } |
278 |
} |
280 |
} |
|
|
281 |
} |
279 |
|
282 |
|
280 |
/** clean undo/redo stacks */ |
283 |
/** |
|
|
284 |
* clean undo/redo stacks |
285 |
*/ |
281 |
public void clear() { |
286 |
public void clear() { |
282 |
undoList.clear(); |
287 |
undoList.clear(); |
283 |
redoList.clear(); |
288 |
redoList.clear(); |
284 |
descriptionMap.clear(); |
289 |
descriptionMap.clear(); |
285 |
BackupFacility.getDefault().clear(); |
290 |
BackupFacility.getDefault().clear(); |
286 |
fireStateChange(); |
291 |
fireChange(); |
287 |
} |
292 |
} |
288 |
|
293 |
|
289 |
public void addItem(RefactoringSession session) { |
294 |
public void addItem(RefactoringSession session) { |
290 |
addItem(new SessionUndoItem(session)); |
295 |
addItem(new SessionUndoItem(session)); |
291 |
} |
296 |
} |
292 |
|
297 |
|
293 |
/** add new item to undo/redo list */ |
298 |
/** |
|
|
299 |
* add new item to undo/redo list |
300 |
*/ |
294 |
private void addItem(UndoItem item) { |
301 |
private void addItem(UndoItem item) { |
295 |
if (wasUndo) { |
302 |
if (wasUndo) { |
296 |
LinkedList<UndoItem> redo = this.redoList.getFirst(); |
303 |
LinkedList<UndoItem> redo = this.redoList.getFirst(); |
Lines 304-312
Link Here
|
304 |
LinkedList<UndoItem> undo = this.undoList.getFirst(); |
311 |
LinkedList<UndoItem> undo = this.undoList.getFirst(); |
305 |
undo.addFirst(item); |
312 |
undo.addFirst(item); |
306 |
} |
313 |
} |
307 |
if (! (wasUndo || wasRedo)) |
314 |
if (!(wasUndo || wasRedo)) { |
308 |
redoList.clear(); |
315 |
redoList.clear(); |
309 |
} |
316 |
} |
|
|
317 |
} |
310 |
|
318 |
|
311 |
public boolean isUndoAvailable() { |
319 |
public boolean isUndoAvailable() { |
312 |
return !undoList.isEmpty(); |
320 |
return !undoList.isEmpty(); |
Lines 316-596
Link Here
|
316 |
return !redoList.isEmpty(); |
324 |
return !redoList.isEmpty(); |
317 |
} |
325 |
} |
318 |
|
326 |
|
319 |
public void addPropertyChangeListener(PropertyChangeListener pcl) { |
327 |
public void addChangeListener(ChangeListener cl) { |
320 |
pcs.addPropertyChangeListener(pcl); |
328 |
changeSupport.addChangeListener(cl); |
321 |
} |
329 |
} |
322 |
|
330 |
|
323 |
public void removePropertyChangeListener(PropertyChangeListener pcl) { |
331 |
public void removeChangeListener(ChangeListener cl) { |
324 |
pcs.removePropertyChangeListener(pcl); |
332 |
changeSupport.removeChangeListener(cl); |
325 |
} |
333 |
} |
326 |
|
334 |
|
327 |
private void fireStateChange() { |
335 |
private void fireChange() { |
328 |
pcs.firePropertyChange(PROP_STATE, null, null); |
336 |
changeSupport.fireChange(); |
329 |
} |
337 |
} |
330 |
|
338 |
|
331 |
public void watch(Collection<? extends CloneableEditorSupport> ceSupports, InvalidationListener l) { |
|
|
332 |
synchronized (allCES) { |
333 |
registerListeners(); |
334 |
} |
335 |
for (final CloneableEditorSupport ces : ceSupports) { |
336 |
final Document d = ces.getDocument(); |
337 |
if (d!=null) { |
338 |
d.render(new Runnable() { |
339 |
@Override |
340 |
public void run() { |
341 |
synchronized(allCES) { |
342 |
if (allCES.add(ces)) { |
343 |
ces.addPropertyChangeListener(UndoManager.this); |
344 |
d.addDocumentListener(UndoManager.this); |
345 |
documentToCES.put(d, ces); |
346 |
Object o = d.getProperty(Document.StreamDescriptionProperty); |
347 |
if (o instanceof DataObject) { |
348 |
FileObject file = ((DataObject)o).getPrimaryFile(); |
349 |
fileObjectToCES.put(file, ces); |
350 |
Project p = FileOwnerQuery.getOwner(file); |
351 |
if (p!= null) |
352 |
projects.add(p); |
353 |
} |
354 |
} |
355 |
} |
356 |
} |
357 |
}); |
358 |
} else { |
359 |
synchronized(allCES) { |
360 |
if (allCES.add(ces)) { |
361 |
ces.addPropertyChangeListener(UndoManager.this); |
362 |
} |
363 |
} |
364 |
} |
365 |
} |
366 |
synchronized(allCES) { |
367 |
if (l != null) { |
368 |
listenerToCES.put(l, ceSupports); |
369 |
} |
370 |
} |
371 |
} |
372 |
|
373 |
public void stopWatching(InvalidationListener l) { |
374 |
//synchronized (undoStack) { |
375 |
synchronized (allCES) { |
376 |
listenerToCES.remove(l); |
377 |
clearIfPossible(); |
378 |
} |
379 |
//} |
380 |
} |
381 |
|
382 |
private static java.lang.reflect.Field undoRedo; |
383 |
|
384 |
static{ |
385 |
try { |
386 |
//obviously hack. See 108616 and 48427 |
387 |
undoRedo = org.openide.text.CloneableEditorSupport.class.getDeclaredField("undoRedo"); //NOI18N |
388 |
undoRedo.setAccessible(true); |
389 |
} catch (NoSuchFieldException ex) { |
390 |
Exceptions.printStackTrace(ex); |
391 |
} catch (SecurityException ex) { |
392 |
Exceptions.printStackTrace(ex); |
393 |
} |
394 |
} |
395 |
|
396 |
private void discardAllEdits(InvalidationListener l) { |
397 |
for (CloneableEditorSupport s:listenerToCES==null||l==null?allCES:listenerToCES.get(l)) { |
398 |
try { |
399 |
org.openide.awt.UndoRedo.Manager manager = (org.openide.awt.UndoRedo.Manager) undoRedo.get(s); |
400 |
if (manager!=null) { |
401 |
//if manager not initialized - there is nothing to discard |
402 |
//#114485 |
403 |
manager.discardAllEdits(); |
404 |
} |
405 |
} catch (SecurityException ex) { |
406 |
Exceptions.printStackTrace(ex); |
407 |
} catch (IllegalArgumentException ex) { |
408 |
Exceptions.printStackTrace(ex); |
409 |
} catch (IllegalAccessException ex) { |
410 |
Exceptions.printStackTrace(ex); |
411 |
} |
412 |
} |
413 |
} |
414 |
|
415 |
// TODO: |
416 |
// public void pathsAdded(GlobalPathRegistryEvent event) { |
417 |
// } |
418 |
// |
419 |
// public void pathsRemoved(GlobalPathRegistryEvent event) { |
420 |
// assert event != null : "event == null"; // NOI18N |
421 |
// if (event.getId().equals(ClassPath.SOURCE)) { |
422 |
// clear(); |
423 |
// } |
424 |
// } |
425 |
|
426 |
private void registerListeners() { |
427 |
if (listenersRegistered) return; |
428 |
// TODO: |
429 |
// GlobalPathRegistry.getDefault().addGlobalPathRegistryListener(this); |
430 |
Util.addFileSystemsListener(this); |
431 |
for (CloneableEditorSupport ces:allCES) { |
432 |
ces.addPropertyChangeListener(this); |
433 |
} |
434 |
for (Document doc:documentToCES.keySet()) { |
435 |
doc.addDocumentListener(this); |
436 |
} |
437 |
OpenProjects.getDefault().addPropertyChangeListener(this); |
438 |
listenersRegistered = true; |
439 |
} |
440 |
|
441 |
private void unregisterListeners() { |
442 |
if (!listenersRegistered) return; |
443 |
OpenProjects.getDefault().removePropertyChangeListener(this); |
444 |
Util.removeFileSystemsListener(this); |
445 |
//TODO: |
446 |
//GlobalPathRegistry.getDefault().removeGlobalPathRegistryListener(this); |
447 |
for (CloneableEditorSupport ces:allCES) { |
448 |
ces.removePropertyChangeListener(this); |
449 |
} |
450 |
for (Document doc:documentToCES.keySet()) { |
451 |
doc.removeDocumentListener(this); |
452 |
} |
453 |
listenersRegistered = false; |
454 |
} |
455 |
|
456 |
private void invalidate(CloneableEditorSupport ces) { |
457 |
synchronized (undoList) { |
458 |
if (!(wasRedo || wasUndo) && !dontDeleteUndo) { |
459 |
clear(); |
460 |
} |
461 |
synchronized (allCES) { |
462 |
if (ces == null) { |
463 |
// invalidate all |
464 |
for (InvalidationListener lis:listenerToCES.keySet()) { |
465 |
lis.invalidateObject(); |
466 |
discardAllEdits(lis); |
467 |
} |
468 |
listenerToCES.clear(); |
469 |
} else { |
470 |
for (Iterator<Map.Entry<InvalidationListener, Collection<? extends CloneableEditorSupport>>> it = listenerToCES.entrySet().iterator(); it.hasNext();) { |
471 |
Map.Entry<InvalidationListener, Collection<? extends CloneableEditorSupport>> e = it.next(); |
472 |
if ((e.getValue()).contains(ces)) { |
473 |
e.getKey().invalidateObject(); |
474 |
it.remove(); |
475 |
} |
476 |
} |
477 |
/*ces.removeChangeListener(this); |
478 |
allCES.remove(ces); |
479 |
Document d = ces.getDocument(); |
480 |
if (d != null) { |
481 |
d.removeDocumentListener(this); |
482 |
documentToCES.remove(d); |
483 |
} |
484 |
*/ |
485 |
} |
486 |
clearIfPossible(); |
487 |
} |
488 |
} |
489 |
} |
490 |
|
491 |
private void clearIfPossible() { |
492 |
if (listenerToCES.isEmpty() && undoList.isEmpty() && redoList.isEmpty()) { |
493 |
unregisterListeners(); |
494 |
allCES.clear(); |
495 |
documentToCES.clear(); |
496 |
fileObjectToCES.clear(); |
497 |
projects.clear(); |
498 |
} |
499 |
} |
500 |
|
501 |
// FileChangeAdapter ........................................................ |
502 |
|
503 |
@Override |
504 |
public void fileChanged(FileEvent fe) { |
505 |
CloneableEditorSupport ces = fileObjectToCES.get(fe.getFile()); |
506 |
if (ces!=null) { |
507 |
invalidate(ces); |
508 |
} |
509 |
} |
510 |
|
511 |
@Override |
512 |
public void fileDeleted(FileEvent fe) { |
513 |
fileChanged(fe); |
514 |
} |
515 |
|
516 |
@Override |
517 |
public void fileRenamed(FileRenameEvent fe) { |
518 |
fileChanged(fe); |
519 |
} |
520 |
|
521 |
// DocumentListener ......................................................... |
522 |
|
523 |
@Override |
524 |
public void changedUpdate(DocumentEvent e) { |
525 |
} |
526 |
|
527 |
@Override |
528 |
public void insertUpdate(DocumentEvent e) { |
529 |
invalidate(documentToCES.get(e.getDocument())); |
530 |
} |
531 |
|
532 |
@Override |
533 |
public void removeUpdate(DocumentEvent e) { |
534 |
invalidate(documentToCES.get(e.getDocument())); |
535 |
} |
536 |
|
537 |
private void documentStateChanged(CloneableEditorSupport ces) { |
538 |
synchronized (allCES) { |
539 |
Document d = ces.getDocument(); |
540 |
for (Iterator it = documentToCES.entrySet().iterator(); it.hasNext();) { |
541 |
Map.Entry en = (Map.Entry) it.next(); |
542 |
if (en.getValue() == ces) { |
543 |
((Document) en.getKey()).removeDocumentListener(this); |
544 |
it.remove(); |
545 |
break; |
546 |
} |
547 |
} |
548 |
if (d != null) { |
549 |
documentToCES.put(d, ces); |
550 |
d.addDocumentListener(this); |
551 |
} |
552 |
} |
553 |
} |
554 |
|
555 |
public void saveAll() { |
556 |
synchronized (allCES) { |
557 |
unregisterListeners(); |
558 |
} |
559 |
try { |
560 |
LifecycleManager.getDefault().saveAll(); |
561 |
} finally { |
562 |
synchronized (allCES) { |
563 |
registerListeners(); |
564 |
} |
565 |
} |
566 |
} |
567 |
|
568 |
private void fireProgressListenerStart(int type, int count) { |
339 |
private void fireProgressListenerStart(int type, int count) { |
569 |
stepCounter = 0; |
340 |
stepCounter = 0; |
570 |
if (progress == null) |
341 |
if (progress == null) { |
571 |
return; |
342 |
return; |
|
|
343 |
} |
572 |
progress.start(new ProgressEvent(this, ProgressEvent.START, type, count)); |
344 |
progress.start(new ProgressEvent(this, ProgressEvent.START, type, count)); |
573 |
} |
345 |
} |
574 |
|
|
|
575 |
private int stepCounter = 0; |
346 |
private int stepCounter = 0; |
576 |
/** Notifies all registered listeners about the event. |
347 |
|
|
|
348 |
/** |
349 |
* Notifies all registered listeners about the event. |
577 |
*/ |
350 |
*/ |
578 |
private void fireProgressListenerStep() { |
351 |
private void fireProgressListenerStep() { |
579 |
if (progress == null) |
352 |
if (progress == null) { |
580 |
return; |
353 |
return; |
|
|
354 |
} |
581 |
progress.step(new ProgressEvent(this, ProgressEvent.STEP, 0, ++stepCounter)); |
355 |
progress.step(new ProgressEvent(this, ProgressEvent.STEP, 0, ++stepCounter)); |
582 |
} |
356 |
} |
583 |
|
357 |
|
584 |
/** Notifies all registered listeners about the event. |
358 |
/** |
|
|
359 |
* Notifies all registered listeners about the event. |
585 |
*/ |
360 |
*/ |
586 |
private void fireProgressListenerStop() { |
361 |
private void fireProgressListenerStop() { |
587 |
if (progress == null) |
362 |
if (progress == null) { |
588 |
return; |
363 |
return; |
|
|
364 |
} |
589 |
progress.stop(new ProgressEvent(this, ProgressEvent.STOP)); |
365 |
progress.stop(new ProgressEvent(this, ProgressEvent.STOP)); |
590 |
} |
366 |
} |
591 |
|
367 |
|
592 |
private interface UndoItem { |
368 |
private interface UndoItem { |
|
|
369 |
|
593 |
void undo(); |
370 |
void undo(); |
|
|
371 |
|
594 |
void redo(); |
372 |
void redo(); |
595 |
} |
373 |
} |
596 |
|
374 |
|
Lines 598-604
Link Here
|
598 |
|
376 |
|
599 |
private RefactoringSession change; |
377 |
private RefactoringSession change; |
600 |
|
378 |
|
601 |
public SessionUndoItem (RefactoringSession change) { |
379 |
public SessionUndoItem(RefactoringSession change) { |
602 |
this.change = change; |
380 |
this.change = change; |
603 |
} |
381 |
} |
604 |
|
382 |
|
Lines 612-627
Link Here
|
612 |
change.doRefactoring(false); |
390 |
change.doRefactoring(false); |
613 |
} |
391 |
} |
614 |
} |
392 |
} |
615 |
|
|
|
616 |
@Override |
617 |
public void propertyChange(PropertyChangeEvent evt) { |
618 |
if (OpenProjects.PROPERTY_OPEN_PROJECTS.equals(evt.getPropertyName())) { |
619 |
Set<Project> p = new HashSet<Project>(projects); |
620 |
p.removeAll(Arrays.asList((Project[])evt.getNewValue())); |
621 |
if (!p.isEmpty()) |
622 |
invalidate(null); |
623 |
} else if (EditorCookie.Observable.PROP_DOCUMENT.equals(evt.getPropertyName())) { |
624 |
documentStateChanged((CloneableEditorSupport) evt.getSource()); |
625 |
} |
393 |
} |
626 |
} |
|
|
627 |
} |