KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > PreferenceNodeFilter


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.dialogs;
12
13 import java.util.Collection JavaDoc;
14 import java.util.HashSet JavaDoc;
15
16 import org.eclipse.jface.preference.IPreferenceNode;
17 import org.eclipse.jface.viewers.Viewer;
18 import org.eclipse.jface.viewers.ViewerFilter;
19
20 /**
21  * The PreferenceNodeFilter is a filter that only matches
22  * a set of ids.
23  */

24 public class PreferenceNodeFilter extends ViewerFilter {
25
26     Collection JavaDoc ids = new HashSet JavaDoc();
27
28     /**
29      * Create a new instance of the receiver on a
30      * list of filteredIds.
31      * @param filteredIds The collection of ids that
32      * will be shown.
33      */

34     public PreferenceNodeFilter(String JavaDoc[] filteredIds) {
35         super();
36         for (int i = 0; i < filteredIds.length; i++) {
37             ids.add(filteredIds[i]);
38         }
39     }
40
41     /* (non-Javadoc)
42      * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
43      */

44     public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc 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 }
71
Popular Tags