tmf: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / uml2sd / handlers / KeyBindingsManager.java
1 /**********************************************************************
2 * Copyright (c) 2011, 2014 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12 package org.eclipse.tracecompass.tmf.ui.views.uml2sd.handlers;
13
14 import java.util.ArrayList;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Set;
18
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;
28
29 /**
30 * <p>
31 * Singleton class that manages key-bindings for certain commands across multiple sequence
32 * diagram view instances.
33 * </p>
34 *
35 * @version 1.0
36 * @author Bernd Hufmann
37 *
38 */
39 public class KeyBindingsManager {
40
41 // ------------------------------------------------------------------------
42 // Attributes
43 // ------------------------------------------------------------------------
44
45 /**
46 * The singleton instance.
47 */
48 private static KeyBindingsManager fInstance = null;
49 /**
50 * The list of view names.
51 */
52 private Set<String> fViews = new HashSet<>();
53 /**
54 * The list of activations Activations to store
55 */
56 private List<IHandlerActivation> fHandlerActivations = new ArrayList<>();
57 /**
58 * The action reference for moving to a message in view.
59 */
60 private MoveToMessage fGoToMessageForKeyBinding;
61 /**
62 * The action reference for opening the find dialog.
63 */
64 private OpenSDFindDialog fFindForKeyBinding;
65 /**
66 * The action reference for moving up in view.
67 */
68 private MoveSDUp fMoveUpForKeyBinding;
69 /**
70 * The action reference for moving down in view.
71 */
72 private MoveSDDown fMoveDownForKeyBinding;
73 /**
74 * The action reference for moving left in view.
75 */
76 private MoveSDLeft fMoveLeftForKeyBinding;
77 /**
78 * The action reference for moving right in view.
79 */
80 private MoveSDRight fMoveRightForKeyBinding;
81 /**
82 * The action reference for showing node start.
83 */
84 private ShowNodeStart fShowNodeStartForKeyBinding;
85 /**
86 * The action reference for showing node end.
87 */
88 private ShowNodeEnd fShowNodeEndForKeyBinding;
89
90 // ------------------------------------------------------------------------
91 // Constructor
92 // ------------------------------------------------------------------------
93
94 /**
95 * Constructor
96 */
97 protected KeyBindingsManager() {
98 }
99
100 // ------------------------------------------------------------------------
101 // Methods
102 // ------------------------------------------------------------------------
103 /**
104 * Returns the KeyBindingsManager singleton instance.
105 *
106 * @return the KeyBindingsManager singleton instance
107 */
108 public static synchronized KeyBindingsManager getInstance() {
109 if (fInstance == null) {
110 fInstance = new KeyBindingsManager();
111 }
112 return fInstance;
113 }
114
115 /**
116 * Adds a view list of managed view list.
117 *
118 * @param viewId Id of SD view to add and to manage
119 */
120 public void add(String viewId) {
121
122 if (fViews.isEmpty()) {
123 initialize();
124 }
125
126 if(!fViews.contains(viewId)) {
127 fViews.add(viewId);
128 }
129 }
130
131 /**
132 * Removes a view from managed view list
133 *
134 * @param viewId Id of SD view to remove
135 */
136 public void remove(String viewId) {
137 if (fViews.contains(viewId)) {
138 fViews.remove(viewId);
139 }
140 if (fViews.isEmpty()) {
141 dispose();
142 }
143 }
144
145 /*
146 * Initialized the KeyBindingsManager.
147 */
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() {
153 @Override
154 public Object execute(ExecutionEvent event) throws ExecutionException {
155 fGoToMessageForKeyBinding.run();
156 return null;
157 }
158 };
159 IHandlerActivation activation = service.activateHandler(fGoToMessageForKeyBinding.getActionDefinitionId(), handler);
160 fHandlerActivations.add(activation);
161
162 fMoveUpForKeyBinding = new MoveSDUp();
163 handler = new AbstractHandler() {
164 @Override
165 public Object execute(ExecutionEvent event) throws ExecutionException {
166 fMoveUpForKeyBinding.run();
167 return null;
168 }
169 };
170 activation = service.activateHandler(fMoveUpForKeyBinding.getActionDefinitionId(), handler);
171 fHandlerActivations.add(activation);
172
173 fMoveDownForKeyBinding = new MoveSDDown();
174 handler = new AbstractHandler() {
175 @Override
176 public Object execute(ExecutionEvent event) throws ExecutionException {
177 fMoveDownForKeyBinding.run();
178 return null;
179 }
180 };
181 activation = service.activateHandler(fMoveDownForKeyBinding.getActionDefinitionId(), handler);
182 fHandlerActivations.add(activation);
183
184 fMoveLeftForKeyBinding = new MoveSDLeft();
185 handler = new AbstractHandler() {
186 @Override
187 public Object execute(ExecutionEvent event) throws ExecutionException {
188 fMoveLeftForKeyBinding.run();
189 return null;
190 }
191 };
192 activation = service.activateHandler(fMoveLeftForKeyBinding.getActionDefinitionId(), handler);
193 fHandlerActivations.add(activation);
194
195 fMoveRightForKeyBinding = new MoveSDRight();
196 handler = new AbstractHandler() {
197 @Override
198 public Object execute(ExecutionEvent event) throws ExecutionException {
199 fMoveRightForKeyBinding.run();
200 return null;
201 }
202 };
203 activation = service.activateHandler(fMoveRightForKeyBinding.getActionDefinitionId(), handler);
204 fHandlerActivations.add(activation);
205
206 fFindForKeyBinding = new OpenSDFindDialog();
207 handler = new AbstractHandler() {
208 @Override
209 public Object execute(ExecutionEvent event) throws ExecutionException {
210 fFindForKeyBinding.run();
211 return null;
212 }
213 };
214 activation = service.activateHandler(fFindForKeyBinding.getActionDefinitionId(), handler);
215 fFindForKeyBinding.setEnabled(false);
216 fHandlerActivations.add(activation);
217
218 fShowNodeStartForKeyBinding = new ShowNodeStart();
219 fShowNodeStartForKeyBinding.setText(Messages.SequenceDiagram_ShowNodeStart);
220
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$
223
224 handler = new AbstractHandler() {
225 @Override
226 public Object execute(ExecutionEvent event) throws ExecutionException {
227 fShowNodeStartForKeyBinding.run();
228 return null;
229 }
230 };
231 activation = service.activateHandler(fShowNodeStartForKeyBinding.getActionDefinitionId(), handler);
232 fHandlerActivations.add(activation);
233
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$
238
239 handler = new AbstractHandler() {
240 @Override
241 public Object execute(ExecutionEvent event) throws ExecutionException {
242 fShowNodeEndForKeyBinding.run();
243 return null;
244 }
245 };
246 activation = service.activateHandler(fShowNodeEndForKeyBinding.getActionDefinitionId(), handler);
247 fHandlerActivations.add(activation);
248
249 }
250
251 /*
252 * Disposes the KeyBindingsManager
253 */
254 private void dispose() {
255 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
256 if (window == null) {
257 //During Eclipse shutdown the active workbench window is null
258 return;
259 }
260 Object serviceObject = window.getService(IHandlerService.class);
261 IHandlerService service = (IHandlerService) serviceObject;
262 for(IHandlerActivation activation : fHandlerActivations) {
263 service.deactivateHandler(activation);
264 }
265
266 fGoToMessageForKeyBinding = null;
267 fFindForKeyBinding = null;
268 fMoveUpForKeyBinding = null;
269 fMoveDownForKeyBinding = null;
270 fMoveLeftForKeyBinding = null;
271 fMoveRightForKeyBinding = null;
272 fShowNodeStartForKeyBinding = null;
273 fShowNodeEndForKeyBinding = null;
274 }
275
276 /**
277 * Set the view in all supported actions
278 *
279 * @param view to set in global actions
280 */
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);
291 }
292 }
293
294 /**
295 * Enable / disable find action
296 *
297 * @param enabled <code>true</code> for enabling else <code>false</code>
298 */
299 public void setFindEnabled(boolean enabled) {
300 if (fFindForKeyBinding != null) {
301 fFindForKeyBinding.setEnabled(enabled);
302 }
303 }
304 }
This page took 0.048756 seconds and 5 git commands to generate.