Package: VdmLaunchShortcut$1

VdmLaunchShortcut$1

nameinstructionbranchcomplexitylinemethod
run(IProgressMonitor)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
{...}
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.debug
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.debug.ui.launchconfigurations;
23:
24: import java.lang.reflect.InvocationTargetException;
25: import java.util.ArrayList;
26: import java.util.Collections;
27: import java.util.List;
28:
29: import org.eclipse.core.resources.IProject;
30: import org.eclipse.core.resources.IResource;
31: import org.eclipse.core.runtime.CoreException;
32: import org.eclipse.core.runtime.IAdaptable;
33: import org.eclipse.core.runtime.IProgressMonitor;
34: import org.eclipse.debug.core.DebugPlugin;
35: import org.eclipse.debug.core.ILaunchConfiguration;
36: import org.eclipse.debug.core.ILaunchConfigurationType;
37: import org.eclipse.debug.core.ILaunchManager;
38: import org.eclipse.debug.ui.DebugUITools;
39: import org.eclipse.debug.ui.IDebugModelPresentation;
40: import org.eclipse.debug.ui.ILaunchShortcut2;
41: import org.eclipse.jface.dialogs.MessageDialog;
42: import org.eclipse.jface.operation.IRunnableContext;
43: import org.eclipse.jface.operation.IRunnableWithProgress;
44: import org.eclipse.jface.viewers.ISelection;
45: import org.eclipse.jface.viewers.IStructuredSelection;
46: import org.eclipse.jface.window.Window;
47: import org.eclipse.swt.widgets.Shell;
48: import org.eclipse.ui.IEditorPart;
49: import org.eclipse.ui.PlatformUI;
50: import org.eclipse.ui.dialogs.ElementListSelectionDialog;
51: import org.eclipse.ui.progress.IProgressService;
52: import org.overture.ast.definitions.AExplicitFunctionDefinition;
53: import org.overture.ast.definitions.AExplicitOperationDefinition;
54: import org.overture.ast.node.INode;
55: import org.overture.ide.core.IVdmModel;
56: import org.overture.ide.core.resources.IVdmProject;
57: import org.overture.ide.debug.core.IDebugConstants;
58: import org.overture.ide.debug.core.VdmDebugPlugin;
59: import org.overture.ide.ui.utility.ast.AstNameUtil;
60: import org.overture.typechecker.assistant.ITypeCheckerAssistantFactory;
61: import org.overture.typechecker.assistant.TypeCheckerAssistantFactory;
62:
63: public abstract class VdmLaunchShortcut implements ILaunchShortcut2
64: {
65:         public final ITypeCheckerAssistantFactory assistantFactory = new TypeCheckerAssistantFactory();
66:
67:         /**
68:          * Returns the type of configuration this shortcut is applicable to.
69:          *
70:          * @return the type of configuration this shortcut is applicable to
71:          */
72:         protected abstract ILaunchConfigurationType getConfigurationType();
73:
74:         /**
75:          * Creates and returns a new configuration based on the specified type.
76:          *
77:          * @param type
78:          * type to create a launch configuration for
79:          * @return launch configuration configured to launch the specified type
80:          */
81:         protected abstract ILaunchConfiguration createConfiguration(INode type,
82:                         String projectName);
83:
84:         /**
85:          * Finds and returns the types in the given collection of elements that can be launched.
86:          *
87:          * @param elements
88:          * scope to search for types that can be launched
89:          * @param context
90:          * progress reporting context
91:          * @return collection of types that can be launched, possibly empty
92:          * @exception InterruptedException
93:          * if the search is canceled
94:          * @exception CoreException
95:          * if the search fails
96:          */
97:         // protected abstract IAstNode[] findTypes(Object[] elements,
98:         // IRunnableContext context) throws InterruptedException,
99:         // CoreException;
100:
101:         protected abstract INode[] filterTypes(Object[] elements,
102:                         IRunnableContext context);
103:
104:         /**
105:          * Returns a title for a type selection dialog used to prompt the user when there is more than one type that can be
106:          * launched.
107:          *
108:          * @return type selection dialog title
109:          */
110:         protected abstract String getTypeSelectionTitle();
111:
112:         /**
113:          * Returns an error message to use when the editor does not contain a type that can be launched.
114:          *
115:          * @return error message when editor cannot be launched
116:          */
117:         protected abstract String getEditorEmptyMessage();
118:
119:         /**
120:          * Returns an error message to use when the selection does not contain a type that can be launched.
121:          *
122:          * @return error message when selection cannot be launched
123:          */
124:         protected abstract String getSelectionEmptyMessage();
125:
126:         IProject project = null;
127:
128:         /**
129:          * Resolves a type that can be launched from the given scope and launches in the specified mode.
130:          *
131:          * @param scope
132:          * the java elements to consider for a type that can be launched
133:          * @param mode
134:          * launch mode
135:          * @param selectTitle
136:          * prompting title for choosing a type to launch
137:          * @param emptyMessage
138:          * error message when no types are resolved for launching
139:          */
140:         private void searchAndLaunch(Object[] scope, String mode,
141:                         String selectTitle, String emptyMessage)
142:         {
143:                 INode[] types = null;
144:
145:                 try
146:                 {
147:                         project = findProject(scope, PlatformUI.getWorkbench().getProgressService());
148:
149:                         ILaunchConfiguration config = findLaunchConfiguration(project.getName(), getConfigurationType());
150:                         if (config != null)
151:                         {
152:                                 // config already exists for the project.
153:                                 launch(config, mode);
154:                                 return;
155:                         }
156:
157:                         types = findTypes(scope, PlatformUI.getWorkbench().getProgressService());
158:
159:                 } catch (InterruptedException e)
160:                 {
161:                         return;
162:                 } catch (CoreException e)
163:                 {
164:                         MessageDialog.openError(getShell(), LauncherMessages.VdmLaunchShortcut_0, e.getMessage());
165:                         return;
166:                 }
167:                 INode type = null;
168:                 if (types == null || types.length == 0)
169:                 {
170:                         MessageDialog.openError(getShell(), LauncherMessages.VdmLaunchShortcut_1, emptyMessage);
171:                 } else if (types.length > 1)
172:                 {
173:                         type = chooseType(types, selectTitle);
174:                 } else
175:                 {
176:                         type = types[0];
177:                 }
178:                 if (type != null && project != null)
179:                 {
180:                         launch(type, mode, project.getName());
181:                 }
182:         }
183:
184:         /**
185:          * Prompts the user to select a type from the given types.
186:          *
187:          * @param types
188:          * the types to choose from
189:          * @param title
190:          * the selection dialog title
191:          * @return the selected type or <code>null</code> if none.
192:          */
193:         protected INode chooseType(INode[] types, String title)
194:         {
195:                 try
196:                 {
197:                         DebugTypeSelectionDialog mmsd = new DebugTypeSelectionDialog(VdmDebugPlugin.getActiveWorkbenchShell(), types, title, project);
198:                         if (mmsd.open() == Window.OK)
199:                         {
200:                                 return (INode) mmsd.getResult()[0];
201:                         }
202:                 } catch (Exception e)
203:                 {
204:                         e.printStackTrace();
205:                 }
206:
207:                 return null;
208:         }
209:
210:         private void launch(INode type, String mode, String projectName)
211:         {
212:                 ILaunchConfiguration config = findLaunchConfiguration(projectName, getConfigurationType());
213:                 if (config == null)
214:                 {
215:                         config = createConfiguration(type, projectName);
216:                 }
217:                 launch(config, mode);
218:
219:         }
220:
221:         private void launch(ILaunchConfiguration config, String mode)
222:         {
223:                 if (config != null)
224:                 {
225:                         DebugUITools.launch(config, mode);
226:                 }
227:         }
228:
229:         /**
230:          * Finds and returns an <b>existing</b> configuration to re-launch for the given type, or <code>null</code> if there
231:          * is no existing configuration.
232:          *
233:          * @return a configuration to use for launching the given type or <code>null</code> if none
234:          */
235:         protected ILaunchConfiguration findLaunchConfiguration(String projectName,
236:                         ILaunchConfigurationType configType)
237:         {
238:                 List<ILaunchConfiguration> candidateConfigs = Collections.emptyList();
239:                 try
240:                 {
241:                         ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(configType);
242:                         candidateConfigs = new ArrayList<ILaunchConfiguration>(configs.length);
243:                         for (int i = 0; i < configs.length; i++)
244:                         {
245:                                 ILaunchConfiguration config = configs[i];
246:
247:                                 // String defaultModule = config.getAttribute(IDebugConstants.VDM_LAUNCH_CONFIG_DEFAULT, "");
248:                                 String pName = config.getAttribute(IDebugConstants.VDM_LAUNCH_CONFIG_PROJECT, "");
249:                                 // String operation = config.getAttribute(IDebugConstants.VDM_LAUNCH_CONFIG_OPERATION, "");
250:
251:                                 if (// defaultModule.equals(getModuleName(type).toString())
252:                                         // &&
253:                                 pName.equalsIgnoreCase(projectName)
254:                                 // && operation.equals(getOperationName(type) + "()")
255:                                 )
256:                                 { //$NON-NLS-1$
257:                                         candidateConfigs.add(config);
258:                                 }
259:                         }
260:
261:                 } catch (CoreException e)
262:                 {
263:                         // JDIDebugUIPlugin.log(e);
264:                 }
265:                 int candidateCount = candidateConfigs.size();
266:                 if (candidateCount == 1)
267:                 {
268:                         return (ILaunchConfiguration) candidateConfigs.get(0);
269:                 } else if (candidateCount > 1)
270:                 {
271:                         return chooseConfiguration(candidateConfigs);
272:                         // return candidateConfigs.get(0);
273:                 }
274:                 return null;
275:         }
276:
277:         protected String getModuleName(INode node)
278:         {
279:                 if (node instanceof AExplicitFunctionDefinition)
280:                 {
281:                         return ((AExplicitFunctionDefinition) node).getLocation().getModule();
282:                 }
283:                 if (node instanceof AExplicitOperationDefinition)
284:                 {
285:                         return ((AExplicitOperationDefinition) node).getLocation().getModule();
286:                 }
287:
288:                 return "";
289:         }
290:
291:         protected String getModuleNameLaunch(INode node)
292:         {
293:                 String name = "";
294:                 if (node instanceof AExplicitFunctionDefinition)
295:                 {
296:                         name = ((AExplicitFunctionDefinition) node).getLocation().getModule();
297:                         if (!assistantFactory.createPAccessSpecifierAssistant().isStatic(((AExplicitFunctionDefinition) node).getAccess()))
298:                         {
299:                                 name += "()";
300:                         }
301:
302:                 }
303:                 if (node instanceof AExplicitOperationDefinition)
304:                 {
305:                         name = ((AExplicitOperationDefinition) node).getLocation().getModule();
306:                         if (!assistantFactory.createPAccessSpecifierAssistant().isStatic(((AExplicitOperationDefinition) node).getAccess()))
307:                         {
308:                                 name += "()";
309:                         }
310:                 }
311:
312:                 return name;
313:         }
314:
315:         protected String getOperationName(INode node)
316:         {
317:                 return AstNameUtil.getName(node);
318:         }
319:
320:         protected boolean isStaticAccessRequired(INode node)
321:         {
322:                 if (node instanceof AExplicitFunctionDefinition)
323:                 {
324:                         return assistantFactory.createPAccessSpecifierAssistant().isStatic(((AExplicitFunctionDefinition) node).getAccess());
325:                 }
326:                 if (node instanceof AExplicitOperationDefinition)
327:                 {
328:                         return assistantFactory.createPAccessSpecifierAssistant().isStatic(((AExplicitOperationDefinition) node).getAccess());
329:                 }
330:
331:                 return true;
332:         }
333:
334:         /**
335:          * Returns a configuration from the given collection of configurations that should be launched, or <code>null</code>
336:          * to cancel. Default implementation opens a selection dialog that allows the user to choose one of the specified
337:          * launch configurations. Returns the chosen configuration, or <code>null</code> if the user cancels.
338:          *
339:          * @param configList
340:          * list of configurations to choose from
341:          * @return configuration to launch or <code>null</code> to cancel
342:          */
343:         protected ILaunchConfiguration chooseConfiguration(
344:                         List<ILaunchConfiguration> configList)
345:         {
346:                 IDebugModelPresentation labelProvider = DebugUITools.newDebugModelPresentation();
347:                 ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(), labelProvider);
348:                 dialog.setElements(configList.toArray());
349:                 dialog.setTitle(getTypeSelectionTitle());
350:                 dialog.setMessage(LauncherMessages.VdmLaunchShortcut_2);
351:                 dialog.setMultipleSelection(false);
352:                 int result = dialog.open();
353:                 labelProvider.dispose();
354:                 if (result == Window.OK)
355:                 {
356:                         return (ILaunchConfiguration) dialog.getFirstResult();
357:                 }
358:                 return null;
359:         }
360:
361:         /**
362:          * Convenience method to return the active workbench window shell.
363:          *
364:          * @return active workbench window shell
365:          */
366:         protected Shell getShell()
367:         {
368:                 return VdmDebugPlugin.getActiveWorkbenchShell();
369:         }
370:
371:         /*
372:          * (non-Javadoc)
373:          * @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.ui.IEditorPart, java.lang.String)
374:          */
375:         public void launch(IEditorPart editor, String mode)
376:         {
377:                 // IEditorInput input = editor.getEditorInput();
378:                 // IJavaElement je = (IJavaElement) input.getAdapter(IJavaElement.class);
379:                 // TODO get ISourceUnit from editor
380:                 // IVdmSourceUnit je = null;
381:                 // if (je != null)
382:                 // {
383:                 // searchAndLaunch(new Object[] { je }, mode, getTypeSelectionTitle(), getEditorEmptyMessage());
384:                 // }
385:         }
386:
387:         public void launch(ISelection selection, String mode)
388:         {
389:                 if (selection instanceof IStructuredSelection)
390:                 {
391:                         searchAndLaunch(((IStructuredSelection) selection).toArray(), mode, getTypeSelectionTitle(), getSelectionEmptyMessage());
392:                 }
393:         }
394:
395:         /*
396:          * (non-Javadoc)
397:          * @see org.eclipse.debug.ui.ILaunchShortcut2#getLaunchableResource(org.eclipse.ui.IEditorPart)
398:          */
399:         public IResource getLaunchableResource(IEditorPart editorpart)
400:         {
401:                 return getLaunchableResource(editorpart.getEditorInput());
402:         }
403:
404:         /*
405:          * (non-Javadoc)
406:          * @see org.eclipse.debug.ui.ILaunchShortcut2#getLaunchableResource(org.eclipse.jface.viewers.ISelection)
407:          */
408:         public IResource getLaunchableResource(ISelection selection)
409:         {
410:                 if (selection instanceof IStructuredSelection)
411:                 {
412:                         IStructuredSelection ss = (IStructuredSelection) selection;
413:                         if (ss.size() == 1)
414:                         {
415:                                 Object element = ss.getFirstElement();
416:                                 if (element instanceof IAdaptable)
417:                                 {
418:                                         return getLaunchableResource((IAdaptable) element);
419:                                 }
420:                         }
421:                 }
422:                 return null;
423:         }
424:
425:         /**
426:          * Returns the resource containing the Java element associated with the given adaptable, or <code>null</code>.
427:          *
428:          * @param adaptable
429:          * adaptable object
430:          * @return containing resource or <code>null</code>
431:          */
432:         private IResource getLaunchableResource(IAdaptable adaptable)
433:         {
434:                 // IJavaElement je = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
435:                 // if (je != null) {
436:                 // return je.getResource();
437:                 // }
438:                 // TODO
439:                 return null;
440:         }
441:
442:         /*
443:          * (non-Javadoc)
444:          * @see org.eclipse.debug.ui.ILaunchShortcut2#getLaunchConfigurations(org.eclipse.ui.IEditorPart)
445:          */
446:         public ILaunchConfiguration[] getLaunchConfigurations(IEditorPart editorpart)
447:         {
448:                 // let the framework resolve configurations based on resource mapping
449:                 return null;
450:         }
451:
452:         /*
453:          * (non-Javadoc)
454:          * @see org.eclipse.debug.ui.ILaunchShortcut2#getLaunchConfigurations(org.eclipse.jface.viewers.ISelection)
455:          */
456:         public ILaunchConfiguration[] getLaunchConfigurations(ISelection selection)
457:         {
458:                 // let the framework resolve configurations based on resource mapping
459:                 return null;
460:         }
461:
462:         protected IProject findProject(Object[] scope,
463:                         IProgressService progressService) throws InterruptedException,
464:                         CoreException
465:         {
466:                 for (Object object : scope)
467:                 {
468:                         if (object instanceof IProject)
469:                         {
470:                                 IProject project = (IProject) object;
471:                                 return project;
472:
473:                         }
474:                 }
475:                 return null;
476:         }
477:
478:         protected ILaunchManager getLaunchManager()
479:         {
480:                 return DebugPlugin.getDefault().getLaunchManager();
481:         }
482:
483:         protected INode[] findTypes(Object[] elements, IRunnableContext context)
484:                         throws InterruptedException, CoreException
485:         {
486:                 for (Object object : elements)
487:                 {
488:                         if (object instanceof IAdaptable)
489:                         {
490:                                 IVdmProject vdmProject = (IVdmProject) ((IAdaptable) object).getAdapter(IVdmProject.class);
491:                                 if (vdmProject != null)
492:                                 {
493:                                         final IVdmModel model = vdmProject.getModel();
494:                                         try
495:                                         {
496:                                                 context.run(false, false, new IRunnableWithProgress()
497:                                                 {
498:
499:                                                         public void run(IProgressMonitor monitor)
500:                                                                         throws InvocationTargetException,
501:                                                                         InterruptedException
502:                                                         {
503:                                                                 model.refresh(false, monitor);
504:
505:                                                         }
506:                                                 });
507:                                         } catch (InvocationTargetException e)
508:                                         {
509:                                                 if (VdmDebugPlugin.DEBUG)
510:                                                 {
511:                                                         e.printStackTrace();
512:                                                 }
513:                                         }
514:
515:                                         return filterTypes(model.getRootElementList().toArray(), context);
516:                                 }
517:                         }
518:                 }
519:                 return null;
520:         }
521:
522: }