blob: a3f9a39ccd2846966649da50f5430c674e70d82f [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2000, 2003 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.debug.ui.jres;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.debug.internal.ui.SWTUtil;
import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.launching.AbstractVMInstallType;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.IVMInstallType;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.VMStandin;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.util.ListenerList;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
/**
* A composite that displays installed JRE's in a table. JREs can be
* added, removed, edited, and searched for.
* <p>
* This block implements ISelectionProvider - it sends selection change events
* when the checked JRE in the table changes, or when the "use default" button
* check state changes.
* </p>
*/
public class InstalledJREsBlock implements IAddVMDialogRequestor, ISelectionProvider {
/**
* This block's control
*/
private Composite fControl;
/**
* VMs being displayed
*/
private List fVMs = new ArrayList();
/**
* The main list control
*/
private CheckboxTableViewer fVMList;
// Action buttons
private Button fAddButton;
private Button fRemoveButton;
private Button fEditButton;
private Button fSearchButton;
// column weights
private float fWeight1 = 1/3F;
private float fWeight2 = 1/3F;
private float fWeight3 = 1/3F;
// ignore column re-sizing when the table is being resized
private boolean fResizingTable = false;
// index of column used for sorting
private int fSortColumn = 0;
/**
* Selection listeners (checked JRE changes)
*/
private ListenerList fSelectionListeners = new ListenerList();
/**
* Flag used to determine if a selection change was fired
*/
private boolean fFired = false;
/**
* Previous selection
*/
private ISelection fPrevSelection = new StructuredSelection();
// Make sure that VMStandin ids are unique if multiple calls to System.currentTimeMillis()
// happen very quickly
private static String fgLastUsedID;
/**
* Content provider to show a list of JREs
*/
class JREsContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object input) {
return fVMs.toArray();
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
public void dispose() {
}
}
/**
* Label provider for installed JREs table.
*/
class VMLabelProvider extends LabelProvider implements ITableLabelProvider {
/**
* @see ITableLabelProvider#getColumnText(Object, int)
*/
public String getColumnText(Object element, int columnIndex) {
if (element instanceof IVMInstall) {
IVMInstall vm= (IVMInstall)element;
switch(columnIndex) {
case 0:
return vm.getName();
case 1:
return vm.getInstallLocation().getAbsolutePath();
case 2:
return vm.getVMInstallType().getName();
}
}
return element.toString();
}
/**
* @see ITableLabelProvider#getColumnImage(Object, int)
*/
public Image getColumnImage(Object element, int columnIndex) {
if (columnIndex == 0) {
return JavaPluginImages.get(JavaPluginImages.IMG_OBJS_LIBRARY);
}
return null;
}
}
/* (non-Javadoc)
* @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
*/
public void addSelectionChangedListener(ISelectionChangedListener listener) {
fSelectionListeners.add(listener);
}
/* (non-Javadoc)
* @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
*/
public ISelection getSelection() {
return new StructuredSelection(fVMList.getCheckedElements());
}
/* (non-Javadoc)
* @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
*/
public void removeSelectionChangedListener(ISelectionChangedListener listener) {
fSelectionListeners.remove(listener);
}
/* (non-Javadoc)
* @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
*/
public void setSelection(ISelection selection) {
if (selection instanceof IStructuredSelection) {
if (!selection.equals(fPrevSelection)) {
fPrevSelection = selection;
Object jre = ((IStructuredSelection)selection).getFirstElement();
fVMList.setCheckedElements(new Object[]{jre});
fVMList.reveal(jre);
fireSelectionChanged();
}
}
}
/**
* Creates this block's control in the given control.
*
* @param anscestor containing control
* @param useManageButton whether to present a single 'manage...' button to
* the user that opens the installed JREs pref page for JRE management,
* or to provide 'add, remove, edit, and search' buttons.
*/
public void createControl(Composite ancestor) {
Composite parent= new Composite(ancestor, SWT.NULL);
GridLayout layout= new GridLayout();
layout.numColumns= 2;
layout.marginHeight = 0;
layout.marginWidth = 0;
parent.setLayout(layout);
Font font = ancestor.getFont();
parent.setFont(font);
fControl = parent;
GridData data;
Label tableLabel = new Label(parent, SWT.NONE);
tableLabel.setText(JREMessages.getString("InstalledJREsBlock.15")); //$NON-NLS-1$
data = new GridData();
data.horizontalSpan = 2;
tableLabel.setLayoutData(data);
tableLabel.setFont(font);
Table table= new Table(parent, SWT.CHECK | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
data= new GridData(GridData.FILL_BOTH);
table.setLayoutData(data);
table.setFont(font);
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableLayout tableLayout= new TableLayout();
table.setLayout(tableLayout);
TableColumn column1= new TableColumn(table, SWT.NULL);
column1.setText(JREMessages.getString("InstalledJREsBlock.0")); //$NON-NLS-1$
column1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
sortByName();
}
});
TableColumn column2= new TableColumn(table, SWT.NULL);
column2.setText(JREMessages.getString("InstalledJREsBlock.1")); //$NON-NLS-1$
column2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
sortByLocation();
}
});
TableColumn column3= new TableColumn(table, SWT.NULL);
column3.setText(JREMessages.getString("InstalledJREsBlock.2")); //$NON-NLS-1$
column3.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
sortByType();
}
});
fVMList= new CheckboxTableViewer(table);
fVMList.setLabelProvider(new VMLabelProvider());
fVMList.setContentProvider(new JREsContentProvider());
// by default, sort by name
sortByName();
fVMList.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent evt) {
enableButtons();
}
});
fVMList.addCheckStateListener(new ICheckStateListener() {
public void checkStateChanged(CheckStateChangedEvent event) {
if (event.getChecked()) {
setCheckedJRE((IVMInstall)event.getElement());
} else {
setCheckedJRE(null);
}
}
});
fVMList.addDoubleClickListener(new IDoubleClickListener() {
public void doubleClick(DoubleClickEvent e) {
editVM();
}
});
table.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent event) {
if (event.character == SWT.DEL && event.stateMask == 0) {
removeVMs();
}
}
});
Composite buttons= new Composite(parent, SWT.NULL);
buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
layout= new GridLayout();
layout.marginHeight= 0;
layout.marginWidth= 0;
buttons.setLayout(layout);
fAddButton = createPushButton(buttons, JREMessages.getString("InstalledJREsBlock.3")); //$NON-NLS-1$
fAddButton.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event evt) {
addVM();
}
});
fEditButton= createPushButton(buttons, JREMessages.getString("InstalledJREsBlock.4")); //$NON-NLS-1$
fEditButton.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event evt) {
editVM();
}
});
fRemoveButton= createPushButton(buttons, JREMessages.getString("InstalledJREsBlock.5")); //$NON-NLS-1$
fRemoveButton.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event evt) {
removeVMs();
}
});
fSearchButton = createPushButton(buttons, JREMessages.getString("InstalledJREsBlock.6")); //$NON-NLS-1$
fSearchButton.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event evt) {
search();
}
});
configureTableResizing(parent, buttons, table, column1, column2, column3);
fillWithWorkspaceJREs();
enableButtons();
fAddButton.setEnabled(JavaRuntime.getVMInstallTypes().length > 0);
}
/**
* Fire current selection
*/
private void fireSelectionChanged() {
SelectionChangedEvent event = new SelectionChangedEvent(this, getSelection());
Object[] listeners = fSelectionListeners.getListeners();
for (int i = 0; i < listeners.length; i++) {
ISelectionChangedListener listener = (ISelectionChangedListener)listeners[i];
listener.selectionChanged(event);
fFired = true;
}
}
/**
* Sorts by VM type, and name within type.
*/
private void sortByType() {
fVMList.setSorter(new ViewerSorter() {
public int compare(Viewer viewer, Object e1, Object e2) {
if ((e1 instanceof IVMInstall) && (e2 instanceof IVMInstall)) {
IVMInstall left= (IVMInstall)e1;
IVMInstall right= (IVMInstall)e2;
String leftType= left.getVMInstallType().getName();
String rightType= right.getVMInstallType().getName();
int res= leftType.compareToIgnoreCase(rightType);
if (res != 0) {
return res;
}
return left.getName().compareToIgnoreCase(right.getName());
}
return super.compare(viewer, e1, e2);
}
public boolean isSorterProperty(Object element, String property) {
return true;
}
});
fSortColumn = 3;
}
/**
* Sorts by VM name.
*/
private void sortByName() {
fVMList.setSorter(new ViewerSorter() {
public int compare(Viewer viewer, Object e1, Object e2) {
if ((e1 instanceof IVMInstall) && (e2 instanceof IVMInstall)) {
IVMInstall left= (IVMInstall)e1;
IVMInstall right= (IVMInstall)e2;
return left.getName().compareToIgnoreCase(right.getName());
}
return super.compare(viewer, e1, e2);
}
public boolean isSorterProperty(Object element, String property) {
return true;
}
});
fSortColumn = 1;
}
/**
* Sorts by VM location.
*/
private void sortByLocation() {
fVMList.setSorter(new ViewerSorter() {
public int compare(Viewer viewer, Object e1, Object e2) {
if ((e1 instanceof IVMInstall) && (e2 instanceof IVMInstall)) {
IVMInstall left= (IVMInstall)e1;
IVMInstall right= (IVMInstall)e2;
return left.getInstallLocation().getAbsolutePath().compareToIgnoreCase(right.getInstallLocation().getAbsolutePath());
}
return super.compare(viewer, e1, e2);
}
public boolean isSorterProperty(Object element, String property) {
return true;
}
});
fSortColumn = 2;
}
private void enableButtons() {
int selectionCount= ((IStructuredSelection)fVMList.getSelection()).size();
fEditButton.setEnabled(selectionCount == 1);
fRemoveButton.setEnabled(selectionCount > 0 && selectionCount < fVMList.getTable().getItemCount());
}
protected Button createPushButton(Composite parent, String label) {
return SWTUtil.createPushButton(parent, label, null);
}
/**
* Correctly resizes the table so no phantom columns appear
*/
protected void configureTableResizing(final Composite parent, final Composite buttons, final Table table, final TableColumn column1, final TableColumn column2, final TableColumn column3) {
parent.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
resizeTable(parent, buttons, table, column1, column2, column3);
}
});
table.addListener(SWT.Paint, new Listener() {
public void handleEvent(Event event) {
table.removeListener(SWT.Paint, this);
resizeTable(parent, buttons, table, column1, column2, column3);
}
});
column1.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
if (column1.getWidth() > 0 && !fResizingTable) {
fWeight1 = getColumnWeight(0);
}
}
});
column2.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
if (column2.getWidth() > 0 && !fResizingTable) {
fWeight2 = getColumnWeight(1);
}
}
});
column3.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
if (column3.getWidth() > 0 && !fResizingTable) {
fWeight3 = getColumnWeight(2);
}
}
});
}
private void resizeTable(Composite parent, Composite buttons, Table table, TableColumn column1, TableColumn column2, TableColumn column3) {
fResizingTable = true;
int parentWidth = -1;
int parentHeight = -1;
if (parent.isVisible()) {
Rectangle area = parent.getClientArea();
parentWidth = area.width;
parentHeight = area.height;
} else {
Point parentSize = parent.computeSize(SWT.DEFAULT, SWT.DEFAULT);
parentWidth = parentSize.x;
parentHeight = parentSize.y;
}
Point preferredSize = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
int width = parentWidth - 2 * table.getBorderWidth();
if (preferredSize.y > parentHeight) {
// Subtract the scrollbar width from the total column width
// if a vertical scrollbar will be required
Point vBarSize = table.getVerticalBar().getSize();
width -= vBarSize.x;
}
width-= buttons.getSize().x;
Point oldSize = table.getSize();
if (oldSize.x > width) {
// table is getting smaller so make the columns
// smaller first and then resize the table to
// match the client area width
column1.setWidth(Math.round(width * fWeight1));
column2.setWidth(Math.round(width * fWeight2));
column3.setWidth(width - (column1.getWidth() + column2.getWidth()));
table.setSize(width, parentHeight);
} else {
// table is getting bigger so make the table
// bigger first and then make the columns wider
// to match the client area width
table.setSize(width, parentHeight);
column1.setWidth(Math.round(width * fWeight1));
column2.setWidth(Math.round(width * fWeight2));
column3.setWidth(width - (column1.getWidth() + column2.getWidth()));
}
fResizingTable = false;
}
/**
* Returns this block's control
*
* @return control
*/
public Control getControl() {
return fControl;
}
/**
* Sets the JREs to be displayed in this block
*
* @param vms JREs to be displayed
*/
protected void setJREs(IVMInstall[] vms) {
fVMs.clear();
for (int i = 0; i < vms.length; i++) {
fVMs.add(vms[i]);
}
fVMList.setInput(fVMs);
fVMList.refresh();
}
/**
* Returns the JREs currently being displayed in this block
*
* @return JREs currently being displayed in this block
*/
public IVMInstall[] getJREs() {
return (IVMInstall[])fVMs.toArray(new IVMInstall[fVMs.size()]);
}
/**
* Bring up a dialog that lets the user create a new VM definition.
*/
private void addVM() {
AddVMDialog dialog= new AddVMDialog(this, getShell(), JavaRuntime.getVMInstallTypes(), null);
dialog.setTitle(JREMessages.getString("InstalledJREsBlock.7")); //$NON-NLS-1$
if (dialog.open() != AddVMDialog.OK) {
return;
}
fVMList.refresh();
}
/**
* @see IAddVMDialogRequestor#vmAdded(IVMInstall)
*/
public void vmAdded(IVMInstall vm) {
fVMs.add(vm);
fVMList.refresh();
}
/**
* @see IAddVMDialogRequestor#isDuplicateName(String)
*/
public boolean isDuplicateName(String name) {
for (int i= 0; i < fVMs.size(); i++) {
IVMInstall vm = (IVMInstall)fVMs.get(i);
if (vm.getName().equals(name)) {
return true;
}
}
return false;
}
private void editVM() {
IStructuredSelection selection= (IStructuredSelection)fVMList.getSelection();
// assume it's length one, otherwise this will not be called
IVMInstall vm= (IVMInstall)selection.getFirstElement();
AddVMDialog dialog= new AddVMDialog(this, getShell(), JavaRuntime.getVMInstallTypes(), vm);
dialog.setTitle(JREMessages.getString("InstalledJREsBlock.8")); //$NON-NLS-1$
if (dialog.open() != AddVMDialog.OK) {
return;
}
fVMList.refresh(vm);
}
private void removeVMs() {
IStructuredSelection selection= (IStructuredSelection)fVMList.getSelection();
IVMInstall[] vms = new IVMInstall[selection.size()];
Iterator iter = selection.iterator();
int i = 0;
while (iter.hasNext()) {
vms[i] = (IVMInstall)iter.next();
i++;
}
removeJREs(vms);
}
/**
* Removes the given VMs from the table.
*
* @param vms
*/
public void removeJREs(IVMInstall[] vms) {
for (int i = 0; i < vms.length; i++) {
fVMs.remove(vms[i]);
}
fVMList.refresh();
}
/**
* Search for installed VMs in the file system
*/
protected void search() {
// choose a root directory for the search
DirectoryDialog dialog = new DirectoryDialog(getShell());
dialog.setMessage(JREMessages.getString("InstalledJREsBlock.9")); //$NON-NLS-1$
dialog.setText(JREMessages.getString("InstalledJREsBlock.10")); //$NON-NLS-1$
String path = dialog.open();
if (path == null) {
return;
}
// ignore installed locations
final Set exstingLocations = new HashSet();
Iterator iter = fVMs.iterator();
while (iter.hasNext()) {
exstingLocations.add(((IVMInstall)iter.next()).getInstallLocation());
}
// search
final File rootDir = new File(path);
final List locations = new ArrayList();
final List types = new ArrayList();
ProgressMonitorDialog pm = new ProgressMonitorDialog(getShell());
IRunnableWithProgress r = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.beginTask(JREMessages.getString("InstalledJREsBlock.11"), IProgressMonitor.UNKNOWN); //$NON-NLS-1$
search(rootDir, locations, types, exstingLocations, monitor);
monitor.done();
}
};
try {
pm.run(true, true, r);
} catch (InvocationTargetException e) {
JDIDebugUIPlugin.log(e);
} catch (InterruptedException e) {
// cancelled
return;
}
if (locations.isEmpty()) {
MessageDialog.openInformation(getShell(), JREMessages.getString("InstalledJREsBlock.12"), MessageFormat.format(JREMessages.getString("InstalledJREsBlock.13"), new String[]{path})); //$NON-NLS-1$ //$NON-NLS-2$
} else {
iter = locations.iterator();
Iterator iter2 = types.iterator();
while (iter.hasNext()) {
File location = (File)iter.next();
IVMInstallType type = (IVMInstallType)iter2.next();
IVMInstall vm = new VMStandin(type, createUniqueId(type));
String name = location.getName();
String nameCopy = new String(name);
int i = 1;
while (isDuplicateName(nameCopy)) {
nameCopy = name + '(' + i++ + ')';
}
vm.setName(nameCopy);
vm.setInstallLocation(location);
if (type instanceof AbstractVMInstallType) {
//set default java doc location
AbstractVMInstallType abs = (AbstractVMInstallType)type;
vm.setJavadocLocation(abs.getDefaultJavadocLocation(location));
}
vmAdded(vm);
}
}
}
protected Shell getShell() {
return getControl().getShell();
}
/**
* Find a unique VM id. Check existing 'real' VMs, as well as the last id used for
* a VMStandin.
*/
private String createUniqueId(IVMInstallType vmType) {
String id= null;
do {
id= String.valueOf(System.currentTimeMillis());
} while (vmType.findVMInstall(id) != null || id.equals(fgLastUsedID));
fgLastUsedID = id;
return id;
}
/**
* Searches the specified directory recursively for installed VMs, adding each
* detected VM to the <code>found</code> list. Any directories specified in
* the <code>ignore</code> are not traversed.
*
* @param directory
* @param found
* @param types
* @param ignore
*/
protected void search(File directory, List found, List types, Set ignore, IProgressMonitor monitor) {
if (monitor.isCanceled()) {
return;
}
String[] names = directory.list();
List subDirs = new ArrayList();
for (int i = 0; i < names.length; i++) {
if (monitor.isCanceled()) {
return;
}
File file = new File(directory, names[i]);
try {
monitor.subTask(MessageFormat.format(JREMessages.getString("InstalledJREsBlock.14"), new String[]{Integer.toString(found.size()), file.getCanonicalPath()})); //$NON-NLS-1$
} catch (IOException e) {
}
IVMInstallType[] vmTypes = JavaRuntime.getVMInstallTypes();
if (file.isDirectory()) {
if (!ignore.contains(file)) {
boolean validLocation = false;
// Take the first VM install type that claims the location as a
// valid VM install. VM install types should be smart enough to not
// claim another type's VM, but just in case...
for (int j = 0; j < vmTypes.length; j++) {
if (monitor.isCanceled()) {
return;
}
IVMInstallType type = vmTypes[j];
IStatus status = type.validateInstallLocation(file);
if (status.isOK()) {
found.add(file);
types.add(type);
validLocation = true;
break;
}
}
if (!validLocation) {
subDirs.add(file);
}
}
}
}
while (!subDirs.isEmpty()) {
File subDir = (File)subDirs.remove(0);
search(subDir, found, types, ignore, monitor);
if (monitor.isCanceled()) {
return;
}
}
}
/**
* Sets the checked JRE, possible <code>null</code>
*
* @param vm JRE or <code>null</code>
*/
public void setCheckedJRE(IVMInstall vm) {
if (vm == null) {
setSelection(new StructuredSelection());
} else {
setSelection(new StructuredSelection(vm));
}
}
/**
* Returns the checked JRE or <code>null</code> if none.
*
* @return the checked JRE or <code>null</code> if none
*/
public IVMInstall getCheckedJRE() {
Object[] objects = fVMList.getCheckedElements();
if (objects.length == 0) {
return null;
}
return (IVMInstall)objects[0];
}
/**
* Persist table settings into the give dialog store, prefixed
* with the given key.
*
* @param settings dialog store
* @param qualifier key qualifier
*/
public void saveColumnSettings(IDialogSettings settings, String qualifier) {
for (int i = 0; i < 3; i++) {
saveColumn(settings, qualifier, i);
}
settings.put(qualifier + ".sortColumn", fSortColumn); //$NON-NLS-1$
}
private void saveColumn(IDialogSettings settings, String qualifier, int col) {
settings.put(qualifier + ".column" + col, getColumnWeight(col)); //$NON-NLS-1$
}
private float getColumnWeight(int col) {
Table table = fVMList.getTable();
Point size = table.getSize();
TableColumn column = table.getColumn(col);
return ((float)column.getWidth()) / size.x;
}
/**
* Retsore table settings from the given dialog store using the
* given key.
*
* @param settings dialog settings store
* @param qualifier key to restore settings from
*/
public void restoreColumnSettings(IDialogSettings settings, String qualifier) {
fWeight1 = restoreColumnWeight(settings, qualifier, 0);
fWeight2 = restoreColumnWeight(settings, qualifier, 1);
fWeight3 = restoreColumnWeight(settings, qualifier, 2);
fVMList.getTable().layout(true);
try {
fSortColumn = settings.getInt(qualifier + ".sortColumn"); //$NON-NLS-1$
} catch (NumberFormatException e) {
fSortColumn = 1;
}
switch (fSortColumn) {
case 1:
sortByName();
break;
case 2:
sortByLocation();
break;
case 3:
sortByType();
break;
}
}
private float restoreColumnWeight(IDialogSettings settings, String qualifier, int col) {
try {
return settings.getFloat(qualifier + ".column" + col); //$NON-NLS-1$
} catch (NumberFormatException e) {
return 1/3F;
}
}
/**
* Populates the JRE table with existing JREs defined in the workspace.
*/
protected void fillWithWorkspaceJREs() {
// fill with JREs
List standins = new ArrayList();
IVMInstallType[] types = JavaRuntime.getVMInstallTypes();
for (int i = 0; i < types.length; i++) {
IVMInstallType type = types[i];
IVMInstall[] installs = type.getVMInstalls();
for (int j = 0; j < installs.length; j++) {
IVMInstall install = installs[j];
standins.add(new VMStandin(install));
}
}
setJREs((IVMInstall[])standins.toArray(new IVMInstall[standins.size()]));
}
}