Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3d432f26b5feadbe23c6876bd4b43175c43ca914 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*******************************************************************************
 * Copyright (c) 2012 Wind River Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.debug.test;

import java.util.Collections;
import java.util.Map;

import org.eclipse.core.resources.IMarkerDelta;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IBreakpointsListener;
import org.eclipse.debug.core.model.IBreakpoint;

/**
 * 
 */
public class BreakpointsListener implements IBreakpointsListener {

    public enum EventType { ADDED, REMOVED, CHANGED };
    
    public interface EventTester {
        boolean checkEvent(EventType type, IBreakpoint bp, Map<String, Object> deltaAttributes); 
    }

    private static final EventTester ANY_EVENT_TESTER = new EventTester() {
        public boolean checkEvent(EventType type, IBreakpoint bp, Map<String,Object> deltaAttributes) {
            return true;
        }
    };

    private EventTester fTester = ANY_EVENT_TESTER;
    private boolean fWaiting = false;

    
    public BreakpointsListener() {
        DebugPlugin.getDefault().getBreakpointManager().addBreakpointListener(this);
    }
    
    public void dispose() {
        DebugPlugin.getDefault().getBreakpointManager().removeBreakpointListener(this);
    }
    
    public void startWaiting() {
        fWaiting = true;
    }
    public void setTester(EventTester tester) { 
        fTester = tester;
        startWaiting();
    }
    
    public void breakpointsAdded(IBreakpoint[] bps) {
        Map<String,Object> emptyAttrs = Collections.emptyMap();
        for (IBreakpoint bp : bps) {
            if (fTester.checkEvent(EventType.ADDED, bp, emptyAttrs)) {
                synchronized(this) {
                    fWaiting = false;
                    notifyAll();
                }
                return;
            }
        }
    }

    public void breakpointsRemoved(IBreakpoint[] bps, IMarkerDelta[] deltas) {
        for (int i =0; i < bps.length; i++) {
        	Map<String,Object> attributes = Collections.emptyMap();
        	if (deltas[i] != null) {
        		attributes = deltas[i].getAttributes();
        	}        	
            if (fTester.checkEvent(EventType.REMOVED, bps[i], attributes)) {
                synchronized(this) {
                    fWaiting = false;
                    notifyAll();
                }
                return;
            }
        }
    }

    public void breakpointsChanged(IBreakpoint[] bps, IMarkerDelta[] deltas) {
        for (int i =0; i < bps.length; i++) {
        	Map<String,Object> attributes = Collections.emptyMap();
        	if (deltas[i] != null) {
        		attributes = deltas[i].getAttributes();
        	}        	        	
            if (fTester.checkEvent(EventType.CHANGED, bps[i], attributes)) {
                synchronized(this) {
                    fWaiting = false;
                    notifyAll();
                }
                return;
            }
        }
    }

    
    public void waitForEvent() throws InterruptedException {
        synchronized(this) {
            while(fWaiting) {
                wait();
            }
        }
    }
}

Back to the top