1 19 20 package org.netbeans.modules.debugger.jpda.projects; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.Set ; 27 import javax.swing.SwingUtilities ; 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 53 public class FixActionProvider extends ActionsProviderSupport { 54 55 private JPDADebugger debugger; 56 private Listener listener; 57 58 59 public FixActionProvider (ContextProvider lookupProvider) { 60 debugger = (JPDADebugger) lookupProvider.lookupFirst 61 (null, JPDADebugger.class); 62 63 listener = new Listener (); 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 getActions () { 81 return Collections.singleton (ActionsManager.ACTION_FIX); 82 } 83 84 public void doAction (Object action) { 85 if (!SwingUtilities.isEventDispatchThread()) { 86 try { 87 SwingUtilities.invokeAndWait(new Runnable () { 88 public void run() { 89 invokeAction(); 90 } 91 }); 92 } catch (InterruptedException iex) { 93 } catch (java.lang.reflect.InvocationTargetException 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 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 if (!debugger.canFixClasses()) return false; 128 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 [] 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 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 l = new ArrayList (); 154 for (i = 0; i < k; i++) { 155 Object 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 , 163 DebuggerManagerListener { 164 public Listener () {} 165 166 public void propertyChange (PropertyChangeEvent 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 |