summaryrefslogtreecommitdiff
path: root/src/plugins/remote.device/src/com/motorola/studio/android/remote/ui/wireless/WirelessPropertiesComposite.java
blob: 3718dcca6d12ebbdcba93d7c87531f8cabc89c15 (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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
 * 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.motorola.studio.android.remote.ui.wireless;

import java.util.Collection;
import java.util.LinkedHashSet;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;
import org.eclipse.sequoyah.device.framework.factory.InstanceRegistry;
import org.eclipse.sequoyah.device.framework.model.AbstractMobileInstance;
import org.eclipse.sequoyah.device.framework.ui.DeviceUIResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

import com.motorola.studio.android.adt.ISerialNumbered;
import com.motorola.studio.android.devices.DevicesManager;
import com.motorola.studio.android.remote.RemoteDeviceConstants;
import com.motorola.studio.android.remote.RemoteDevicePlugin;
import com.motorola.studio.android.remote.RemoteDeviceUtils;
import com.motorola.studio.android.remote.i18n.RemoteDeviceNLS;
import com.motorola.studio.android.remote.instance.RemoteDeviceInstance;

/**
 * Composite containing the Android Wireless Remote Device properties for edition.
 */
public class WirelessPropertiesComposite extends Composite
{
    /**
     * Empty String
     */
    private static final String EMPTY_STRING = "";

    /**
     * Default invalid integer
     */
    private static final int DEFAULT_INVALID_INTEGER = -1;

    private String name;

    private final String host;

    private int port;

    private int timeout;

    // DEVICE - if it already exist
    ISerialNumbered device;

    private IProgressMonitor monitor;

    private final Collection<WirelessPropertiesChangedListener> listeners =
            new LinkedHashSet<WirelessPropertiesChangedListener>();

    // Error messages
    private static final String NAME_ERR_MESSAGE =
            RemoteDeviceNLS.ERR_WirelessDeviceWizardPage_Name;

    private static final String HOST_ERR_MESSAGE = RemoteDeviceNLS.ERR_RemoteDeviceWizardPage_IP;

    private static final String PORT_ERR_MESSAGE = RemoteDeviceNLS.ERR_RemoteDeviceWizardPage_Port;

    private static final String TIMEOUT_ERR_MESSAGE =
            RemoteDeviceNLS.ERR_RemoteDeviceWizardPage_Timeout;

    private final Text nameText;

    private final Text portText;

    private final Text timeoutText;

    /*
     * Listener used for handle Timeout text change.
     */
    private final class TimeoutModifyListener implements ModifyListener
    {
        public void modifyText(ModifyEvent e)
        {
            String timeoutStr = timeoutText.getText();
            try
            {
                timeout = Integer.parseInt(timeoutStr);
            }
            catch (NumberFormatException e1)
            {
                timeout = DEFAULT_INVALID_INTEGER;
            }
            finally
            {
                notifyListeners();
            }
        }
    }

    /*
     * Listener used for handle Port text change.
     */
    private final class PortModifyListener implements ModifyListener
    {
        public void modifyText(ModifyEvent e)
        {
            String portStr = portText.getText();
            try
            {
                port = Integer.parseInt(portStr);
                // manage the case where the entered IP/Port matches of the an existing device
                manageSameIPAndPortOfRemoteDevice();
            }
            catch (NumberFormatException e1)
            {
                port = DEFAULT_INVALID_INTEGER;
            }
            finally
            {
                notifyListeners();
            }
        }
    }

    /*
     * Listener used for handle Name text change.
     */
    private final class NameModifyListener implements ModifyListener
    {
        public void modifyText(ModifyEvent e)
        {
            name = nameText.getText();
            notifyListeners();
        }
    }

    /**
     * Listener that must be implemented by others who want to monitor changes
     * in this composite
     */
    public interface WirelessPropertiesChangedListener
    {
        public void propertiesChanged();
    }

    /**
     * Constructor
     * 
     * @param parent the parent composite
     */
    public WirelessPropertiesComposite(Composite parent, String host, ISerialNumbered device)
    {
        this(parent, host, EMPTY_STRING, EMPTY_STRING, device);
    }

    /**
     * Create contents of the composite
     * 
     * @param parent the parent composite
     * @param initialHost initial value for host
     * @param initialPort initial value for port number
     * @param initialTiomeout initial value for timeout
     */
    public WirelessPropertiesComposite(Composite parent, String initialHost, String initialPort,
            String initialTimeout, ISerialNumbered device)
    {
        super(parent, SWT.NONE);
        this.name = device.getDeviceName() + RemoteDeviceConstants.DEFAULT_WIRELESS_SUFIX;
        this.host = initialHost;
        this.port = RemoteDeviceConstants.DEFAULT_PORT;
        this.timeout = RemoteDeviceConstants.DEFAULT_TIMEOUT;

        // Set Help
        PlatformUI.getWorkbench().getHelpSystem()
                .setHelp(parent, RemoteDeviceConstants.WIRELESS_HELP_ID);
        setLayout(new GridLayout(2, false));

        // add device name
        Label nameLabel = new Label(this, SWT.NONE);
        nameLabel.setText(RemoteDeviceNLS.UI_Name);
        nameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, false));

        // add device text
        nameText = new Text(this, SWT.BORDER);
        nameText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        nameText.addModifyListener(new NameModifyListener());
        nameText.setText(name);

        // add IP name
        Label hostLabel = new Label(this, SWT.NONE);
        hostLabel.setText(RemoteDeviceNLS.UI_Host);
        hostLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, false));

        // add IP text
        Text hostText = new Text(this, SWT.BORDER);
        hostText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        hostText.setEnabled(false);
        hostText.setText(host);

        // add port name
        Label portLabel = new Label(this, SWT.NONE);
        portLabel.setText(RemoteDeviceNLS.UI_Port);
        portLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, false));

        // add port text
        portText = new Text(this, SWT.BORDER);
        portText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        portText.setText(String.valueOf(port));
        portText.addModifyListener(new PortModifyListener());
        portText.setText(String.valueOf(port));

        // add timeout label
        Label timeoutLabel = new Label(this, SWT.NONE);
        timeoutLabel.setText(RemoteDeviceNLS.UI_Timeout);
        timeoutLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, false));

        // add timeout text
        timeoutText = new Text(this, SWT.BORDER);
        timeoutText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        timeoutText.addModifyListener(new TimeoutModifyListener());
        timeoutText.setText((!initialTimeout.equals(EMPTY_STRING)) ? initialTimeout : String
                .valueOf(RemoteDeviceConstants.DEFAULT_TIMEOUT));

        // manage the case where the entered IP/Port matches of the an existing device
        manageSameIPAndPortOfRemoteDevice();
    }

    /**
     * Add a listener which will be notified when there is a change in the composite
     *  
     * @param wirelessDeviceWizardPage a listener which will be notified when there is a change in the composite
     */
    public void addPropertyChangeListener(WirelessPropertiesChangedListener listener)
    {
        synchronized (listeners)
        {
            listeners.add(listener);
        }
    }

    /**
     * Remove a listener from the list
     * 
     * @param wirelessDeviceWizardPage the listener to be removed
     */
    public void removePropertyChangeListener(WirelessPropertiesChangedListener listener)
    {
        synchronized (listeners)
        {
            listeners.remove(listener);
        }
    }

    /**
     * Get the configured host
     *  
     * @return the configured host
     */
    public String getHost()
    {
        return host;
    }

    /**
     * Get the configured timeout
     *  
     * @return the configured timeout
     */
    public int getTimeout()
    {
        return timeout;
    }

    /**
     * Get the configured port number
     * 
     * @return the configured port number
     */
    public int getPort()
    {
        return port;
    }

    /**
     * Get the status associated with the current state.
     * 
     * @return The {@link IStatus}.
     */
    public IStatus getStatus()
    {
        IStatus status =
                new Status(IStatus.OK, RemoteDevicePlugin.PLUGIN_ID,
                        RemoteDeviceNLS.UI_WirelessInformationPage_Description);
        String errorMessage = null;
        boolean isValidationOK = true;

        if ((name == null) || name.equals(""))
        {
            errorMessage = NAME_ERR_MESSAGE;
            status = new Status(IStatus.ERROR, RemoteDevicePlugin.PLUGIN_ID, errorMessage);
            isValidationOK = false;
        }

        if (isValidationOK && (port < 0))
        {
            errorMessage = PORT_ERR_MESSAGE;
            status = new Status(IStatus.ERROR, RemoteDevicePlugin.PLUGIN_ID, errorMessage);
            isValidationOK = false;
        }
        if ((isValidationOK && (port < RemoteDeviceConstants.MINIMUM_PORT_NUMBER))
                || (port > RemoteDeviceConstants.MAXIMUM_PORT_NUMBER))
        {
            errorMessage =
                    NLS.bind(
                            RemoteDeviceNLS.WirelessPropertiesComposite_MsgPortNumberEqualOrHigherThan,
                            RemoteDeviceConstants.MINIMUM_PORT_NUMBER,
                            RemoteDeviceConstants.MAXIMUM_PORT_NUMBER);
            status = new Status(IStatus.ERROR, RemoteDevicePlugin.PLUGIN_ID, errorMessage);
            isValidationOK = false;
        }
        if (isValidationOK && (timeout < 0))
        {
            errorMessage = TIMEOUT_ERR_MESSAGE;
            status = new Status(IStatus.ERROR, RemoteDevicePlugin.PLUGIN_ID, errorMessage);
            isValidationOK = false;
        }
        if (isValidationOK && (host != null))
        {
            if (host.equals(EMPTY_STRING))
            {
                errorMessage = HOST_ERR_MESSAGE;
                status = new Status(IStatus.ERROR, RemoteDevicePlugin.PLUGIN_ID, errorMessage);
                isValidationOK = false;
            }
        }

        if (isValidationOK)
        {
            // check if host:port already exist
            Collection<ISerialNumbered> existentRemoteDeviceInstances =
                    DevicesManager.getInstance().getInstancesByType(RemoteDeviceInstance.class);
            for (ISerialNumbered device : existentRemoteDeviceInstances)
            {
                if (RemoteDeviceUtils.hasSameHostAndPort(device, host, port))
                {
                    if ((this.device == null)
                            || ((this.device != null) && (!this.device.getDeviceName().equals(
                                    device.getDeviceName()))))
                    {
                        errorMessage =
                                NLS.bind(
                                        RemoteDeviceNLS.ERR_RemoteDeviceWizardPage_WirelessDuplicated,
                                        device.getDeviceName());
                        status =
                                new Status(IStatus.WARNING, RemoteDevicePlugin.PLUGIN_ID,
                                        errorMessage);
                        isValidationOK = false;
                        break;
                    }
                }
            }
        }

        if (isValidationOK && (name != null))
        {
            // this verification is only applied if the IP/Port does not coincide with another application
            if (getRemoteDeviceWithSameIPAndPort() == null)
            {
                InstanceRegistry registry = InstanceRegistry.getInstance();
                if (!name.equals(EMPTY_STRING))
                { //$NON-NLS-1$
                    if (!(registry.getInstancesByName(name).size() == 0))
                    {
                        errorMessage =
                                DeviceUIResources.SEQUOYAH_Emulator_Wizard_Project_Description_Duplicated_Error;
                        status =
                                new Status(IStatus.ERROR, RemoteDevicePlugin.PLUGIN_ID,
                                        errorMessage);
                        isValidationOK = false;
                    }
                    else if (!AbstractMobileInstance.validName(name))
                    {
                        errorMessage = DeviceUIResources.SEQUOYAH_Instance_Name_Invalid_Error;
                        status =
                                new Status(IStatus.ERROR, RemoteDevicePlugin.PLUGIN_ID,
                                        errorMessage);
                        isValidationOK = false;
                    }
                }
            }
        }

        return status;
    }

    /*
     * Notify change listeners that there was a change in the values
     */
    private void notifyListeners()
    {
        synchronized (listeners)
        {
            for (WirelessPropertiesChangedListener listener : listeners)
            {
                listener.propertiesChanged();
            }
        }
    }

    /*
     * Manage the case in which was inputed the same IP and port of
     * an existing remote device.
     */
    private void manageSameIPAndPortOfRemoteDevice()
    {
        ISerialNumbered matchedDevice = getRemoteDeviceWithSameIPAndPort();
        // if there is a device which matches the IP/Port of this wizard, set its Name and disable the field
        if (matchedDevice != null)
        {
            nameText.setText(matchedDevice.getDeviceName());
            nameText.setEnabled(false);
        }
        // since there is no device, enable the name field
        else
        {
            nameText.setEnabled(true);
        }
    }

    /*
     * Returns a device which matches the IP/Port of this wizard,
     * in cas there is any. 
     * 
     * @return get the {@link ISerialNumbered} device which matches
     * the IP/Port of this wizard.
     */
    private ISerialNumbered getRemoteDeviceWithSameIPAndPort()
    {
        ISerialNumbered matchedDevice = null;
        Collection<ISerialNumbered> existentRemoteDeviceInstances =
                DevicesManager.getInstance().getInstancesByType(RemoteDeviceInstance.class);
        for (ISerialNumbered device : existentRemoteDeviceInstances)
        {
            if (RemoteDeviceUtils.hasSameHostAndPort(device, host, port))
            {
                if ((this.device == null)
                        || ((this.device != null) && (!this.device.getDeviceName().equals(
                                device.getDeviceName()))))
                {
                    matchedDevice = device;
                    break;
                }
            }
        }
        return matchedDevice;
    }

    /**
     * Get the {@link IProgressMonitor}
     * 
     * @return Return the monitor
     */
    public IProgressMonitor getProgressMonitor()
    {
        return monitor;
    }

    /**
     * @param monitor the monitor to set
     */
    public void setProgressMonitor(IProgressMonitor monitor)
    {
        this.monitor = monitor;
    }

    /**
     * Get the device name.
     * 
     * @return Device name
     */
    public String getDeviceName()
    {
        return this.name;
    }
}