### Eclipse Workspace Patch 1.0 #P org.eclipse.pde.ui Index: src/org/eclipse/pde/internal/ui/editor/product/JRESection.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/editor/product/JRESection.java,v retrieving revision 1.18 diff -u -r1.18 JRESection.java --- src/org/eclipse/pde/internal/ui/editor/product/JRESection.java 3 Jan 2008 15:42:48 -0000 1.18 +++ src/org/eclipse/pde/internal/ui/editor/product/JRESection.java 26 Feb 2008 18:58:55 -0000 @@ -7,25 +7,25 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Benjamin Cabe - bug 217908 *******************************************************************************/ package org.eclipse.pde.internal.ui.editor.product; import com.ibm.icu.text.MessageFormat; -import java.util.ArrayList; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Platform; import org.eclipse.jdt.launching.IVMInstall; import org.eclipse.jdt.launching.JavaRuntime; import org.eclipse.jdt.launching.environments.IExecutionEnvironment; -import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager; import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.viewers.*; import org.eclipse.pde.core.IModelChangedEvent; import org.eclipse.pde.core.plugin.TargetPlatform; import org.eclipse.pde.internal.core.iproduct.*; import org.eclipse.pde.internal.ui.*; import org.eclipse.pde.internal.ui.editor.*; import org.eclipse.pde.internal.ui.launcher.VMHelper; -import org.eclipse.pde.internal.ui.parts.ComboPart; +import org.eclipse.pde.internal.ui.parts.ComboViewerPart; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CTabFolder; import org.eclipse.swt.custom.CTabItem; @@ -42,13 +42,38 @@ public class JRESection extends PDESection { + private final class EELabelProvider extends LabelProvider { + public String getText(Object element) { + IExecutionEnvironment env = (IExecutionEnvironment) element; + IPath path = JavaRuntime.newJREContainerPath(env); + IVMInstall install = JavaRuntime.getVMInstall(path); + String eeItem; + if (install != null) { + eeItem = MessageFormat.format(PDEUIMessages.JRESection_eeBoundJRE, new String[] {env.getId(), install.getName()}); + } else { + eeItem = MessageFormat.format(PDEUIMessages.JRESection_eeUnboundJRE, new String[] {env.getId()}); + } + return eeItem; + } + } + + private final class JRELabelProvider extends LabelProvider { + public String getText(Object element) { + if (element instanceof IVMInstall) { + IVMInstall vm = (IVMInstall) element; + return vm.getName(); + } + // else: null-object + return ""; //$NON-NLS-1$ + } + } + private Button fJRERadioButton; private Button fEERadioButton; private Button fInstalledJREsButton; private Button fExecutionEnvironmentsButton; - private ComboPart fJREsCombo; - private ComboPart fEEsCombo; - private ArrayList fEEChoices; + private ComboViewerPart fJREsCombo; + private ComboViewerPart fEEsCombo; private boolean fBlockChanges; private static final String[] TAB_LABELS = {"linux", "macosx", "solaris", "win32"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ @@ -98,20 +123,20 @@ fJRERadioButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { updateWidgets(); - setJRE(fJREsCombo.getSelection()); + setJRE((IVMInstall) fJREsCombo.getSelection()); } }); - fJREsCombo = new ComboPart(); + fJREsCombo = new ComboViewerPart(); fJREsCombo.createControl(client, toolkit, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY); fJREsCombo.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); - String[] installs = VMHelper.getVMInstallNames(); - fJREsCombo.setItems(installs); - fJREsCombo.add("", 0); //$NON-NLS-1$ - fJREsCombo.addSelectionListener(new SelectionAdapter() { - public void widgetSelected(SelectionEvent e) { + fJREsCombo.setItems(VMHelper.getAllVMInstances()); + fJREsCombo.addItem(null, 0); + fJREsCombo.setLabelProvider(new JRELabelProvider()); + fJREsCombo.addSelectionChangedListener(new ISelectionChangedListener() { + public void selectionChanged(SelectionChangedEvent event) { if (!fBlockChanges) - setJRE(fJREsCombo.getSelection()); + setJRE(fJREsCombo.getSelection() == ComboViewerPart.NULL_OBJECT ? null : (IVMInstall) fJREsCombo.getSelection()); } }); @@ -128,18 +153,20 @@ fEERadioButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { updateWidgets(); - setEE(fEEsCombo.getSelectionIndex()); + setEE((IExecutionEnvironment) fEEsCombo.getSelection()); } }); - fEEsCombo = new ComboPart(); + fEEsCombo = new ComboViewerPart(); fEEsCombo.createControl(client, toolkit, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY); fEEsCombo.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); - initializeExecutionEnvironments(); - fEEsCombo.addSelectionListener(new SelectionAdapter() { - public void widgetSelected(SelectionEvent e) { + fEEsCombo.setLabelProvider(new EELabelProvider()); + fEEsCombo.setComparator(new ViewerComparator()); + fEEsCombo.setItems(VMHelper.getExecutionEnvironments()); + fEEsCombo.addSelectionChangedListener(new ISelectionChangedListener() { + public void selectionChanged(SelectionChangedEvent event) { if (!fBlockChanges) - setEE(fEEsCombo.getSelectionIndex()); + setEE((IExecutionEnvironment) ((IStructuredSelection) event.getSelection()).getFirstElement()); } }); @@ -159,50 +186,20 @@ getProductModel().addModelChangedListener(this); } - private void setEE(int selectionIndex) { - if (selectionIndex >= 0 && selectionIndex < fEEChoices.size()) { - IExecutionEnvironment ee = (IExecutionEnvironment) fEEChoices.get(selectionIndex); - if (ee != null) { - IPath eePath = JavaRuntime.newJREContainerPath(ee); - getJVMLocations().setJREContainerPath(getOS(fLastTab), eePath); - } + private void setEE(IExecutionEnvironment ee) { + if (ee != null) { + IPath eePath = JavaRuntime.newJREContainerPath(ee); + getJVMLocations().setJREContainerPath(getOS(fLastTab), eePath); } } - private void setJRE(String vmName) { - IVMInstall install = VMHelper.getVMInstall(vmName); + private void setJRE(IVMInstall install) { if (install != null) { IPath jrePath = JavaRuntime.newJREContainerPath(install); getJVMLocations().setJREContainerPath(getOS(fLastTab), jrePath); } } - private void initializeExecutionEnvironments() { - fEEChoices = new ArrayList(); - IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager(); - IExecutionEnvironment[] envs = manager.getExecutionEnvironments(); - for (int i = 0; i < envs.length; i++) { - addToEECombo(envs[i]); - } - } - - /** - * Adds the given execution environment to the list of known EEs and - * adds an entry to the combo box "EE_ID (Associated_VM)". The entries - * will always be added to the end of the list/combo. - * @param env environment to add - */ - private void addToEECombo(IExecutionEnvironment env) { - IPath path = JavaRuntime.newJREContainerPath(env); - IVMInstall install = JavaRuntime.getVMInstall(path); - fEEChoices.add(env); - if (install != null) { - fEEsCombo.add(MessageFormat.format(PDEUIMessages.JRESection_eeBoundJRE, new String[] {env.getId(), install.getName()})); - } else { - fEEsCombo.add(MessageFormat.format(PDEUIMessages.JRESection_eeUnboundJRE, new String[] {env.getId()})); - } - } - private IProductModel getProductModel() { return (IProductModel) getPage().getPDEEditor().getAggregateModel(); } @@ -246,17 +243,17 @@ String eeID = JavaRuntime.getExecutionEnvironmentId(jrePath); IExecutionEnvironment env = VMHelper.getExecutionEnvironment(eeID); if (env != null) { - if (!fEEChoices.contains(env)) - addToEECombo(env); - fEEsCombo.select(fEEsCombo.getItemCount() - 1); + if (!fEEsCombo.getItems().contains(env)) + fEEsCombo.addItem(env); + fEEsCombo.select(env); fEERadioButton.setSelection(true); fJRERadioButton.setSelection(false); } else { IVMInstall install = JavaRuntime.getVMInstall(jrePath); if (install != null) { - if (fJREsCombo.indexOf(install.getName()) < 0) - fJREsCombo.add(install.getName()); - fJREsCombo.setText(install.getName()); + if (!fJREsCombo.getItems().contains(install)) + fJREsCombo.addItem(install); + fJREsCombo.select(install); fJRERadioButton.setSelection(true); fEERadioButton.setSelection(false); } Index: src/org/eclipse/pde/internal/ui/parts/ComboViewerPart.java =================================================================== RCS file: src/org/eclipse/pde/internal/ui/parts/ComboViewerPart.java diff -N src/org/eclipse/pde/internal/ui/parts/ComboViewerPart.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/pde/internal/ui/parts/ComboViewerPart.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,109 @@ +/******************************************************************************* + * Copyright (c) 2008 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + * Benjamin Cabe - creation of this class + *******************************************************************************/ +package org.eclipse.pde.internal.ui.parts; + +import java.util.*; +import java.util.List; +import org.eclipse.jface.viewers.*; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.CCombo; +import org.eclipse.swt.widgets.*; +import org.eclipse.ui.forms.widgets.FormToolkit; + +public class ComboViewerPart { + private Control fCombo; + private ComboViewer fComboViewer; + private List fObjects; + + public static Object NULL_OBJECT = new Object(); + + public ComboViewerPart() { + } + + public void createControl(Composite parent, FormToolkit toolkit, int style) { + if (toolkit.getBorderStyle() == SWT.BORDER) { + fCombo = new Combo(parent, style | SWT.BORDER); + fComboViewer = new ComboViewer((Combo) fCombo); + } else { + fCombo = new CCombo(parent, style | SWT.FLAT); + fComboViewer = new ComboViewer((CCombo) fCombo); + } + + fObjects = new ArrayList(); + fComboViewer.setLabelProvider(new LabelProvider()); + fComboViewer.setContentProvider(new ArrayContentProvider()); + fComboViewer.setInput(fObjects); + } + + public Control getControl() { + return fCombo; + } + + public void setEnabled(boolean enabled) { + fCombo.setEnabled(enabled); + } + + public void refresh() { + fComboViewer.refresh(); + } + + public void addItem(Object item) { + fObjects.add((item == null) ? NULL_OBJECT : item); + refresh(); + } + + public void addItem(Object item, int index) { + fObjects.add(index, (item == null) ? NULL_OBJECT : item); + refresh(); + } + + public Collection getItems() { + return fObjects; + } + + public void setItems(Object[] items) { + fObjects.clear(); + for (int i = 0; i < items.length; i++) + fObjects.add((items[i] == null) ? NULL_OBJECT : items[i]); + refresh(); + } + + public void setItems(Collection items) { + fObjects.clear(); + Iterator it = items.iterator(); + while (it.hasNext()) { + Object o = it.next(); + fObjects.add((o == null) ? NULL_OBJECT : o); + } + refresh(); + } + + public void select(Object item) { + fComboViewer.setSelection(new StructuredSelection(item)); + } + + public void setLabelProvider(IBaseLabelProvider labelProvider) { + fComboViewer.setLabelProvider(labelProvider); + } + + public void setComparator(ViewerComparator comparator) { + fComboViewer.setComparator(comparator); + } + + public void addSelectionChangedListener(ISelectionChangedListener listener) { + fComboViewer.addSelectionChangedListener(listener); + } + + public Object getSelection() { + return ((IStructuredSelection) fComboViewer.getSelection()).getFirstElement(); + } +}