001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.refactoring.spi.impl;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.beans.PropertyChangeSupport;
047: import java.lang.reflect.InvocationTargetException;
048: import java.util.Arrays;
049: import java.util.Collection;
050: import java.util.HashMap;
051: import java.util.HashSet;
052: import java.util.IdentityHashMap;
053: import java.util.Iterator;
054: import java.util.LinkedList;
055: import java.util.Map;
056: import javax.swing.SwingUtilities;
057: import javax.swing.event.ChangeEvent;
058: import javax.swing.event.ChangeListener;
059: import javax.swing.event.DocumentEvent;
060: import javax.swing.event.DocumentListener;
061: import javax.swing.text.Document;
062: import javax.swing.text.StyledDocument;
063: import org.netbeans.api.project.FileOwnerQuery;
064: import org.netbeans.api.project.Project;
065: import org.netbeans.api.project.ui.OpenProjects;
066: import org.netbeans.modules.refactoring.spi.BackupFacility;
067: import org.netbeans.modules.refactoring.api.ProgressEvent;
068: import org.netbeans.modules.refactoring.api.ProgressListener;
069: import org.netbeans.modules.refactoring.api.RefactoringSession;
070: import org.openide.LifecycleManager;
071: import org.openide.filesystems.FileChangeAdapter;
072: import org.openide.filesystems.FileEvent;
073: import org.openide.filesystems.FileObject;
074: import org.openide.filesystems.FileRenameEvent;
075: import org.openide.loaders.DataObject;
076: import org.openide.text.CloneableEditorSupport;
077: import org.openide.text.NbDocument;
078: import org.openide.util.Exceptions;
079:
080: /**
081: *
082: * @author Jan Becicka
083: */
084: public class UndoManager extends FileChangeAdapter implements
085: DocumentListener, ChangeListener, PropertyChangeListener /*, GlobalPathRegistryListener */{
086:
087: /** stack of undo items */
088: private LinkedList<LinkedList<UndoItem>> undoList;
089:
090: /** stack of redo items */
091: private LinkedList<LinkedList<UndoItem>> redoList;
092:
093: /** set of all CloneableEditorSupports */
094: private final HashSet<CloneableEditorSupport> allCES = new HashSet();
095:
096: private final HashMap<FileObject, CloneableEditorSupport> fileObjectToCES = new HashMap();
097:
098: /** map document -> CloneableEditorSupport */
099: private final HashMap<Document, CloneableEditorSupport> documentToCES = new HashMap();
100:
101: /** map listener -> CloneableEditorSupport */
102: private final HashMap<InvalidationListener, Collection<? extends CloneableEditorSupport>> listenerToCES = new HashMap();
103: private boolean listenersRegistered = false;
104:
105: public static final String PROP_STATE = "state"; //NOI18N
106:
107: private final PropertyChangeSupport pcs = new PropertyChangeSupport(
108: this );
109:
110: private boolean wasUndo = false;
111: private boolean wasRedo = false;
112: private boolean transactionStart;
113: private boolean dontDeleteUndo = false;
114:
115: private IdentityHashMap<LinkedList, String> descriptionMap;
116: private String description;
117: private ProgressListener progress;
118:
119: private static UndoManager instance;
120: private HashSet<Project> projects;
121:
122: public static UndoManager getDefault() {
123: if (instance == null) {
124: instance = new UndoManager();
125: }
126: return instance;
127: }
128:
129: /** Creates a new instance of UndoManager */
130: private UndoManager() {
131: undoList = new LinkedList();
132: redoList = new LinkedList();
133: descriptionMap = new IdentityHashMap();
134: projects = new HashSet();
135: }
136:
137: private UndoManager(ProgressListener progress) {
138: this ();
139: this .progress = progress;
140: }
141:
142: public void setUndoDescription(String desc) {
143: description = desc;
144: }
145:
146: public String getUndoDescription() {
147: if (undoList.isEmpty())
148: return null;
149: return descriptionMap.get(undoList.getFirst());
150: }
151:
152: public String getRedoDescription() {
153: if (redoList.isEmpty())
154: return null;
155: return descriptionMap.get(redoList.getFirst());
156: }
157:
158: /** called to mark transaction start
159: */
160: public void transactionStarted() {
161: transactionStart = true;
162: unregisterListeners();
163: //RepositoryUpdater.getDefault().setListenOnChanges(false);
164: }
165:
166: /**
167: * called to mark end of transaction
168: */
169: public void transactionEnded(boolean fail) {
170: try {
171: description = null;
172: dontDeleteUndo = true;
173: if (fail && !undoList.isEmpty())
174: undoList.removeFirst();
175: else {
176: // [TODO] (jb) this code disables undos for changes using org.openide.src
177: if (isUndoAvailable() && getUndoDescription() == null) {
178: descriptionMap.remove(undoList.removeFirst());
179: dontDeleteUndo = false;
180: }
181:
182: }
183:
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: public void run() {
194: registerListeners();
195: }
196: });
197: } catch (InterruptedException ex) {
198: Exceptions.printStackTrace(ex);
199: } catch (InvocationTargetException ex) {
200: Exceptions.printStackTrace(ex);
201: }
202: }
203: fireStateChange();
204: }
205: }
206:
207: /** undo last transaction */
208: public void undo() {
209: //System.out.println("************* Starting UNDO");
210: if (isUndoAvailable()) {
211: boolean fail = true;
212: try {
213: transactionStarted();
214: wasUndo = true;
215: LinkedList undo = (LinkedList) undoList.getFirst();
216: fireProgressListenerStart(0, undo.size());
217: undoList.removeFirst();
218: Iterator undoIterator = undo.iterator();
219: UndoItem item;
220: redoList.addFirst(new LinkedList());
221: descriptionMap.put(redoList.getFirst(), descriptionMap
222: .remove(undo));
223: while (undoIterator.hasNext()) {
224: fireProgressListenerStep();
225: item = (UndoItem) undoIterator.next();
226: item.undo();
227: if (item instanceof SessionUndoItem) {
228: addItem(item);
229: }
230: }
231: fail = false;
232: } finally {
233: try {
234: wasUndo = false;
235: transactionEnded(fail);
236: } finally {
237: fireProgressListenerStop();
238: fireStateChange();
239: }
240: }
241: }
242: }
243:
244: /** redo last undo
245: */
246: public void redo() {
247: //System.out.println("************* Starting REDO");
248: if (isRedoAvailable()) {
249: boolean fail = true;
250: try {
251: transactionStarted();
252: wasRedo = true;
253: LinkedList redo = redoList.getFirst();
254: fireProgressListenerStart(1, redo.size());
255: redoList.removeFirst();
256: Iterator<UndoItem> redoIterator = redo.iterator();
257: UndoItem item;
258: description = descriptionMap.remove(redo);
259: while (redoIterator.hasNext()) {
260: fireProgressListenerStep();
261: item = redoIterator.next();
262: item.redo();
263: if (item instanceof SessionUndoItem) {
264: addItem(item);
265: }
266: }
267: fail = false;
268: } finally {
269: try {
270: wasRedo = false;
271: transactionEnded(fail);
272: } finally {
273: fireProgressListenerStop();
274: fireStateChange();
275: }
276: }
277: }
278: }
279:
280: /** clean undo/redo stacks */
281: public void clear() {
282: undoList.clear();
283: redoList.clear();
284: descriptionMap.clear();
285: BackupFacility.getDefault().clear();
286: fireStateChange();
287: }
288:
289: public void addItem(RefactoringSession session) {
290: addItem(new SessionUndoItem(session));
291: }
292:
293: /** add new item to undo/redo list */
294: public void addItem(UndoItem item) {
295: if (wasUndo) {
296: LinkedList redo = this .redoList.getFirst();
297: redo.addFirst(item);
298: } else {
299: if (transactionStart) {
300: undoList.addFirst(new LinkedList());
301: descriptionMap.put(undoList.getFirst(), description);
302: transactionStart = false;
303: }
304: LinkedList undo = this .undoList.getFirst();
305: undo.addFirst(item);
306: }
307: if (!(wasUndo || wasRedo))
308: redoList.clear();
309: }
310:
311: public boolean isUndoAvailable() {
312: return !undoList.isEmpty();
313: }
314:
315: public boolean isRedoAvailable() {
316: return !redoList.isEmpty();
317: }
318:
319: public void addPropertyChangeListener(PropertyChangeListener pcl) {
320: pcs.addPropertyChangeListener(pcl);
321: }
322:
323: public void removePropertyChangeListener(PropertyChangeListener pcl) {
324: pcs.removePropertyChangeListener(pcl);
325: }
326:
327: private void fireStateChange() {
328: pcs.firePropertyChange(PROP_STATE, null, null);
329: }
330:
331: public void watch(
332: Collection<? extends CloneableEditorSupport> ceSupports,
333: InvalidationListener l) {
334: synchronized (allCES) {
335: registerListeners();
336: }
337: for (Iterator<? extends CloneableEditorSupport> it = ceSupports
338: .iterator(); it.hasNext();) {
339: final CloneableEditorSupport ces = it.next();
340: final Document d = ces.getDocument();
341: if (d != null) {
342: NbDocument.runAtomic((StyledDocument) d,
343: new Runnable() {
344: public void run() {
345: synchronized (allCES) {
346: if (allCES.add(ces)) {
347: ces
348: .addChangeListener(UndoManager.this );
349: d
350: .addDocumentListener(UndoManager.this );
351: documentToCES.put(d, ces);
352: Object o = d
353: .getProperty(Document.StreamDescriptionProperty);
354: if (o instanceof DataObject) {
355: FileObject file = ((DataObject) o)
356: .getPrimaryFile();
357: fileObjectToCES.put(file,
358: ces);
359: Project p = FileOwnerQuery
360: .getOwner(file);
361: if (p != null)
362: projects.add(p);
363: }
364: }
365: }
366: }
367: });
368: } else {
369: synchronized (allCES) {
370: if (allCES.add(ces)) {
371: ces.addChangeListener(UndoManager.this );
372: }
373: }
374: }
375: }
376: synchronized (allCES) {
377: if (l != null) {
378: listenerToCES.put(l, ceSupports);
379: }
380: }
381: }
382:
383: public void stopWatching(InvalidationListener l) {
384: //synchronized (undoStack) {
385: synchronized (allCES) {
386: listenerToCES.remove(l);
387: clearIfPossible();
388: }
389: //}
390: }
391:
392: private static java.lang.reflect.Field undoRedo;
393:
394: static {
395: try {
396: //obviously hack. See 108616 and 48427
397: undoRedo = org.openide.text.CloneableEditorSupport.class
398: .getDeclaredField("undoRedo"); //NOI18N
399: undoRedo.setAccessible(true);
400: } catch (NoSuchFieldException ex) {
401: Exceptions.printStackTrace(ex);
402: } catch (SecurityException ex) {
403: Exceptions.printStackTrace(ex);
404: }
405: }
406:
407: private void discardAllEdits(InvalidationListener l) {
408: for (CloneableEditorSupport s : listenerToCES == null
409: || l == null ? allCES : listenerToCES.get(l)) {
410: try {
411: org.openide.awt.UndoRedo.Manager manager = (org.openide.awt.UndoRedo.Manager) undoRedo
412: .get(s);
413: if (manager != null) {
414: //if manager not initialized - there is nothing to discard
415: //#114485
416: manager.discardAllEdits();
417: }
418: } catch (SecurityException ex) {
419: Exceptions.printStackTrace(ex);
420: } catch (IllegalArgumentException ex) {
421: Exceptions.printStackTrace(ex);
422: } catch (IllegalAccessException ex) {
423: Exceptions.printStackTrace(ex);
424: }
425: }
426: }
427:
428: // TODO:
429: // public void pathsAdded(GlobalPathRegistryEvent event) {
430: // }
431: //
432: // public void pathsRemoved(GlobalPathRegistryEvent event) {
433: // assert event != null : "event == null"; // NOI18N
434: // if (event.getId().equals(ClassPath.SOURCE)) {
435: // clear();
436: // }
437: // }
438:
439: private void registerListeners() {
440: if (listenersRegistered)
441: return;
442: // TODO:
443: // GlobalPathRegistry.getDefault().addGlobalPathRegistryListener(this);
444: Util.addFileSystemsListener(this );
445: for (CloneableEditorSupport ces : allCES) {
446: ces.addChangeListener(this );
447: }
448: for (Document doc : documentToCES.keySet()) {
449: doc.addDocumentListener(this );
450: }
451: OpenProjects.getDefault().addPropertyChangeListener(this );
452: listenersRegistered = true;
453: }
454:
455: private void unregisterListeners() {
456: if (!listenersRegistered)
457: return;
458: OpenProjects.getDefault().removePropertyChangeListener(this );
459: Util.removeFileSystemsListener(this );
460: //TODO:
461: //GlobalPathRegistry.getDefault().removeGlobalPathRegistryListener(this);
462: for (CloneableEditorSupport ces : allCES) {
463: ces.removeChangeListener(this );
464: }
465: for (Document doc : documentToCES.keySet()) {
466: doc.removeDocumentListener(this );
467: }
468: listenersRegistered = false;
469: }
470:
471: private void invalidate(CloneableEditorSupport ces) {
472: synchronized (undoList) {
473: if (!(wasRedo || wasUndo) && !dontDeleteUndo) {
474: clear();
475: }
476: synchronized (allCES) {
477: if (ces == null) {
478: // invalidate all
479: for (InvalidationListener lis : listenerToCES
480: .keySet()) {
481: lis.invalidateObject();
482: discardAllEdits(lis);
483: }
484: listenerToCES.clear();
485: } else {
486: for (Iterator<Map.Entry<InvalidationListener, Collection<? extends CloneableEditorSupport>>> it = listenerToCES
487: .entrySet().iterator(); it.hasNext();) {
488: Map.Entry<InvalidationListener, Collection<? extends CloneableEditorSupport>> e = it
489: .next();
490: if ((e.getValue()).contains(ces)) {
491: e.getKey().invalidateObject();
492: it.remove();
493: }
494: }
495: /*ces.removeChangeListener(this);
496: allCES.remove(ces);
497: Document d = ces.getDocument();
498: if (d != null) {
499: d.removeDocumentListener(this);
500: documentToCES.remove(d);
501: }
502: */
503: }
504: clearIfPossible();
505: }
506: }
507: }
508:
509: private void clearIfPossible() {
510: if (listenerToCES.isEmpty() && undoList.isEmpty()
511: && redoList.isEmpty()) {
512: unregisterListeners();
513: allCES.clear();
514: documentToCES.clear();
515: fileObjectToCES.clear();
516: projects.clear();
517: }
518: }
519:
520: // FileChangeAdapter ........................................................
521:
522: public void fileChanged(FileEvent fe) {
523: CloneableEditorSupport ces = fileObjectToCES.get(fe.getFile());
524: if (ces != null) {
525: invalidate(ces);
526: }
527: }
528:
529: public void fileDeleted(FileEvent fe) {
530: fileChanged(fe);
531: }
532:
533: public void fileRenamed(FileRenameEvent fe) {
534: fileChanged(fe);
535: }
536:
537: // DocumentListener .........................................................
538:
539: public void changedUpdate(DocumentEvent e) {
540: }
541:
542: public void insertUpdate(DocumentEvent e) {
543: invalidate(documentToCES.get(e.getDocument()));
544: }
545:
546: public void removeUpdate(DocumentEvent e) {
547: invalidate(documentToCES.get(e.getDocument()));
548: }
549:
550: public void stateChanged(ChangeEvent e) {
551: synchronized (allCES) {
552: CloneableEditorSupport ces = (CloneableEditorSupport) e
553: .getSource();
554: Document d = ces.getDocument();
555: for (Iterator it = documentToCES.entrySet().iterator(); it
556: .hasNext();) {
557: Map.Entry en = (Map.Entry) it.next();
558: if (en.getValue() == ces) {
559: ((Document) en.getKey())
560: .removeDocumentListener(this );
561: it.remove();
562: break;
563: }
564: }
565: if (d != null) {
566: documentToCES.put(d, ces);
567: d.addDocumentListener(this );
568: }
569: }
570: }
571:
572: public void saveAll() {
573: synchronized (allCES) {
574: unregisterListeners();
575: }
576: try {
577: LifecycleManager.getDefault().saveAll();
578: } finally {
579: synchronized (allCES) {
580: registerListeners();
581: }
582: }
583: }
584:
585: private void fireProgressListenerStart(int type, int count) {
586: stepCounter = 0;
587: if (progress == null)
588: return;
589: progress.start(new ProgressEvent(this , ProgressEvent.START,
590: type, count));
591: }
592:
593: private int stepCounter = 0;
594:
595: /** Notifies all registered listeners about the event.
596: */
597: private void fireProgressListenerStep() {
598: if (progress == null)
599: return;
600: progress.step(new ProgressEvent(this , ProgressEvent.STEP, 0,
601: ++stepCounter));
602: }
603:
604: /** Notifies all registered listeners about the event.
605: */
606: private void fireProgressListenerStop() {
607: if (progress == null)
608: return;
609: progress.stop(new ProgressEvent(this , ProgressEvent.STOP));
610: }
611:
612: private interface UndoItem {
613: void undo();
614:
615: void redo();
616: }
617:
618: private final class SessionUndoItem implements UndoItem {
619:
620: private RefactoringSession change;
621:
622: public SessionUndoItem(RefactoringSession change) {
623: this .change = change;
624: }
625:
626: public void undo() {
627: change.undoRefactoring(false);
628: }
629:
630: public void redo() {
631: change.doRefactoring(false);
632: }
633: }
634:
635: public void propertyChange(PropertyChangeEvent evt) {
636: if (OpenProjects.PROPERTY_OPEN_PROJECTS.equals(evt
637: .getPropertyName())) {
638: HashSet<Project> p = new HashSet(projects);
639: p.removeAll(Arrays.asList((Project[]) evt.getNewValue()));
640: if (!p.isEmpty())
641: invalidate(null);
642: }
643: }
644: }
|