KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > AbstractEvaluationHandler


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;
13
14 import org.eclipse.core.expressions.Expression;
15 import org.eclipse.jface.util.IPropertyChangeListener;
16 import org.eclipse.jface.util.PropertyChangeEvent;
17 import org.eclipse.ui.PlatformUI;
18 import org.eclipse.ui.internal.services.IEvaluationReference;
19 import org.eclipse.ui.internal.services.IEvaluationService;
20
21 /**
22  * This internal class serves as a foundation for any handler that would like
23  * its enabled state controlled by core expressions and the IEvaluationService.
24  *
25  * @since 3.3
26  */

27 public abstract class AbstractEvaluationHandler extends AbstractEnabledHandler {
28     private final static String JavaDoc PROP_ENABLED = "enabled"; //$NON-NLS-1$
29
private IEvaluationService evaluationService;
30     private IPropertyChangeListener enablementListener;
31     private IEvaluationReference enablementRef;
32
33     protected IEvaluationService getEvaluationService() {
34         if (evaluationService == null) {
35             evaluationService = (IEvaluationService) PlatformUI.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 JavaDoc) {
58                             setEnabled(((Boolean JavaDoc) 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 }
85
Popular Tags