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;
11:
12: import org.eclipse.core.expressions.Expression;
13: import org.eclipse.jface.util.IPropertyChangeListener;
14: import org.eclipse.jface.util.PropertyChangeEvent;
15: import org.eclipse.ui.PlatformUI;
16: import org.eclipse.ui.internal.services.IEvaluationReference;
17: import org.eclipse.ui.internal.services.IEvaluationService;
18:
19: /**
20: * This internal class serves as a foundation for any handler that would like
21: * its enabled state controlled by core expressions and the IEvaluationService.
22: *
23: * @since 3.3
24: */
25: public abstract class AbstractEvaluationHandler extends
26: AbstractEnabledHandler {
27: private final static String PROP_ENABLED = "enabled"; //$NON-NLS-1$
28: private IEvaluationService evaluationService;
29: private IPropertyChangeListener enablementListener;
30: private IEvaluationReference enablementRef;
31:
32: protected IEvaluationService getEvaluationService() {
33: if (evaluationService == null) {
34: evaluationService = (IEvaluationService) PlatformUI
35: .getWorkbench()
36: .getService(IEvaluationService.class);
37: }
38: return evaluationService;
39: }
40:
41: protected void registerEnablement() {
42: enablementRef = getEvaluationService().addEvaluationListener(
43: getEnabledWhenExpression(), getEnablementListener(),
44: PROP_ENABLED, null);
45: }
46:
47: protected abstract Expression getEnabledWhenExpression();
48:
49: /**
50: * @return
51: */
52: private IPropertyChangeListener getEnablementListener() {
53: if (enablementListener == null) {
54: enablementListener = new IPropertyChangeListener() {
55: public void propertyChange(PropertyChangeEvent event) {
56: if (event.getProperty() == PROP_ENABLED) {
57: if (event.getNewValue() instanceof Boolean) {
58: setEnabled(((Boolean) event.getNewValue())
59: .booleanValue());
60: } else {
61: setEnabled(false);
62: }
63: }
64: }
65: };
66: }
67: return enablementListener;
68: }
69:
70: /*
71: * (non-Javadoc)
72: *
73: * @see org.eclipse.core.commands.AbstractHandler#dispose()
74: */
75: public void dispose() {
76: if (enablementRef != null) {
77: evaluationService.removeEvaluationListener(enablementRef);
78: enablementRef = null;
79: enablementListener = null;
80: evaluationService = null;
81: }
82: super.dispose();
83: }
84: }
|