Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 89c2f366f50f00e510e3432027cc8aa64d327690 (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
/*******************************************************************************
 * Copyright (c) 2008 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;

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

import org.eclipse.swt.SWT;


/**
 * Tree of virtual items that is analogous to SWT's tree control. 
 * 
 * @since 3.5
 */
class VirtualTree extends VirtualItem {

    /**
     * Lazy virtual tree does not retrieve elements or labels,
     * except for the selected elements.
     */
    private boolean fLazy;
    
    /**
     * The item currently at the top of the "virtual" view-port.
     */
    private VirtualItem fTopItem;
    
    /**
     * Interface for listeners that need to be notified when items 
     * are disposed or revealed.  It  should be implemented by the viewer.
     */
    public static interface IVirtualItemListener {
        
        /**
         * Called when the item has been shown in the virtual viewer's 
         * view-port.  This indicates to the viewer that it should check
         * the item's status and request needed data.
         * 
         * @param item The item that was revealed.
         */
        public void revealed(VirtualItem item);
        
        /**
         * Called when an item is disposed.  It tells the viewer to
         * clean up any remaining mappings and cached data of this item.
         * 
         * @param item The itam that was disposed.
         */
        public void disposed(VirtualItem item);
    }
    
    /**
     * Set of listeners of the virtual tree.
     */
    private Set fVirtualItemListeners = new HashSet(1);

    /**
     * The currently selected items.  This array contains only
     * the leaf items which are selected.
     */
    private VirtualItem[] fSelection = new VirtualItem[0];
    
    VirtualTree(int style) {
        super(null, new VirtualItem.Index(0));
        fLazy = (style & SWT.VIRTUAL) != 0;
        clearNeedsLabelUpdate();
        clearNeedsDataUpdate();
    }
    
    void dispose() {
        super.dispose();
        fVirtualItemListeners.clear();
    }

    void setNeedsCountUpdate() {
        super.setNeedsCountUpdate();
        clearNeedsLabelUpdate();
        clearNeedsDataUpdate();
    }
    
    void setNeedsLabelUpdate() {
        // no-op
    }

    void setData(String key, Object data) {
        super.setData(key, data);
        if (data == null) {
            clearNeedsDataUpdate();
        }
    }
    
    void addItemListener(IVirtualItemListener listener) {
        fVirtualItemListeners.add(listener);
    }

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

    VirtualItem[] getSelection() {
        return fSelection;
    }
    
    void setSelection(VirtualItem[] items) {
        fSelection = items;
    }
    
    void showItem(VirtualItem item) {
        setTopItem(item);
    }
    
    void fireItemDisposed(VirtualItem item) {
        for (Iterator itr = fVirtualItemListeners.iterator(); itr.hasNext();) {
            ((IVirtualItemListener)itr.next()).disposed(item);
        }
    }
    
    void fireItemRevealed(VirtualItem item) {
        for (Iterator itr = fVirtualItemListeners.iterator(); itr.hasNext();) {
            ((IVirtualItemListener)itr.next()).revealed(item);
        }        
    }

    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);
    }

    void setTopItem(VirtualItem item) {
        fTopItem = item;
    }
    
    VirtualItem getTopItem() {
        return fTopItem;
    }
    
    void setHasItems(boolean hasChildren) {
        super.setHasItems(hasChildren);
        // The root item is always expanded as long as it has children. 
        if (hasChildren) {
            setExpanded(true);
        }
    }
    
    boolean isItemVisible(VirtualItem item) {
        if (!fLazy) {
            // If not in lazy mode, all items are visible.
            return true;
        } else {
            // TODO: use top item and visible item count to determine list of 
            // 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;
        }
    }

    /**
     * Validates the entire tree.
     */
    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.
     */
    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