KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > CompareHandlerService


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 package org.eclipse.compare.internal;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.compare.ICompareContainer;
17 import org.eclipse.core.expressions.Expression;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.commands.ActionHandler;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.ui.*;
22 import org.eclipse.ui.handlers.IHandlerActivation;
23 import org.eclipse.ui.handlers.IHandlerService;
24 import org.eclipse.ui.services.IServiceLocator;
25
26 public class CompareHandlerService {
27     
28     private final List JavaDoc fActivations = new ArrayList JavaDoc();
29     private final Expression fExpression;
30     private ICompareContainer fContainer;
31     private boolean fDisposed;
32     private List JavaDoc fPaneActivations = new ArrayList JavaDoc();
33     private IHandlerService fHandlerService;
34
35     public static CompareHandlerService createFor(ICompareContainer container, Shell shell) {
36         IServiceLocator serviceLocator = container.getServiceLocator();
37         if (serviceLocator != null) {
38             IHandlerService service = (IHandlerService)serviceLocator.getService(IHandlerService.class);
39             if (service != null)
40                 return new CompareHandlerService(container, null);
41         }
42         if (container.getWorkbenchPart() == null && shell != null) {
43             // We're in a dialog so we can use an active shell expression
44
IHandlerService service = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
45             if (service != null) {
46                 Expression e = new ActiveShellExpression(shell);
47                 return new CompareHandlerService(container, e);
48             }
49         }
50         return new CompareHandlerService(null, null);
51     }
52     
53     private CompareHandlerService(ICompareContainer container,
54             Expression expression) {
55         fContainer = container;
56         fExpression = expression;
57         initialize();
58     }
59     
60     public void registerAction(IAction action, String JavaDoc commandId) {
61         IHandlerService handlerService = getHandlerService();
62         if (handlerService == null)
63             return;
64         action.setActionDefinitionId(commandId);
65         IHandlerActivation activation;
66         if (fExpression == null) {
67             activation = handlerService.activateHandler(commandId, new ActionHandler(action));
68         } else {
69             activation = handlerService.activateHandler(commandId, new ActionHandler(action), fExpression);
70         }
71         if (activation != null) {
72             fActivations .add(activation);
73         }
74     }
75     
76     private IHandlerService getHandlerService() {
77         if (fDisposed)
78             return null;
79         return fHandlerService;
80     }
81
82     private void initialize() {
83         if (fHandlerService == null) {
84             IServiceLocator serviceLocator = fContainer.getServiceLocator();
85             if (serviceLocator != null) {
86                 IHandlerService service = (IHandlerService)serviceLocator.getService(IHandlerService.class);
87                 if (service != null)
88                     fHandlerService = service;
89             }
90             if (fHandlerService == null && fContainer.getWorkbenchPart() == null && fExpression != null) {
91                 // We're in a dialog so we can use an active shell expression
92
IHandlerService service = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
93                 if (service != null) {
94                     fHandlerService = service;
95                 }
96             }
97         }
98     }
99
100     public void setGlobalActionHandler(String JavaDoc actionId, IAction actionHandler) {
101         IActionBars bars = getActionBars();
102         if (bars != null) {
103             bars.setGlobalActionHandler(actionId, actionHandler);
104             return;
105         } else if (fExpression != null && actionHandler != null && actionHandler.getActionDefinitionId() != null) {
106             IHandlerService service = getHandlerService();
107             if (service != null) {
108                 IHandlerActivation activation = service.activateHandler(actionHandler.getActionDefinitionId(), new ActionHandler(actionHandler), fExpression);
109                 fPaneActivations.add(activation);
110                 return;
111             }
112         }
113         // Remove the action definition id since we won't get key bindings
114
if (actionHandler != null)
115             actionHandler.setActionDefinitionId(null);
116     }
117     
118     private void updateActionBars() {
119         IActionBars bars = getActionBars();
120         if (bars != null)
121             bars.updateActionBars();
122     }
123
124     private void clearPaneActionHandlers() {
125         if (!fPaneActivations.isEmpty()) {
126             IHandlerService service = getHandlerService();
127             if (service != null) {
128                 service.deactivateHandlers(fPaneActivations);
129                 fPaneActivations.clear();
130             }
131         }
132     }
133     
134     private IActionBars getActionBars() {
135         return fContainer.getActionBars();
136     }
137
138     public void dispose() {
139         clearPaneActionHandlers();
140         IHandlerService service = getHandlerService();
141         if (service == null)
142             return;
143         service.deactivateHandlers(fActivations);
144         fActivations.clear();
145         fDisposed = true;
146     }
147
148     public void updatePaneActionHandlers(Runnable JavaDoc runnable) {
149         clearPaneActionHandlers();
150         runnable.run();
151         updateActionBars();
152     }
153 }
154
Popular Tags