aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.monitor/src/com/android/ide/eclipse/monitor/ddms/StaticPortConfigDialog.java
blob: 456cdcf51de87ceba68447bb616043ce0a8d85ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.monitor.ddms;

import com.android.ddmuilib.TableHelper;
import com.android.ide.eclipse.ddms.DdmsPlugin;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Dialog to configure the static debug ports.
 *
 */
public class StaticPortConfigDialog extends Dialog {

    /** Preference name for the 0th column width */
    private static final String PREFS_DEVICE_COL = "spcd.deviceColumn"; //$NON-NLS-1$

    /** Preference name for the 1st column width */
    private static final String PREFS_APP_COL = "spcd.AppColumn"; //$NON-NLS-1$

    /** Preference name for the 2nd column width */
    private static final String PREFS_PORT_COL = "spcd.PortColumn"; //$NON-NLS-1$

    private static final int COL_DEVICE = 0;
    private static final int COL_APPLICATION = 1;
    private static final int COL_PORT = 2;


    private static final int DLG_WIDTH = 500;
    private static final int DLG_HEIGHT = 300;

    private Shell mShell;
    private Shell mParent;

    private Table mPortTable;

    /**
     * Array containing the list of already used static port to avoid
     * duplication.
     */
    private ArrayList<Integer> mPorts = new ArrayList<Integer>();

    /**
     * Basic constructor.
     * @param parent
     */
    public StaticPortConfigDialog(Shell parent) {
        super(parent, SWT.DIALOG_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL);
    }

    /**
     * Open and display the dialog. This method returns only when the
     * user closes the dialog somehow.
     *
     */
    public void open() {
        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();
        }
    }

    /**
     * Creates the dialog ui.
     */
    private void createUI() {
        mParent = getParent();
        mShell = new Shell(mParent, getStyle());
        mShell.setText("Static Port Configuration");

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

        mShell.addListener(SWT.Close, new Listener() {
            @Override
            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, DdmsPlugin.getDefault().getPreferenceStore());

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

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

        // 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();

    }

    /**
     * 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
     */
    private void addEntry(String deviceName, String appName, int portNumber) {
        // 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);
    }

    /**
     * Remove an entry from the list.
     * @param index The index of the entry to be removed
     */
    private void removeEntry(int index) {
        // remove from the ui
        mPortTable.remove(index);

        // and from the port list.
        mPorts.remove(index);
    }

    /**
     * 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.
     */
    private void replaceEntry(int index, String deviceName, String appName, int portNumber) {
        // 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);
    }


    /**
     * Returns the device name for a specific index
     * @param index The index
     * @return the java package name of the application
     */
    private String getDeviceName(int index) {
        TableItem item = mPortTable.getItem(index);
        return item.getText(COL_DEVICE);
    }

    /**
     * Returns the application name for a specific index
     * @param index The index
     * @return the java package name of the application
     */
    private String getAppName(int index) {
        TableItem item = mPortTable.getItem(index);
        return item.getText(COL_APPLICATION);
    }

    /**
     * Returns the port number for a specific index
     * @param index The index
     * @return the port number
     */
    private String getPortNumber(int index) {
        TableItem item = mPortTable.getItem(index);
        return item.getText(COL_PORT);
    }

    /**
     * Updates the ui from the value in the preference store.
     */
    private void updateFromStore() {
        // 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);
                    }
                }
            }
        }
    }

    /**
     * Update the store from the content of the ui.
     */
    private void updateStore() {
        // 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);
    }
}