KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > actions > LookupSensitiveAction


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.modules.project.ui.actions;
21
22 import java.awt.EventQueue JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.LogRecord JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import javax.swing.Icon JavaDoc;
28 import javax.swing.JButton JavaDoc;
29 import javax.swing.JMenuItem JavaDoc;
30 import org.openide.util.Lookup;
31 import org.openide.util.LookupEvent;
32 import org.openide.util.LookupListener;
33 import org.openide.util.NbBundle;
34 import org.openide.util.Utilities;
35 import org.openide.util.WeakListeners;
36
37 /** Action sensitive to current project
38  *
39  * @author Pet Hrebejk
40  */

41 public abstract class LookupSensitiveAction extends BasicAction implements LookupListener {
42     private static Logger JavaDoc UILOG = Logger.getLogger("org.netbeans.ui.actions"); // NOI18N
43

44     private Lookup lookup;
45     private Class JavaDoc<?>[] watch;
46     private Lookup.Result results[];
47     private boolean needsRefresh = true;
48     private boolean initialized = false;
49         
50     private boolean refreshing = false;
51     
52     /** Formats the name with following
53      */

54     /*
55     public LookupSensitiveAction(String iconResource, Lookup lookup) {
56         this( iconResource == null ? null : new ImageIcon( Utilities.loadImage( iconResource ) ), lookup );
57     }
58     */

59         
60     /**
61      * Constructor for global actions. E.g. actions in main menu which
62      * listen to the global context.
63      *
64      */

65     public LookupSensitiveAction(Icon JavaDoc icon, Lookup lookup, Class JavaDoc[] watch ) {
66         super( null, icon );
67         if (lookup == null) {
68             lookup = Utilities.actionsGlobalContext();
69         }
70         this.lookup = lookup;
71         this.watch = watch;
72     }
73     
74     private void init () {
75         if (initialized) {
76             return ;
77         }
78         assert EventQueue.isDispatchThread () : "Cannot be called outside EQ!";
79         this.results = new Lookup.Result[watch.length];
80         // Needs to listen on changes in results
81
for ( int i = 0; i < watch.length; i++ ) {
82             results[i] = lookup.lookupResult(watch[i]);
83             results[i].allItems();
84             LookupListener resultListener = WeakListeners.create(LookupListener.class, this, results[i]);
85             results[i].addLookupListener( resultListener );
86         }
87         initialized = true;
88     }
89     
90     /** Needs to override getValue in order to force refresh
91      */

92     public Object JavaDoc getValue( String JavaDoc key ) {
93         init ();
94         if ( needsRefresh ) {
95             doRefresh();
96         }
97         return super.getValue( key );
98     }
99     
100     /** Needs to override isEnabled in order to force refresh
101      */

102     public boolean isEnabled() {
103         init ();
104         if ( needsRefresh ) {
105             doRefresh();
106         }
107         return super.isEnabled();
108     }
109     
110     public final void actionPerformed( ActionEvent JavaDoc e ) {
111         init ();
112         
113         if (UILOG.isLoggable(Level.FINE)) {
114             LogRecord JavaDoc r;
115             boolean isKey;
116             if (e.getSource() instanceof JMenuItem JavaDoc) {
117                 isKey = false;
118             } else if (e.getSource() instanceof JButton JavaDoc) {
119                 isKey = false;
120             } else {
121                 isKey = true;
122             }
123             
124             if (!isKey) {
125                 r = new LogRecord JavaDoc(Level.FINE, "UI_ACTION_BUTTON_PRESS"); // NOI18N
126
r.setResourceBundle(NbBundle.getBundle(LookupSensitiveAction.class));
127                 r.setParameters(new Object JavaDoc[] {
128                     e.getSource(),
129                     e.getSource().getClass().getName(),
130                     this,
131                     getClass().getName(),
132                     getValue(NAME)
133                 });
134                 r.setLoggerName(UILOG.getName());
135                 UILOG.log(r);
136             }
137         }
138         
139         actionPerformed( lookup );
140     }
141     
142     protected final Lookup getLookup() {
143         return lookup;
144     }
145         
146     private void doRefresh() {
147         refreshing = true;
148         try {
149             refresh( lookup );
150         } finally {
151             refreshing = false;
152         }
153         needsRefresh = false;
154     }
155     
156     // Abstract methods --------------------------------------------------------
157

158     /** Called when the action is performed
159      */

160     protected abstract void actionPerformed( Lookup context );
161            
162     /** Place where to change properties (enablement/name) when
163      * the set of current projects changes.
164      */

165     protected abstract void refresh( Lookup context );
166     
167     // Implementation of LookupListener ----------------------------------------
168

169     public void resultChanged( LookupEvent e ) {
170         if ( refreshing ) {
171             return;
172         }
173         else if ( getPropertyChangeListeners().length == 0 ) {
174             needsRefresh = true;
175         }
176         else {
177             doRefresh();
178         }
179     }
180     
181 }
Popular Tags