01: /*******************************************************************************
02: * Copyright (c) 2005, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.dialogs;
11:
12: import java.util.Collection;
13: import java.util.HashSet;
14:
15: import org.eclipse.jface.preference.IPreferenceNode;
16: import org.eclipse.jface.viewers.Viewer;
17: import org.eclipse.jface.viewers.ViewerFilter;
18:
19: /**
20: * The PreferenceNodeFilter is a filter that only matches
21: * a set of ids.
22: */
23: public class PreferenceNodeFilter extends ViewerFilter {
24:
25: Collection ids = new HashSet();
26:
27: /**
28: * Create a new instance of the receiver on a
29: * list of filteredIds.
30: * @param filteredIds The collection of ids that
31: * will be shown.
32: */
33: public PreferenceNodeFilter(String[] filteredIds) {
34: super ();
35: for (int i = 0; i < filteredIds.length; i++) {
36: ids.add(filteredIds[i]);
37: }
38: }
39:
40: /* (non-Javadoc)
41: * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
42: */
43: public boolean select(Viewer viewer, Object parentElement,
44: Object element) {
45: return checkNodeAndChildren((IPreferenceNode) element);
46: }
47:
48: /**
49: * Check to see if the node or any of its children
50: * have an id in the ids.
51: * @param node WorkbenchPreferenceNode
52: * @return boolean <code>true</code> if node or oe of its children
53: * has an id in the ids.
54: */
55: private boolean checkNodeAndChildren(IPreferenceNode node) {
56: if (ids.contains(node.getId())) {
57: return true;
58: }
59:
60: IPreferenceNode[] subNodes = node.getSubNodes();
61: for (int i = 0; i < subNodes.length; i++) {
62: if (checkNodeAndChildren(subNodes[i])) {
63: return true;
64: }
65:
66: }
67: return false;
68: }
69:
70: }
|