Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c5205f77726cc21b22a92e42368a3de0b3679066 (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
/*******************************************************************************
 * Copyright (c) 2011 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.internal.debug.ui.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import org.eclipse.core.runtime.IStatus;
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.IHasChildrenUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.tcf.protocol.Protocol;

/**
 * TCFSnapshot is used to create snapshots of debug views presentation data.
 * Such snapshots are used to implement various view update policies.
 */
class TCFSnapshot {

    private final IPresentationContext ctx;

    private final HashMap<TCFNode,PresentationData> cache = new HashMap<TCFNode,PresentationData>();

    private final String[] columns;
    private final RGB rgb_stalled = new RGB(128, 128, 128);

    private boolean ignore_bg_color = true;

    private class PresentationData implements IChildrenCountUpdate, IChildrenUpdate, ILabelUpdate, Runnable {

        IViewerUpdate update;
        Runnable done;
        boolean canceled;
        IStatus status;

        String[] label;
        FontData[] font_data;
        ImageDescriptor[] image_desc;
        RGB[] fg_color;
        RGB[] bg_color;
        boolean label_done;

        TCFNode[] children;
        boolean children_done;

        boolean stalled;

        private final ArrayList<Runnable> waiting_list = new ArrayList<Runnable>();

        public IPresentationContext getPresentationContext() {
            return ctx;
        }

        public Object getElement() {
            return update.getElement();
        }

        public TreePath getElementPath() {
            return update.getElementPath();
        }

        public Object getViewerInput() {
            return update.getViewerInput();
        }

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

        public IStatus getStatus() {
            return status;
        }

        public void done() {
            assert false;
        }

        public void cancel() {
            canceled = true;
        }

        public boolean isCanceled() {
            return canceled;
        }

        public String[] getColumnIds() {
            return columns;
        }

        public void setLabel(String text, int col) {
            if (!label_done) {
                if (label == null) {
                    int cnt = columns == null ? 1 : columns.length;
                    label = new String[cnt];
                }
                label[col] = text;
            }
            else {
                if (col >= label.length) stalled = true;
                else if (label[col] != text) {
                    if (label[col] == null || text == null || !text.equals(label[col])) {
                        stalled = true;
                    }
                }
            }
        }

        public void setFontData(FontData fnt, int col) {
            if (!label_done) {
                if (font_data == null) {
                    int cnt = columns == null ? 1 : columns.length;
                    font_data = new FontData[cnt];
                }
                font_data[col] = fnt;
            }
        }

        public void setImageDescriptor(ImageDescriptor image, int col) {
            if (!label_done) {
                if (image_desc == null) {
                    int cnt = columns == null ? 1 : columns.length;
                    image_desc = new ImageDescriptor[cnt];
                }
                image_desc[col] = image;
            }
        }

        public void setForeground(RGB rgb, int col) {
            if (!label_done) {
                if (fg_color == null) {
                    int cnt = columns == null ? 1 : columns.length;
                    fg_color = new RGB[cnt];
                }
                fg_color[col] = rgb;
            }
        }

        public void setBackground(RGB rgb, int col) {
            if (!label_done) {
                if (bg_color == null) {
                    int cnt = columns == null ? 1 : columns.length;
                    bg_color = new RGB[cnt];
                }
                bg_color[col] = rgb;
            }
        }

        public int getOffset() {
            return 0;
        }

        public int getLength() {
            return children.length;
        }

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

        public void setChildCount(int cnt) {
            if (!children_done) {
                children = new TCFNode[cnt];
            }
        }

        public void run() {
            Runnable d = done;
            update = null;
            done = null;
            for (Runnable r : waiting_list) Protocol.invokeLater(r);
            waiting_list.clear();
            d.run();
        }
    }

    private PresentationData data;

    TCFSnapshot(IPresentationContext ctx) {
        this.ctx = ctx;
        columns = ctx.getColumns();
    }

    void dispose() {
        for (PresentationData d : cache.values()) {
            for (Runnable r : d.waiting_list) Protocol.invokeLater(r);
        }
        cache.clear();
    }

    /**
     * Retrieve children count for a presentation context.
     * The method is always called on TCF dispatch thread.
     * @param update - children count update request.
     * @param node - debug model node.
     * @param done - client call back interface, during data waiting it is
     * called every time new portion of data becomes available.
     * @return false if waiting data retrieval, true if all done.
     */
    public boolean getData(IChildrenCountUpdate update, TCFNode node, Runnable done) {
        if (!getChildren(update, node, done)) return false;
        update.setChildCount(data.children.length);
        return true;
    }

    /**
     * Retrieve children for a presentation context.
     * The method is always called on TCF dispatch thread.
     * @param update - children update request.
     * @param node - debug model node.
     * @param done - client call back interface, during data waiting it is
     * called every time new portion of data becomes available.
     * @return false if waiting data retrieval, true if all done.
     */
    public boolean getData(IChildrenUpdate update, TCFNode node, Runnable done) {
        if (!getChildren(update, node, done)) return false;
        int offset = 0;
        int r_offset = update.getOffset();
        int r_length = update.getLength();
        for (TCFNode n : data.children) {
            if (offset >= r_offset && offset < r_offset + r_length) {
                update.setChild(n, offset);
            }
            offset++;
        }
        return true;
    }

    /**
     * Check if the node has children in a presentation context.
     * The method is always called on TCF dispatch thread.
     * @param update - "has children" update request.
     * @param node - debug model node.
     * @param done - client call back interface, during data waiting it is
     * called every time new portion of data becomes available.
     * @return false if waiting data retrieval, true if all done.
     */
    public boolean getData(IHasChildrenUpdate update, TCFNode node, Runnable done) {
        if (!getChildren(update, node, done)) return false;
        update.setHasChilren(data.children.length > 0);
        return true;
    }

    /**
     * Retrieve node label for a presentation context.
     * The method is always called on TCF dispatch thread.
     * @param update - label update request.
     * @param node - debug model node.
     * @param done - client call back interface, during data waiting it is
     * called every time new portion of data becomes available.
     * @return false if waiting data retrieval, true if all done.
     */
    public boolean getData(ILabelUpdate update, TCFNode node, Runnable done) {
        if (!getLabel(update, node, done)) return false;
        String[] ids_update = update.getColumnIds();
        String[] ids_data = columns;
        if (ids_update != ids_data && !Arrays.equals(ids_update, ids_data)) {
            int n = ids_update == null ? 1 : ids_update.length;
            for (int i = 0; i < n; i++) update.setBackground(rgb_stalled, i);
        }
        else {
            if (data.label != null) {
                for (int i = 0; i < data.label.length; i++) {
                    if (data.label[i] != null) update.setLabel(data.label[i], i);
                }
            }
            if (data.font_data != null) {
                for (int i = 0; i < data.font_data.length; i++) {
                    if (data.font_data[i] != null) update.setFontData(data.font_data[i], i);
                }
            }
            if (data.image_desc != null) {
                for (int i = 0; i < data.image_desc.length; i++) {
                    if (data.image_desc[i] != null) update.setImageDescriptor(data.image_desc[i], i);
                }
            }
            if (data.stalled) {
                int n = ids_update == null ? 1 : ids_update.length;
                for (int i = 0; i < n; i++) update.setForeground(rgb_stalled, i);
            }
            else {
                if (data.fg_color != null) {
                    for (int i = 0; i < data.fg_color.length; i++) {
                        if (data.fg_color[i] != null) update.setForeground(data.fg_color[i], i);
                    }
                }
            }
            if (!ignore_bg_color && data.bg_color != null) {
                for (int i = 0; i < data.bg_color.length; i++) {
                    if (data.bg_color[i] != null) update.setBackground(data.bg_color[i], i);
                }
            }
        }
        return true;
    }

    private boolean getChildren(IViewerUpdate update, TCFNode node, Runnable done) {
        data = cache.get(node);
        if (data == null) cache.put(node, data = new PresentationData());
        assert data.update != update;
        if (data.children_done) return true;
        if (data.update != null) {
            data.waiting_list.add(done);
            return false;
        }
        data.update = update;
        data.done = done;
        if (data.children == null) {
            if (!node.getData((IChildrenCountUpdate)data, data)) return false;
            assert data.children != null;
        }
        if (!node.getData((IChildrenUpdate)data, data)) return false;
        data.children_done = true;
        data.update = null;
        data.done = null;
        return true;
    }

    private boolean getLabel(IViewerUpdate update, TCFNode node, Runnable done) {
        data = cache.get(node);
        if (data == null) cache.put(node, data = new PresentationData());
        assert data.update != update;
        if (data.label_done && data.stalled) return true;
        if (data.update != null) {
            data.waiting_list.add(done);
            return false;
        }
        data.update = update;
        data.done = done;
        if (!node.getData((ILabelUpdate)data, data)) return false;
        data.label_done = true;
        data.update = null;
        data.done = null;
        return true;
    }
}

Back to the top