Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: caecbe3135c40bfe3ff2d8d94218da6c2ea3ae1c (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*******************************************************************************
 * 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.internal.cdt.ui.breakpoints;

import java.text.MessageFormat;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.cdt.debug.core.model.ICBreakpoint;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.tcf.internal.cdt.ui.Activator;
import org.eclipse.tcf.internal.cdt.ui.ImageCache;
import org.eclipse.tcf.internal.debug.model.ITCFConstants;
import org.eclipse.tcf.internal.debug.model.TCFBreakpointsModel;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
 * Element which represents a breakpoint scope grouping in Breakpoints view.
 * <p>
 * Scope grouping contains both a filter and context IDs string, however 
 * only one of them can be active on a breakpoint at one time.
 * 
 */
public class BreakpointScopeCategory extends PlatformObject implements IWorkbenchAdapter {

    private static Object[] EMPTY_CHILDREN_ARRAY = new Object[0];
    
    private String fFilter;
    private String fContextIds;
    
    public BreakpointScopeCategory(String filter, String contextIds) {
        fFilter = filter;
        fContextIds = contextIds;
    }

    public String getFilter() {
        return fFilter;
    }
    
    public String getContextIds() {
        return fContextIds;
    }

    /**
     * Sets the given filter and context strings to all breakpoints which match 
     * the group's current scope settings.
     */
    void setFilter(String filter, String contextIds) {
        final List<ICBreakpoint> bps = findCategoryBreakpoints();
        fFilter = filter;
        fContextIds = contextIds;
        
        if (bps.isEmpty()) return;  // No breakpoints to update
        try {
            ResourcesPlugin.getWorkspace().run(
                new IWorkspaceRunnable() {
                    public void run(IProgressMonitor monitor) throws CoreException {
                        for (ICBreakpoint bp : bps) {
                            getScopeExtension(bp).setPropertiesFilter(fFilter);
                            getScopeExtension(bp).setRawContextIds(fContextIds);
                        }
                    }
                }, 
                new NullProgressMonitor()
                );
        }
        catch (CoreException e) {
            Activator.log(e);
        }
    }
    
    public String getLabel(Object o) {
        if (getFilter() != null) {
            return MessageFormat.format(Messages.BreakpointScopeCategory_filter_label, new Object[] { getFilter() });
        } else if (getContextIds() != null) {
            return MessageFormat.format(Messages.BreakpointScopeCategory_contexts_label, new Object[] { getContextIds() });
        }
        return Messages.BreakpointScopeCategory_global_label;
    }
    
    public ImageDescriptor getImageDescriptor(Object object) {
        return ImageCache.getImageDescriptor(ImageCache.IMG_BREAKPOINT_SCOPE);
    }
    
    public Object[] getChildren(Object o) {
        // Not used
        return EMPTY_CHILDREN_ARRAY;
    }
    
    public Object getParent(Object o) {
        // Not used
        return null;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof BreakpointScopeCategory) {
            BreakpointScopeCategory other = (BreakpointScopeCategory)obj; 
            return ((getFilter() == null && other.getFilter() == null) || 
                    (getFilter() != null && getFilter().equals(other.getFilter()))) &&
                   ((getContextIds() == null && other.getContextIds() == null) || 
                    (getContextIds() != null && getContextIds().equals(other.getContextIds())));
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return (getFilter() != null ? getFilter().hashCode() : 0) + 
                (getContextIds() != null ? getContextIds().hashCode() : 0); 
    }
    
    private List<ICBreakpoint> findCategoryBreakpoints() {
        List<ICBreakpoint> categoryBreakpoints = new LinkedList<ICBreakpoint>();
        IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints();
        for (IBreakpoint bp : breakpoints) {
            IMarker bpMarker = bp.getMarker();
            if (bp instanceof ICBreakpoint && bpMarker != null) {
                String filter = bpMarker.getAttribute(TCFBreakpointsModel.ATTR_CONTEXT_QUERY, (String)null);
                String contextIds = bpMarker.getAttribute(TCFBreakpointsModel.ATTR_CONTEXTIDS, (String)null);
                if( ((getFilter() == null && filter == null) || 
                        (getFilter() != null && getFilter().equals(filter))) &&
                       ((getContextIds() == null && contextIds == null) || 
                        (getContextIds() != null && getContextIds().equals(contextIds))) )
                {
                    categoryBreakpoints.add((ICBreakpoint)bp);
                }
            }
        }
        return categoryBreakpoints;
    }

    private TCFBreakpointScopeExtension getScopeExtension(ICBreakpoint bp) throws CoreException {
        return (TCFBreakpointScopeExtension) bp.getExtension(
                ITCFConstants.ID_TCF_DEBUG_MODEL, TCFBreakpointScopeExtension.class);
    }

}

Back to the top