1 19 20 package org.netbeans.modules.debugger.ui.models; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import org.netbeans.api.debugger.Breakpoint; 28 import org.netbeans.api.debugger.DebuggerManager; 29 import org.netbeans.spi.debugger.ui.Constants; 30 import org.netbeans.spi.viewmodel.ModelEvent; 31 import org.netbeans.spi.viewmodel.TableModel; 32 import org.netbeans.spi.viewmodel.ModelListener; 33 import org.netbeans.spi.viewmodel.UnknownTypeException; 34 import org.openide.util.RequestProcessor; 35 36 37 41 public class BreakpointsTableModel implements TableModel, Constants { 42 43 private Map breakpointsBeingEnabled = new HashMap (); 44 private RequestProcessor rp; 45 private Collection modelListeners = new ArrayList (); 46 47 public Object getValueAt (Object row, String columnID) throws 48 UnknownTypeException { 49 if (columnID.equals (BREAKPOINT_ENABLED_COLUMN_ID)) { 50 if (row instanceof Breakpoint) { 51 return Boolean.valueOf (((Breakpoint) row).isEnabled ()); 52 } else if (row instanceof String ) { 53 String groupName = (String ) row; 55 Breakpoint[] bs = DebuggerManager.getDebuggerManager (). 56 getBreakpoints (); 57 Boolean enabled = null; 58 for (int i = 0; i < bs.length; i++) { 59 if (bs [i].getGroupName ().equals (groupName)) { 60 if (enabled == null) { 61 enabled = Boolean.valueOf (bs[i].isEnabled ()); 62 } else { 63 if (enabled.booleanValue() != bs[i].isEnabled ()) { 64 return null; } 66 } 67 } 68 } 69 return enabled; 70 } 71 } 72 throw new UnknownTypeException (row); 73 } 74 75 public boolean isReadOnly (Object row, String columnID) throws 76 UnknownTypeException { 77 if (row instanceof Breakpoint) { 78 if (columnID.equals (BREAKPOINT_ENABLED_COLUMN_ID)) { 79 synchronized (breakpointsBeingEnabled) { 80 if (breakpointsBeingEnabled.containsKey(row)) { 81 return true; 82 } else { 83 return false; 84 } 85 } 86 } 87 } else if (row instanceof String ) { 88 return false; 90 } 91 throw new UnknownTypeException (row); 92 } 93 94 public void setValueAt (final Object row, final String columnID, final Object value) 95 throws UnknownTypeException { 96 if (columnID.equals (BREAKPOINT_ENABLED_COLUMN_ID)) { 97 if (row instanceof Breakpoint) { 98 synchronized (breakpointsBeingEnabled) { 99 breakpointsBeingEnabled.put(row, Boolean.valueOf(((Breakpoint) row).isEnabled())); 101 if (rp == null) { 102 rp = new RequestProcessor("Enable Breakpoints RP", 1); } 104 } 105 rp.post(new BreakpointEnabler((Breakpoint) row, ((Boolean ) value).booleanValue ())); 106 return; 107 } else if (row instanceof String ) { 108 String groupName = (String ) row; 109 Breakpoint[] bs = DebuggerManager.getDebuggerManager (). 110 getBreakpoints (); 111 ArrayList breakpoints = new ArrayList (); 112 for (int i = 0; i < bs.length; i++) { 113 if (bs [i].getGroupName ().equals (groupName)) { 114 breakpoints.add(bs[i]); 115 } 116 } 117 if (breakpoints.size() > 0) { 118 synchronized (breakpointsBeingEnabled) { 119 for (Iterator it = breakpoints.iterator(); it.hasNext(); ) { 121 Breakpoint bp = (Breakpoint) it.next(); 122 breakpointsBeingEnabled.put(bp, Boolean.valueOf(bp.isEnabled())); 123 } 124 if (rp == null) { 125 rp = new RequestProcessor("Enable Breakpoints RP", 1); } 127 for (Iterator it = breakpoints.iterator(); it.hasNext(); ) { 128 Breakpoint bp = (Breakpoint) it.next(); 129 rp.post(new BreakpointEnabler(bp, ((Boolean ) value).booleanValue ())); 130 } 131 } 132 } 133 return ; 134 } 135 } 136 throw new UnknownTypeException (row); 137 } 138 139 private class BreakpointEnabler extends Object implements Runnable { 140 141 private Breakpoint bp; 142 private boolean enable; 143 144 public BreakpointEnabler(Breakpoint bp, boolean enable) { 145 this.bp = bp; 146 this.enable = enable; 147 } 148 149 public void run() { 150 if (enable) 151 bp.enable (); 152 else 153 bp.disable (); 154 synchronized (breakpointsBeingEnabled) { 155 breakpointsBeingEnabled.remove(bp); 156 } 157 fireModelEvent(new ModelEvent.TableValueChanged( 158 BreakpointsTableModel.this, 159 bp, BREAKPOINT_ENABLED_COLUMN_ID)); 160 String groupName = bp.getGroupName(); 162 if (groupName != null) { 163 fireModelEvent(new ModelEvent.TableValueChanged( 164 BreakpointsTableModel.this, 165 groupName, BREAKPOINT_ENABLED_COLUMN_ID)); 166 } 167 } 168 } 169 170 175 public void addModelListener (ModelListener l) { 176 synchronized (modelListeners) { 177 modelListeners.add(l); 178 } 179 } 180 181 186 public void removeModelListener (ModelListener l) { 187 synchronized (modelListeners) { 188 modelListeners.remove(l); 189 } 190 } 191 192 private void fireModelEvent(ModelEvent ev) { 193 Collection listeners; 194 synchronized (modelListeners) { 195 listeners = new ArrayList (modelListeners); 196 } 197 for (Iterator it = listeners.iterator(); it.hasNext(); ) { 198 ModelListener l = (ModelListener) it.next(); 199 l.modelChanged(ev); 200 } 201 } 202 } 203 | Popular Tags |