KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > util > DelegatingUndoRedo


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.versioning.util;
20
21 import org.openide.awt.UndoRedo;
22
23 import javax.swing.event.ChangeListener JavaDoc;
24 import javax.swing.event.ChangeEvent JavaDoc;
25 import javax.swing.*;
26 import javax.swing.undo.CannotUndoException JavaDoc;
27 import javax.swing.undo.CannotRedoException JavaDoc;
28 import java.util.*;
29
30 /**
31  * Delegates UndoRedo to the currently active component's UndoRedo.
32
33  * @author Maros Sandor
34  */

35 public class DelegatingUndoRedo implements UndoRedo, ChangeListener JavaDoc {
36
37     private List<ChangeListener JavaDoc> listeners = new ArrayList<ChangeListener JavaDoc>(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 JavaDoc(this));
56         delegate.addChangeListener(this);
57     }
58         
59     public void stateChanged(ChangeEvent JavaDoc e) {
60         List<ChangeListener JavaDoc> currentListeners;
61         synchronized(this) {
62             currentListeners = listeners;
63         }
64         for (ChangeListener JavaDoc 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 JavaDoc {
78         delegate.undo();
79     }
80
81     public void redo() throws CannotRedoException JavaDoc {
82         delegate.redo();
83     }
84         
85     public synchronized void addChangeListener(ChangeListener JavaDoc l) {
86         List<ChangeListener JavaDoc> newListeners = new ArrayList<ChangeListener JavaDoc>(listeners);
87         newListeners.add(l);
88         listeners = newListeners;
89     }
90
91     public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
92         List<ChangeListener JavaDoc> newListeners = new ArrayList<ChangeListener JavaDoc>(listeners);
93         newListeners.remove(l);
94         listeners = newListeners;
95     }
96
97     public String JavaDoc getUndoPresentationName() {
98         return delegate.getUndoPresentationName();
99     }
100
101     public String JavaDoc getRedoPresentationName() {
102         return delegate.getRedoPresentationName();
103     }
104 }
105
Popular Tags