Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 07517dfff5f34d943ac9850d1c90bfa221f579f2 (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
/*******************************************************************************
 * Copyright (c) 2013 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.te.tcf.ui.navigator.filter;

import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.tcf.core.interfaces.IPeerType;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNode;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNodeProperties;
import org.eclipse.tcf.te.tcf.ui.navigator.nodes.PeerRedirectorGroupNode;
import org.eclipse.tcf.te.ui.views.interfaces.ICategory;
import org.eclipse.tcf.te.ui.views.interfaces.IUIConstants;

/**
 * Filter implementation filtering generic from the root level.
 */
public class GenericFilter extends ViewerFilter {

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
	 */
	@Override
	public boolean select(Viewer viewer, Object parentElement, final Object element) {
		boolean visible = true;

		if (element instanceof IPeerNode) {
			final AtomicReference<String> type = new AtomicReference<String>();

			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					type.set(((IPeerNode)element).getPeer().getAttributes().get(IPeerNodeProperties.PROP_TYPE));
				}
			};

			if (Protocol.isDispatchThread()) runnable.run();
			else Protocol.invokeAndWait(runnable);

			boolean belongsTo = type.get() == null || IPeerType.TYPE_GENERIC.equals(type.get());

			if (parentElement instanceof TreePath) {
				// If the direct parent is not a PeerRedirectorGroupNode, look at the very first
				// element in the tree path as parent, which is likely the category.
				Object candidate = ((TreePath)parentElement).getSegment(((TreePath)parentElement).getSegmentCount() - 1);
				if (candidate instanceof PeerRedirectorGroupNode) parentElement = candidate;
				else parentElement = ((TreePath)parentElement).getFirstSegment();
			}
			if (parentElement instanceof ICategory) {
				if (IUIConstants.ID_CAT_MY_TARGETS.equals(((ICategory)parentElement).getId()) || IUIConstants.ID_CAT_NEIGHBORHOOD.equals(((ICategory)parentElement).getId())) {
					visible = belongsTo;
				}
				else if (!IUIConstants.ID_CAT_FAVORITES.equals(((ICategory)parentElement).getId())) {
					visible = !belongsTo;
				}
			}
			else if (!(parentElement instanceof PeerRedirectorGroupNode)) {
				visible = belongsTo;
			}
		}

		return visible;
	}

}

Back to the top