001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.php.dbgp.breakpoints;
042:
043: import java.beans.PropertyChangeEvent;
044: import java.util.Collection;
045:
046: import org.netbeans.api.debugger.Breakpoint;
047: import org.netbeans.api.debugger.DebuggerManager;
048: import org.netbeans.api.debugger.DebuggerManagerAdapter;
049: import org.netbeans.api.debugger.Session;
050: import org.netbeans.modules.php.dbgp.DebugSession;
051: import org.netbeans.modules.php.dbgp.StartActionProviderImpl;
052: import org.netbeans.modules.php.dbgp.api.SessionId;
053: import org.netbeans.modules.php.dbgp.packets.BrkpntRemoveCommand;
054: import org.netbeans.modules.php.dbgp.packets.BrkpntSetCommand;
055: import org.netbeans.modules.php.dbgp.packets.BrkpntUpdateCommand;
056: import org.netbeans.modules.php.dbgp.packets.BrkpntSetCommand.State;
057:
058: /**
059: * Responsible for setting breakpoints while debugging.
060: * ( Otherwise breakpoints are used that was set before debugger start ).
061: * @author ads
062: *
063: */
064: public class BreakpointRuntimeSetter extends DebuggerManagerAdapter {
065:
066: /* (non-Javadoc)
067: * @see org.netbeans.api.debugger.LazyDebuggerManagerListener#getProperties()
068: */
069: public String[] getProperties() {
070: if (myProperties == null) {
071: myProperties = new String[] { DebuggerManager.PROP_BREAKPOINTS };
072: }
073: return myProperties;
074: }
075:
076: /* (non-Javadoc)
077: * @see org.netbeans.api.debugger.DebuggerManagerListener#breakpointAdded(org.netbeans.api.debugger.Breakpoint)
078: */
079: public void breakpointAdded(Breakpoint breakpoint) {
080: performCommand(breakpoint, Lazy.SET_COMMAND);
081: }
082:
083: /* (non-Javadoc)
084: * @see org.netbeans.api.debugger.DebuggerManagerListener#breakpointRemoved(org.netbeans.api.debugger.Breakpoint)
085: */
086: public void breakpointRemoved(Breakpoint breakpoint) {
087: performCommand(breakpoint, Lazy.REMOVE_COMMAND);
088: }
089:
090: /* (non-Javadoc)
091: * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
092: */
093: public void propertyChange(PropertyChangeEvent event) {
094: if (event.getPropertyName() != Breakpoint.PROP_ENABLED) {
095: return;
096: }
097: Object source = event.getSource();
098: performCommand((Breakpoint) source, Lazy.UPDATE_COMMAND);
099: }
100:
101: private void performCommand(Breakpoint breakpoint, Command command) {
102: if (!(breakpoint instanceof AbstractBreakpoint)) {
103: return;
104: }
105: AbstractBreakpoint bpoint = (AbstractBreakpoint) breakpoint;
106: Session[] sessions = DebuggerManager.getDebuggerManager()
107: .getSessions();
108: for (Session sess : sessions) {
109: SessionId id = (SessionId) sess.lookupFirst(null,
110: SessionId.class);
111: if (id == null) {
112: continue;
113: }
114: Collection<DebugSession> collection = StartActionProviderImpl
115: .getInstance().getSessions(id);
116: for (DebugSession debugSession : collection) {
117: command.perform(bpoint, id, debugSession);
118: }
119: }
120: }
121:
122: private static interface Command {
123: void perform(AbstractBreakpoint breakpoint, SessionId id,
124: DebugSession session);
125: }
126:
127: private static class SetBreakpointCommand implements Command {
128:
129: public void perform(AbstractBreakpoint breakpoint,
130: SessionId id, DebugSession session) {
131: BrkpntSetCommand command = Utils.getCommand(session, id,
132: breakpoint);
133: if (command != null) {
134: session.sendCommandLater(command);
135: }
136: }
137: }
138:
139: private static class RemoveBreakpointCommand implements Command {
140:
141: public void perform(AbstractBreakpoint breakpoint,
142: SessionId id, DebugSession session) {
143: if (!breakpoint.isSessionRelated(session)) {
144: return;
145: }
146: BrkpntRemoveCommand command = new BrkpntRemoveCommand(
147: session.getTransactionId(), breakpoint
148: .getBreakpointId());
149: session.sendCommandLater(command);
150: }
151: }
152:
153: private static class UpdateBreakpointCommand implements Command {
154:
155: public void perform(AbstractBreakpoint breakpoint,
156: SessionId id, DebugSession session) {
157: BrkpntUpdateCommand command = new BrkpntUpdateCommand(
158: session.getTransactionId(), breakpoint
159: .getBreakpointId());
160: State state = breakpoint.isEnabled() ? State.ENABLED
161: : State.DISABLED;
162: command.setState(state);
163: session.sendCommandLater(command);
164: }
165: }
166:
167: private static class Lazy {
168: static final Command SET_COMMAND = new SetBreakpointCommand();
169: static final Command REMOVE_COMMAND = new RemoveBreakpointCommand();
170: static final Command UPDATE_COMMAND = new UpdateBreakpointCommand();
171: }
172:
173: private String[] myProperties;
174:
175: }
|