FileDocCategorySizeDatePackage
StaticPortConfigDialog.javaAPI DocAndroid 1.5 API13282Wed May 06 22:41:08 BST 2009com.android.ddms

StaticPortConfigDialog

public class StaticPortConfigDialog extends org.eclipse.swt.widgets.Dialog
Dialog to configure the static debug ports.

Fields Summary
private static final String
PREFS_DEVICE_COL
Preference name for the 0th column width
private static final String
PREFS_APP_COL
Preference name for the 1st column width
private static final String
PREFS_PORT_COL
Preference name for the 2nd column width
private static final int
COL_DEVICE
private static final int
COL_APPLICATION
private static final int
COL_PORT
private static final int
DLG_WIDTH
private static final int
DLG_HEIGHT
private org.eclipse.swt.widgets.Shell
mShell
private org.eclipse.swt.widgets.Shell
mParent
private org.eclipse.swt.widgets.Table
mPortTable
private ArrayList
mPorts
Array containing the list of already used static port to avoid duplication.
Constructors Summary
public StaticPortConfigDialog(org.eclipse.swt.widgets.Shell parent)
Basic constructor.

param
parent


             
       
        super(parent, SWT.DIALOG_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL);
    
Methods Summary
private voidaddEntry(java.lang.String deviceName, java.lang.String appName, int portNumber)
Add a new entry in the list.

param
deviceName the serial number of the device
param
appName java package for the application
param
portNumber port number

        // create a new item for the table
        TableItem item = new TableItem(mPortTable, SWT.NONE);

        item.setText(COL_DEVICE, deviceName);
        item.setText(COL_APPLICATION, appName);
        item.setText(COL_PORT, Integer.toString(portNumber));

        // add the port to the list of port number used.
        mPorts.add(portNumber);
    
private voidcreateUI()
Creates the dialog ui.

        mParent = getParent();
        mShell = new Shell(mParent, getStyle());
        mShell.setText("Static Port Configuration");

        mShell.setLayout(new GridLayout(1, true));

        mShell.addListener(SWT.Close, new Listener() {
            public void handleEvent(Event event) {
                event.doit = true;
            }
        });

        // center part with the list on the left and the buttons
        // on the right.
        Composite main = new Composite(mShell, SWT.NONE);
        main.setLayoutData(new GridData(GridData.FILL_BOTH));
        main.setLayout(new GridLayout(2, false));

        // left part: list view
        mPortTable = new Table(main, SWT.SINGLE | SWT.FULL_SELECTION);
        mPortTable.setLayoutData(new GridData(GridData.FILL_BOTH));
        mPortTable.setHeaderVisible(true);
        mPortTable.setLinesVisible(true);

        TableHelper.createTableColumn(mPortTable, "Device Serial Number",
                SWT.LEFT, "emulator-5554", //$NON-NLS-1$
                PREFS_DEVICE_COL, PrefsDialog.getStore());

        TableHelper.createTableColumn(mPortTable, "Application Package",
                SWT.LEFT, "com.android.samples.phone", //$NON-NLS-1$
                PREFS_APP_COL, PrefsDialog.getStore());

        TableHelper.createTableColumn(mPortTable, "Debug Port",
                SWT.RIGHT, "Debug Port", //$NON-NLS-1$
                PREFS_PORT_COL, PrefsDialog.getStore());

        // right part: buttons
        Composite buttons = new Composite(main, SWT.NONE);
        buttons.setLayoutData(new GridData(GridData.FILL_VERTICAL));
        buttons.setLayout(new GridLayout(1, true));

        Button newButton = new Button(buttons, SWT.NONE);
        newButton.setText("New...");
        newButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                StaticPortEditDialog dlg = new StaticPortEditDialog(mShell,
                        mPorts);
                if (dlg.open()) {
                    // get the text
                    String device = dlg.getDeviceSN();
                    String app = dlg.getAppName();
                    int port = dlg.getPortNumber();

                    // add it to the list
                    addEntry(device, app, port);
                }
            }
        });

        final Button editButton = new Button(buttons, SWT.NONE);
        editButton.setText("Edit...");
        editButton.setEnabled(false);
        editButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                int index = mPortTable.getSelectionIndex();
                String oldDeviceName = getDeviceName(index);
                String oldAppName = getAppName(index);
                String oldPortNumber = getPortNumber(index);
                StaticPortEditDialog dlg = new StaticPortEditDialog(mShell,
                        mPorts, oldDeviceName, oldAppName, oldPortNumber);
                if (dlg.open()) {
                    // get the text
                    String deviceName = dlg.getDeviceSN();
                    String app = dlg.getAppName();
                    int port = dlg.getPortNumber();

                    // add it to the list
                    replaceEntry(index, deviceName, app, port);
                }
            }
        });

        final Button deleteButton = new Button(buttons, SWT.NONE);
        deleteButton.setText("Delete");
        deleteButton.setEnabled(false);
        deleteButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                int index = mPortTable.getSelectionIndex();
                removeEntry(index);
            }
        });

        // bottom part with the ok/cancel
        Composite bottomComp = new Composite(mShell, SWT.NONE);
        bottomComp.setLayoutData(new GridData(
                GridData.HORIZONTAL_ALIGN_CENTER));
        bottomComp.setLayout(new GridLayout(2, true));

        Button okButton = new Button(bottomComp, SWT.NONE);
        okButton.setText("OK");
        okButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                updateStore();
                mShell.close();
            }
        });

        Button cancelButton = new Button(bottomComp, SWT.NONE);
        cancelButton.setText("Cancel");
        cancelButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                mShell.close();
            }
        });

        mPortTable.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                // get the selection index
                int index = mPortTable.getSelectionIndex();

                boolean enabled = index != -1;
                editButton.setEnabled(enabled);
                deleteButton.setEnabled(enabled);
            }
        });

        mShell.pack();

    
private java.lang.StringgetAppName(int index)
Returns the application name for a specific index

param
index The index
return
the java package name of the application

        TableItem item = mPortTable.getItem(index);
        return item.getText(COL_APPLICATION);
    
private java.lang.StringgetDeviceName(int index)
Returns the device name for a specific index

param
index The index
return
the java package name of the application

        TableItem item = mPortTable.getItem(index);
        return item.getText(COL_DEVICE);
    
private java.lang.StringgetPortNumber(int index)
Returns the port number for a specific index

param
index The index
return
the port number

        TableItem item = mPortTable.getItem(index);
        return item.getText(COL_PORT);
    
public voidopen()
Open and display the dialog. This method returns only when the user closes the dialog somehow.

        createUI();

        if (mParent == null || mShell == null) {
            return;
        }

        updateFromStore();

        // Set the dialog size.
        mShell.setMinimumSize(DLG_WIDTH, DLG_HEIGHT);
        Rectangle r = mParent.getBounds();
        // get the center new top left.
        int cx = r.x + r.width/2;
        int x = cx - DLG_WIDTH / 2;
        int cy = r.y + r.height/2;
        int y = cy - DLG_HEIGHT / 2;
        mShell.setBounds(x, y, DLG_WIDTH, DLG_HEIGHT);

        mShell.pack();

        // actually open the dialog
        mShell.open();

        // event loop until the dialog is closed.
        Display display = mParent.getDisplay();
        while (!mShell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    
private voidremoveEntry(int index)
Remove an entry from the list.

param
index The index of the entry to be removed

        // remove from the ui
        mPortTable.remove(index);

        // and from the port list.
        mPorts.remove(index);
    
private voidreplaceEntry(int index, java.lang.String deviceName, java.lang.String appName, int portNumber)
Replace an entry in the list with new values.

param
index The index of the item to be replaced
param
deviceName the serial number of the device
param
appName The new java package for the application
param
portNumber The new port number.

        // get the table item by index
        TableItem item = mPortTable.getItem(index);

        // set its new value
        item.setText(COL_DEVICE, deviceName);
        item.setText(COL_APPLICATION, appName);
        item.setText(COL_PORT, Integer.toString(portNumber));

        // and replace the port number in the port list.
        mPorts.set(index, portNumber);
    
private voidupdateFromStore()
Updates the ui from the value in the preference store.

        // get the map from the debug port manager
        DebugPortProvider provider = DebugPortProvider.getInstance();
        Map<String, Map<String, Integer>> map = provider.getPortList();

        // we're going to loop on the keys and fill the table.
        Set<String> deviceKeys = map.keySet();

        for (String deviceKey : deviceKeys) {
            Map<String, Integer> deviceMap = map.get(deviceKey);
            if (deviceMap != null) {
                Set<String> appKeys = deviceMap.keySet();

                for (String appKey : appKeys) {
                    Integer port = deviceMap.get(appKey);
                    if (port != null) {
                        addEntry(deviceKey, appKey, port);
                    }
                }
            }
        }
    
private voidupdateStore()
Update the store from the content of the ui.

        // create a new Map object and fill it.
        HashMap<String, Map<String, Integer>> map = new HashMap<String, Map<String, Integer>>();

        int count = mPortTable.getItemCount();

        for (int i = 0 ; i < count ; i++) {
            TableItem item = mPortTable.getItem(i);
            String deviceName = item.getText(COL_DEVICE);

            Map<String, Integer> deviceMap = map.get(deviceName);
            if (deviceMap == null) {
                deviceMap = new HashMap<String, Integer>();
                map.put(deviceName, deviceMap);
            }

            deviceMap.put(item.getText(COL_APPLICATION), Integer.valueOf(item.getText(COL_PORT)));
        }

        // set it in the store through the debug port manager.
        DebugPortProvider provider = DebugPortProvider.getInstance();
        provider.setPortList(map);