Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7f6c66fcbbe0a5e7117bc90bd89ec8a2e19e6732 (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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*******************************************************************************
 * Copyright (c) 2011, 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.debug.internal.ui.viewers.model.provisional;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.eclipse.core.runtime.Assert;
import org.eclipse.debug.internal.core.IInternalDebugCoreConstants;

/**
 * Virtual item, which is analogous to the SWT's tree item.  This class is used
 * by the {@link VirtualTreeModelViewer}.
 *
 * @see VirtualTreeModelViewer
 * @since 3.8
 */
public class VirtualItem {

    // Data keys for display attributes of an item.
    public static String LABEL_KEY = "LABEL_KEY"; //$NON-NLS-1$
    public static String IMAGE_KEY = "IMAGE_KEY"; //$NON-NLS-1$
    public static String FONT_KEY = "FONT_KEY"; //$NON-NLS-1$
    public static String FOREGROUND_KEY = "FOREGROUND_KEY"; //$NON-NLS-1$
    public static String BACKGROUND_KEY = "BACKGROUND_KEY"; //$NON-NLS-1$

    public static String ELEMENT_DATA_KEY = "element"; //$NON-NLS-1$

    /**
     * Index object of a tree item. It allows the indexes to be modified
     * as items are inserted and removed.
     */
	public static class Index implements Comparable<Object> {
        private Integer fIndexValue;

        public Index(int index) {
            fIndexValue = Integer.valueOf(index);
        }

        @Override
		public boolean equals(Object obj) {
            return obj instanceof Index && ((Index)obj).fIndexValue.equals(fIndexValue);
        }

        @Override
		public int hashCode() {
            return fIndexValue.hashCode();
        }

        public void increment() {
            fIndexValue = Integer.valueOf(fIndexValue.intValue() + 1);
        }

        public void decrement() {
            fIndexValue = Integer.valueOf(fIndexValue.intValue() - 1);
        }

        public int intValue() {
            return fIndexValue.intValue();
        }

        @Override
		public int compareTo(Object obj) {
            return obj instanceof Index ? fIndexValue.compareTo(((Index)obj).fIndexValue) : 0;
        }

        @Override
		public String toString() {
            return fIndexValue.toString();
        }
    }

    /**
     * Parent items of this item.
     */
    final private VirtualItem fParent;

    /**
     * The index of this item.
     */
    final private Index fIndex;

    /**
     * Map of child items.  The key to the map is the item's index, which
     * must be the same object instance as the index in the item.  The tree map
     * keeps the items sorted while allowing indexes (keys) to be modified as
     * child items are inserted and removed.
     */
	private Map<Index, VirtualItem> fItems = new TreeMap<Index, VirtualItem>();

    /**
     * Flag indicating whether this item has child items.
     */
    private boolean fHasItems = false;

    /**
     * Indicates that this item has been expanded.  It should only
     * be set to <code>true</code> if fHasItems is <code>true</code>.
     */
    private boolean fExpanded = false;

    /**
     * The count of child items.  <code>-1</code> indicates that the count
     * is not known.
     */
    private int fItemCount = -1;

    /**
     * The data held by this item.  It includes the element as well as the item
     * display attributes.
     */
	private Map<String, Object> fData = new HashMap<String, Object>(1);

    /**
     * Flag indicating that the item needs to have it's label updated.
     */
    private boolean fNeedsLabelUpdate = true;

    /**
     * Flag indicating that the item's count needs to be updated.
     */
    private boolean fNeedsCountUpdate = true;

    /**
     * Flag indicating that the item's element needs to be updated.
     */
    private boolean fNeedsDataUpdate = true;

    /**
     * Indicates that this item has been disposed.
     */
    private boolean fDisposed = false;


    /**
     * Virtual item constructor.
     * @param parent parent virtual item
     * @param index index of the item in the parent
     */
    public VirtualItem(VirtualItem parent, Index index) {
        fParent = parent;
        fIndex = index;
    }

    /**
     * Clears the child item at the given index.
     * @param index index of item to clear.
     */
    public void clear(Index index) {
        VirtualItem item = fItems.remove(index);
        if (item != null) {
            item.dispose();
        }
    }

    /**
     * Clears all child items.
     *
     * @since 3.9
     */
    public void clearAll() {
        fData.clear();
		for (VirtualItem item : fItems.values()) {
			item.dispose();
        }
        fItems.clear();
    }
    /**
     * Returns the parent item.
     * @return parent item.
     */
    public VirtualItem getParent() {
        return fParent;
    }

    /**
     * @return Returns the index of this item.
     */
    public Index getIndex() {
        return fIndex;
    }

    /**
     * Finds the given item in the child items of this element.
     * @param element Data object of the item to be found.
     * @return Item if found, <code>null</code> if not.
     */
    public VirtualItem findItem(Object element) {
		for (VirtualItem item : fItems.values()) {
			Object nextData = item.getData();
            if ( (element != null && element.equals(nextData)) || (element == null && nextData == null) ) {
				return item;
            }
        }
        return null;
    }

    /**
     * @return Returns whether the data element of this item is stale.
     */
    public boolean needsDataUpdate() {
        return fNeedsDataUpdate;
    }

    /**
     * Marks the item as having a stale data item.
     */
    public void setNeedsDataUpdate() {
        fNeedsDataUpdate = true;
    }

    /**
     * Clears the stale status of the item's data element.
     */
    public void clearNeedsDataUpdate() {
        fNeedsDataUpdate = false;
    }

    /**
     * @return Returns whether the item has stale item count.
     */
    public boolean needsCountUpdate() {
        return fNeedsCountUpdate;
    }

    /**
     * Marks the item as having a stale child count.
     */
    public void setNeedsCountUpdate() {
        fNeedsCountUpdate = true;
        fItemCount = -1;
    }

    /**
     * Clears the stale status of the item's child count.
     */
    public void clearNeedsCountUpdate() {
        fNeedsCountUpdate = false;
    }

    /**
     * @return Returns whether the item has stale label.
     */
    public boolean needsLabelUpdate() {
        return fNeedsLabelUpdate;
    }

    /**
     * Marks the item as having a stale label data.
     */
    public void setNeedsLabelUpdate() {
        fNeedsLabelUpdate = true;
    }

    /**
     * Clears the stale status of the item's label.
     */
    public void clearNeedsLabelUpdate() {
        fNeedsLabelUpdate = false;
    }

    /**
     * @return Returns whether the item has been disposed.
     */
    public boolean isDisposed() {
        return fDisposed;
    }

    /**
     * Disposes the item.
     */
    public void dispose() {
        clearAll();

        fDisposed = true;
        findTree().fireItemDisposed(this);
    }

    /**
     * @param key Key to retrieve data for.
     * @return Returns item data corresponding to given key.
     */
    public Object getData (String key) {
        return fData.get(key);
    }

    /**
     * Sets given data element for given key.
     * @param key Key for data.
     * @param data Data value.
     */
    public void setData(String key, Object data) {
        fData.put(key, data);
    }

    /**
     * Sets the item's data element.
     * @param data Item's new element.
     */
    public void setData(Object data) {
        fData.put(ELEMENT_DATA_KEY, data);
    }

    /**
     * @return Returns item's data element.
     */
    public Object getData () {
        return fData.get(ELEMENT_DATA_KEY);
    }

    /**
     * Marks the given item as expanded or collapsed.
     * @param expanded If true, item will be marked as expanded.
     */
    public void setExpanded(boolean expanded) {
        if (fExpanded == expanded) {
            return;
        }
        fExpanded = expanded;

        if (fExpanded && getItemCount() == -1) {
            setNeedsCountUpdate();
        }


        Assert.isTrue(!fExpanded || hasItems());

        // If collapsed, make sure that all the children are collapsed as well.
        if (!fExpanded) {
			for (VirtualItem item : fItems.values()) {
				item.setExpanded(expanded);
            }
        }
    }

    /**
     * @return Returns item's expanded state.
     */
    public boolean getExpanded() {
        return fExpanded;
    }

    /**
     * Sets the flag indicating whether item has child items.
     * @param hasChildren Set to true if child has items.
     */
    public void setHasItems(boolean hasChildren) {
        fHasItems = hasChildren;
        if (!fHasItems) {
            if (getItemCount() != 0) {
                setItemCount(0);
            }
        } else if (getItemCount() == 0) {
            setItemCount(-1);
        }
    }

    /**
     * @return Returns true if item has child items.
     */
    public boolean hasItems() {
        return fHasItems;
    }

    /**
     * Sets the item's child count.
     * @param count Child count.
     */
    public void setItemCount(int count) {
        fItemCount = count;
		for (Iterator<Entry<Index, VirtualItem>> itr = fItems.entrySet().iterator(); itr.hasNext();) {
			Entry<Index, VirtualItem> entry = itr.next();
            int index = entry.getKey().intValue();
            if (index >= count) {
                VirtualItem item = entry.getValue();
                item.dispose();
                itr.remove();
            }
        }
        if (fItemCount == 0) {
            if (hasItems()) {
                setHasItems(false);
            }
            if (getExpanded()) {
                setExpanded(false);
            }
        } else {
            if (!hasItems()) {
                setHasItems(true);
            }
        }
    }

    /**
     * @return  Returns item's child count.
     */
    public int getItemCount() {
        return fItemCount;
    }

    /**
     * Returns the child item at given index.  Child item is created if needed.
     *
     * @param index Index of the child item.
     * @return Child item.
     */
    public VirtualItem getItem(Index index) {
        ensureItems();

        VirtualItem item = fItems.get(index);
        if (item == null) {
            item = new VirtualItem(this, index);
            fItems.put(index, item);
        }
        return item;
    }

    /**
     * @return Returns true if any of the child items need a data update.
     */
    public boolean childrenNeedDataUpdate() {
        if (getItemCount() == 0) {
            return false;
        }
        if (fItems == null || fItems.size() != fItemCount) {
            return true;
        }
		for (VirtualItem child : fItems.values()) {
            if (child.needsDataUpdate()) {
                return true;
            }
        }
        return false;
    }

    /**
     * Returns an array of current child items.  The returned array contains
     * only the items that have been created.  It may not contain as many items as the
     * item count.
     *
     * @return Child items array.
     */
    public VirtualItem[] getItems() {
        return fItems.values().toArray(new VirtualItem[fItems.size()]);
    }

    /**
     * Adds a child item at the given index position.
     * @param position The index position to inser the new item at.
     * @return Returns the added item.
     */
    public VirtualItem addItem(int position) {
        if (!fHasItems) {
            fHasItems = true;
        }
        if (fItemCount < 0) {
            fItemCount = 0;
        }

        // Increment all items with an index higher than the given position.
        fItemCount++;
        ensureItems();
		for (Index childIndex : fItems.keySet()) {
            if (childIndex.intValue() >= position) {
                childIndex.increment();
            }
        }

        // Note: the same index object used to create the item has to
        // be used as the key into the map.
        Index childIndex = new Index(position);
        VirtualItem newChild = new VirtualItem(this, childIndex);
        fItems.put(childIndex, newChild);
        return newChild;
    }

    /**
     * Removes the item at the given index.
     * @param position Index of the item to remove.
     */
    public void remove(Index position) {
        fItemCount--;
        if (fItemCount < 0) {
            fHasItems = false;
        }

        ensureItems();

        VirtualItem removedItem = null;
		for (Iterator<Entry<Index, VirtualItem>> itr = fItems.entrySet().iterator(); itr.hasNext();) {
			Entry<Index, VirtualItem> entry = itr.next();
            Index childIndex = entry.getKey();
            if (childIndex.intValue() > position.intValue()) {
                childIndex.decrement();
            } else if (childIndex.intValue() == position.intValue()) {
                removedItem = entry.getValue();
                removedItem.dispose();
                itr.remove();
            }
        }
    }

    private void ensureItems() {
        if (fItems == null) {
			fItems = new HashMap<Index, VirtualItem>(Math.max(1, Math.min(fItemCount, 16)));
        }
    }

    private VirtualTree findTree() {
        VirtualItem item = this;
        while (!(item instanceof VirtualTree)) {
            item = item.fParent;
        }
        return (VirtualTree)item;
    }

    @Override
	public String toString() {
        StringBuffer buffer = new StringBuffer();
        toStringItem(buffer, IInternalDebugCoreConstants.EMPTY_STRING);
        return buffer.toString();
    }

    void toStringItem(StringBuffer buffer, String indent) {
        buffer.append(indent);
        buffer.append(toStringElement());
        buffer.append("\n"); //$NON-NLS-1$
        indent = indent + "  "; //$NON-NLS-1$
        for (int i = 0; i < fItemCount; i++) {
            VirtualItem item = fItems.get(new Index(i));
            if (item != null) {
                item.toStringItem(buffer, indent);
            } else {
                buffer.append("<no item>\n"); //$NON-NLS-1$
            }
        }
    }

    private String toStringElement() {
        String[] label = (String[])fData.get(LABEL_KEY);
        if (label != null && label.length != 0) {
            return label[0];
        }
        Object data = fData.get(ELEMENT_DATA_KEY);
        if (data != null) {
            return data.toString();
        }
        return "<no data>"; //$NON-NLS-1$
    }
}

Back to the top