01: /*******************************************************************************
02: * Copyright (c) 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.activities;
11:
12: import org.eclipse.core.expressions.PropertyTester;
13: import org.eclipse.ui.IWorkbench;
14: import org.eclipse.ui.activities.IActivityManager;
15: import org.eclipse.ui.activities.IWorkbenchActivitySupport;
16: import org.eclipse.ui.activities.WorkbenchActivityHelper;
17:
18: /**
19: * An expressions property tester that tests whether or not an activity or
20: * category of activities is enabled. Useful for cross-component links and to
21: * control discoverability of functionality.
22: *
23: * @since 3.3
24: */
25: public class ActivityPropertyTester extends PropertyTester {
26:
27: private static final String PROPERTY_IS_ACTIVITY_ENABLED = "isActivityEnabled"; //$NON-NLS-1$
28: private static final String PROPERTY_IS_CATEGORY_ENABLED = "isCategoryEnabled"; //$NON-NLS-1$
29:
30: /*
31: * (non-Javadoc)
32: *
33: * @see org.eclipse.core.expressions.PropertyTester#test(java.lang.Object,
34: * java.lang.String, java.lang.Object[], java.lang.Object)
35: */
36: public boolean test(Object receiver, String property,
37: Object[] args, Object expectedValue) {
38: if (args.length == 1 && receiver instanceof IWorkbench
39: && args[0] instanceof String) {
40: if (PROPERTY_IS_ACTIVITY_ENABLED.equals(property)) {
41: return isActivityEnabled((String) args[0],
42: (IWorkbench) receiver);
43: } else if (PROPERTY_IS_CATEGORY_ENABLED.equals(property)) {
44: return isCategoryEnabled((String) args[0],
45: (IWorkbench) receiver);
46: }
47: }
48: return false;
49: }
50:
51: private static boolean isActivityEnabled(String activityId,
52: IWorkbench workbench) {
53: try {
54: IWorkbenchActivitySupport workbenchActivitySupport = workbench
55: .getActivitySupport();
56: IActivityManager activityManager = workbenchActivitySupport
57: .getActivityManager();
58: return activityManager.getActivity(activityId).isEnabled();
59: } catch (Exception e) {
60: // workbench not yet activated; nothing enabled yet
61: }
62: return false;
63: }
64:
65: private static boolean isCategoryEnabled(String categoryId,
66: IWorkbench workbench) {
67: try {
68: IWorkbenchActivitySupport workbenchActivitySupport = workbench
69: .getActivitySupport();
70: IActivityManager activityManager = workbenchActivitySupport
71: .getActivityManager();
72: return WorkbenchActivityHelper.isEnabled(activityManager,
73: categoryId);
74: } catch (Exception e) {
75: // workbench not yet activated; nothing enabled yet
76: }
77: return false;
78: }
79: }
|