KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > UAContentFilter


1 /*******************************************************************************
2  * Copyright (c) 2006, 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 package org.eclipse.help;
12
13 import org.eclipse.core.expressions.EvaluationContext;
14 import org.eclipse.core.expressions.IEvaluationContext;
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.help.internal.HelpPlugin;
17
18 /**
19  * <p>
20  * This class provides the ability to filter out user assistance model elements
21  * that support filtering (e.g. <code>IToc</code>, <code>ITopic</code>, ...).
22  * Implementations that display such elements should consult this class before
23  * attempting to display them.
24  * </p>
25  *
26  * @since 3.2
27  */

28 public class UAContentFilter {
29     
30     private static final String JavaDoc VARIABLE_PLATFORM = "platform"; //$NON-NLS-1$
31
private static IEvaluationContext defaultContext;
32
33     /**
34      * <p>
35      * Returns whether or not the given object should be filtered out. This
36      * applies to any user assistance component's elements where filters apply
37      * (e.g. help tocs, topics, intro elements, context help topics). If the
38      * element is <code>null</code> or is not filterable, this method returns
39      * <code>false</code>.
40      * </p>
41      * <p>
42      * This method is for use in non-UI environments, when serving help outside
43      * the workbench. If filtering from the UI, use the <code>isFiltered</code>
44      * method that accepts the evaluation context as well.
45      * </p>
46      *
47      * @param element the element to check
48      * @return whether or not the element should be filtered out
49      */

50     public static boolean isFiltered(Object JavaDoc element) {
51         if (defaultContext == null) {
52             defaultContext = new EvaluationContext(null, Platform.class) {
53                 public Object JavaDoc getVariable(String JavaDoc name) {
54                     if (VARIABLE_PLATFORM.equals(name)) {
55                         return Platform.class;
56                     }
57                     return null;
58                 }
59             };
60         }
61         return isFiltered(element, defaultContext);
62     }
63
64     /**
65      * <p>
66      * Returns whether or not the given object should be filtered out. This
67      * applies to any user assistance component's elements where filters apply
68      * (e.g. help tocs, topics, intro elements, context help topics). If the
69      * element is <code>null</code> or is not filterable, this method returns
70      * <code>false</code>. The evaluation context provides the default object
71      * to test on and a set of variables that can be accessed.
72      * </p>
73      *
74      * @param element the element to check
75      * @param context the evaluation context for evaluating expressions
76      * @return whether or not the element should be filtered out
77      */

78     public static boolean isFiltered(Object JavaDoc element, IEvaluationContext context) {
79         if (element instanceof IUAElement) {
80             try {
81                 return !((IUAElement)element).isEnabled(context);
82             }
83             catch (Throwable JavaDoc t) {
84                 String JavaDoc msg = "Error while checking element filter"; //$NON-NLS-1$
85
HelpPlugin.logError(msg, t);
86             }
87         }
88         return false;
89     }
90 }
91
Popular Tags