package qq.builder; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.debug.core.DebugPlugin; import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.ILaunchConfigurationType; import org.eclipse.debug.ui.DebugUITools; import org.eclipse.debug.ui.IDebugModelPresentation; import org.eclipse.debug.ui.ILaunchShortcut2; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.internal.core.CompilationUnit; import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; import org.eclipse.jdt.internal.debug.ui.launcher.LauncherMessages; import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; import org.eclipse.jface.operation.IRunnableContext; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.dialogs.ElementListSelectionDialog; public abstract class JavaServerLaunchShortcut implements ILaunchShortcut2 { private String javaProject = ""; public String getJavaProject() { return javaProject; } public void setJavaProject(String javaProject) { this.javaProject = javaProject; } protected abstract ILaunchConfigurationType getConfigurationType(); protected abstract ILaunchConfiguration createConfiguration(IType paramIType); protected abstract IType[] findTypes(Object[] paramArrayOfObject, IRunnableContext paramIRunnableContext) throws InterruptedException, CoreException; protected abstract String getTypeSelectionTitle(); protected abstract String getEditorEmptyMessage(); protected abstract String getSelectionEmptyMessage(); private void searchAndLaunch(Object[] scope, String mode, String selectTitle, String emptyMessage) { // IType[] types = (IType[]) null; // try { // types = findTypes(scope, PlatformUI.getWorkbench() // .getProgressService()); // } catch (InterruptedException localInterruptedException) { // return; // } catch (CoreException e) { // MessageDialog.openError(getShell(), // LauncherMessages.JavaLaunchShortcut_0, e.getMessage()); // return; // } IType type = null; // if (types.length == 0) { // MessageDialog.openError(getShell(), // LauncherMessages.JavaLaunchShortcut_1, emptyMessage); // } else if (types.length > 1) { // type = chooseType(types, selectTitle); // } else { // type = types[0]; // } // if (type != null) if (scope[0] instanceof CompilationUnit) { CompilationUnit c = (CompilationUnit) scope[0]; setJavaProject(((IJavaProject) c.getAncestor(2)).getElementName()); } else { setJavaProject(((IJavaProject) scope[0]).getElementName()); } launch(type, mode, scope); } protected IType chooseType(IType[] types, String title) { for (IType type : types) { if (type.isBinary() && type.getElementName().equals("ServerManageStart")) { return type; } } // DebugTypeSelectionDialog mmsd = new // DebugTypeSelectionDialog(JDIDebugUIPlugin.getShell(), types, title); // if (mmsd.open() == 0) { // return ((IType)mmsd.getResult()[0]); // } return null; } protected void launch(IType type, String mode, Object[] scope) { ILaunchConfiguration config = findLaunchConfiguration(type, getConfigurationType()); if (config == null) { config = createConfiguration(type); } if (config != null) DebugUITools.launch(config, mode); } protected ILaunchConfiguration findLaunchConfiguration(IType type, ILaunchConfigurationType configType) { List candidateConfigs = Collections.EMPTY_LIST; try { ILaunchConfiguration[] configs = DebugPlugin.getDefault() .getLaunchManager().getLaunchConfigurations(configType); candidateConfigs = new ArrayList(configs.length); for (int i = 0; i < configs.length; ++i) { ILaunchConfiguration config = configs[i]; if ((!(config.getAttribute( IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "") .equals("com.vci.rmip.framework.server.servermanagement.ServerManageStart"))) || (!(config .getAttribute( IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, "").equals(getJavaProject())))) continue; candidateConfigs.add(config); } } catch (CoreException e) { JDIDebugUIPlugin.log(e); } int candidateCount = candidateConfigs.size(); if (candidateCount == 1) return ((ILaunchConfiguration) candidateConfigs.get(0)); if (candidateCount > 1) { return chooseConfiguration(candidateConfigs); } return null; } protected ILaunchConfiguration chooseConfiguration(List configList) { IDebugModelPresentation labelProvider = DebugUITools .newDebugModelPresentation(); ElementListSelectionDialog dialog = new ElementListSelectionDialog( getShell(), labelProvider); dialog.setElements(configList.toArray()); dialog.setTitle(getTypeSelectionTitle()); dialog.setMessage(LauncherMessages.JavaLaunchShortcut_2); dialog.setMultipleSelection(false); int result = dialog.open(); labelProvider.dispose(); if (result == 0) { return ((ILaunchConfiguration) dialog.getFirstResult()); } return null; } protected Shell getShell() { return JDIDebugUIPlugin.getActiveWorkbenchShell(); } public void launch(IEditorPart editor, String mode) { IEditorInput input = editor.getEditorInput(); IJavaElement je = (IJavaElement) input.getAdapter(IJavaElement.class); if (je != null) searchAndLaunch(new Object[] { je }, mode, getTypeSelectionTitle(), getEditorEmptyMessage()); } public void launch(ISelection selection, String mode) { if (selection instanceof IStructuredSelection) searchAndLaunch(((IStructuredSelection) selection).toArray(), mode, getTypeSelectionTitle(), getSelectionEmptyMessage()); } public IResource getLaunchableResource(IEditorPart editorpart) { return getLaunchableResource(editorpart.getEditorInput()); } public IResource getLaunchableResource(ISelection selection) { if (selection instanceof IStructuredSelection) { IStructuredSelection ss = (IStructuredSelection) selection; if (ss.size() == 1) { Object element = ss.getFirstElement(); if (element instanceof IAdaptable) { return getLaunchableResource((IAdaptable) element); } } } return null; } private IResource getLaunchableResource(IAdaptable adaptable) { IJavaElement je = (IJavaElement) adaptable .getAdapter(IJavaElement.class); if (je != null) { return je.getResource(); } return null; } public ILaunchConfiguration[] getLaunchConfigurations(IEditorPart editorpart) { return null; } public ILaunchConfiguration[] getLaunchConfigurations(ISelection selection) { return null; } }