Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c76348744c1b968e7508414ea8a8b1a68a1b14d3 (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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/*******************************************************************************
 * Copyright (c) 2009, 2011 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 - ongoing bug fixes and enhancements
 *******************************************************************************/
package org.eclipse.debug.internal.ui.viewers.model;

import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelChangedListener;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelDelta;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IStateUpdateListener;
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.model.provisional.TreeModelViewer;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;

/**
 * Specialized tree viewer which displays only sub-tree of a full model. 
 * 
 * @since 3.5
 */
public class SubTreeModelViewer extends TreeModelViewer {

    /**
     * The tree path in the model to the root element of this viewer.
     */
    private TreePath fRootPath = TreePath.EMPTY;

    /**
     * Viewer delegate that content and label providers refer to for viewer data.
     */
    private DelegatingTreeModelViewer fDelegatingViewer;
    
    /**
     * @return Returns the root element's model tree path. 
     */
    public TreePath getRootPath() {
        return fRootPath;
    }

    public SubTreeModelViewer(Composite parent, int style, IPresentationContext context) {
        super(parent, style, context);
    }

    /**
     * Sets the viewer's input and root element's path
     * 
     * @param input New viewer input.
     * @param rootPath New root element path.
     */
    public void setInput(Object input, TreePath rootPath) {
        fRootPath = rootPath;
        super.setInput(input);
    }
    
    /**
     * A proxy for the sub tree viewer which is given to the content and 
     * label providers.  It translates the sub-tree paths in the viewer to the 
     * full model paths that the providers expect.
     */
    public class DelegatingTreeModelViewer extends Viewer 
        implements IInternalTreeModelViewer 
    {
        public void reveal(TreePath path, int index) {
            if (path.startsWith(fRootPath, null)) {
                SubTreeModelViewer.this.reveal(createSubPath(path), index);
            }
        }
        
        public void replace(Object parentOrTreePath, int index, Object element) {
            if (parentOrTreePath instanceof TreePath) {
                TreePath path = (TreePath)parentOrTreePath;
                if (path.startsWith(fRootPath, null)) {
                    SubTreeModelViewer.this.replace(createSubPath(path), index, element);
                }
            } else {
                SubTreeModelViewer.this.replace(parentOrTreePath, index, element);
            }
        }
        
        public void setChildCount(Object elementOrTreePath, int count) {
            if (elementOrTreePath instanceof TreePath) {
                TreePath path = (TreePath)elementOrTreePath;
                if (path.startsWith(fRootPath, null)) {
                    SubTreeModelViewer.this.setChildCount(createSubPath(path), count);
                }
            } else {
                SubTreeModelViewer.this.setChildCount(elementOrTreePath, count);
            }
        }
        
        public void setHasChildren(Object elementOrTreePath, boolean hasChildren) {
            if (elementOrTreePath instanceof TreePath) {
                TreePath path = (TreePath)elementOrTreePath;
                if (path.startsWith(fRootPath, null)) {
                    SubTreeModelViewer.this.setHasChildren(createSubPath(path), hasChildren);
                }
            } else {
                SubTreeModelViewer.this.setHasChildren(elementOrTreePath, hasChildren);
            }
        }

        public void autoExpand(TreePath elementPath) {
            // not supported
        }

        public void setExpandedState(Object elementOrTreePath, boolean expanded) {
            if (elementOrTreePath instanceof TreePath) {
                TreePath path = (TreePath)elementOrTreePath;
                if (path.startsWith(fRootPath, null)) {
                    SubTreeModelViewer.this.setExpandedState(createSubPath(path), expanded);
                }
            } else {
                SubTreeModelViewer.this.setExpandedState(elementOrTreePath, expanded);
            }
        }
        
        public void expandToLevel(Object elementOrTreePath, int level) {
            if (elementOrTreePath instanceof TreePath) {
                TreePath path = (TreePath)elementOrTreePath;
                if (path.startsWith(fRootPath, null)) {
                    SubTreeModelViewer.this.expandToLevel(createSubPath(path), level);
                }
            } else {
                SubTreeModelViewer.this.expandToLevel(elementOrTreePath, level);
            }
        }

        public void remove(Object elementOrTreePath) {
            if (elementOrTreePath instanceof TreePath) {
                TreePath path = (TreePath)elementOrTreePath;
                if (path.startsWith(fRootPath, null)) {
                    SubTreeModelViewer.this.remove(createSubPath(path));
                }
            } else {
                SubTreeModelViewer.this.remove(elementOrTreePath);
            }
        }

        public void remove(Object parentOrTreePath, final int index) {
            if (parentOrTreePath instanceof TreePath) {
                TreePath path = (TreePath)parentOrTreePath;
                if (path.startsWith(fRootPath, null)) {
                    SubTreeModelViewer.this.remove(createSubPath(path), index);
                }
            } else {
                SubTreeModelViewer.this.remove(parentOrTreePath, index);
            }
        }

        public void insert(Object parentOrTreePath, Object element, int position) {
            if (parentOrTreePath instanceof TreePath) {
                TreePath path = (TreePath)parentOrTreePath;
                if (path.startsWith(fRootPath, null)) {
                    SubTreeModelViewer.this.insert(createSubPath(path), element, position);
                }
            } else {
                SubTreeModelViewer.this.insert(parentOrTreePath, element, position);
            }
        }

        public boolean getExpandedState(Object elementOrTreePath) {
            if (elementOrTreePath instanceof TreePath) {
                TreePath path = (TreePath)elementOrTreePath;
                if (path.startsWith(fRootPath, null)) {
                    return SubTreeModelViewer.this.getExpandedState(createSubPath(path));
                }
            } else {
                return SubTreeModelViewer.this.getExpandedState(elementOrTreePath);
            }
            return false;
        }
        
        public int getChildCount(TreePath path) {
            if (path.startsWith(fRootPath, null)) {
                return SubTreeModelViewer.this.getChildCount(createSubPath(path));
            }        
            return -1;
        }

        public boolean getHasChildren(Object elementOrTreePath) {
            if (elementOrTreePath instanceof TreePath) {
                TreePath path = (TreePath)elementOrTreePath;
                if (path.startsWith(fRootPath, null)) {
                    return SubTreeModelViewer.this.getHasChildren(createSubPath(path));
                }
            } else {
                return SubTreeModelViewer.this.getHasChildren(elementOrTreePath);
            }
            return false;
        }

        public Object getChildElement(TreePath path, int index) {
            if (path.startsWith(fRootPath, null)) {
                return SubTreeModelViewer.this.getChildElement(createSubPath(path), index);
            }        
            return null;
        }
        
        public TreePath getTopElementPath() {
            return createFullPath(SubTreeModelViewer.this.getTopElementPath());
        }
        
        public int findElementIndex(TreePath parentPath, Object element) {
            if (parentPath.startsWith(fRootPath, null)) {
                return SubTreeModelViewer.this.findElementIndex(createSubPath(parentPath), element);
            }        
            return -1;
        }

        public boolean getElementChildrenRealized(TreePath parentPath) {
            if (parentPath.startsWith(fRootPath, null)) {
                return SubTreeModelViewer.this.getElementChildrenRealized(createSubPath(parentPath));
            }        
            return true;
        }
        
        public void setElementData(TreePath path, int numColumns, String[] labels, ImageDescriptor[] images, FontData[] fontDatas, RGB[] foregrounds, RGB[] backgrounds) {
            if (path.startsWith(fRootPath, null)) {
                SubTreeModelViewer.this.setElementData(createSubPath(path), numColumns, labels, images, fontDatas, foregrounds, backgrounds);
            }        
        }

        public Control getControl() {
            return SubTreeModelViewer.this.getControl();
        }

        public Object getInput() {
            return SubTreeModelViewer.this.getInput();
        }

        public ISelection getSelection() {
            return SubTreeModelViewer.this.getSelection();
        }

        public void refresh() {
            SubTreeModelViewer.this.refresh();
        }

        public void setInput(Object input) {
            SubTreeModelViewer.this.setInput(input);
        }

        public void setSelection(ISelection selection, boolean reveal) {
            SubTreeModelViewer.this.setSelection(selection, reveal);
        }

        public String[] getVisibleColumns() {
             return SubTreeModelViewer.this.getVisibleColumns();
        }

        public void addLabelUpdateListener(ILabelUpdateListener listener) {
            SubTreeModelViewer.this.addLabelUpdateListener(listener);
        }

        public void addModelChangedListener(IModelChangedListener listener) {
            SubTreeModelViewer.this.addModelChangedListener(listener);
        }

        public void addStateUpdateListener(IStateUpdateListener listener) {
            SubTreeModelViewer.this.addStateUpdateListener(listener);
        }

        public void addViewerUpdateListener(IViewerUpdateListener listener) {
            SubTreeModelViewer.this.addViewerUpdateListener(listener);
        }

        public int getAutoExpandLevel() {
            return SubTreeModelViewer.this.getAutoExpandLevel();
        }

        public Display getDisplay() {
            return SubTreeModelViewer.this.getDisplay();
        }

        public ViewerLabel getElementLabel(TreePath path, String columnId) {
            return SubTreeModelViewer.this.getElementLabel(path, columnId);
        }

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

        public void removeLabelUpdateListener(ILabelUpdateListener listener) {
            SubTreeModelViewer.this.removeLabelUpdateListener(listener);
        }

        public void removeModelChangedListener(IModelChangedListener listener) {
            SubTreeModelViewer.this.removeModelChangedListener(listener);
        }

        public void removeStateUpdateListener(IStateUpdateListener listener) {
            SubTreeModelViewer.this.removeStateUpdateListener(listener);
        }

        public void removeViewerUpdateListener(IViewerUpdateListener listener) {
            SubTreeModelViewer.this.removeViewerUpdateListener(listener);
        }

        public boolean saveElementState(TreePath path, ModelDelta delta, int deltaFlags) {
            return SubTreeModelViewer.this.saveElementState(path, delta, deltaFlags);
        }

        public void setAutoExpandLevel(int level) {
            SubTreeModelViewer.this.setAutoExpandLevel(level);
        }

        public void setSelection(ISelection selection, boolean reveal, boolean force) {
            SubTreeModelViewer.this.setSelection(selection, reveal, force);
        }
        
        public boolean trySelection(ISelection selection, boolean reveal, boolean force) {
        	return SubTreeModelViewer.this.trySelection(selection, reveal, force);
        }

        public void updateViewer(IModelDelta delta) {
            SubTreeModelViewer.this.updateViewer(delta);
        }

        
        public ViewerFilter[] getFilters() {
            return SubTreeModelViewer.this.getFilters();
        }

        public void addFilter(ViewerFilter filter) {
            SubTreeModelViewer.this.addFilter(filter);
        }
        
        public void setFilters(ViewerFilter[] filters) {
        	SubTreeModelViewer.this.setFilters(filters);
        }
        
        public boolean overrideSelection(ISelection current, ISelection candidate) {
            return SubTreeModelViewer.this.overrideSelection(current, candidate);
        }

        public void refresh(Object element) {
            SubTreeModelViewer.this.refresh(element);
        }

        public void update(Object element) {
            SubTreeModelViewer.this.update(element);
        }
        
        public void clearSelectionQuiet() {
        	SubTreeModelViewer.this.clearSelectionQuiet();
        }
        
        public TreePath[] getElementPaths(Object element) {
            TreePath[] subViewerPaths = SubTreeModelViewer.this.getElementPaths(element);
            TreePath[] retVal = new TreePath[subViewerPaths.length];
            for (int i = 0; i < subViewerPaths.length; i++) {
                retVal[i] = createFullPath(subViewerPaths[i]);
            }
            return retVal;
        }
        
        public boolean getElementChecked(TreePath path) {
            return SubTreeModelViewer.this.getElementChecked(createSubPath(path));
        }
        
        public boolean getElementGrayed(TreePath path) {
            return SubTreeModelViewer.this.getElementGrayed(createSubPath(path));
        }
        
        public void setElementChecked(TreePath path, boolean checked, boolean grayed) {
            SubTreeModelViewer.this.setElementChecked(createSubPath(path), checked, grayed);
        }
    }

    
    /**
     * Delegating content provider.  It translates all the calls to the 
     * underlying content provider to use full model tree paths.
     */
    private class SubTreeModelContentProvider implements ITreeModelContentProvider {
        
        private TreeModelContentProvider fBaseProvider;
        
        public SubTreeModelContentProvider() {
            fBaseProvider = new TreeModelContentProvider();
        }
        
        public void updateHasChildren(TreePath path) {
            fBaseProvider.updateHasChildren(createFullPath(path));
        }
        
        public void updateChildCount(TreePath path, int currentChildCount) {
            fBaseProvider.updateChildCount(createFullPath(path), currentChildCount);
        }

        public void updateElement(TreePath parentPath, int viewIndex) {
            fBaseProvider.updateElement(createFullPath(parentPath), viewIndex);
        }
        
        public int viewToModelCount(TreePath parentPath, int count) {
            return fBaseProvider.viewToModelCount(createFullPath(parentPath), count);
        }
        
        public int viewToModelIndex(TreePath parentPath, int index) {
            return fBaseProvider.viewToModelIndex(createFullPath(parentPath), index);
        }

        public void addModelChangedListener(IModelChangedListener listener) {
            fBaseProvider.addModelChangedListener(listener);
        }

        public void preserveState(TreePath subPath) {
            fBaseProvider.preserveState(createFullPath(subPath));
        }

        public void addStateUpdateListener(IStateUpdateListener listener) {
            fBaseProvider.addStateUpdateListener(listener);
        }

        public void addViewerUpdateListener(IViewerUpdateListener listener) {
            fBaseProvider.addViewerUpdateListener(listener);
        }

        public int getModelDeltaMask() {
            return fBaseProvider.getModelDeltaMask();
        }

        public int modelToViewChildCount(TreePath parentPath, int count) {
            return fBaseProvider.modelToViewChildCount(createFullPath(parentPath), count);
        }

        public int modelToViewIndex(TreePath parentPath, int index) {
            return fBaseProvider.modelToViewIndex(createFullPath(parentPath), index);
        }

        public void removeModelChangedListener(IModelChangedListener listener) {
            fBaseProvider.removeModelChangedListener(listener);
        }

        public void removeStateUpdateListener(IStateUpdateListener listener) {
            fBaseProvider.removeStateUpdateListener(listener);
        }

        public void removeViewerUpdateListener(IViewerUpdateListener listener) {
            fBaseProvider.removeViewerUpdateListener(listener);
        }

        public void setModelDeltaMask(int mask) {
            fBaseProvider.setModelDeltaMask(mask);
        }

        public boolean areTreeModelViewerFiltersApplicable(Object parentElement) {
            return fBaseProvider.areTreeModelViewerFiltersApplicable(parentElement);
        }
        
        public boolean shouldFilter(Object parentElementOrTreePath, Object element) {
            if (parentElementOrTreePath instanceof TreePath) {
                TreePath path = (TreePath)parentElementOrTreePath;
                return fBaseProvider.shouldFilter(createFullPath(path), element);
            } else {
                return fBaseProvider.shouldFilter(parentElementOrTreePath, element);
            }
            
        }

        public void unmapPath(TreePath path) {
            fBaseProvider.unmapPath(createFullPath(path));
        }

        public void updateModel(IModelDelta delta, int mask) {
            fBaseProvider.updateModel(delta, mask);
        }

        public TreePath[] getParents(Object element) {
            // Not used
            return null;
        }

        public void cancelRestore(TreePath path, int flags) {
            fBaseProvider.cancelRestore(createFullPath(path), flags);
        }
        
        public void dispose() {
            fBaseProvider.dispose();
        }

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            fBaseProvider.inputChanged(fDelegatingViewer, oldInput, newInput);
        }
        
		public void postInputChanged(IInternalTreeModelViewer viewer,
				Object oldInput, Object newInput) {
			fBaseProvider.postInputChanged(viewer, oldInput, newInput);
		}
		
        public boolean setChecked(TreePath path, boolean checked) {
            return fBaseProvider.setChecked(createFullPath(path), checked);
        }

    }

    /**
     * Delegating label provider.  It translates all the calls to the 
     * underlying label provider to use full model tree paths.
     */
    private class SubTreeModelLabelProvider extends ColumnLabelProvider
        implements ITreeModelLabelProvider 
    {

        private TreeModelLabelProvider fBaseProvider;
        
        public SubTreeModelLabelProvider(IInternalTreeModelViewer viewer) {
            fBaseProvider = new TreeModelLabelProvider(viewer);
        }
        
        public boolean update(TreePath elementPath) {
            return fBaseProvider.update( createFullPath(elementPath) );
        }

        public void addLabelUpdateListener(ILabelUpdateListener listener) {
            fBaseProvider.addLabelUpdateListener(listener);
        }

        public Color getColor(RGB rgb) {
            return fBaseProvider.getColor(rgb);
        }

        public Font getFont(FontData fontData) {
            return fBaseProvider.getFont(fontData);
        }

        public Image getImage(ImageDescriptor descriptor) {
            return fBaseProvider.getImage(descriptor);
        }

        public void removeLabelUpdateListener(ILabelUpdateListener listener) {
            fBaseProvider.removeLabelUpdateListener(listener);
        }

        public void addListener(ILabelProviderListener listener) {
            fBaseProvider.addListener(listener);
        }

        public void dispose() {
            fBaseProvider.dispose();
            super.dispose();
        }

        public boolean isLabelProperty(Object element, String property) {
            return fBaseProvider.isLabelProperty(element, property);
        }

        public void removeListener(ILabelProviderListener listener) {
            fBaseProvider.removeListener(listener);
        }
    }
        
    private TreePath createFullPath(TreePath subPath) {
        if (fRootPath == null) {
            return TreePath.EMPTY;
        }
        
        Object[] segments = new Object[fRootPath.getSegmentCount() + subPath.getSegmentCount()];
        for (int i = 0; i < fRootPath.getSegmentCount(); i++) {
            segments[i] = fRootPath.getSegment(i);
        }
        for (int i = 0; i < subPath.getSegmentCount(); i++) {
            segments[i + fRootPath.getSegmentCount()] = subPath.getSegment(i);
        }
        return new TreePath(segments);
    }
    
    private TreePath createSubPath(TreePath fullPath) {
        if (fRootPath == null) {
            return TreePath.EMPTY;
        }

        if (fullPath.getSegmentCount() <= fRootPath.getSegmentCount()) {
            return TreePath.EMPTY;
        }
        Object[] segments = new Object[fullPath.getSegmentCount() - fRootPath.getSegmentCount()];
        for (int i = 0; i < segments.length; i++) {
            segments[i] = fullPath.getSegment(i + fRootPath.getSegmentCount());
        }
        return new TreePath(segments);
    }
    
    private DelegatingTreeModelViewer getDelegatingViewer() {
    	if (fDelegatingViewer == null) {
    		fDelegatingViewer = new DelegatingTreeModelViewer();
    	}
    	return fDelegatingViewer;
    }
    
    protected ITreeModelContentProvider createContentProvider() {
        return new SubTreeModelContentProvider();
    }
    
    protected ITreeModelLabelProvider createLabelProvider() {
        return new SubTreeModelLabelProvider(getDelegatingViewer());
    }
    

}

Back to the top