1 11 package org.eclipse.jdt.debug.ui.launchConfigurations; 12 13 14 import java.util.Map ; 15 16 import org.eclipse.core.resources.IProject; 17 import org.eclipse.core.resources.ResourcesPlugin; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.Path; 22 import org.eclipse.core.runtime.Status; 23 import org.eclipse.debug.core.ILaunchConfiguration; 24 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 25 import org.eclipse.debug.ui.ILaunchConfigurationTab; 26 import org.eclipse.jdt.core.IJavaProject; 27 import org.eclipse.jdt.core.JavaCore; 28 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants; 29 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds; 30 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 31 import org.eclipse.jdt.internal.debug.ui.SWTFactory; 32 import org.eclipse.jdt.internal.debug.ui.jres.JREDescriptor; 33 import org.eclipse.jdt.internal.debug.ui.jres.JREsComboBlock; 34 import org.eclipse.jdt.internal.debug.ui.launcher.LauncherMessages; 35 import org.eclipse.jdt.launching.AbstractVMInstall; 36 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 37 import org.eclipse.jdt.launching.IVMInstall; 38 import org.eclipse.jdt.launching.JavaRuntime; 39 import org.eclipse.jdt.ui.ISharedImages; 40 import org.eclipse.jdt.ui.JavaUI; 41 import org.eclipse.jface.util.IPropertyChangeListener; 42 import org.eclipse.jface.util.PropertyChangeEvent; 43 import org.eclipse.swt.graphics.Font; 44 import org.eclipse.swt.graphics.Image; 45 import org.eclipse.swt.layout.GridData; 46 import org.eclipse.swt.widgets.Composite; 47 import org.eclipse.swt.widgets.Control; 48 import org.eclipse.ui.PlatformUI; 49 50 import com.ibm.icu.text.MessageFormat; 51 52 60 61 public class JavaJRETab extends JavaLaunchTab { 62 63 protected JREsComboBlock fJREBlock; 65 66 protected ILaunchConfigurationTab fDynamicTab; 68 protected Composite fDynamicTabHolder; 69 protected boolean fUseDynamicArea = true; 70 71 protected ILaunchConfigurationWorkingCopy fWorkingCopy; 72 protected ILaunchConfiguration fLaunchConfiguration; 73 74 protected boolean fIsInitializing = false; 76 77 private IPropertyChangeListener fCheckListener = new IPropertyChangeListener() { 79 public void propertyChange(PropertyChangeEvent event) { 80 handleSelectedJREChanged(); 81 } 82 }; 83 84 protected static final String EMPTY_STRING = ""; 87 90 public void dispose() { 91 super.dispose(); 92 if (fJREBlock != null) { 93 fJREBlock.removePropertyChangeListener(fCheckListener); 94 } 95 } 96 97 100 public void createControl(Composite parent) { 101 Font font = parent.getFont(); 102 Composite topComp = SWTFactory.createComposite(parent, font, 1, 1, GridData.FILL_HORIZONTAL, 0, 0); 103 104 fJREBlock = new JREsComboBlock(); 105 fJREBlock.setDefaultJREDescriptor(getDefaultJREDescriptor()); 106 fJREBlock.setSpecificJREDescriptor(getSpecificJREDescriptor()); 107 fJREBlock.createControl(topComp); 108 Control control = fJREBlock.getControl(); 109 fJREBlock.addPropertyChangeListener(fCheckListener); 110 control.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 111 112 setDynamicTabHolder(SWTFactory.createComposite(topComp, font, 1, 1, GridData.FILL_BOTH, 0, 0)); 113 setControl(topComp); 114 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_JRE_TAB); 115 } 116 117 protected void setDynamicTabHolder(Composite tabHolder) { 118 this.fDynamicTabHolder = tabHolder; 119 } 120 121 protected Composite getDynamicTabHolder() { 122 return fDynamicTabHolder; 123 } 124 125 protected void setDynamicTab(ILaunchConfigurationTab tab) { 126 fDynamicTab = tab; 127 } 128 129 protected ILaunchConfigurationTab getDynamicTab() { 130 return fDynamicTab; 131 } 132 133 136 public void setDefaults(ILaunchConfigurationWorkingCopy config) { 137 setLaunchConfigurationWorkingCopy(config); 138 ILaunchConfigurationTab dynamicTab = getDynamicTab(); 139 if (dynamicTab != null) { 140 dynamicTab.setDefaults(config); 141 } 142 } 143 144 147 public void initializeFrom(ILaunchConfiguration configuration) { 148 fIsInitializing = true; 149 getControl().setRedraw(false); 150 setLaunchConfiguration(configuration); 151 updateJREFromConfig(configuration); 152 fJREBlock.setDefaultJREDescriptor(getDefaultJREDescriptor()); 153 ILaunchConfigurationTab dynamicTab = getDynamicTab(); 154 if (dynamicTab != null) { 155 dynamicTab.initializeFrom(configuration); 156 } 157 getControl().setRedraw(true); 158 fIsInitializing = false; 159 } 160 161 164 public void performApply(ILaunchConfigurationWorkingCopy configuration) { 165 if (fJREBlock.isDefaultJRE()) { 166 configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, (String )null); 167 } else { 168 IPath containerPath = fJREBlock.getPath(); 169 String portablePath = null; 170 if (containerPath != null) { 171 portablePath = containerPath.toPortableString(); 172 } 173 configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, portablePath); 174 } 175 configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME, (String )null); 177 configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, (String )null); 178 179 ILaunchConfigurationTab dynamicTab = getDynamicTab(); 181 if (dynamicTab == null) { 182 configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE_SPECIFIC_ATTRS_MAP, (Map )null); 183 } else { 184 dynamicTab.performApply(configuration); 185 } 186 } 187 188 191 public boolean isValid(ILaunchConfiguration config) { 192 193 setErrorMessage(null); 194 setMessage(null); 195 196 IStatus status = fJREBlock.getStatus(); 197 if (!status.isOK()) { 198 setErrorMessage(status.getMessage()); 199 return false; 200 } 201 status = checkCompliance(); 202 if (!status.isOK()) { 203 setErrorMessage(status.getMessage()); 204 return false; 205 } 206 207 ILaunchConfigurationTab dynamicTab = getDynamicTab(); 208 if (dynamicTab != null) { 209 return dynamicTab.isValid(config); 210 } 211 return true; 212 } 213 214 219 private IStatus checkCompliance() { 220 IJavaProject javaProject = getJavaProject(); 221 if (javaProject == null) { 222 return Status.OK_STATUS; 223 } 224 String source = LauncherMessages.JavaJRETab_3; 225 String compliance = javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, false); 226 if(compliance == null) { 227 compliance = javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true); 228 source = LauncherMessages.JavaJRETab_4; 229 } 230 IPath vmPath = fJREBlock.getPath(); 231 IVMInstall vm = null; 232 if(JavaRuntime.newDefaultJREContainerPath().equals(vmPath)) { 233 if(javaProject.isOpen()) { 234 try { 235 vm = JavaRuntime.getVMInstall(getJavaProject()); 236 } catch (CoreException e) { 237 JDIDebugUIPlugin.log(e); 238 return Status.OK_STATUS; 239 } 240 if(vm == null) { 241 vm = JavaRuntime.getVMInstall(vmPath); 242 } 243 } 244 } 245 else { 246 vm = JavaRuntime.getVMInstall(vmPath); 247 } 248 String environmentId = JavaRuntime.getExecutionEnvironmentId(vmPath); 249 if(vm instanceof AbstractVMInstall) { 250 AbstractVMInstall install = (AbstractVMInstall) vm; 251 String vmver = install.getJavaVersion(); 252 if(vmver != null) { 253 int val = compliance.compareTo(vmver); 254 if(val > 0) { 255 String setting = null; 256 if (environmentId == null) { 257 setting = LauncherMessages.JavaJRETab_2; 258 } else { 259 setting = LauncherMessages.JavaJRETab_1; 260 } 261 return new Status(IStatus.ERROR, IJavaDebugUIConstants.PLUGIN_ID,IJavaDebugUIConstants.INTERNAL_ERROR, 262 MessageFormat.format(LauncherMessages.JavaJRETab_0, 263 new String []{setting, source, compliance}), null); 264 } 265 } 266 } 267 return Status.OK_STATUS; 268 } 269 270 273 public String getName() { 274 return LauncherMessages.JavaJRETab__JRE_1; 275 } 276 277 280 public Image getImage() { 281 return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_LIBRARY); 282 } 283 284 289 public String getId() { 290 return "org.eclipse.jdt.debug.ui.javaJRETab"; } 292 293 297 protected void updateJREFromConfig(ILaunchConfiguration config) { 298 try { 299 String path = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, (String )null); 300 if (path != null) { 301 fJREBlock.setPath(Path.fromPortableString(path)); 302 return; 303 } 304 } catch (CoreException e) { 305 JDIDebugUIPlugin.log(e); 306 } 307 String vmName = null; 308 String vmTypeID = null; 309 try { 310 vmTypeID = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, (String )null); 311 vmName = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME, (String )null); 312 } catch (CoreException ce) { 313 JDIDebugUIPlugin.log(ce); 314 } 315 selectJRE(vmTypeID, vmName); 316 } 317 318 321 protected void handleSelectedJREChanged() { 322 loadDynamicJREArea(); 323 324 ILaunchConfigurationWorkingCopy wc = getLaunchConfigurationWorkingCopy(); 326 if (getDynamicTab() == null) { 327 if (wc == null) { 329 if (getLaunchConfiguration().isWorkingCopy()) { 330 wc = (ILaunchConfigurationWorkingCopy)getLaunchConfiguration(); 331 } 332 } 333 if (!fIsInitializing) { 334 if (wc != null) { 335 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE_SPECIFIC_ATTRS_MAP, (Map )null); 336 } 337 } 338 } else { 339 if (wc == null) { 340 try { 341 ILaunchConfiguration config = getLaunchConfiguration(); 342 if (config.isWorkingCopy()) { 343 wc = (ILaunchConfigurationWorkingCopy) config; 345 if(wc.getOriginal() != null) { 346 wc.getOriginal().getWorkingCopy(); 347 } 348 } else { 349 wc = getLaunchConfiguration().getWorkingCopy(); 350 } 351 } catch (CoreException e) { 352 IStatus status = e.getStatus(); 353 JDIDebugUIPlugin.statusDialog(status); 354 JDIDebugUIPlugin.log(status); 355 return; 356 } 357 } 358 if (!fIsInitializing) { 359 getDynamicTab().setDefaults(wc); 360 getDynamicTab().initializeFrom(wc); 361 } 362 } 363 364 updateLaunchConfigurationDialog(); 365 } 366 367 protected void selectJRE(String typeID, String vmName) { 368 if (typeID == null) { 369 fJREBlock.setPath(JavaRuntime.newDefaultJREContainerPath()); 370 } else { 371 fJREBlock.setPath(JavaRuntime.newJREContainerPath(typeID, vmName)); 372 } 373 } 374 375 379 protected ILaunchConfigurationTab getTabForCurrentJRE() { 380 if (!fJREBlock.isDefaultJRE()) { 381 IPath path = fJREBlock.getPath(); 382 if (path != null && JavaRuntime.getExecutionEnvironmentId(path) == null) { 383 IVMInstall vm = fJREBlock.getJRE(); 384 if (vm != null) { 385 String vmInstallTypeID = vm.getVMInstallType().getId(); 386 return JDIDebugUIPlugin.getDefault().getVMInstallTypePage(vmInstallTypeID); 387 } 388 } 389 } 390 return null; 391 } 392 393 397 protected void loadDynamicJREArea() { 398 399 Control[] children = getDynamicTabHolder().getChildren(); 401 for (int i = 0; i < children.length; i++) { 402 children[i].dispose(); 403 } 404 405 if (isUseDynamicJREArea()) { 406 setDynamicTab(getTabForCurrentJRE()); 408 if (getDynamicTab() == null) { 409 return; 410 } 411 412 getDynamicTab().setLaunchConfigurationDialog(getLaunchConfigurationDialog()); 414 getDynamicTab().createControl(getDynamicTabHolder()); 415 getDynamicTabHolder().layout(); 416 } 417 418 } 419 420 protected ILaunchConfigurationWorkingCopy getLaunchConfigurationWorkingCopy() { 421 return fWorkingCopy; 422 } 423 424 429 public String getErrorMessage() { 430 ILaunchConfigurationTab tab = getDynamicTab(); 431 if ((super.getErrorMessage() != null) || (tab == null)) { 432 return super.getErrorMessage(); 433 } 434 return tab.getErrorMessage(); 435 } 436 437 protected void setLaunchConfigurationWorkingCopy(ILaunchConfigurationWorkingCopy workingCopy) { 438 fWorkingCopy = workingCopy; 439 } 440 441 protected ILaunchConfiguration getLaunchConfiguration() { 442 return fLaunchConfiguration; 443 } 444 445 protected void setLaunchConfiguration(ILaunchConfiguration launchConfiguration) { 446 fLaunchConfiguration = launchConfiguration; 447 } 448 449 456 public void setVMSpecificArgumentsVisible(boolean visible) { 457 fUseDynamicArea = visible; 458 } 459 460 protected boolean isUseDynamicJREArea() { 461 return fUseDynamicArea; 462 } 463 464 protected JREDescriptor getDefaultJREDescriptor() { 465 return new JREDescriptor() { 466 467 470 public String getDescription() { 471 IJavaProject project = getJavaProject(); 472 String name = LauncherMessages.JavaJRETab_7; 473 if (project == null) { 474 IVMInstall vm = JavaRuntime.getDefaultVMInstall(); 475 if (vm != null) { 476 name = vm.getName(); 477 } 478 return MessageFormat.format(LauncherMessages.JavaJRETab_8, new String []{name}); 479 } 480 try { 481 IVMInstall vm = JavaRuntime.getVMInstall(project); 482 if (vm != null) { 483 name = vm.getName(); 484 } 485 } catch (CoreException e) { 486 } 487 return MessageFormat.format(LauncherMessages.JavaJRETab_9, new String []{name}); 488 } 489 }; 490 } 491 492 protected JREDescriptor getSpecificJREDescriptor() { 493 return null; 494 } 495 496 502 protected IJavaProject getJavaProject() { 503 if (getLaunchConfiguration() != null) { 504 try { 505 String name = getLaunchConfiguration().getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String )null); 506 if (name != null && name.length() > 0) { 507 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name); 508 if (project.exists()) { 509 return JavaCore.create(project); 510 } 511 } 512 } catch (CoreException e) { 513 JDIDebugUIPlugin.log(e); 514 } 515 } 516 return null; 517 } 518 519 522 public void activated(ILaunchConfigurationWorkingCopy workingCopy) { 523 fJREBlock.refresh(); 526 } 527 528 531 public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) { 532 } 534 } 535 | Popular Tags |