1 /**********************************************************************
2 * Copyright (c) 2011, 2014 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12 package org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.handlers
;
14 import java
.util
.ArrayList
;
15 import java
.util
.HashSet
;
16 import java
.util
.List
;
19 import org
.eclipse
.core
.commands
.AbstractHandler
;
20 import org
.eclipse
.core
.commands
.ExecutionEvent
;
21 import org
.eclipse
.core
.commands
.ExecutionException
;
22 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.SDView
;
23 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.util
.Messages
;
24 import org
.eclipse
.ui
.IWorkbenchWindow
;
25 import org
.eclipse
.ui
.PlatformUI
;
26 import org
.eclipse
.ui
.handlers
.IHandlerActivation
;
27 import org
.eclipse
.ui
.handlers
.IHandlerService
;
31 * Singleton class that manages key-bindings for certain commands across multiple sequence
32 * diagram view instances.
36 * @author Bernd Hufmann
39 public class KeyBindingsManager
{
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
46 * The singleton instance.
48 private static KeyBindingsManager fInstance
= null;
50 * The list of view names.
52 private Set
<String
> fViews
= new HashSet
<>();
54 * The list of activations Activations to store
56 private List
<IHandlerActivation
> fHandlerActivations
= new ArrayList
<>();
58 * The action reference for moving to a message in view.
60 private MoveToMessage fGoToMessageForKeyBinding
;
62 * The action reference for opening the find dialog.
64 private OpenSDFindDialog fFindForKeyBinding
;
66 * The action reference for moving up in view.
68 private MoveSDUp fMoveUpForKeyBinding
;
70 * The action reference for moving down in view.
72 private MoveSDDown fMoveDownForKeyBinding
;
74 * The action reference for moving left in view.
76 private MoveSDLeft fMoveLeftForKeyBinding
;
78 * The action reference for moving right in view.
80 private MoveSDRight fMoveRightForKeyBinding
;
82 * The action reference for showing node start.
84 private ShowNodeStart fShowNodeStartForKeyBinding
;
86 * The action reference for showing node end.
88 private ShowNodeEnd fShowNodeEndForKeyBinding
;
90 // ------------------------------------------------------------------------
92 // ------------------------------------------------------------------------
97 protected KeyBindingsManager() {
100 // ------------------------------------------------------------------------
102 // ------------------------------------------------------------------------
104 * Returns the KeyBindingsManager singleton instance.
106 * @return the KeyBindingsManager singleton instance
108 public static synchronized KeyBindingsManager
getInstance() {
109 if (fInstance
== null) {
110 fInstance
= new KeyBindingsManager();
116 * Adds a view list of managed view list.
118 * @param viewId Id of SD view to add and to manage
120 public void add(String viewId
) {
122 if (fViews
.isEmpty()) {
126 if(!fViews
.contains(viewId
)) {
132 * Removes a view from managed view list
134 * @param viewId Id of SD view to remove
136 public void remove(String viewId
) {
137 if (fViews
.contains(viewId
)) {
138 fViews
.remove(viewId
);
140 if (fViews
.isEmpty()) {
146 * Initialized the KeyBindingsManager.
148 private void initialize() {
149 fGoToMessageForKeyBinding
= new MoveToMessage();
150 Object serviceObject
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getService(IHandlerService
.class);
151 IHandlerService service
= (IHandlerService
) serviceObject
;
152 AbstractHandler handler
= new AbstractHandler() {
154 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
155 fGoToMessageForKeyBinding
.run();
159 IHandlerActivation activation
= service
.activateHandler(fGoToMessageForKeyBinding
.getActionDefinitionId(), handler
);
160 fHandlerActivations
.add(activation
);
162 fMoveUpForKeyBinding
= new MoveSDUp();
163 handler
= new AbstractHandler() {
165 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
166 fMoveUpForKeyBinding
.run();
170 activation
= service
.activateHandler(fMoveUpForKeyBinding
.getActionDefinitionId(), handler
);
171 fHandlerActivations
.add(activation
);
173 fMoveDownForKeyBinding
= new MoveSDDown();
174 handler
= new AbstractHandler() {
176 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
177 fMoveDownForKeyBinding
.run();
181 activation
= service
.activateHandler(fMoveDownForKeyBinding
.getActionDefinitionId(), handler
);
182 fHandlerActivations
.add(activation
);
184 fMoveLeftForKeyBinding
= new MoveSDLeft();
185 handler
= new AbstractHandler() {
187 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
188 fMoveLeftForKeyBinding
.run();
192 activation
= service
.activateHandler(fMoveLeftForKeyBinding
.getActionDefinitionId(), handler
);
193 fHandlerActivations
.add(activation
);
195 fMoveRightForKeyBinding
= new MoveSDRight();
196 handler
= new AbstractHandler() {
198 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
199 fMoveRightForKeyBinding
.run();
203 activation
= service
.activateHandler(fMoveRightForKeyBinding
.getActionDefinitionId(), handler
);
204 fHandlerActivations
.add(activation
);
206 fFindForKeyBinding
= new OpenSDFindDialog();
207 handler
= new AbstractHandler() {
209 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
210 fFindForKeyBinding
.run();
214 activation
= service
.activateHandler(fFindForKeyBinding
.getActionDefinitionId(), handler
);
215 fFindForKeyBinding
.setEnabled(false);
216 fHandlerActivations
.add(activation
);
218 fShowNodeStartForKeyBinding
= new ShowNodeStart();
219 fShowNodeStartForKeyBinding
.setText(Messages
.SequenceDiagram_ShowNodeStart
);
221 fShowNodeStartForKeyBinding
.setId("org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.ShowNodeStart");//$NON-NLS-1$
222 fShowNodeStartForKeyBinding
.setActionDefinitionId("org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.ShowNodeStart");//$NON-NLS-1$
224 handler
= new AbstractHandler() {
226 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
227 fShowNodeStartForKeyBinding
.run();
231 activation
= service
.activateHandler(fShowNodeStartForKeyBinding
.getActionDefinitionId(), handler
);
232 fHandlerActivations
.add(activation
);
234 fShowNodeEndForKeyBinding
= new ShowNodeEnd();
235 fShowNodeEndForKeyBinding
.setText(Messages
.SequenceDiagram_ShowNodeEnd
);
236 fShowNodeEndForKeyBinding
.setId("org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.ShowNodeEnd");//$NON-NLS-1$
237 fShowNodeEndForKeyBinding
.setActionDefinitionId("org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.ShowNodeEnd");//$NON-NLS-1$
239 handler
= new AbstractHandler() {
241 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
242 fShowNodeEndForKeyBinding
.run();
246 activation
= service
.activateHandler(fShowNodeEndForKeyBinding
.getActionDefinitionId(), handler
);
247 fHandlerActivations
.add(activation
);
252 * Disposes the KeyBindingsManager
254 private void dispose() {
255 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
256 if (window
== null) {
257 //During Eclipse shutdown the active workbench window is null
260 Object serviceObject
= window
.getService(IHandlerService
.class);
261 IHandlerService service
= (IHandlerService
) serviceObject
;
262 for(IHandlerActivation activation
: fHandlerActivations
) {
263 service
.deactivateHandler(activation
);
266 fGoToMessageForKeyBinding
= null;
267 fFindForKeyBinding
= null;
268 fMoveUpForKeyBinding
= null;
269 fMoveDownForKeyBinding
= null;
270 fMoveLeftForKeyBinding
= null;
271 fMoveRightForKeyBinding
= null;
272 fShowNodeStartForKeyBinding
= null;
273 fShowNodeEndForKeyBinding
= null;
277 * Set the view in all supported actions
279 * @param view to set in global actions
281 public void setSdView(SDView view
) {
282 if (!fViews
.isEmpty()) {
283 fGoToMessageForKeyBinding
.setView(view
);
284 fFindForKeyBinding
.setView(view
);
285 fMoveUpForKeyBinding
.setView(view
);
286 fMoveDownForKeyBinding
.setView(view
);
287 fMoveLeftForKeyBinding
.setView(view
);
288 fMoveRightForKeyBinding
.setView(view
);
289 fShowNodeStartForKeyBinding
.setView(view
);
290 fShowNodeEndForKeyBinding
.setView(view
);
295 * Enable / disable find action
297 * @param enabled <code>true</code> for enabling else <code>false</code>
299 public void setFindEnabled(boolean enabled
) {
300 if (fFindForKeyBinding
!= null) {
301 fFindForKeyBinding
.setEnabled(enabled
);