1 11 package org.eclipse.jdt.debug.ui.launchConfigurations; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.resources.IProject; 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.resources.IWorkspace; 18 import org.eclipse.core.resources.ResourcesPlugin; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.debug.core.ILaunchConfiguration; 22 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 23 import org.eclipse.jdt.core.IJavaElement; 24 import org.eclipse.jdt.core.IJavaModel; 25 import org.eclipse.jdt.core.IJavaProject; 26 import org.eclipse.jdt.core.IType; 27 import org.eclipse.jdt.core.JavaCore; 28 import org.eclipse.jdt.core.JavaModelException; 29 import org.eclipse.jdt.core.search.IJavaSearchScope; 30 import org.eclipse.jdt.core.search.SearchEngine; 31 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants; 32 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds; 33 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 34 import org.eclipse.jdt.internal.debug.ui.SWTFactory; 35 import org.eclipse.jdt.internal.debug.ui.launcher.DebugTypeSelectionDialog; 36 import org.eclipse.jdt.internal.debug.ui.launcher.LauncherMessages; 37 import org.eclipse.jdt.internal.debug.ui.launcher.MainMethodSearchEngine; 38 import org.eclipse.jdt.internal.debug.ui.launcher.SharedJavaMainTab; 39 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 40 import org.eclipse.jdt.ui.ISharedImages; 41 import org.eclipse.jdt.ui.JavaUI; 42 import org.eclipse.jface.window.Window; 43 import org.eclipse.swt.graphics.Image; 44 import org.eclipse.swt.layout.GridData; 45 import org.eclipse.swt.layout.GridLayout; 46 import org.eclipse.swt.widgets.Button; 47 import org.eclipse.swt.widgets.Composite; 48 import org.eclipse.ui.PlatformUI; 49 50 import com.ibm.icu.text.MessageFormat; 51 52 60 61 public class JavaMainTab extends SharedJavaMainTab { 62 63 70 public static final String ATTR_INCLUDE_EXTERNAL_JARS = IJavaDebugUIConstants.PLUGIN_ID + ".INCLUDE_EXTERNAL_JARS"; 78 public static final String ATTR_CONSIDER_INHERITED_MAIN = IJavaDebugUIConstants.PLUGIN_ID + ".CONSIDER_INHERITED_MAIN"; 80 private Button fSearchExternalJarsCheckButton; 82 private Button fConsiderInheritedMainButton; 83 private Button fStopInMainCheckButton; 84 85 88 public void createControl(Composite parent) { 89 Composite comp = SWTFactory.createComposite(parent, parent.getFont(), 1, 1, GridData.FILL_BOTH); 90 ((GridLayout)comp.getLayout()).verticalSpacing = 0; 91 createProjectEditor(comp); 92 createVerticalSpacer(comp, 1); 93 createMainTypeEditor(comp, LauncherMessages.JavaMainTab_Main_cla_ss__4); 94 setControl(comp); 95 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_MAIN_TAB); 96 } 97 98 101 protected void createMainTypeExtensions(Composite parent) { 102 fSearchExternalJarsCheckButton = SWTFactory.createCheckButton(parent, LauncherMessages.JavaMainTab_E_xt__jars_6, null, false, 2); 103 fSearchExternalJarsCheckButton.addSelectionListener(getDefaultListener()); 104 105 fConsiderInheritedMainButton = SWTFactory.createCheckButton(parent, LauncherMessages.JavaMainTab_22, null, false, 2); 106 fConsiderInheritedMainButton.addSelectionListener(getDefaultListener()); 107 108 fStopInMainCheckButton = SWTFactory.createCheckButton(parent, LauncherMessages.JavaMainTab_St_op_in_main_1, null, false, 1); 109 fStopInMainCheckButton.addSelectionListener(getDefaultListener()); 110 } 111 112 115 public Image getImage() { 116 return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_CLASS); 117 } 118 119 122 public String getName() { 123 return LauncherMessages.JavaMainTab__Main_19; 124 } 125 126 131 public String getId() { 132 return "org.eclipse.jdt.debug.ui.javaMainTab"; } 134 135 138 protected void handleSearchButtonSelected() { 139 IJavaProject project = getJavaProject(); 140 IJavaElement[] elements = null; 141 if ((project == null) || !project.exists()) { 142 IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); 143 if (model != null) { 144 try { 145 elements = model.getJavaProjects(); 146 } 147 catch (JavaModelException e) {JDIDebugUIPlugin.log(e);} 148 } 149 } 150 else { 151 elements = new IJavaElement[]{project}; 152 } 153 if (elements == null) { 154 elements = new IJavaElement[]{}; 155 } 156 int constraints = IJavaSearchScope.SOURCES; 157 constraints |= IJavaSearchScope.APPLICATION_LIBRARIES; 158 if (fSearchExternalJarsCheckButton.getSelection()) { 159 constraints |= IJavaSearchScope.SYSTEM_LIBRARIES; 160 } 161 IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(elements, constraints); 162 MainMethodSearchEngine engine = new MainMethodSearchEngine(); 163 IType[] types = null; 164 try { 165 types = engine.searchMainMethods(getLaunchConfigurationDialog(), searchScope, fConsiderInheritedMainButton.getSelection()); 166 } 167 catch (InvocationTargetException e) { 168 setErrorMessage(e.getMessage()); 169 return; 170 } 171 catch (InterruptedException e) { 172 setErrorMessage(e.getMessage()); 173 return; 174 } 175 DebugTypeSelectionDialog mmsd = new DebugTypeSelectionDialog(getShell(), types, LauncherMessages.JavaMainTab_Choose_Main_Type_11); 176 if (mmsd.open() == Window.CANCEL) { 177 return; 178 } 179 Object [] results = mmsd.getResult(); 180 IType type = (IType)results[0]; 181 if (type != null) { 182 fMainText.setText(type.getFullyQualifiedName()); 183 fProjText.setText(type.getJavaProject().getElementName()); 184 } 185 } 186 187 190 public void initializeFrom(ILaunchConfiguration config) { 191 super.initializeFrom(config); 192 updateMainTypeFromConfig(config); 193 updateStopInMainFromConfig(config); 194 updateInheritedMainsFromConfig(config); 195 updateExternalJars(config); 196 } 197 198 201 public boolean isValid(ILaunchConfiguration config) { 202 setErrorMessage(null); 203 setMessage(null); 204 String name = fProjText.getText().trim(); 205 if (name.length() > 0) { 206 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 207 IStatus status = workspace.validateName(name, IResource.PROJECT); 208 if (status.isOK()) { 209 IProject project= ResourcesPlugin.getWorkspace().getRoot().getProject(name); 210 if (!project.exists()) { 211 setErrorMessage(MessageFormat.format(LauncherMessages.JavaMainTab_20, new String [] {name})); 212 return false; 213 } 214 if (!project.isOpen()) { 215 setErrorMessage(MessageFormat.format(LauncherMessages.JavaMainTab_21, new String [] {name})); 216 return false; 217 } 218 } 219 else { 220 setErrorMessage(MessageFormat.format(LauncherMessages.JavaMainTab_19, new String []{status.getMessage()})); 221 return false; 222 } 223 } 224 name = fMainText.getText().trim(); 225 if (name.length() == 0) { 226 setErrorMessage(LauncherMessages.JavaMainTab_Main_type_not_specified_16); 227 return false; 228 } 229 return true; 230 } 231 232 235 public void performApply(ILaunchConfigurationWorkingCopy config) { 236 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, fProjText.getText().trim()); 237 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, fMainText.getText().trim()); 238 mapResources(config); 239 240 if (fStopInMainCheckButton.getSelection()) { 242 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_STOP_IN_MAIN, true); 243 } 244 else { 245 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_STOP_IN_MAIN, (String )null); 246 } 247 248 if (fSearchExternalJarsCheckButton.getSelection()) { 250 config.setAttribute(ATTR_INCLUDE_EXTERNAL_JARS, true); 251 } 252 else { 253 config.setAttribute(ATTR_INCLUDE_EXTERNAL_JARS, (String )null); 254 } 255 256 if (fConsiderInheritedMainButton.getSelection()) { 258 config.setAttribute(ATTR_CONSIDER_INHERITED_MAIN, true); 259 } 260 else { 261 config.setAttribute(ATTR_CONSIDER_INHERITED_MAIN, (String )null); 262 } 263 } 264 265 268 public void setDefaults(ILaunchConfigurationWorkingCopy config) { 269 IJavaElement javaElement = getContext(); 270 if (javaElement != null) { 271 initializeJavaProject(javaElement, config); 272 } 273 else { 274 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, EMPTY_STRING); 275 } 276 initializeMainTypeAndName(javaElement, config); 277 } 278 279 283 private void updateExternalJars(ILaunchConfiguration config) { 284 boolean search = false; 285 try { 286 search = config.getAttribute(ATTR_INCLUDE_EXTERNAL_JARS, false); 287 } 288 catch (CoreException e) {JDIDebugUIPlugin.log(e);} 289 fSearchExternalJarsCheckButton.setSelection(search); 290 } 291 292 296 private void updateInheritedMainsFromConfig(ILaunchConfiguration config) { 297 boolean inherit = false; 298 try { 299 inherit = config.getAttribute(ATTR_CONSIDER_INHERITED_MAIN, false); 300 } 301 catch (CoreException e) {JDIDebugUIPlugin.log(e);} 302 fConsiderInheritedMainButton.setSelection(inherit); 303 } 304 305 309 private void updateStopInMainFromConfig(ILaunchConfiguration config) { 310 boolean stop = false; 311 try { 312 stop = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_STOP_IN_MAIN, false); 313 } 314 catch (CoreException e) {JDIDebugUIPlugin.log(e);} 315 fStopInMainCheckButton.setSelection(stop); 316 } 317 318 } 319 | Popular Tags |