package xvrengine.launching;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.ILaunchShortcut;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ide.ResourceUtil;
import xvrengine.debug.IXVRConstants;
public class XVRLaunchShortcut implements ILaunchShortcut {
private ILaunchConfiguration createConfiguration(IResource resource){
ILaunchConfiguration config = null;
ILaunchConfigurationWorkingCopy wc = null;
try {
ILaunchConfigurationType configType = getConfigurationType();
wc = configType.newInstance(null, DebugPlugin.getDefault().getLaunchManager().generateLaunchConfigurationName(resource.getProject().getName() + " Configuration"));
//wc.setAttribute(IXVRConstants.ATTR_XVR_COMPILER, "C:\\VRMedia\\s3dc.exe");
wc.setAttribute(IXVRConstants.ATTR_XVR_VM, "C:\\VRMedia\\XVRGlut.exe");
//wc.setAttribute(IXVRConstants.ATTR_XVR_HEADERS, "C:\\VRMedia\\Include\n");
wc.setAttribute(IXVRConstants.ATTR_XVR_BROWSER, "C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe");
XVRMainTab.fillConfigurationFromPath(wc, resource.getFullPath().toString());
config = wc.doSave();
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return config;
}
private ILaunchConfigurationType getConfigurationType() {
return DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(IXVRConstants.ID_XVR_LAUNCH_TYPE);
}
private ILaunchConfiguration findConfiguration(IProject project){
try {
ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations();
for(int i = 0; i < configs.length; i++){
if(configs[i].getType().getIdentifier().equals("xvrengine.launching.XVR") &&
configs[i].getAttribute(IXVRConstants.ATTR_XVR_PROJECT, "").equals(project.getName())){
return configs[i];
}
}
} catch (CoreException e) {
}
return null;
}
@Override
public void launch(ISelection selection, String mode) {
if(selection instanceof IStructuredSelection){
IStructuredSelection structuredSelection = (IStructuredSelection)selection;
for(Object obj : structuredSelection.toArray()){
if(obj instanceof IResource){
IResource resource = (IResource)obj;
//if(!resource.getFileExtension().equals("s3d")) continue;
try {
//ILaunchConfiguration configuration = findConfiguration(resource.getFullPath().toString());
ILaunchConfiguration configuration = findConfiguration(resource.getProject());
if(configuration == null) configuration = createConfiguration(resource);
configuration.launch(mode, null);
} catch (CoreException e) {
}
break;
}
}
}
}
@Override
public void launch(IEditorPart editor, String mode) {
IEditorInput input = editor.getEditorInput();
IResource resource = ResourceUtil.getResource(input);
if(resource.getFileExtension().equals("s3d")){
ILaunchConfiguration config = findConfiguration(resource.getProject());
try {
if(config == null) config = createConfiguration(resource);
config.launch(mode, null);
} catch (CoreException e) {
}
}
}
}