tmf: Switch tmf.ui to Java 7 + fix warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / handlers / KeyBindingsManager.java
1 /**********************************************************************
2 * Copyright (c) 2011, 2012 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.linuxtools.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.linuxtools.tmf.ui.views.uml2sd.SDView;
23 import org.eclipse.linuxtools.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 IHandlerService service = (IHandlerService) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(IHandlerService.class);
151 AbstractHandler handler = new AbstractHandler() {
152 @Override
153 public Object execute(ExecutionEvent event) throws ExecutionException {
154 fGoToMessageForKeyBinding.run();
155 return null;
156 }
157 };
158 IHandlerActivation activation = service.activateHandler(fGoToMessageForKeyBinding.getActionDefinitionId(), handler);
159 fHandlerActivations.add(activation);
160
161 fMoveUpForKeyBinding = new MoveSDUp();
162 handler = new AbstractHandler() {
163 @Override
164 public Object execute(ExecutionEvent event) throws ExecutionException {
165 fMoveUpForKeyBinding.run();
166 return null;
167 }
168 };
169 activation = service.activateHandler(fMoveUpForKeyBinding.getActionDefinitionId(), handler);
170 fHandlerActivations.add(activation);
171
172 fMoveDownForKeyBinding = new MoveSDDown();
173 handler = new AbstractHandler() {
174 @Override
175 public Object execute(ExecutionEvent event) throws ExecutionException {
176 fMoveDownForKeyBinding.run();
177 return null;
178 }
179 };
180 activation = service.activateHandler(fMoveDownForKeyBinding.getActionDefinitionId(), handler);
181 fHandlerActivations.add(activation);
182
183 fMoveLeftForKeyBinding = new MoveSDLeft();
184 handler = new AbstractHandler() {
185 @Override
186 public Object execute(ExecutionEvent event) throws ExecutionException {
187 fMoveLeftForKeyBinding.run();
188 return null;
189 }
190 };
191 activation = service.activateHandler(fMoveLeftForKeyBinding.getActionDefinitionId(), handler);
192 fHandlerActivations.add(activation);
193
194 fMoveRightForKeyBinding = new MoveSDRight();
195 handler = new AbstractHandler() {
196 @Override
197 public Object execute(ExecutionEvent event) throws ExecutionException {
198 fMoveRightForKeyBinding.run();
199 return null;
200 }
201 };
202 activation = service.activateHandler(fMoveRightForKeyBinding.getActionDefinitionId(), handler);
203 fHandlerActivations.add(activation);
204
205 fFindForKeyBinding = new OpenSDFindDialog();
206 handler = new AbstractHandler() {
207 @Override
208 public Object execute(ExecutionEvent event) throws ExecutionException {
209 fFindForKeyBinding.run();
210 return null;
211 }
212 };
213 activation = service.activateHandler(fFindForKeyBinding.getActionDefinitionId(), handler);
214 fFindForKeyBinding.setEnabled(false);
215 fHandlerActivations.add(activation);
216
217 fShowNodeStartForKeyBinding = new ShowNodeStart();
218 fShowNodeStartForKeyBinding.setText(Messages.SequenceDiagram_ShowNodeStart);
219
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$
222
223 handler = new AbstractHandler() {
224 @Override
225 public Object execute(ExecutionEvent event) throws ExecutionException {
226 fShowNodeStartForKeyBinding.run();
227 return null;
228 }
229 };
230 activation = service.activateHandler(fShowNodeStartForKeyBinding.getActionDefinitionId(), handler);
231 fHandlerActivations.add(activation);
232
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$
237
238 handler = new AbstractHandler() {
239 @Override
240 public Object execute(ExecutionEvent event) throws ExecutionException {
241 fShowNodeEndForKeyBinding.run();
242 return null;
243 }
244 };
245 activation = service.activateHandler(fShowNodeEndForKeyBinding.getActionDefinitionId(), handler);
246 fHandlerActivations.add(activation);
247
248 }
249
250 /*
251 * Disposes the KeyBindingsManager
252 */
253 private void dispose() {
254 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
255 if (window == null) {
256 //During Eclipse shutdown the active workbench window is null
257 return;
258 }
259 IHandlerService service = (IHandlerService) window.getService(IHandlerService.class);
260 for(IHandlerActivation activation : fHandlerActivations) {
261 service.deactivateHandler(activation);
262 }
263
264 fGoToMessageForKeyBinding = null;
265 fFindForKeyBinding = null;
266 fMoveUpForKeyBinding = null;
267 fMoveDownForKeyBinding = null;
268 fMoveLeftForKeyBinding = null;
269 fMoveRightForKeyBinding = null;
270 fShowNodeStartForKeyBinding = null;
271 fShowNodeEndForKeyBinding = null;
272 }
273
274 /**
275 * Set the view in all supported actions
276 *
277 * @param view to set in global actions
278 */
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);
289 }
290 }
291
292 /**
293 * Enable / disable find action
294 *
295 * @param enabled <code>true</code> for enabling else <code>false</code>
296 */
297 public void setFindEnabled(boolean enabled) {
298 if (fFindForKeyBinding != null) {
299 fFindForKeyBinding.setEnabled(enabled);
300 }
301 }
302 }
This page took 0.044686 seconds and 5 git commands to generate.