01: /*******************************************************************************
02: * Copyright (c) 2007 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.keys;
11:
12: import org.eclipse.core.commands.Category;
13: import org.eclipse.core.commands.ParameterizedCommand;
14: import org.eclipse.core.commands.common.NotDefinedException;
15: import org.eclipse.jface.bindings.Binding;
16: import org.eclipse.jface.viewers.Viewer;
17: import org.eclipse.ui.dialogs.PatternFilter;
18:
19: class CategoryPatternFilter extends PatternFilter {
20: private boolean filterCategories;
21: final Category uncategorized;
22:
23: public CategoryPatternFilter(boolean filterCategories, Category c) {
24: uncategorized = c;
25: filterCategories(filterCategories);
26: }
27:
28: public void filterCategories(boolean b) {
29: filterCategories = b;
30: if (filterCategories) {
31: setPattern("org.eclipse.ui.keys.optimization.false"); //$NON-NLS-1$
32: } else {
33: setPattern("org.eclipse.ui.keys.optimization.true"); //$NON-NLS-1$
34: }
35: }
36:
37: public boolean isFilteringCategories() {
38: return filterCategories;
39: }
40:
41: /*
42: * (non-Javadoc)
43: *
44: * @see org.eclipse.ui.dialogs.PatternFilter#isLeafMatch(org.eclipse.jface.viewers.Viewer,
45: * java.lang.Object)
46: */
47: protected boolean isLeafMatch(Viewer viewer, Object element) {
48: if (filterCategories) {
49: final ParameterizedCommand cmd = getCommand(element);
50: try {
51: if (cmd != null
52: && cmd.getCommand().getCategory() == uncategorized) {
53: return false;
54: }
55: } catch (NotDefinedException e) {
56: return false;
57: }
58: }
59: return super .isLeafMatch(viewer, element);
60: }
61:
62: private ParameterizedCommand getCommand(Object element) {
63: if (element instanceof ParameterizedCommand) {
64: return (ParameterizedCommand) element;
65: }
66: if (element instanceof Binding) {
67: return ((Binding) element).getParameterizedCommand();
68: }
69: return null;
70: }
71: }
|