KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > debugger > ActionsProviderSupport


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
20 package org.netbeans.spi.debugger;
21
22 import java.util.HashSet JavaDoc;
23 import java.util.Vector JavaDoc;
24
25
26 /**
27  * Support for {@link ActionsProvider} implementation. You should implement
28  * {@link #doAction} and {@link #getActions} only, and call {@link #setEnabled}
29  * when the action state is changed.
30  *
31  * @author Jan Jancura
32  */

33 public abstract class ActionsProviderSupport extends ActionsProvider {
34
35     private HashSet JavaDoc enabled = new HashSet JavaDoc ();
36     private Vector JavaDoc listeners = new Vector JavaDoc ();
37
38
39     /**
40      * Called when the action is called (action button is pressed).
41      *
42      * @param action an action which has been called
43      */

44     public abstract void doAction (Object JavaDoc action);
45     
46     /**
47      * Returns a state of given action defined by {@link #setEnabled}
48      * method call.
49      *
50      * Do not override. Should be final - the enabled state is cached,
51      * therefore this method is not consulted unless the state change
52      * is fired.
53      *
54      * @param action action
55      */

56     public boolean isEnabled (Object JavaDoc action) {
57         return enabled.contains (action);
58     }
59     
60     /**
61      * Sets state of enabled property.
62      *
63      * @param action action whose state should be changed
64      * @param enabled the new state
65      */

66     protected final void setEnabled (Object JavaDoc action, boolean enabled) {
67         boolean fire = false;
68         if (enabled)
69             fire = this.enabled.add (action);
70         else
71             fire = this.enabled.remove (action);
72         if (fire)
73             fireActionStateChanged (action, enabled);
74     }
75     
76     /**
77      * Fires a change of action state.
78      *
79      * @param action action whose state has been changed
80      * @param enabled the new state
81      */

82     protected void fireActionStateChanged (Object JavaDoc action, boolean enabled) {
83         Vector JavaDoc v = (Vector JavaDoc) listeners.clone ();
84         int i, k = v.size ();
85         for (i = 0; i < k; i++)
86             ((ActionsProviderListener) v.elementAt (i)).actionStateChange (
87                 action, enabled
88             );
89     }
90     
91     /**
92      * Adds property change listener.
93      *
94      * @param l new listener.
95      */

96     public final void addActionsProviderListener (ActionsProviderListener l) {
97         listeners.addElement (l);
98     }
99     
100     /**
101      * Removes property change listener.
102      *
103      * @param l removed listener.
104      */

105     public final void removeActionsProviderListener (ActionsProviderListener l) {
106         listeners.removeElement (l);
107     }
108 }
109
110
Popular Tags