]> git.argeo.org Git - gpl/argeo-slc.git/blob - OsgiBootLaunchShortcut.java
097adc836067f34a3b86e1cf8517a259e9410dbd
[gpl/argeo-slc.git] / OsgiBootLaunchShortcut.java
1 package org.argeo.slc.ide.ui.launch.osgi;
2
3 import java.util.Iterator;
4
5 import org.eclipse.core.resources.IFile;
6 import org.eclipse.core.resources.IFolder;
7 import org.eclipse.core.resources.IProject;
8 import org.eclipse.core.resources.IResource;
9 import org.eclipse.core.runtime.CoreException;
10 import org.eclipse.core.runtime.IPath;
11 import org.eclipse.debug.core.DebugPlugin;
12 import org.eclipse.debug.core.ILaunchConfiguration;
13 import org.eclipse.debug.core.ILaunchConfigurationType;
14 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
15 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
16 import org.eclipse.jface.dialogs.ErrorDialog;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
20 import org.eclipse.pde.ui.launcher.OSGiLaunchShortcut;
21 import org.eclipse.swt.widgets.Display;
22 import org.eclipse.swt.widgets.Shell;
23
24 public class OsgiBootLaunchShortcut extends OSGiLaunchShortcut implements
25 OsgiLauncherConstants {
26 private StringBuffer name = null;
27 private IFile propertiesFile = null;
28
29 @Override
30 protected String getLaunchConfigurationTypeName() {
31 return OsgiBootEquinoxLaunchConfiguration.ID;
32 }
33
34 @Override
35 public void launch(ISelection selection, String mode) {
36 // System.out.println("Launch shortcut... " + selection);
37
38 // we assume that:
39 // - only one
40 // - file
41 // - is selected
42 IStructuredSelection sSelection = (IStructuredSelection) selection;
43 Iterator<?> it = sSelection.iterator();
44 propertiesFile = (IFile) it.next();
45
46 name = new StringBuffer(extractName(propertiesFile));
47
48 super.launch(selection, mode);
49 }
50
51 @Override
52 protected void initializeConfiguration(
53 ILaunchConfigurationWorkingCopy configuration) {
54 IResource[] resources = { propertiesFile };
55 configuration.setMappedResources(resources);
56 super.initializeConfiguration(configuration);
57
58 try {
59 configuration.setAttribute(ATTR_ADD_JVM_PATHS, false);
60 configuration.setAttribute(ATTR_ADDITIONAL_VM_ARGS, "-Xmx128m");
61 configuration
62 .setAttribute(ATTR_ADDITIONAL_PROGRAM_ARGS, "-console");
63
64 // Defaults
65 String originalVmArgs = configuration.getAttribute(
66 IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, "");
67 configuration.setAttribute(ATTR_DEFAULT_VM_ARGS, originalVmArgs);
68 configuration.setAttribute(IPDELauncherConstants.CONFIG_CLEAR_AREA,
69 true);
70
71 configuration.setAttribute(
72 IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
73 findWorkingDirectory());
74
75 OsgiLaunchHelper.updateLaunchConfiguration(configuration);
76 } catch (CoreException e) {
77 Shell shell = Display.getCurrent().getActiveShell();
78 ErrorDialog.openError(shell, "Error",
79 "Cannot execute initalize configuration", e.getStatus());
80 }
81 }
82
83 protected String findWorkingDirectory() {
84 try {
85 IProject project = propertiesFile.getProject();
86 IPath parent = propertiesFile.getProjectRelativePath()
87 .removeLastSegments(1);
88 IFolder execFolder = project.getFolder(parent.append("exec"));
89 if (!execFolder.exists())
90 execFolder.create(true, true, null);
91 IFolder launchFolder = project.getFolder(execFolder
92 .getProjectRelativePath().append(
93 extractName(propertiesFile)));
94 if (!launchFolder.exists())
95 launchFolder.create(true, true, null);
96 return "${workspace_loc:"
97 + launchFolder.getFullPath().toString().substring(1) + "}";
98 } catch (Exception e) {
99 e.printStackTrace();
100 throw new RuntimeException("Cannot create working directory", e);
101 }
102 }
103
104 protected String extractName(IFile file) {
105 IPath path = propertiesFile.getFullPath();
106 IPath pathNoExt = path.removeFileExtension();
107 return pathNoExt.segment(pathNoExt.segmentCount() - 1);
108
109 }
110
111 protected String getName(ILaunchConfigurationType type) {
112 if (name != null && !name.toString().trim().equals(""))
113 return DebugPlugin.getDefault().getLaunchManager()
114 .generateUniqueLaunchConfigurationNameFrom(name.toString());
115 else
116 return DebugPlugin.getDefault().getLaunchManager()
117 .generateUniqueLaunchConfigurationNameFrom("SLC");
118 }
119
120 @Override
121 protected boolean isGoodMatch(ILaunchConfiguration configuration) {
122 if (name != null) {
123 return name.toString().equals(configuration.getName());
124 }
125 return super.isGoodMatch(configuration);
126 }
127
128 }