1 /**********************************************************************
2 * Copyright (c) 2011, 2013 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 IHandlerService service
= (IHandlerService
) PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getService(IHandlerService
.class);
151 AbstractHandler handler
= new AbstractHandler() {
153 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
154 fGoToMessageForKeyBinding
.run();
158 IHandlerActivation activation
= service
.activateHandler(fGoToMessageForKeyBinding
.getActionDefinitionId(), handler
);
159 fHandlerActivations
.add(activation
);
161 fMoveUpForKeyBinding
= new MoveSDUp();
162 handler
= new AbstractHandler() {
164 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
165 fMoveUpForKeyBinding
.run();
169 activation
= service
.activateHandler(fMoveUpForKeyBinding
.getActionDefinitionId(), handler
);
170 fHandlerActivations
.add(activation
);
172 fMoveDownForKeyBinding
= new MoveSDDown();
173 handler
= new AbstractHandler() {
175 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
176 fMoveDownForKeyBinding
.run();
180 activation
= service
.activateHandler(fMoveDownForKeyBinding
.getActionDefinitionId(), handler
);
181 fHandlerActivations
.add(activation
);
183 fMoveLeftForKeyBinding
= new MoveSDLeft();
184 handler
= new AbstractHandler() {
186 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
187 fMoveLeftForKeyBinding
.run();
191 activation
= service
.activateHandler(fMoveLeftForKeyBinding
.getActionDefinitionId(), handler
);
192 fHandlerActivations
.add(activation
);
194 fMoveRightForKeyBinding
= new MoveSDRight();
195 handler
= new AbstractHandler() {
197 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
198 fMoveRightForKeyBinding
.run();
202 activation
= service
.activateHandler(fMoveRightForKeyBinding
.getActionDefinitionId(), handler
);
203 fHandlerActivations
.add(activation
);
205 fFindForKeyBinding
= new OpenSDFindDialog();
206 handler
= new AbstractHandler() {
208 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
209 fFindForKeyBinding
.run();
213 activation
= service
.activateHandler(fFindForKeyBinding
.getActionDefinitionId(), handler
);
214 fFindForKeyBinding
.setEnabled(false);
215 fHandlerActivations
.add(activation
);
217 fShowNodeStartForKeyBinding
= new ShowNodeStart();
218 fShowNodeStartForKeyBinding
.setText(Messages
.SequenceDiagram_ShowNodeStart
);
220 fShowNodeStartForKeyBinding
.setId("org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.ShowNodeStart");//$NON-NLS-1$
221 fShowNodeStartForKeyBinding
.setActionDefinitionId("org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.ShowNodeStart");//$NON-NLS-1$
223 handler
= new AbstractHandler() {
225 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
226 fShowNodeStartForKeyBinding
.run();
230 activation
= service
.activateHandler(fShowNodeStartForKeyBinding
.getActionDefinitionId(), handler
);
231 fHandlerActivations
.add(activation
);
233 fShowNodeEndForKeyBinding
= new ShowNodeEnd();
234 fShowNodeEndForKeyBinding
.setText(Messages
.SequenceDiagram_ShowNodeEnd
);
235 fShowNodeEndForKeyBinding
.setId("org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.ShowNodeEnd");//$NON-NLS-1$
236 fShowNodeEndForKeyBinding
.setActionDefinitionId("org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.ShowNodeEnd");//$NON-NLS-1$
238 handler
= new AbstractHandler() {
240 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
241 fShowNodeEndForKeyBinding
.run();
245 activation
= service
.activateHandler(fShowNodeEndForKeyBinding
.getActionDefinitionId(), handler
);
246 fHandlerActivations
.add(activation
);
251 * Disposes the KeyBindingsManager
253 private void dispose() {
254 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
255 if (window
== null) {
256 //During Eclipse shutdown the active workbench window is null
259 IHandlerService service
= (IHandlerService
) window
.getService(IHandlerService
.class);
260 for(IHandlerActivation activation
: fHandlerActivations
) {
261 service
.deactivateHandler(activation
);
264 fGoToMessageForKeyBinding
= null;
265 fFindForKeyBinding
= null;
266 fMoveUpForKeyBinding
= null;
267 fMoveDownForKeyBinding
= null;
268 fMoveLeftForKeyBinding
= null;
269 fMoveRightForKeyBinding
= null;
270 fShowNodeStartForKeyBinding
= null;
271 fShowNodeEndForKeyBinding
= null;
275 * Set the view in all supported actions
277 * @param view to set in global actions
279 public void setSdView(SDView view
) {
280 if (!fViews
.isEmpty()) {
281 fGoToMessageForKeyBinding
.setView(view
);
282 fFindForKeyBinding
.setView(view
);
283 fMoveUpForKeyBinding
.setView(view
);
284 fMoveDownForKeyBinding
.setView(view
);
285 fMoveLeftForKeyBinding
.setView(view
);
286 fMoveRightForKeyBinding
.setView(view
);
287 fShowNodeStartForKeyBinding
.setView(view
);
288 fShowNodeEndForKeyBinding
.setView(view
);
293 * Enable / disable find action
295 * @param enabled <code>true</code> for enabling else <code>false</code>
297 public void setFindEnabled(boolean enabled
) {
298 if (fFindForKeyBinding
!= null) {
299 fFindForKeyBinding
.setEnabled(enabled
);