KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > scripting > php > dbginterface > ModelSupport


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
20 package org.netbeans.modules.scripting.php.dbginterface;
21
22 import java.util.Collection JavaDoc;
23 import java.util.Vector JavaDoc;
24 import org.netbeans.spi.viewmodel.ModelEvent;
25 import org.netbeans.spi.viewmodel.ModelEvent.TreeChanged;
26 import org.netbeans.spi.viewmodel.ModelListener;
27
28
29 /**
30  *
31  * @author Peter Williams
32  */

33 public abstract class ModelSupport {
34
35     private static final boolean DEBUG = false;
36
37     private Vector JavaDoc<ModelListener> listeners = new Vector JavaDoc<ModelListener>();
38
39     protected ModelSupport() {
40     }
41
42     // ------------------------------------------------------------------------
43
// ModelListener implementation (shared between TreeModel & ...)
44
// ------------------------------------------------------------------------
45
public void addModelListener(ModelListener l) {
46         listeners.add(l);
47     }
48
49     public void removeModelListener(ModelListener l) {
50         listeners.remove(l);
51     }
52
53     // ------------------------------------------------------------------------
54
// Event support
55
// ------------------------------------------------------------------------
56

57     private volatile boolean needsRefresh = false;
58
59     public void setNeedsRefresh() {
60         needsRefresh = true;
61     }
62
63     public void refresh(boolean force) {
64         if (force || needsRefresh) {
65             needsRefresh = false;
66             fireChangeEvent(new TreeChanged(this));
67         }
68     }
69
70     protected void fireChangeEvent(ModelEvent modelEvent) {
71         Vector JavaDoc<ModelListener> v;
72         
73         synchronized (listeners) {
74             v = new Vector JavaDoc<ModelListener>(listeners);
75         }
76
77         int k = v.size();
78         for (int i = 0; i < k; i++) {
79             v.get(i).modelChanged(modelEvent);
80         }
81     }
82
83     protected void fireChangeEvents(ModelEvent[] events) {
84         Vector JavaDoc<ModelListener> v;
85         
86         synchronized (listeners) {
87             v = new Vector JavaDoc<ModelListener>(listeners);
88         }
89
90         int k = v.size();
91         for(ModelEvent modelEvent : events) {
92             for (int i = 0; i < k; i++) {
93                 v.get(i).modelChanged(modelEvent);
94             }
95         }
96     }
97
98     protected void fireChangeEvents(Collection JavaDoc<ModelEvent> events) {
99         Vector JavaDoc<ModelListener> v;
100         
101         synchronized (listeners) {
102             v = new Vector JavaDoc<ModelListener>(listeners);
103         }
104
105         int k = v.size();
106         for(ModelEvent modelEvent: events) {
107             for(int i = 0; i < k; i++) {
108                 v.get(i).modelChanged(modelEvent);
109             }
110         }
111     }
112 }
113
Popular Tags