Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dcc5d3795f865741368102c7d14547a467302331 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*******************************************************************************
 * Copyright (c) 2007, 2010 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.tm.internal.tcf.debug.ui.model;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.debug.internal.ui.viewers.model.ITreeModelContentProviderTarget;
import org.eclipse.debug.internal.ui.viewers.model.ITreeModelViewer;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenCountUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelDelta;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelProxy;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdateListener;
import org.eclipse.debug.internal.ui.viewers.model.provisional.ModelDelta;
import org.eclipse.debug.internal.ui.viewers.provisional.AbstractModelProxy;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.widgets.Display;
import org.eclipse.tm.internal.tcf.debug.model.TCFLaunch;
import org.eclipse.tm.tcf.protocol.Protocol;

/**
 * A model proxy represents a model for a specific presentation context and
 * fires deltas to notify listeners of changes in the model.
 * Model proxy listeners are debuggers views.
 */
@SuppressWarnings("restriction")
public class TCFModelProxy extends AbstractModelProxy implements IModelProxy, Runnable {

    private static final TCFNode[] EMPTY_NODE_ARRAY = new TCFNode[0];

    private final TCFModel model;
    private final TCFLaunch launch;
    private final Display display;
    private final Map<TCFNode,Integer> node2flags = new HashMap<TCFNode,Integer>();
    private final Map<TCFNode,TCFNode[]> node2children = new HashMap<TCFNode,TCFNode[]>();
    private final Map<TCFNode,ModelDelta> node2delta = new HashMap<TCFNode,ModelDelta>();
    private final Set<ModelDelta> content_deltas = new HashSet<ModelDelta>();
    private final LinkedList<TCFNode> selection = new LinkedList<TCFNode>();

    private boolean posted;
    private boolean installed;
    private boolean disposed;
    private boolean realized;
    private long last_update_time;

    private final Runnable timer = new Runnable() {

        public void run() {
            posted = false;
            long idle_time = System.currentTimeMillis() - last_update_time;
            long min_idle_time = model.getMinViewUpdatesInterval();
            if (model.getViewUpdatesThrottleEnabled()) {
                int congestion = Protocol.getCongestionLevel() + 50;
                if (congestion > 0) min_idle_time += congestion * 10;
            }
            if (model.getChannelThrottleEnabled()) {
                int congestion = model.getChannel().getCongestion() + 50;
                if (congestion > 0) min_idle_time += congestion * 10;
            }
            if (idle_time < min_idle_time - 5) {
                Protocol.invokeLater(min_idle_time - idle_time, this);
                posted = true;
            }
            else {
                TCFModelProxy.this.run();
            }
        }
    };

    private class ViewerUpdate implements IViewerUpdate {

        IStatus status;

        public Object getElement() {
            return null;
        }

        public TreePath getElementPath() {
            return null;
        }

        public IPresentationContext getPresentationContext() {
            return TCFModelProxy.this.getPresentationContext();
        }

        public Object getViewerInput() {
            return TCFModelProxy.this.getInput();
        }

        public void cancel() {
        }

        public void done() {
        }

        public IStatus getStatus() {
            return status;
        }

        public boolean isCanceled() {
            return false;
        }

        public void setStatus(IStatus status) {
            this.status = status;
        }
    }

    private class ChildrenCountUpdate extends ViewerUpdate implements IChildrenCountUpdate {

        int count;

        public void setChildCount(int count) {
            this.count = count;
        }
    }

    private class ChildrenUpdate extends ViewerUpdate implements IChildrenUpdate {

        int length;
        TCFNode[] children;

        void setLength(int length) {
            this.length = length;
            this.children = length == 0 ? EMPTY_NODE_ARRAY : new TCFNode[length];
        }

        public int getLength() {
            return length;
        }

        public int getOffset() {
            return 0;
        }

        public void setChild(Object child, int offset) {
            children[offset] = (TCFNode)child;
        }
    }

    private final IViewerUpdateListener update_listener = new IViewerUpdateListener() {

        public void viewerUpdatesBegin() {
            if (!model.getWaitForViewsUpdateAfterStep()) return;
            launch.addPendingClient(this);
        }

        public void viewerUpdatesComplete() {
            launch.removePendingClient(this);
        }

        public void updateStarted(IViewerUpdate update) {
        }

        public void updateComplete(IViewerUpdate update) {
        }
    };

    private final ChildrenCountUpdate children_count_update = new ChildrenCountUpdate();
    private final ChildrenUpdate children_update = new ChildrenUpdate();

    private TCFNode pending_node;

    TCFModelProxy(TCFModel model) {
        this.model = model;
        launch = model.getLaunch();
        display = model.getDisplay();
    }

    public void installed(Viewer viewer) {
        if (isDisposed()) return;
        super.installed(viewer);
        Protocol.invokeAndWait(new Runnable() {
            public void run() {
                assert !installed;
                assert !disposed;
                ((ITreeModelViewer)getViewer()).addViewerUpdateListener(update_listener);
                model.onProxyInstalled(TCFModelProxy.this);
                installed = true;
            }
        });
    }

    public void dispose() {
        if (isDisposed()) return;
        Protocol.invokeAndWait(new Runnable() {
            public void run() {
                assert !disposed;
                if (installed) {
                    model.onProxyDisposed(TCFModelProxy.this);
                    ((ITreeModelViewer)getViewer()).removeViewerUpdateListener(update_listener);
                    launch.removePendingClient(update_listener);
                    launch.removePendingClient(TCFModelProxy.this);
                }
                disposed = true;
            }
        });
        super.dispose();
    }

    /**
     * Add model change information (delta) to a buffer of pending deltas.
     * Implementation will coalesce and post deltas to the view.
     * @param node - a model node that changed.
     * @param flags - flags that describe the change, see IModelDelta
     */
    void addDelta(TCFNode node, int flags) {
        assert Protocol.isDispatchThread();
        assert installed && !disposed;
        if (flags == 0) return;
        Integer delta = node2flags.get(node);
        if (delta != null) flags |= delta.intValue();
        node2flags.put(node, flags);
        post();
    }

    /**
     * Request node to be expanded in the view.
     * @param node - a model node that will become expanded.
     */
    void expand(TCFNode node) {
        Object input = getInput();
        IPresentationContext ctx = getPresentationContext();
        while (node != null && node != input) {
            addDelta(node, IModelDelta.EXPAND);
            node = node.getParent(ctx);
        }
        post();
    }

    /**
     * Request view selection to be set to given node.
     * @param node - a model node that will become new selection.
     */
    void setSelection(TCFNode node) {
        if (selection.size() > 0 && selection.getLast() == node) return;
        selection.add(node);
        expand(node.getParent(getPresentationContext()));
    }

    /**
     * Get current value of the view input.
     * @return view input object.
     */
    Object getInput() {
        return getViewer().getInput();
    }

    public void post() {
        assert Protocol.isDispatchThread();
        assert installed && !disposed;
        if (!posted) {
            long idle_time = System.currentTimeMillis() - last_update_time;
            Protocol.invokeLater(model.getMinViewUpdatesInterval() - idle_time, timer);
            if (model.getWaitForViewsUpdateAfterStep()) launch.addPendingClient(this);
            posted = true;
        }
    }

    private TCFNode[] getNodeChildren(TCFNode node) {
        TCFNode[] res = node2children.get(node);
        if (res == null) {
            if (node.isDisposed()) {
                res = EMPTY_NODE_ARRAY;
            }
            else if (!node.getLockedData(children_count_update, null)) {
                pending_node = node;
                res = EMPTY_NODE_ARRAY;
            }
            else {
                children_update.setLength(children_count_update.count);
                if (!node.getLockedData(children_update, null)) {
                    assert false;
                    pending_node = node;
                    res = EMPTY_NODE_ARRAY;
                }
                else {
                    res = children_update.children;
                }
            }
            node2children.put(node, res);
        }
        return res;
    }

    private int getNodeIndex(TCFNode node) {
        TCFNode p = node.getParent(getPresentationContext());
        if (p == null) return -1;
        TCFNode[] arr = getNodeChildren(p);
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == node) return i;
        }
        return -1;
    }

    private ModelDelta makeDelta(ModelDelta root, TCFNode node, TCFNode selection) {
        ModelDelta delta = node2delta.get(node);
        if (delta == null) {
            if (node == root.getElement()) {
                delta = root;
            }
            else {
                int flags = 0;
                Integer flags_obj = node2flags.get(node);
                if (flags_obj != null) flags = flags_obj.intValue();
                if ((flags & IModelDelta.REMOVED) != 0 && (flags & (IModelDelta.INSERTED|IModelDelta.ADDED)) != 0) return null;
                if (node == selection) {
                    // Bug in Eclipse 3.6.1: SELECT delta has no effect without STATE
                    flags |= IModelDelta.SELECT | IModelDelta.STATE;
                    if (this.selection.size() <= 1) flags |= IModelDelta.REVEAL;
                }
                if (node.parent == null) {
                    // The node is TCF launch node
                    if (root.getElement() instanceof TCFNode) return null;
                    int children = -1;
                    if (selection != null && selection != node || (flags & IModelDelta.EXPAND) != 0) {
                        children = getNodeChildren(node).length;
                    }
                    delta = root.addNode(launch, -1, flags, children);
                }
                else {
                    TCFNode parent = node.getParent(getPresentationContext());
                    if (parent == null) return null;
                    ModelDelta up = makeDelta(root, parent, selection);
                    if (up == null) return null;
                    boolean content = content_deltas.contains(up);
                    if (content) {
                        assert selection == null;
                        flags &= ~(IModelDelta.ADDED | IModelDelta.REMOVED |
                                        IModelDelta.REPLACED | IModelDelta.INSERTED |
                                        IModelDelta.CONTENT | IModelDelta.STATE);
                        if (flags == 0) return null;
                    }
                    int index = -1;
                    int children = -1;
                    if (selection != null || (flags & IModelDelta.INSERTED) != 0 || (flags & IModelDelta.EXPAND) != 0) {
                        index = getNodeIndex(node);
                    }
                    if (selection != null && selection != node || (flags & IModelDelta.EXPAND) != 0) {
                        children = getNodeChildren(node).length;
                    }
                    delta = up.addNode(node, index, flags, children);
                    if (content) content_deltas.add(delta);
                }
                node2delta.put(node, delta);
            }
        }
        int flags = delta.getFlags();
        if ((flags & IModelDelta.REMOVED) != 0) return null;
        //if ((flags & IModelDelta.CONTENT) != 0 && (flags & IModelDelta.EXPAND) == 0) return null;
        return delta;
    }

    private void asyncExec(Runnable r) {
        synchronized (Device.class) {
            if (!display.isDisposed()) {
                display.asyncExec(r);
            }
        }
    }

    private final Comparator<IModelDelta> delta_comparator = new Comparator<IModelDelta>() {
        public int compare(IModelDelta o1, IModelDelta o2) {
            int f1 = o1.getFlags();
            int f2 = o2.getFlags();
            if ((f1 & IModelDelta.REMOVED) != 0 && (f2 & IModelDelta.REMOVED) == 0) return -1;
            if ((f1 & IModelDelta.REMOVED) == 0 && (f2 & IModelDelta.REMOVED) != 0) return +1;
            if ((f1 & IModelDelta.ADDED) != 0 && (f2 & IModelDelta.ADDED) == 0) return -1;
            if ((f1 & IModelDelta.ADDED) == 0 && (f2 & IModelDelta.ADDED) != 0) return +1;
            if ((f1 & IModelDelta.INSERTED) != 0 && (f2 & IModelDelta.INSERTED) == 0) return -1;
            if ((f1 & IModelDelta.INSERTED) == 0 && (f2 & IModelDelta.INSERTED) != 0) return +1;
            int i1 = o1.getIndex();
            int i2 = o2.getIndex();
            if (i1 < i2) return -1;
            if (i1 > i2) return +1;
            return 0;
        }
    };

    private void sortDeltaChildren(IModelDelta delta) {
        IModelDelta arr[] = delta.getChildDeltas();
        Arrays.sort(arr, delta_comparator);
        for (IModelDelta d : arr) sortDeltaChildren(d);
    }

    private void postDelta(final ModelDelta root) {
        assert pending_node == null;
        if (root.getFlags() != 0 || root.getChildDeltas().length > 0) {
            last_update_time = System.currentTimeMillis();
            asyncExec(new Runnable() {
                public void run() {
                    sortDeltaChildren(root);
                    fireModelChanged(root);
                }
            });
        }
    }

    public void run() {
        assert Protocol.isDispatchThread();
        if (disposed) return;
        if (node2flags.isEmpty() && selection.isEmpty()) return;
        if (!realized) {
            if (getPresentationContext().getId().equals(IDebugUIConstants.ID_DEBUG_VIEW)) {
                // Wait until launch manager done creating our launch item in the Debug view.
                // Deltas do NOT work without the launch item.
                asyncExec(new Runnable() {
                    boolean found;
                    public void run() {
                        found = ((ITreeModelContentProviderTarget)getViewer()).findElementIndex(TreePath.EMPTY, launch) >= 0;
                        Protocol.invokeLater(new Runnable() {
                            public void run() {
                                if (disposed) return;
                                if (found) realized = true;
                                else last_update_time = System.currentTimeMillis() + 20;
                                post();
                            }
                        });
                    }
                });
                return;
            }
            else {
                realized = true;
            }
        }
        Object input = getInput();
        int flags = 0;
        if (input instanceof TCFNode) {
            // Optimize away STATE delta on a view input node
            TCFNode node = (TCFNode)input;
            Integer i = node2flags.get(node);
            if (i != null) {
                flags = i;
                if ((flags & IModelDelta.STATE) != 0) {
                    flags &= ~IModelDelta.STATE;
                    if (flags == 0) {
                        node2flags.remove(node);
                        if (node2flags.isEmpty() && selection.isEmpty()) return;
                    }
                    else {
                        node2flags.put(node, flags);
                    }
                }
            }
        }
        pending_node = null;
        node2children.clear();
        if (flags != 0 || node2flags.size() > 0) {
            node2delta.clear();
            content_deltas.clear();
            ModelDelta root = new ModelDelta(input, flags);
            if ((flags & IModelDelta.CONTENT) != 0) content_deltas.add(root);
            for (TCFNode node : node2flags.keySet()) makeDelta(root, node, null);
            if (pending_node == null) {
                node2flags.clear();
                postDelta(root);
            }
        }
        node2delta.clear();
        content_deltas.clear();
        if (pending_node == null) {
            while (!selection.isEmpty()) {
                TCFNode node = selection.getFirst();
                if (!node.isDisposed()) {
                    ModelDelta root = new ModelDelta(input, IModelDelta.NO_CHANGE);
                    makeDelta(root, node, node);
                    node2delta.clear();
                    content_deltas.clear();
                    if (pending_node != null) break;
                    postDelta(root);
                }
                selection.remove(node);
            }
        }

        if (pending_node == null) {
            launch.removePendingClient(this);
        }
        else if (pending_node.getLockedData(children_count_update, this)) {
            assert false;
            Protocol.invokeLater(this);
        }
        node2children.clear();
    }
}

Back to the top