FileDocCategorySizeDatePackage
AvdSelector.javaAPI DocAndroid 1.5 API14645Wed May 06 22:41:10 BST 2009com.android.sdkuilib

AvdSelector

public final class AvdSelector extends Object
The AVD selector is a table that is added to the given parent composite.

To use, create it using {@link #AvdSelector(Composite, AvdInfo[])} then call {@link #setSelection(AvdInfo)}, {@link #setSelectionListener(SelectionListener)} and finally use {@link #getFirstSelected()} to retrieve the selection.

Fields Summary
private com.android.sdklib.avd.AvdManager.AvdInfo[]
mAvds
private org.eclipse.swt.events.SelectionListener
mSelectionListener
private org.eclipse.swt.widgets.Table
mTable
private org.eclipse.swt.widgets.Label
mDescription
Constructors Summary
public AvdSelector(org.eclipse.swt.widgets.Composite parent, com.android.sdklib.avd.AvdManager.AvdInfo[] avds, com.android.sdklib.IAndroidTarget filter)
Creates a new SDK Target Selector, and fills it with a list of {@link AvdInfo}, filtered by a {@link IAndroidTarget}.

Only the {@link AvdInfo} able to run application developed for the given {@link IAndroidTarget} will be displayed.

param
parent The parent composite where the selector will be added.
param
avds The list of AVDs. This is not copied, the caller must not modify.

        mAvds = avds;

        // Layout has 1 column
        Composite group = new Composite(parent, SWT.NONE);
        group.setLayout(new GridLayout());
        group.setLayoutData(new GridData(GridData.FILL_BOTH));
        group.setFont(parent.getFont());
        
        mTable = new Table(group, SWT.CHECK | SWT.FULL_SELECTION | SWT.SINGLE | SWT.BORDER);
        mTable.setHeaderVisible(true);
        mTable.setLinesVisible(false);

        GridData data = new GridData();
        data.grabExcessVerticalSpace = true;
        data.grabExcessHorizontalSpace = true;
        data.horizontalAlignment = GridData.FILL;
        data.verticalAlignment = GridData.FILL;
        mTable.setLayoutData(data);

        mDescription = new Label(group, SWT.WRAP);
        mDescription.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        // create the table columns
        final TableColumn column0 = new TableColumn(mTable, SWT.NONE);
        column0.setText("AVD Name");
        final TableColumn column1 = new TableColumn(mTable, SWT.NONE);
        column1.setText("Target Name");
        final TableColumn column2 = new TableColumn(mTable, SWT.NONE);
        column2.setText("Platform");
        final TableColumn column3 = new TableColumn(mTable, SWT.NONE);
        column3.setText("API Level");

        adjustColumnsWidth(mTable, column0, column1, column2, column3);
        setupSelectionListener(mTable);
        fillTable(mTable, filter);
        setupTooltip(mTable);
    
public AvdSelector(org.eclipse.swt.widgets.Composite parent, com.android.sdklib.avd.AvdManager.AvdInfo[] avds)
Creates a new SDK Target Selector, and fills it with a list of {@link AvdInfo}.

param
parent The parent composite where the selector will be added.
param
avds The list of AVDs. This is not copied, the caller must not modify.

        this(parent, avds, null /* filter */);
    
Methods Summary
private voidadjustColumnsWidth(org.eclipse.swt.widgets.Table table, org.eclipse.swt.widgets.TableColumn column0, org.eclipse.swt.widgets.TableColumn column1, org.eclipse.swt.widgets.TableColumn column2, org.eclipse.swt.widgets.TableColumn column3)
Adds a listener to adjust the columns width when the parent is resized.

If we need something more fancy, we might want to use this: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet77.java?view=co

        // Add a listener to resize the column to the full width of the table
        table.addControlListener(new ControlAdapter() {
            @Override
            public void controlResized(ControlEvent e) {
                Rectangle r = table.getClientArea();
                column0.setWidth(r.width * 30 / 100); // 30%  
                column1.setWidth(r.width * 45 / 100); // 45%
                column2.setWidth(r.width * 10 / 100); // 10%
                column3.setWidth(r.width * 15 / 100); // 15%
            }
        });
    
private voidfillTable(org.eclipse.swt.widgets.Table table, com.android.sdklib.IAndroidTarget filter)
Fills the table with all AVD. The table columns are:
  • column 0: sdk name
  • column 1: sdk vendor
  • column 2: sdk api name
  • column 3: sdk version

        table.removeAll();
        if (mAvds != null && mAvds.length > 0) {
            table.setEnabled(true);
            for (AvdInfo avd : mAvds) {
                if (filter == null || filter.isCompatibleBaseFor(avd.getTarget())) {
                    TableItem item = new TableItem(table, SWT.NONE);
                    item.setData(avd);
                    item.setText(0, avd.getName());
                    IAndroidTarget target = avd.getTarget();
                    item.setText(1, target.getFullName());
                    item.setText(2, target.getApiVersionName());
                    item.setText(3, Integer.toString(target.getApiVersionNumber()));
                }
            }
        }
        
        if (table.getItemCount() == 0) {
            table.setEnabled(false);
            TableItem item = new TableItem(table, SWT.NONE);
            item.setData(null);
            item.setText(0, "--");
            item.setText(1, "No AVD available");
            item.setText(2, "--");
            item.setText(3, "--");
        }
    
public com.android.sdklib.avd.AvdManager.AvdInfo[]getAvds()
Returns the list of known AVDs.

This is not a copy. Callers must not modify this array.

        return mAvds;
    
public com.android.sdklib.avd.AvdManager.AvdInfogetFirstSelected()
Returns the first selected item. This is useful when the table is in single-selection mode.

return
The first selected item or null.

        for (TableItem i : mTable.getItems()) {
            if (i.getChecked()) {
                return (AvdInfo) i.getData();
            }
        }
        return null;
    
public voidsetAvds(com.android.sdklib.avd.AvdManager.AvdInfo[] avds, com.android.sdklib.IAndroidTarget filter)
Sets a new set of AVD, with an optional filter.

This must be called from the UI thread.

param
avds The list of AVDs. This is not copied, the caller must not modify.
param
filter An IAndroidTarget. If non-null, only AVD whose target are compatible with the filter target will displayed an available for selection.

        mAvds = avds;
        fillTable(mTable, filter);
    
public voidsetEnabled(boolean enabled)
Enables the receiver if the argument is true, and disables it otherwise. A disabled control is typically not selectable from the user interface and draws with an inactive or "grayed" look.

param
enabled the new enabled state.

        mTable.setEnabled(enabled);
        mDescription.setEnabled(enabled);
    
public booleansetSelection(com.android.sdklib.avd.AvdManager.AvdInfo target)
Sets the current target selection.

If the selection is actually changed, this will invoke the selection listener (if any) with a null event.

param
target the target to be selection
return
true if the target could be selected, false otherwise.

        boolean found = false;
        boolean modified = false;
        for (TableItem i : mTable.getItems()) {
            if ((AvdInfo) i.getData() == target) {
                found = true;
                if (!i.getChecked()) {
                    modified = true;
                    i.setChecked(true);
                }
            } else if (i.getChecked()) {
                modified = true;
                i.setChecked(false);
            }
        }
        
        if (modified && mSelectionListener != null) {
            mSelectionListener.widgetSelected(null);
        }
        
        return found;
    
public voidsetSelectionListener(org.eclipse.swt.events.SelectionListener selectionListener)
Sets a selection listener. Set it to null to remove it. The listener will be called after this table processed its selection events so that the caller can see the updated state.

The event's item contains a {@link TableItem}. The {@link TableItem#getData()} contains an {@link IAndroidTarget}.

It is recommended that the caller uses the {@link #getFirstSelected()} method instead.

param
selectionListener The new listener or null to remove it.

        mSelectionListener = selectionListener;
    
public voidsetTableHeightHint(int heightHint)

        GridData data = new GridData();
        data.heightHint = heightHint;
        data.grabExcessVerticalSpace = true;
        data.grabExcessHorizontalSpace = true;
        data.horizontalAlignment = GridData.FILL;
        data.verticalAlignment = GridData.FILL;
        mTable.setLayoutData(data);
    
private voidsetupSelectionListener(org.eclipse.swt.widgets.Table table)
Creates a selection listener that will check or uncheck the whole line when double-clicked (aka "the default selection").

        // Add a selection listener that will check/uncheck items when they are double-clicked
        table.addSelectionListener(new SelectionListener() {
            
            /**
             * Handles single-click selection on the table.
             * {@inheritDoc}
             */
            public void widgetSelected(SelectionEvent e) {
                if (e.item instanceof TableItem) {
                    TableItem i = (TableItem) e.item;
                    enforceSingleSelection(i);
                    updateDescription(i);
                }

                if (mSelectionListener != null) {
                    mSelectionListener.widgetSelected(e);
                }
            }

            /**
             * Handles double-click selection on the table.
             * Note that the single-click handler will probably already have been called.
             * 
             * On double-click, <em>always</em> check the table item.
             * 
             * {@inheritDoc}
             */
            public void widgetDefaultSelected(SelectionEvent e) {
                if (e.item instanceof TableItem) {
                    TableItem i = (TableItem) e.item;
                    i.setChecked(true);
                    enforceSingleSelection(i);
                    updateDescription(i);
                }

                if (mSelectionListener != null) {
                    mSelectionListener.widgetDefaultSelected(e);
                }
            }

            /**
             * To ensure single selection, uncheck all other items when this one is selected.
             * This makes the chekboxes act as radio buttons.
             */
            private void enforceSingleSelection(TableItem item) {
                if (item.getChecked()) {
                    Table parentTable = item.getParent();
                    for (TableItem i2 : parentTable.getItems()) {
                        if (i2 != item && i2.getChecked()) {
                            i2.setChecked(false);
                        }
                    }
                }
            }
        });
    
private voidsetupTooltip(org.eclipse.swt.widgets.Table table)
Sets up a tooltip that displays the current item description.

Displaying a tooltip over the table looks kind of odd here. Instead we actually display the description in a label under the table.

        /*
         * Reference: 
         * http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet125.java?view=markup
         */
        
        final Listener listener = new Listener() {
            public void handleEvent(Event event) {
                
                switch(event.type) {
                case SWT.KeyDown:
                case SWT.MouseExit:
                case SWT.MouseDown:
                    return;
                    
                case SWT.MouseHover:
                    updateDescription(table.getItem(new Point(event.x, event.y)));
                    break;
                    
                case SWT.Selection:
                    if (event.item instanceof TableItem) {
                        updateDescription((TableItem) event.item);
                    }
                    break;
                    
                default:
                    return;
                }

            }
        };
        
        table.addListener(SWT.Dispose, listener);
        table.addListener(SWT.KeyDown, listener);
        table.addListener(SWT.MouseMove, listener);
        table.addListener(SWT.MouseHover, listener);
    
private voidupdateDescription(org.eclipse.swt.widgets.TableItem item)
Updates the description label with the path of the item's AVD, if any.

        if (item != null) {
            Object data = item.getData();
            if (data instanceof AvdInfo) {
                String newTooltip = ((AvdInfo) data).getPath();
                mDescription.setText(newTooltip == null ? "" : newTooltip);  //$NON-NLS-1$
            }
        }