Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5b84e359d14fc6adab623729b4d2d35efee6cd8e (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*******************************************************************************
 * Copyright (c) 2009, 2015 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.cdt.tests.dsf.vm;

import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import org.eclipse.cdt.tests.dsf.ViewerUpdatesListener;
import org.eclipse.cdt.tests.dsf.vm.TestModel.TestElement;
import org.eclipse.debug.internal.ui.viewers.model.IInternalTreeModelViewer;
import org.eclipse.debug.internal.ui.viewers.model.provisional.ITreeModelViewer;
import org.eclipse.jface.viewers.TreePath;

/**
 * @since 2.2
 */
public class TestModelUpdatesListener extends ViewerUpdatesListener
{
    
    private final static Comparator<String> fStringComparator = new Comparator<String>() {
        
        @Override
	public int compare(String s1, String s2) {
            int l1 = s1.length();
            int l2 = s2.length();
            int lmin = l1;
            int result = 0;
            if (l1 < l2) {
                result = -1;
            } else if (l1 > l2) {
                result = 1;
                lmin = l2;
            }

            char c1 = 0;
            char c2 = 0;
            int i = 0;
            for (; i < lmin; i++) {
                c1 = s1.charAt(i);
                c2 = s2.charAt(i);
                if (c1 != c2) {
                    break;
                }
            }

            if (i == lmin) {
                return result;
            }
            return c1 - c2;
        };
    };
    
    private final static Comparator<TreePath> fTestElementVMCComparator = new Comparator<TreePath>() {
        @Override
	public int compare(TreePath p1, TreePath p2) {
            int l1 = p1.getSegmentCount();
            int l2 = p2.getSegmentCount();
            int lmin = l1;
            int result = 0;
            if (l1 < l2) {
                result = -1;
            } else if (l1 > l2) {
                result = 1;
                lmin = l2;
            }

            TestElement e1 = null;
            TestElement e2 = null;
            int i = 0;
            for (; i < lmin; i++) {
                e1 = getTestElement(p1.getSegment(i));
                e2 = getTestElement(p2.getSegment(i));
                if ((e1 == null && e2 != null) || (e1 != null && !e1.equals(e2))) {
                    break;
                }
            }

            if (i == lmin) {
                return result;
            }
            String id1 = e1 == null ? "" : e1.getID();
            String id2 = e2 == null ? "" : e2.getID();
            return fStringComparator.compare(id1, id2);
        }
        
        private TestElement getTestElement(Object o) {
            if (o instanceof TestElement) {
                return (TestElement)o;
            } else if (o instanceof TestElementVMContext) {
                return ((TestElementVMContext)o).getElement();
            } 
            return null;
        }
            
    };

	@Override
    protected Set<TreePath> makeTreePathSet() {
	    return new TreeSet<TreePath>(fTestElementVMCComparator);
	}
	
    @Override
    protected <V> Map<TreePath, V> makeTreePathMap() {
        return new TreeMap<TreePath, V>(fTestElementVMCComparator);
    }
	

    public TestModelUpdatesListener() {
        super();
    }

    public TestModelUpdatesListener(ITreeModelViewer viewer, boolean failOnRedundantUpdates, boolean failOnMultipleModelUpdateSequences) {
        super(viewer, failOnRedundantUpdates, failOnMultipleModelUpdateSequences);
    }
    
    public void reset(TreePath path, TestElement element, int levels, boolean failOnRedundantUpdates, boolean failOnMultipleUpdateSequences) {
        reset(failOnRedundantUpdates, failOnMultipleUpdateSequences);
        addUpdates(path, element, levels);
    }

    public void reset(boolean failOnRedundantUpdates, boolean failOnMultipleUpdateSequences) {
        reset();
        setFailOnRedundantUpdates(failOnRedundantUpdates);
        setFailOnMultipleModelUpdateSequences(failOnMultipleUpdateSequences);
        setFailOnMultipleLabelUpdateSequences(false);
    }

    public void addUpdates(TreePath path, TestElement element, int levels) {
        addUpdates(path, element, levels, ALL_UPDATES_COMPLETE);
    }

    public void addStateUpdates(IInternalTreeModelViewer viewer, TreePath path, TestElement element) {
        addUpdates(viewer, path, element, -1, STATE_UPDATES);
    }
    
    public void addUpdates(TreePath path, TestElement element, int levels, int flags) {
        addUpdates(null, path, element, levels, flags);
    }

    public void addUpdates(IInternalTreeModelViewer viewer, TreePath path, TestElement element, int levels, int flags) {
        if (!path.equals(TreePath.EMPTY)) {
            if ((flags & LABEL_UPDATES) != 0) {
                addLabelUpdate(path);
            }
            if ((flags & PROPERTY_UPDATES) != 0) {
                addPropertiesUpdate(path);
            }
            if ((flags & HAS_CHILDREN_UPDATES) != 0) {
                addHasChildrenUpdate(path);
            }
        }

        if (levels-- != 0) {
            TestElement[] children = element.getChildren();
            if (children.length > 0 && (viewer == null || path.getSegmentCount() == 0 || viewer.getExpandedState(path))) {
                if ((flags & CHILD_COUNT_UPDATES) != 0) {
                    addChildCountUpdate(path);
                }
                if ((flags & CHILDREN_UPDATES) != 0) {
                    for (int i = 0; i < children.length; i++) {
                        addChildreUpdate(path, i);
                    }
                }

                if ((flags & STATE_UPDATES) != 0 && viewer != null) {
                    addStateUpdate(path);
                }

                for (int i = 0; i < children.length; i++) {
                    addUpdates(viewer, path.createChildPath(children[i]), children[i], levels, flags);
                }
            }
        
        }
    }
}


Back to the top