Commit | Line | Data |
---|---|---|
73005152 | 1 | /********************************************************************** |
60ae41e1 | 2 | * Copyright (c) 2011, 2013 Ericsson |
526d9026 | 3 | * |
73005152 BH |
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 | |
526d9026 BH |
8 | * |
9 | * Contributors: | |
73005152 BH |
10 | * Bernd Hufmann - Initial API and implementation |
11 | **********************************************************************/ | |
12 | package org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers; | |
13 | ||
eb63f5ff | 14 | import java.util.ArrayList; |
73005152 BH |
15 | import java.util.HashSet; |
16 | import java.util.List; | |
17 | import java.util.Set; | |
73005152 BH |
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; | |
92330441 | 23 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.Messages; |
526d9026 | 24 | import org.eclipse.ui.IWorkbenchWindow; |
73005152 BH |
25 | import org.eclipse.ui.PlatformUI; |
26 | import org.eclipse.ui.handlers.IHandlerActivation; | |
27 | import org.eclipse.ui.handlers.IHandlerService; | |
28 | ||
29 | /** | |
73005152 | 30 | * <p> |
526d9026 | 31 | * Singleton class that manages key-bindings for certain commands across multiple sequence |
73005152 BH |
32 | * diagram view instances. |
33 | * </p> | |
526d9026 | 34 | * |
df0b8ff4 BH |
35 | * @version 1.0 |
36 | * @author Bernd Hufmann | |
526d9026 | 37 | * |
73005152 BH |
38 | */ |
39 | public class KeyBindingsManager { | |
40 | ||
41 | // ------------------------------------------------------------------------ | |
42 | // Attributes | |
43 | // ------------------------------------------------------------------------ | |
526d9026 | 44 | |
df0b8ff4 | 45 | /** |
526d9026 | 46 | * The singleton instance. |
df0b8ff4 | 47 | */ |
eb63f5ff | 48 | private static KeyBindingsManager fInstance = null; |
df0b8ff4 | 49 | /** |
526d9026 | 50 | * The list of view names. |
df0b8ff4 | 51 | */ |
507b1336 | 52 | private Set<String> fViews = new HashSet<>(); |
df0b8ff4 BH |
53 | /** |
54 | * The list of activations Activations to store | |
55 | */ | |
507b1336 | 56 | private List<IHandlerActivation> fHandlerActivations = new ArrayList<>(); |
df0b8ff4 | 57 | /** |
526d9026 | 58 | * The action reference for moving to a message in view. |
df0b8ff4 | 59 | */ |
73005152 | 60 | private MoveToMessage fGoToMessageForKeyBinding; |
df0b8ff4 | 61 | /** |
526d9026 | 62 | * The action reference for opening the find dialog. |
df0b8ff4 | 63 | */ |
73005152 | 64 | private OpenSDFindDialog fFindForKeyBinding; |
df0b8ff4 | 65 | /** |
526d9026 | 66 | * The action reference for moving up in view. |
df0b8ff4 | 67 | */ |
73005152 | 68 | private MoveSDUp fMoveUpForKeyBinding; |
df0b8ff4 | 69 | /** |
526d9026 | 70 | * The action reference for moving down in view. |
df0b8ff4 | 71 | */ |
73005152 | 72 | private MoveSDDown fMoveDownForKeyBinding; |
df0b8ff4 | 73 | /** |
526d9026 | 74 | * The action reference for moving left in view. |
df0b8ff4 | 75 | */ |
73005152 | 76 | private MoveSDLeft fMoveLeftForKeyBinding; |
df0b8ff4 | 77 | /** |
526d9026 | 78 | * The action reference for moving right in view. |
df0b8ff4 | 79 | */ |
73005152 | 80 | private MoveSDRight fMoveRightForKeyBinding; |
df0b8ff4 | 81 | /** |
526d9026 | 82 | * The action reference for showing node start. |
df0b8ff4 | 83 | */ |
73005152 | 84 | private ShowNodeStart fShowNodeStartForKeyBinding; |
df0b8ff4 BH |
85 | /** |
86 | * The action reference for showing node end. | |
87 | */ | |
73005152 BH |
88 | private ShowNodeEnd fShowNodeEndForKeyBinding; |
89 | ||
90 | // ------------------------------------------------------------------------ | |
91 | // Constructor | |
92 | // ------------------------------------------------------------------------ | |
526d9026 | 93 | |
df0b8ff4 | 94 | /** |
526d9026 | 95 | * Constructor |
df0b8ff4 | 96 | */ |
eb63f5ff | 97 | protected KeyBindingsManager() { |
73005152 BH |
98 | } |
99 | ||
100 | // ------------------------------------------------------------------------ | |
df0b8ff4 | 101 | // Methods |
73005152 | 102 | // ------------------------------------------------------------------------ |
df0b8ff4 BH |
103 | /** |
104 | * Returns the KeyBindingsManager singleton instance. | |
526d9026 | 105 | * |
df0b8ff4 BH |
106 | * @return the KeyBindingsManager singleton instance |
107 | */ | |
f26e290b | 108 | public static synchronized KeyBindingsManager getInstance() { |
eb63f5ff BH |
109 | if (fInstance == null) { |
110 | fInstance = new KeyBindingsManager(); | |
111 | } | |
73005152 BH |
112 | return fInstance; |
113 | } | |
526d9026 | 114 | |
73005152 | 115 | /** |
526d9026 BH |
116 | * Adds a view list of managed view list. |
117 | * | |
73005152 BH |
118 | * @param viewId Id of SD view to add and to manage |
119 | */ | |
120 | public void add(String viewId) { | |
526d9026 | 121 | |
eb63f5ff | 122 | if (fViews.isEmpty()) { |
73005152 BH |
123 | initialize(); |
124 | } | |
526d9026 | 125 | |
73005152 BH |
126 | if(!fViews.contains(viewId)) { |
127 | fViews.add(viewId); | |
128 | } | |
129 | } | |
526d9026 | 130 | |
73005152 | 131 | /** |
df0b8ff4 | 132 | * Removes a view from managed view list |
526d9026 | 133 | * |
73005152 BH |
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 | } | |
eb63f5ff | 140 | if (fViews.isEmpty()) { |
73005152 BH |
141 | dispose(); |
142 | } | |
143 | } | |
526d9026 | 144 | |
df0b8ff4 BH |
145 | /* |
146 | * Initialized the KeyBindingsManager. | |
147 | */ | |
73005152 BH |
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(); | |
92330441 | 218 | fShowNodeStartForKeyBinding.setText(Messages.SequenceDiagram_ShowNodeStart); |
73005152 BH |
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(); | |
92330441 | 234 | fShowNodeEndForKeyBinding.setText(Messages.SequenceDiagram_ShowNodeEnd); |
73005152 BH |
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 | } | |
526d9026 | 249 | |
df0b8ff4 BH |
250 | /* |
251 | * Disposes the KeyBindingsManager | |
252 | */ | |
73005152 | 253 | private void dispose() { |
526d9026 BH |
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); | |
73005152 BH |
260 | for(IHandlerActivation activation : fHandlerActivations) { |
261 | service.deactivateHandler(activation); | |
262 | } | |
526d9026 | 263 | |
73005152 BH |
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 | /** | |
526d9026 BH |
275 | * Set the view in all supported actions |
276 | * | |
73005152 BH |
277 | * @param view to set in global actions |
278 | */ | |
279 | public void setSdView(SDView view) { | |
eb63f5ff | 280 | if (!fViews.isEmpty()) { |
73005152 BH |
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 | |
526d9026 BH |
294 | * |
295 | * @param enabled <code>true</code> for enabling else <code>false</code> | |
73005152 BH |
296 | */ |
297 | public void setFindEnabled(boolean enabled) { | |
298 | if (fFindForKeyBinding != null) { | |
299 | fFindForKeyBinding.setEnabled(enabled); | |
300 | } | |
301 | } | |
302 | } |