KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > projects > FixActionProvider


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.debugger.jpda.projects;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.swing.SwingUtilities JavaDoc;
28 import org.netbeans.api.debugger.ActionsManager;
29
30 import org.netbeans.api.debugger.Breakpoint;
31 import org.netbeans.api.debugger.DebuggerEngine;
32 import org.netbeans.api.debugger.DebuggerManagerListener;
33 import org.netbeans.spi.debugger.ContextProvider;
34 import org.netbeans.api.debugger.Session;
35 import org.netbeans.api.debugger.Watch;
36 import org.netbeans.api.debugger.jpda.JPDADebugger;
37 import org.netbeans.api.project.Project;
38 import org.netbeans.api.project.FileOwnerQuery;
39 import org.netbeans.spi.debugger.ActionsProviderSupport;
40 import org.netbeans.spi.project.ActionProvider;
41 import org.netbeans.api.java.project.JavaProjectConstants;
42 import org.openide.ErrorManager;
43 import org.openide.loaders.DataObject;
44 import org.openide.nodes.Node;
45 import org.openide.util.Lookup;
46 import org.openide.util.lookup.Lookups;
47 import org.openide.windows.TopComponent;
48
49 /**
50 *
51 * @author Jan Jancura
52 */

53 public class FixActionProvider extends ActionsProviderSupport {
54
55     private JPDADebugger debugger;
56     private Listener JavaDoc listener;
57     
58     
59     public FixActionProvider (ContextProvider lookupProvider) {
60         debugger = (JPDADebugger) lookupProvider.lookupFirst
61             (null, JPDADebugger.class);
62         
63         listener = new Listener JavaDoc ();
64         MainProjectManager.getDefault ().addPropertyChangeListener (listener);
65         debugger.addPropertyChangeListener (JPDADebugger.PROP_STATE, listener);
66         TopComponent.getRegistry ().addPropertyChangeListener (listener);
67         
68         setEnabled (
69             ActionsManager.ACTION_FIX,
70             shouldBeEnabled ()
71         );
72     }
73     
74     private void destroy () {
75         debugger.removePropertyChangeListener (JPDADebugger.PROP_STATE, listener);
76         MainProjectManager.getDefault ().removePropertyChangeListener (listener);
77         TopComponent.getRegistry ().removePropertyChangeListener (listener);
78     }
79     
80     public Set JavaDoc getActions () {
81         return Collections.singleton (ActionsManager.ACTION_FIX);
82     }
83     
84     public void doAction (Object JavaDoc action) {
85         if (!SwingUtilities.isEventDispatchThread()) {
86             try {
87                 SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
88                     public void run() {
89                         invokeAction();
90                     }
91                 });
92             } catch (InterruptedException JavaDoc iex) {
93                 // Procceed
94
} catch (java.lang.reflect.InvocationTargetException JavaDoc itex) {
95                 ErrorManager.getDefault().notify(itex);
96             }
97         } else {
98             invokeAction();
99         }
100     }
101     
102     private void invokeAction() {
103         ((ActionProvider) getCurrentProject().getLookup ().lookup (
104                 ActionProvider.class
105             )).invokeAction (
106                 JavaProjectConstants.COMMAND_DEBUG_FIX,
107                 getLookup ()
108             );
109     }
110
111     /**
112      * Returns the project that the active node's fileobject belongs to.
113      * If this cannot be determined for some reason, returns the main project.
114      *
115      * @return the project that the active node's fileobject belongs to
116      */

117     private Project getCurrentProject() {
118         Node[] nodes = TopComponent.getRegistry ().getActivatedNodes ();
119         if (nodes == null || nodes.length == 0) return MainProjectManager.getDefault().getMainProject();
120         DataObject dao = (DataObject) nodes[0].getCookie(DataObject.class);
121         if (dao == null) return MainProjectManager.getDefault().getMainProject();
122         return FileOwnerQuery.getOwner(dao.getPrimaryFile());
123     }
124     
125     private boolean shouldBeEnabled () {
126         // check if current debugger supports this action
127
if (!debugger.canFixClasses()) return false;
128         // check if current project supports this action
129
Project p = getCurrentProject();
130         if (p == null) return false;
131         ActionProvider actionProvider = (ActionProvider) p.getLookup ().
132             lookup (ActionProvider.class);
133         if (actionProvider == null) return false;
134         String JavaDoc[] sa = actionProvider.getSupportedActions ();
135         int i, k = sa.length;
136         for (i = 0; i < k; i++)
137             if (JavaProjectConstants.COMMAND_DEBUG_FIX.equals (sa [i]))
138                 break;
139         if (i == k) return false;
140
141         // check if this action should be enabled
142
return ((ActionProvider) p.getLookup ().lookup (
143                 ActionProvider.class
144             )).isActionEnabled (
145                 JavaProjectConstants.COMMAND_DEBUG_FIX,
146                 getLookup ()
147             );
148     }
149     
150     private Lookup getLookup () {
151         Node[] nodes = TopComponent.getRegistry ().getActivatedNodes ();
152         int i, k = nodes.length;
153         ArrayList JavaDoc l = new ArrayList JavaDoc ();
154         for (i = 0; i < k; i++) {
155             Object JavaDoc o = nodes [i].getCookie (DataObject.class);
156             if (o != null)
157                 l.add (o);
158         }
159         return Lookups.fixed (l.toArray (new DataObject [l.size ()]));
160     }
161     
162     private class Listener implements PropertyChangeListener JavaDoc,
163     DebuggerManagerListener {
164         public Listener () {}
165         
166         public void propertyChange (PropertyChangeEvent JavaDoc e) {
167             boolean en = shouldBeEnabled ();
168             setEnabled (
169                 ActionsManager.ACTION_FIX,
170                 en
171             );
172             if (debugger.getState () == JPDADebugger.STATE_DISCONNECTED)
173                 destroy ();
174         }
175         public void sessionRemoved (Session session) {}
176         public void breakpointAdded (Breakpoint breakpoint) {}
177         public void breakpointRemoved (Breakpoint breakpoint) {}
178         public Breakpoint[] initBreakpoints () {return new Breakpoint [0];}
179         public void initWatches () {}
180         public void sessionAdded (Session session) {}
181         public void watchAdded (Watch watch) {}
182         public void watchRemoved (Watch watch) {}
183         public void engineAdded (DebuggerEngine engine) {}
184         public void engineRemoved (DebuggerEngine engine) {}
185     }
186 }
187
Popular Tags