2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.debugger.jpda.projects;
44
import java.beans.PropertyChangeEvent;
45
import java.beans.PropertyChangeListener;
46
import java.util.ArrayList;
47
import java.util.Collections;
49
import javax.swing.SwingUtilities;
50
import org.netbeans.api.debugger.ActionsManager;
52
import org.netbeans.api.debugger.Breakpoint;
53
import org.netbeans.api.debugger.DebuggerEngine;
54
import org.netbeans.api.debugger.DebuggerManagerListener;
55
import org.netbeans.spi.debugger.ContextProvider;
56
import org.netbeans.api.debugger.Session;
57
import org.netbeans.api.debugger.Watch;
58
import org.netbeans.api.debugger.jpda.JPDADebugger;
59
import org.netbeans.api.project.Project;
60
import org.netbeans.api.project.FileOwnerQuery;
61
import org.netbeans.spi.debugger.ActionsProviderSupport;
62
import org.netbeans.spi.project.ActionProvider;
63
import org.netbeans.api.java.project.JavaProjectConstants;
64
import org.openide.ErrorManager;
65
import org.openide.loaders.DataObject;
66
import org.openide.nodes.Node;
67
import org.openide.util.Lookup;
68
import org.openide.util.lookup.Lookups;
69
import org.openide.windows.TopComponent;
75
public class FixActionProvider extends ActionsProviderSupport {
77
private JPDADebugger debugger;
78
private Listener listener;
81
public FixActionProvider (ContextProvider lookupProvider) {
82
debugger = (JPDADebugger) lookupProvider.lookupFirst
83
(null, JPDADebugger.class);
85
listener = new Listener ();
86
MainProjectManager.getDefault ().addPropertyChangeListener (listener);
87
debugger.addPropertyChangeListener (JPDADebugger.PROP_STATE, listener);
88
TopComponent.getRegistry ().addPropertyChangeListener (listener);
91
ActionsManager.ACTION_FIX,
96
private void destroy () {
97
debugger.removePropertyChangeListener (JPDADebugger.PROP_STATE, listener);
98
MainProjectManager.getDefault ().removePropertyChangeListener (listener);
99
TopComponent.getRegistry ().removePropertyChangeListener (listener);
102
public Set getActions () {
103
return Collections.singleton (ActionsManager.ACTION_FIX);
106
public void doAction (Object action) {
107
if (!SwingUtilities.isEventDispatchThread()) {
109
SwingUtilities.invokeAndWait(new Runnable() {
114
} catch (InterruptedException iex) {
116
} catch (java.lang.reflect.InvocationTargetException itex) {
117
ErrorManager.getDefault().notify(itex);
124
private void invokeAction() {
125
((ActionProvider) getCurrentProject().getLookup ().lookup (
128
JavaProjectConstants.COMMAND_DEBUG_FIX,
134
* Returns the project that the active node's fileobject belongs to.
135
* If this cannot be determined for some reason, returns the main project.
137
* @return the project that the active node's fileobject belongs to
139
private Project getCurrentProject() {
140
Node[] nodes = TopComponent.getRegistry ().getActivatedNodes ();
141
if (nodes == null || nodes.length == 0) return MainProjectManager.getDefault().getMainProject();
142
DataObject dao = (DataObject) nodes[0].getCookie(DataObject.class);
143
if (dao == null) return MainProjectManager.getDefault().getMainProject();
144
return FileOwnerQuery.getOwner(dao.getPrimaryFile());
147
private boolean shouldBeEnabled () {
148
// check if current debugger supports this action
149
if (!debugger.canFixClasses()) return false;
150
// check if current project supports this action
151
Project p = getCurrentProject();
152
if (p == null) return false;
153
ActionProvider actionProvider = (ActionProvider) p.getLookup ().
154
lookup (ActionProvider.class);
155
if (actionProvider == null) return false;
156
String[] sa = actionProvider.getSupportedActions ();
157
int i, k = sa.length;
158
for (i = 0; i < k; i++)
159
if (JavaProjectConstants.COMMAND_DEBUG_FIX.equals (sa [i]))
161
if (i == k) return false;
163
// check if this action should be enabled
164
return ((ActionProvider) p.getLookup ().lookup (
167
JavaProjectConstants.COMMAND_DEBUG_FIX,
172
private Lookup getLookup () {
173
Node[] nodes = TopComponent.getRegistry ().getActivatedNodes ();
174
int i, k = nodes.length;
175
ArrayList l = new ArrayList ();
176
for (i = 0; i < k; i++) {
177
Object o = nodes [i].getCookie (DataObject.class);
181
return Lookups.fixed (l.toArray (new DataObject [l.size ()]));
184
private class Listener implements PropertyChangeListener,
185
DebuggerManagerListener {
186
public Listener () {}
188
public void propertyChange (PropertyChangeEvent e) {
189
boolean en = shouldBeEnabled ();
191
ActionsManager.ACTION_FIX,
194
if (debugger.getState () == JPDADebugger.STATE_DISCONNECTED)
197
public void sessionRemoved (Session session) {}
198
public void breakpointAdded (Breakpoint breakpoint) {}
199
public void breakpointRemoved (Breakpoint breakpoint) {}
200
public Breakpoint[] initBreakpoints () {return new Breakpoint [0];}
201
public void initWatches () {}
202
public void sessionAdded (Session session) {}
203
public void watchAdded (Watch watch) {}
204
public void watchRemoved (Watch watch) {}
205
public void engineAdded (DebuggerEngine engine) {}
206
public void engineRemoved (DebuggerEngine engine) {}