1 19 package org.netbeans.modules.versioning.util; 20 21 import org.openide.awt.UndoRedo; 22 23 import javax.swing.event.ChangeListener ; 24 import javax.swing.event.ChangeEvent ; 25 import javax.swing.*; 26 import javax.swing.undo.CannotUndoException ; 27 import javax.swing.undo.CannotRedoException ; 28 import java.util.*; 29 30 35 public class DelegatingUndoRedo implements UndoRedo, ChangeListener { 36 37 private List<ChangeListener > listeners = new ArrayList<ChangeListener >(2); 38 39 private UndoRedo delegate = UndoRedo.NONE; 40 41 public void setDiffView(JComponent componentDelegate) { 42 if (componentDelegate == null) { 43 setDelegate(UndoRedo.NONE); 44 } else { 45 UndoRedo delegate = (UndoRedo) componentDelegate.getClientProperty(UndoRedo.class); 46 if (delegate == null) delegate = UndoRedo.NONE; 47 setDelegate(delegate); 48 } 49 } 50 51 private void setDelegate(UndoRedo newDelegate) { 52 if (newDelegate == delegate) return; 53 delegate.removeChangeListener(this); 54 delegate = newDelegate; 55 stateChanged(new ChangeEvent (this)); 56 delegate.addChangeListener(this); 57 } 58 59 public void stateChanged(ChangeEvent e) { 60 List<ChangeListener > currentListeners; 61 synchronized(this) { 62 currentListeners = listeners; 63 } 64 for (ChangeListener listener : currentListeners) { 65 listener.stateChanged(e); 66 } 67 } 68 69 public boolean canUndo() { 70 return delegate.canUndo(); 71 } 72 73 public boolean canRedo() { 74 return delegate.canRedo(); 75 } 76 77 public void undo() throws CannotUndoException { 78 delegate.undo(); 79 } 80 81 public void redo() throws CannotRedoException { 82 delegate.redo(); 83 } 84 85 public synchronized void addChangeListener(ChangeListener l) { 86 List<ChangeListener > newListeners = new ArrayList<ChangeListener >(listeners); 87 newListeners.add(l); 88 listeners = newListeners; 89 } 90 91 public synchronized void removeChangeListener(ChangeListener l) { 92 List<ChangeListener > newListeners = new ArrayList<ChangeListener >(listeners); 93 newListeners.remove(l); 94 listeners = newListeners; 95 } 96 97 public String getUndoPresentationName() { 98 return delegate.getUndoPresentationName(); 99 } 100 101 public String getRedoPresentationName() { 102 return delegate.getRedoPresentationName(); 103 } 104 } 105 | Popular Tags |