1 11 package org.eclipse.jdt.internal.debug.ui.jres; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.Collections ; 16 import java.util.Comparator ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.ListenerList; 23 import org.eclipse.core.runtime.Status; 24 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants; 25 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 26 import org.eclipse.jdt.internal.debug.ui.SWTFactory; 27 import org.eclipse.jdt.internal.debug.ui.actions.ControlAccessibleListener; 28 import org.eclipse.jdt.launching.IVMInstall; 29 import org.eclipse.jdt.launching.IVMInstallType; 30 import org.eclipse.jdt.launching.JavaRuntime; 31 import org.eclipse.jdt.launching.VMStandin; 32 import org.eclipse.jdt.launching.environments.IExecutionEnvironment; 33 import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager; 34 import org.eclipse.jface.util.IPropertyChangeListener; 35 import org.eclipse.jface.util.PropertyChangeEvent; 36 import org.eclipse.swt.SWT; 37 import org.eclipse.swt.events.SelectionAdapter; 38 import org.eclipse.swt.events.SelectionEvent; 39 import org.eclipse.swt.graphics.Font; 40 import org.eclipse.swt.layout.GridData; 41 import org.eclipse.swt.widgets.Button; 42 import org.eclipse.swt.widgets.Combo; 43 import org.eclipse.swt.widgets.Composite; 44 import org.eclipse.swt.widgets.Control; 45 import org.eclipse.swt.widgets.Event; 46 import org.eclipse.swt.widgets.Group; 47 import org.eclipse.swt.widgets.Listener; 48 import org.eclipse.swt.widgets.Shell; 49 50 import com.ibm.icu.text.MessageFormat; 51 52 61 public class JREsComboBlock { 62 63 public static final String PROPERTY_JRE = "PROPERTY_JRE"; 65 68 private Composite fControl; 69 70 73 private List fVMs = new ArrayList (); 74 75 78 private Combo fCombo; 79 80 private Button fManageButton; 82 83 86 private ListenerList fListeners = new ListenerList(); 87 88 91 private JREDescriptor fDefaultDescriptor = null; 92 93 96 private JREDescriptor fSpecificDescriptor = null; 97 98 101 private Button fDefaultButton = null; 102 103 106 private Button fSpecificButton = null; 107 108 111 private String fTitle = null; 112 113 116 private Button fEnvironmentsButton = null; 117 118 121 private Combo fEnvironmentsCombo = null; 122 123 private Button fManageEnvironmentsButton = null; 124 125 private IPath fErrorPath; 127 128 131 private List fEnvironments = new ArrayList (); 132 133 private IStatus fStatus = OK_STATUS; 134 135 private static IStatus OK_STATUS = new Status(IStatus.OK, JDIDebugUIPlugin.getUniqueIdentifier(), 0, "", null); 137 public void addPropertyChangeListener(IPropertyChangeListener listener) { 138 fListeners.add(listener); 139 } 140 141 public void removePropertyChangeListener(IPropertyChangeListener listener) { 142 fListeners.remove(listener); 143 } 144 145 private void firePropertyChange() { 146 PropertyChangeEvent event = new PropertyChangeEvent(this, PROPERTY_JRE, null, getPath()); 147 Object [] listeners = fListeners.getListeners(); 148 for (int i = 0; i < listeners.length; i++) { 149 IPropertyChangeListener listener = (IPropertyChangeListener) listeners[i]; 150 listener.propertyChange(event); 151 } 152 } 153 154 159 public void createControl(Composite ancestor) { 160 Font font = ancestor.getFont(); 161 fControl = SWTFactory.createComposite(ancestor, font, 1, 1, GridData.FILL_BOTH); 162 if (fTitle == null) { 163 fTitle = JREMessages.JREsComboBlock_3; 164 } 165 Group group = SWTFactory.createGroup(fControl, fTitle, 1, 1, GridData.FILL_HORIZONTAL); 166 Composite comp = SWTFactory.createComposite(group, font, 3, 1, GridData.FILL_BOTH, 0, 0); 167 if (fDefaultDescriptor != null) { 169 fDefaultButton = SWTFactory.createRadioButton(comp, fDefaultDescriptor.getDescription(), 3); 170 fDefaultButton.addSelectionListener(new SelectionAdapter() { 171 public void widgetSelected(SelectionEvent e) { 172 if (fDefaultButton.getSelection()) { 173 setUseDefaultJRE(); 174 setStatus(OK_STATUS); 175 firePropertyChange(); 176 } 177 } 178 }); 179 } 180 181 String text = JREMessages.JREsComboBlock_1; 183 if (fSpecificDescriptor != null) { 184 text = fSpecificDescriptor.getDescription(); 185 } 186 fSpecificButton = SWTFactory.createRadioButton(comp, text, 1); 187 fSpecificButton.addSelectionListener(new SelectionAdapter() { 188 public void widgetSelected(SelectionEvent e) { 189 if (fSpecificButton.getSelection()) { 190 fCombo.setEnabled(true); 191 if (fCombo.getText().length() == 0 && !fVMs.isEmpty()) { 192 fCombo.select(0); 193 } 194 if (fVMs.isEmpty()) { 195 setError(JREMessages.JREsComboBlock_0); 196 } else { 197 setStatus(OK_STATUS); 198 } 199 fEnvironmentsCombo.setEnabled(false); 200 firePropertyChange(); 201 } 202 } 203 }); 204 fCombo = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY); 205 fCombo.setFont(font); 206 GridData data= new GridData(GridData.FILL_HORIZONTAL); 207 data.horizontalSpan = 1; 208 fCombo.setLayoutData(data); 209 ControlAccessibleListener.addListener(fCombo, fSpecificButton.getText()); 210 211 fCombo.addSelectionListener(new SelectionAdapter() { 212 public void widgetSelected(SelectionEvent e) { 213 setStatus(OK_STATUS); 214 firePropertyChange(); 215 } 216 }); 217 218 fManageButton = createPushButton(comp, JREMessages.JREsComboBlock_2); 219 fManageButton.addListener(SWT.Selection, new Listener() { 220 public void handleEvent(Event event) { 221 showPrefPage("org.eclipse.jdt.debug.ui.preferences.VMPreferencePage"); } 223 }); 224 fillWithWorkspaceJREs(); 225 226 fEnvironmentsButton = SWTFactory.createRadioButton(comp, JREMessages.JREsComboBlock_4); 228 fEnvironmentsButton.addSelectionListener(new SelectionAdapter() { 229 public void widgetSelected(SelectionEvent e) { 230 if (fEnvironmentsButton.getSelection()) { 231 fCombo.setEnabled(false); 232 if (fEnvironmentsCombo.getText().length() == 0 && !fEnvironments.isEmpty()) { 233 fEnvironmentsCombo.select(0); 234 } 235 fEnvironmentsCombo.setEnabled(true); 236 if (fEnvironments.isEmpty()) { 237 setError(JREMessages.JREsComboBlock_5); 238 } else { 239 setStatus(OK_STATUS); 240 } 241 firePropertyChange(); 242 } 243 } 244 }); 245 246 247 fEnvironmentsCombo = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY); 248 fEnvironmentsCombo.setFont(font); 249 data= new GridData(GridData.FILL_HORIZONTAL); 250 data.horizontalSpan = 1; 251 fEnvironmentsCombo.setLayoutData(data); 252 253 fEnvironmentsCombo.addSelectionListener(new SelectionAdapter() { 254 public void widgetSelected(SelectionEvent e) { 255 setPath(JavaRuntime.newJREContainerPath(getEnvironment())); 256 firePropertyChange(); 257 } 258 }); 259 260 fManageEnvironmentsButton = createPushButton(comp, JREMessages.JREsComboBlock_14); 261 fManageEnvironmentsButton.addListener(SWT.Selection, new Listener() { 262 public void handleEvent(Event event) { 263 showPrefPage("org.eclipse.jdt.debug.ui.jreProfiles"); } 265 }); 266 267 fillWithWorkspaceProfiles(); 268 } 269 270 276 private void showPrefPage(String id) { 277 IVMInstall prevJRE = getJRE(); 278 IExecutionEnvironment prevEnv = getEnvironment(); 279 JDIDebugUIPlugin.showPreferencePage(id); 280 fillWithWorkspaceJREs(); 281 fillWithWorkspaceProfiles(); 282 restoreCombo(fVMs, prevJRE, fCombo); 283 restoreCombo(fEnvironments, prevEnv, fEnvironmentsCombo); 284 setDefaultJREDescriptor(fDefaultDescriptor); 286 if (isDefaultJRE()) { 287 setUseDefaultJRE(); 289 } 290 setPath(getPath()); 291 firePropertyChange(); 292 } 293 294 private void restoreCombo(List elements, Object element, Combo combo) { 295 int index = -1; 296 if (element != null) { 297 index = elements.indexOf(element); 298 } 299 if (index >= 0) { 300 combo.select(index); 301 } else { 302 combo.select(0); 303 } 304 } 305 306 protected Button createPushButton(Composite parent, String label) { 307 return SWTFactory.createPushButton(parent, label, null); 308 } 309 310 315 public Control getControl() { 316 return fControl; 317 } 318 319 324 protected void setJREs(List jres) { 325 fVMs.clear(); 326 fVMs.addAll(jres); 327 Collections.sort(fVMs, new Comparator () { 329 public int compare(Object o1, Object o2) { 330 IVMInstall left = (IVMInstall)o1; 331 IVMInstall right = (IVMInstall)o2; 332 return left.getName().compareToIgnoreCase(right.getName()); 333 } 334 335 public boolean equals(Object obj) { 336 return obj == this; 337 } 338 }); 339 String [] names = new String [fVMs.size()]; 341 Iterator iter = fVMs.iterator(); 342 int i = 0; 343 while (iter.hasNext()) { 344 IVMInstall vm = (IVMInstall)iter.next(); 345 names[i] = vm.getName(); 346 i++; 347 } 348 fCombo.setItems(names); 349 fCombo.setVisibleItemCount(Math.min(names.length, 20)); 350 } 351 352 protected Shell getShell() { 353 return getControl().getShell(); 354 } 355 356 361 private void selectJRE(IVMInstall vm) { 362 fSpecificButton.setSelection(true); 363 fDefaultButton.setSelection(false); 364 fEnvironmentsButton.setSelection(false); 365 fCombo.setEnabled(true); 366 fEnvironmentsCombo.setEnabled(false); 367 if (vm != null) { 368 int index = fVMs.indexOf(vm); 369 if (index >= 0) { 370 fCombo.select(index); 371 } 372 } 373 firePropertyChange(); 374 } 375 376 381 private void selectEnvironment(IExecutionEnvironment env) { 382 fSpecificButton.setSelection(false); 383 fDefaultButton.setSelection(false); 384 fCombo.setEnabled(false); 385 fEnvironmentsButton.setSelection(true); 386 fEnvironmentsCombo.setEnabled(true); 387 if (env != null) { 388 int index = fEnvironments.indexOf(env); 389 if (index >= 0) { 390 fEnvironmentsCombo.select(index); 391 } 392 } 393 firePropertyChange(); 394 } 395 396 401 public IVMInstall getJRE() { 402 int index = fCombo.getSelectionIndex(); 403 if (index >= 0) { 404 return (IVMInstall)fVMs.get(index); 405 } 406 return null; 407 } 408 409 414 private IExecutionEnvironment getEnvironment() { 415 int index = fEnvironmentsCombo.getSelectionIndex(); 416 if (index >= 0) { 417 return (IExecutionEnvironment)fEnvironments.get(index); 418 } 419 return null; 420 } 421 422 425 protected void fillWithWorkspaceJREs() { 426 List standins = new ArrayList (); 428 IVMInstallType[] types = JavaRuntime.getVMInstallTypes(); 429 for (int i = 0; i < types.length; i++) { 430 IVMInstallType type = types[i]; 431 IVMInstall[] installs = type.getVMInstalls(); 432 for (int j = 0; j < installs.length; j++) { 433 IVMInstall install = installs[j]; 434 standins.add(new VMStandin(install)); 435 } 436 } 437 setJREs(standins); 438 } 439 440 443 protected void fillWithWorkspaceProfiles() { 444 fEnvironments.clear(); 445 IExecutionEnvironment[] environments = JavaRuntime.getExecutionEnvironmentsManager().getExecutionEnvironments(); 446 for (int i = 0; i < environments.length; i++) { 447 fEnvironments.add(environments[i]); 448 } 449 Collections.sort(fEnvironments, new Comparator () { 451 public int compare(Object o1, Object o2) { 452 IExecutionEnvironment left = (IExecutionEnvironment)o1; 453 IExecutionEnvironment right = (IExecutionEnvironment)o2; 454 return left.getId().compareToIgnoreCase(right.getId()); 455 } 456 457 public boolean equals(Object obj) { 458 return obj == this; 459 } 460 }); 461 String [] names = new String [fEnvironments.size()]; 463 Iterator iter = fEnvironments.iterator(); 464 int i = 0; 465 while (iter.hasNext()) { 466 IExecutionEnvironment env = (IExecutionEnvironment)iter.next(); 467 IPath path = JavaRuntime.newJREContainerPath(env); 468 IVMInstall install = JavaRuntime.getVMInstall(path); 469 if (install != null) { 470 names[i] = MessageFormat.format(JREMessages.JREsComboBlock_15, new String []{env.getId(), install.getName()}); 471 } else { 472 names[i] = MessageFormat.format(JREMessages.JREsComboBlock_16, new String []{env.getId()}); 473 } 474 i++; 475 } 476 fEnvironmentsCombo.setItems(names); 477 fEnvironmentsCombo.setVisibleItemCount(Math.min(names.length, 20)); 478 } 479 480 485 public void setDefaultJREDescriptor(JREDescriptor descriptor) { 486 fDefaultDescriptor = descriptor; 487 setButtonTextFromDescriptor(fDefaultButton, descriptor); 488 } 489 490 private void setButtonTextFromDescriptor(Button button, JREDescriptor descriptor) { 491 if (button != null) { 492 String currentText = button.getText(); 494 String newText = descriptor.getDescription(); 495 if (!newText.equals(currentText)) { 496 button.setText(newText); 497 fControl.layout(); 498 } 499 } 500 } 501 502 507 public void setSpecificJREDescriptor(JREDescriptor descriptor) { 508 fSpecificDescriptor = descriptor; 509 setButtonTextFromDescriptor(fSpecificButton, descriptor); 510 } 511 512 517 public boolean isDefaultJRE() { 518 if (fDefaultButton != null) { 519 return fDefaultButton.getSelection(); 520 } 521 return false; 522 } 523 524 527 private void setUseDefaultJRE() { 528 if (fDefaultDescriptor != null) { 529 fDefaultButton.setSelection(true); 530 fSpecificButton.setSelection(false); 531 fEnvironmentsButton.setSelection(false); 532 fCombo.setEnabled(false); 533 fEnvironmentsCombo.setEnabled(false); 534 firePropertyChange(); 535 } 536 } 537 538 543 public void setTitle(String title) { 544 fTitle = title; 545 } 546 547 550 public void refresh() { 551 setDefaultJREDescriptor(fDefaultDescriptor); 552 } 553 554 560 public IPath getPath() { 561 if (!getStatus().isOK() && fErrorPath != null) { 562 return fErrorPath; 563 } 564 if (fEnvironmentsButton.getSelection()) { 565 int index = fEnvironmentsCombo.getSelectionIndex(); 566 if (index >= 0) { 567 IExecutionEnvironment env = (IExecutionEnvironment) fEnvironments.get(index); 568 return JavaRuntime.newJREContainerPath(env); 569 } 570 return null; 571 } 572 if (fSpecificButton.getSelection()) { 573 int index = fCombo.getSelectionIndex(); 574 if (index >= 0) { 575 IVMInstall vm = (IVMInstall) fVMs.get(index); 576 return JavaRuntime.newJREContainerPath(vm); 577 } 578 return null; 579 } 580 return JavaRuntime.newDefaultJREContainerPath(); 581 } 582 583 590 public void setPath(IPath containerPath) { 591 fErrorPath = null; 592 setStatus(OK_STATUS); 593 if (JavaRuntime.newDefaultJREContainerPath().equals(containerPath)) { 594 setUseDefaultJRE(); 595 } else { 596 String envId = JavaRuntime.getExecutionEnvironmentId(containerPath); 597 if (envId != null) { 598 IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager(); 599 IExecutionEnvironment environment = manager.getEnvironment(envId); 600 if (environment == null) { 601 fErrorPath = containerPath; 602 selectEnvironment(environment); 603 setError(MessageFormat.format(JREMessages.JREsComboBlock_6, new String []{envId})); 604 } else { 605 selectEnvironment(environment); 606 IVMInstall[] installs = environment.getCompatibleVMs(); 607 if (installs.length == 0) { 608 setError(MessageFormat.format(JREMessages.JREsComboBlock_7, new String []{environment.getId()})); 609 } 610 } 611 } else { 612 IVMInstall install = JavaRuntime.getVMInstall(containerPath); 613 if (install == null) { 614 selectJRE(install); 615 fErrorPath = containerPath; 616 String installTypeId = JavaRuntime.getVMInstallTypeId(containerPath); 617 if (installTypeId == null) { 618 setError(JREMessages.JREsComboBlock_8); 619 } else { 620 IVMInstallType installType = JavaRuntime.getVMInstallType(installTypeId); 621 if (installType == null) { 622 setError(MessageFormat.format(JREMessages.JREsComboBlock_9, new String []{installTypeId})); 623 } else { 624 String installName = JavaRuntime.getVMInstallName(containerPath); 625 if (installName == null) { 626 setError(MessageFormat.format(JREMessages.JREsComboBlock_10, new String []{installType.getName()})); 627 } else { 628 setError(MessageFormat.format(JREMessages.JREsComboBlock_11, new String []{installName, installType.getName()})); 629 } 630 } 631 } 632 } else { 633 selectJRE(install); 634 File location = install.getInstallLocation(); 635 if (location == null) { 636 setError(JREMessages.JREsComboBlock_12); 637 } else if (!location.exists()) { 638 setError(JREMessages.JREsComboBlock_13); 639 } 640 } 641 } 642 } 643 } 644 645 private void setError(String message) { 646 setStatus(new Status(IStatus.ERROR, JDIDebugUIPlugin.getUniqueIdentifier(), 647 IJavaDebugUIConstants.INTERNAL_ERROR, message, null)); 648 } 649 650 655 public IStatus getStatus() { 656 return fStatus; 657 } 658 659 private void setStatus(IStatus status) { 660 fStatus = status; 661 } 662 } 663 | Popular Tags |