KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > keys > CategoryPatternFilter


1 /*******************************************************************************
2  * Copyright (c) 2007 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
12 package org.eclipse.ui.internal.keys;
13
14 import org.eclipse.core.commands.Category;
15 import org.eclipse.core.commands.ParameterizedCommand;
16 import org.eclipse.core.commands.common.NotDefinedException;
17 import org.eclipse.jface.bindings.Binding;
18 import org.eclipse.jface.viewers.Viewer;
19 import org.eclipse.ui.dialogs.PatternFilter;
20
21 class CategoryPatternFilter extends PatternFilter {
22     private boolean filterCategories;
23     final Category uncategorized;
24
25     public CategoryPatternFilter(boolean filterCategories, Category c) {
26         uncategorized = c;
27         filterCategories(filterCategories);
28     }
29
30     public void filterCategories(boolean b) {
31         filterCategories = b;
32         if (filterCategories) {
33             setPattern("org.eclipse.ui.keys.optimization.false"); //$NON-NLS-1$
34
} else {
35             setPattern("org.eclipse.ui.keys.optimization.true"); //$NON-NLS-1$
36
}
37     }
38     
39     public boolean isFilteringCategories() {
40         return filterCategories;
41     }
42
43     /*
44      * (non-Javadoc)
45      *
46      * @see org.eclipse.ui.dialogs.PatternFilter#isLeafMatch(org.eclipse.jface.viewers.Viewer,
47      * java.lang.Object)
48      */

49     protected boolean isLeafMatch(Viewer viewer, Object JavaDoc element) {
50         if (filterCategories) {
51             final ParameterizedCommand cmd = getCommand(element);
52             try {
53                 if (cmd != null
54                         && cmd.getCommand().getCategory() == uncategorized) {
55                     return false;
56                 }
57             } catch (NotDefinedException e) {
58                 return false;
59             }
60         }
61         return super.isLeafMatch(viewer, element);
62     }
63
64     private ParameterizedCommand getCommand(Object JavaDoc element) {
65         if (element instanceof ParameterizedCommand) {
66             return (ParameterizedCommand) element;
67         }
68         if (element instanceof Binding) {
69             return ((Binding) element).getParameterizedCommand();
70         }
71         return null;
72     }
73 }
74
Popular Tags