Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: de3fc60014f18e866fd423c38a74e078667371a3 (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
190
191
192
193
194
195
196
197
198
199
200
/*******************************************************************************
 * Copyright (c) 2014 Wind River Systems, Inc. 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.AbstractSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.internal.ui.viewers.model.provisional.VirtualItem;
import org.eclipse.debug.internal.ui.viewers.model.provisional.VirtualTreeModelViewer;
import org.eclipse.jface.viewers.IElementComparer;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.tcf.debug.ui.ITCFObject;

/**
 * Extends base listener to use virtual viewer capabilities. 
 */
@SuppressWarnings("restriction")
public class VirtualViewerUpdatesListener extends ViewerUpdatesListener {
    private final VirtualTreeModelViewer fVirtualViewer;
    
    public VirtualViewerUpdatesListener(VirtualTreeModelViewer viewer) {
        super(viewer, false, false);
        fVirtualViewer  = viewer;
    }

    public VirtualItem findElement(Pattern[] patterns) {
        return findElement(fVirtualViewer.getTree(), patterns);
    }
    
    public VirtualItem findElement(VirtualItem parent, Pattern[] patterns) {
        return findElement(parent, patterns, 0);
    }

    private VirtualItem findElement(VirtualItem parent, Pattern[] patterns, int patternIdx) {
        if (patternIdx >= patterns.length) return parent;
        for (VirtualItem child : parent.getItems()) {
            String[] label = (String[])child.getData(VirtualItem.LABEL_KEY);
            if (label != null && label.length >= 1 && label[0] != null && 
                patterns[patternIdx].matcher(label[0]).matches()) 
            {
                VirtualItem item = findElement(child, patterns, patternIdx+1);
                if (item != null) {
                    return item;
                }
            }
        }
        return null;
    }

    @Override
    protected Set<TreePath> makeTreePathSet() {
        return new MatchingSet();
    }
    
    protected boolean matchPath(TreePath patternPath, TreePath elementPath) {
        if (patternPath.getSegmentCount() != elementPath.getSegmentCount()) {
            return false;
        }
        
        for (int i = 0; i < patternPath.getSegmentCount(); i++) {
            Object patternSegment = patternPath.getSegment(i);
            Object elementSegment = elementPath.getSegment(i);
            if ( fPatternComparer.equals(elementSegment, patternSegment) ) {
                continue;
            } else if (fTCFContextComparer.equals(elementSegment, patternSegment) ) {
                continue;
            } else if (patternSegment.equals(elementSegment)) {
                continue;
            } 
            return false;
        }
        return true;
    }

    
    private final IElementComparer fPatternComparer = new IElementComparer() {
        public boolean equals(Object a, Object b) {
            Pattern pattern = null;
            Object element = null;
            if (a instanceof Pattern) {
                pattern = (Pattern) a;
                element = b;
            } else if (b instanceof Pattern) {
                pattern = (Pattern) b;
                element = a;
            }
            if (pattern != null) {
                return elementMatches(element, pattern);
            }
            return false;
        }
        
        private boolean elementMatches(Object element, Pattern pattern) {
            VirtualItem[] items = fVirtualViewer.findItems(element);
            if (items.length >= 0) {
                String[] label = (String[])items[0].getData(VirtualItem.LABEL_KEY);
                if (label != null && label.length >= 1 && label[0] != null && pattern.matcher(label[0]).matches()) {
                    return true;
                }
            }
            return false;
        }
        
        public int hashCode(Object element) {
            throw new UnsupportedOperationException();
        }
    };

    private final IElementComparer fTCFContextComparer = new IElementComparer() {
        public boolean equals(Object a, Object b) {
            ITCFObject context = null;
            Object element = null;
            if (a instanceof ITCFObject) {
                context = (ITCFObject) a;
                element = b;
            } else if (b instanceof ITCFObject) {
                context = (ITCFObject) b;
                element = a;
            }
            if (context != null) {
                return elementMatches((ITCFObject)DebugPlugin.getAdapter(element, ITCFObject.class), context);
            }
            return false;
        }
        
        private boolean elementMatches(ITCFObject element, ITCFObject pattern) {
            return element != null && element.getID().equals(pattern.getID());
        }
        
        public int hashCode(Object element) {
            throw new UnsupportedOperationException();
        }
    };

    class MatchingSet extends AbstractSet<TreePath> {
        List<TreePath> fList = new ArrayList<TreePath>(4);
        
        @Override
        public Iterator<TreePath> iterator() {
            return fList.iterator();
        }

        @Override
        public int size() {
            return fList.size();
        }
        
        @Override
        public boolean add(TreePath o) {
            return fList.add(o);
        }
        
        @Override
        public void clear() {
            fList.clear();
        }
        
        @Override
        public boolean contains(Object o) {
            if (o instanceof TreePath) {
                return find((TreePath)o) >= 0;
            }
            return false;
        }
        
        @Override
        public boolean remove(Object o) {
            if (o instanceof TreePath) {
                int index = find((TreePath)o);
                if (index >= 0) {
                    fList.remove(index);
                    return true;
                }
            }
            return false;
        }
        
        private int find(TreePath path) {
            for (int i = 0; i < fList.size(); i++) {
                if (matchPath(fList.get(i), path)) {
                    return i;
                }
            }
            return -1;
        }
        
    }
}

Back to the top