KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > event > TreeEventManager


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.tax.event;
20
21 import java.util.*;
22
23 /**
24  *
25  * @author Libor Kramolis
26  * @version 0.1
27  */

28 public final class TreeEventManager {
29
30     /** Event will never be fired out. */
31     // private static final short FIRE_NEVER = 0; // This is dangerous to cancel firing while old fire policy is not in stack
32

33     /** Event will be fired immediately. */
34     public static final short FIRE_NOW = 1;
35
36     /** Event will be fired later, when state is FIRE_NOW again. */
37     public static final short FIRE_LATER = 2;
38
39
40     /** Fire policy = FIRE_NEVER, FIRE_NOW or FIRE_LATER. */
41     private short firePolicy;
42     
43     /*
44      * Holds all supports that fired in fire FIRE_LATER mode.
45      */

46     private Set cachedSupports = new HashSet ();
47     
48     //
49
// init
50
//
51

52     /** Creates new TreeEventManager. */
53     public TreeEventManager (short policy) {
54         firePolicy = policy;
55     }
56     
57     /** Creates new TreeEventManager. */
58     public TreeEventManager () {
59         this (FIRE_NOW);
60     }
61     
62     /** Creates new TreeEventManager -- copy constructor. */
63     public TreeEventManager (TreeEventManager eventManager) {
64         this.firePolicy = eventManager.firePolicy;
65     }
66     
67     
68     //
69
// itself
70
//
71

72     /** Get fire policy.
73      * @return fire policy.
74      */

75     public final short getFirePolicy () {
76         return firePolicy;
77     }
78     
79     /** Set new fire policy.
80      * @param firePol fire policy.
81      */

82     public final /* synchronized */ void setFirePolicy (short firePolicy) {
83         if (this.firePolicy == firePolicy)
84             return;
85         this.firePolicy = firePolicy;
86         if (firePolicy == FIRE_NOW)
87             fireCached ();
88         
89     }
90     
91     /*
92      * Now it may fire out of order
93      */

94     private void fireCached () {
95         Iterator it = cachedSupports.iterator ();
96         while (it.hasNext ()) {
97             TreeEventChangeSupport next = (TreeEventChangeSupport) it.next ();
98             next.firePropertyChangeCache ();
99             it.remove ();
100         }
101     }
102     
103     /*
104      * Add passed support to cache. the cache is then fired in any order!
105      * It can be a BOTTLENECK method because it is called for every event change.
106      */

107     private void addToCache (TreeEventChangeSupport support) {
108         cachedSupports.add (support);
109     }
110     
111     /**
112      */

113     public final void firePropertyChange (TreeEventChangeSupport eventChangeSupport, TreeEvent evt) {
114         // if (firePolicy == FIRE_NEVER) {
115
// eventChangeSupport.clearPropertyChangeCache();
116
// return;
117
// }
118
if (firePolicy == FIRE_NOW) {
119             eventChangeSupport.firePropertyChangeCache ();
120             eventChangeSupport.firePropertyChangeNow (evt);
121             return;
122         }
123         if (firePolicy == FIRE_LATER) {
124             eventChangeSupport.firePropertyChangeLater (evt);
125             addToCache (eventChangeSupport);
126             return;
127         }
128     }
129     
130     
131     /**
132      */

133 /* public final void runAtomicAction (Runnable runnable) {
134         // there should be a lock in runAtomicAction and fire???Change methods,
135         // but I am not so strong in multi threding and synchronization. :-(
136         if (firePolicy == FIRE_NEVER) {
137             runnable.run();
138             return;
139         }
140 // synchronized (this) {
141             short oldFirePolicy = firePolicy;
142             setFirePolicy (FIRE_LATER);
143             runnable.run();
144             setFirePolicy (oldFirePolicy);
145 // }
146     }*/

147     
148 }
149
Popular Tags