Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b84d47aa20f012801585874da00636e2773c40d0 (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
/*******************************************************************************
 * Copyright (c) 2011, 2013 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
 *     IBM Corporation - bug fixing
 *******************************************************************************/
package org.eclipse.debug.internal.ui.viewers.model.provisional;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.swt.SWT;


/**
 * Tree of virtual items that is analogous to SWT's tree control.  The tree is used
 * by the {@link VirtualTreeModelViewer}.
 *
 * @see VirtualTreeModelViewer
 * @since 3.8
 */
public class VirtualTree extends VirtualItem {

    /**
     * Lazy virtual tree does not retrieve elements or labels,
     * except for the selected elements.
     */
    private boolean fLazy;

    private IVirtualItemValidator fValidator;

    private class SelectedItemValidator implements IVirtualItemValidator {
        @Override
		public boolean isItemVisible(VirtualItem item) {
            // visible items.  For now only mark the selected items as visible.
            for (int i = 0; i < fSelection.length; i++) {
                VirtualItem selectionItem = fSelection[i];
                while (selectionItem != null) {
                    if (item.equals(selectionItem)) {
                        return true;
                    }
                    selectionItem = selectionItem.getParent();
                }
            }
            return false;
        }

        @Override
		public void showItem(VirtualItem item) {
        }
    }

    /**
     * Set of listeners of the virtual tree.
     */
	private Set<IVirtualItemListener> fVirtualItemListeners = new HashSet<>(1);

    /**
     * The currently selected items.  This array contains only
     * the leaf items which are selected.
     */
    private VirtualItem[] fSelection = new VirtualItem[0];

    /**
     * Constructs the virtual tree with the given style and validator.
     *
     * @param style The style flag.  Only SWT.VIRTUAL flag is used.
     * @param validator Item validator used to determine item visibility.
     */
    public VirtualTree(int style, IVirtualItemValidator validator) {
        super(null, new VirtualItem.Index(0));
        fLazy = (style & SWT.VIRTUAL) != 0;
        if (fLazy && validator == null) {
            fValidator = new SelectedItemValidator();
        } else {
            fValidator = validator;
        }
        clearNeedsLabelUpdate();
        clearNeedsDataUpdate();
    }

    /**
     * Disposes the virtual tree.
     */
    @Override
	public void dispose() {
        super.dispose();
        fVirtualItemListeners.clear();
    }

    @Override
	public void setNeedsCountUpdate() {
        super.setNeedsCountUpdate();
        clearNeedsLabelUpdate();
        clearNeedsDataUpdate();
    }

    @Override
	public void setNeedsLabelUpdate() {
        // no-op
    }

    @Override
	public void setData(String key, Object data) {
        super.setData(key, data);
        if (data == null) {
            clearNeedsDataUpdate();
        }
    }

    /**
     * Adds a listener for when virtual items are revealed in the view.
     * @param listener Listener to add to list of listeners.
     */
    public void addItemListener(IVirtualItemListener listener) {
        fVirtualItemListeners.add(listener);
    }

    public void removeItemListener(IVirtualItemListener listener) {
        fVirtualItemListeners.remove(listener);
    }

    public VirtualItem[] getSelection() {
        return fSelection;
    }

    public void setSelection(VirtualItem[] items) {
        fSelection = items;
    }

    public void showItem(VirtualItem item) {
        if (fValidator != null) {
            fValidator.showItem(item);
        }
    }

    public void fireItemDisposed(VirtualItem item) {
		for (IVirtualItemListener listener : fVirtualItemListeners) {
			listener.disposed(item);
        }
    }

    public void fireItemRevealed(VirtualItem item) {
		for (IVirtualItemListener listener : fVirtualItemListeners) {
			listener.revealed(item);
        }
    }

    @Override
	public void setData(Object data) {
        super.setData(data);
        // The root item always has children as long as the input is non-null,
        // so that it should be expanded.
        setHasItems(data != null);
    }

    @Override
	public void setHasItems(boolean hasChildren) {
        super.setHasItems(hasChildren);
        // The root item is always expanded as long as it has children.
        if (hasChildren) {
            setExpanded(true);
        }
    }

    /**
     * Returns whether the given item is considered visible by the tree as
     * determined by its virtual item validator.
     *
     * @param item Item to check.
     * @return true if items is vislble.
     * @see IVirtualItemValidator
     */
    public boolean isItemVisible(VirtualItem item) {
        if (fLazy) {
            return fValidator.isItemVisible(item);
        }
        return true;
    }

    /**
     * Validates the entire tree.
     */
    public void validate() {
        validate(VirtualTree.this);
    }

    /**
     * Validates the item and its children, identifying children which were
     * revealed and need to be updated.
     *
     * @param item The item which to validate.
     */
    public void validate(VirtualItem item) {
        if (item.needsDataUpdate()) {
            if (isItemVisible(item)) {
                fireItemRevealed(item);
            }
        } else if (item.getData() != null) {
            if ( item.needsLabelUpdate() || (item.needsCountUpdate() && item.hasItems() && item.getExpanded()) ) {
                if (isItemVisible(item)) {
                    fireItemRevealed(item);
                }
            }

            if (item.getData() != null && item.getItemCount() > 0 && item.getExpanded()) {
                for (int i = 0; i < item.getItemCount(); i++) {
                    validate(item.getItem(new Index(i)));
                }
            }
        }
    }
}

Back to the top