diff options
Diffstat (limited to 'eclipse/plugins/com.android.ide.eclipse.monitor/src/com/android/ide/eclipse/monitor/ddms')
3 files changed, 0 insertions, 895 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.monitor/src/com/android/ide/eclipse/monitor/ddms/DebugPortProvider.java b/eclipse/plugins/com.android.ide.eclipse.monitor/src/com/android/ide/eclipse/monitor/ddms/DebugPortProvider.java deleted file mode 100644 index a2ffa042b..000000000 --- a/eclipse/plugins/com.android.ide.eclipse.monitor/src/com/android/ide/eclipse/monitor/ddms/DebugPortProvider.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * 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.ddmlib.DebugPortManager.IDebugPortProvider; -import com.android.ddmlib.IDevice; -import com.android.ide.eclipse.ddms.DdmsPlugin; - -import org.eclipse.jface.preference.IPreferenceStore; - -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -/** - * DDMS implementation of the IDebugPortProvider interface. - * This class handles saving/loading the list of static debug port from - * the preference store and provides the port number to the Device Monitor. - */ -public class DebugPortProvider implements IDebugPortProvider { - - private static DebugPortProvider sThis = new DebugPortProvider(); - - /** Preference name for the static port list. */ - public static final String PREFS_STATIC_PORT_LIST = "android.staticPortList"; //$NON-NLS-1$ - - /** - * Mapping device serial numbers to maps. The embedded maps are mapping application names to - * debugger ports. - */ - private Map<String, Map<String, Integer>> mMap; - - public static DebugPortProvider getInstance() { - return sThis; - } - - private DebugPortProvider() { - computePortList(); - } - - /** - * Returns a static debug port for the specified application running on the - * specified {@link IDevice}. - * @param device The device the application is running on. - * @param appName The application name, as defined in the - * AndroidManifest.xml package attribute. - * @return The static debug port or {@link #NO_STATIC_PORT} if there is none setup. - * - * @see IDebugPortProvider#getPort(IDevice, String) - */ - @Override - public int getPort(IDevice device, String appName) { - if (mMap != null) { - Map<String, Integer> deviceMap = mMap.get(device.getSerialNumber()); - if (deviceMap != null) { - Integer i = deviceMap.get(appName); - if (i != null) { - return i.intValue(); - } - } - } - return IDebugPortProvider.NO_STATIC_PORT; - } - - /** - * Returns the map of Static debugger ports. The map links device serial numbers to - * a map linking application name to debugger ports. - */ - public Map<String, Map<String, Integer>> getPortList() { - return mMap; - } - - /** - * Create the map member from the values contained in the Preference Store. - */ - private void computePortList() { - mMap = new HashMap<String, Map<String, Integer>>(); - - // get the prefs store - IPreferenceStore store = DdmsPlugin.getDefault().getPreferenceStore(); - String value = store.getString(PREFS_STATIC_PORT_LIST); - - if (value != null && value.length() > 0) { - // format is - // port1|port2|port3|... - // where port# is - // appPackageName:appPortNumber:device-serial-number - String[] portSegments = value.split("\\|"); //$NON-NLS-1$ - for (String seg : portSegments) { - String[] entry = seg.split(":"); //$NON-NLS-1$ - - // backward compatibility support. if we have only 2 entry, we default - // to the first emulator. - String deviceName = null; - if (entry.length == 3) { - deviceName = entry[2]; - } else { - deviceName = IDevice.FIRST_EMULATOR_SN; - } - - // get the device map - Map<String, Integer> deviceMap = mMap.get(deviceName); - if (deviceMap == null) { - deviceMap = new HashMap<String, Integer>(); - mMap.put(deviceName, deviceMap); - } - - deviceMap.put(entry[0], Integer.valueOf(entry[1])); - } - } - } - - /** - * Sets new [device, app, port] values. - * The values are also sync'ed in the preference store. - * @param map The map containing the new values. - */ - public void setPortList(Map<String, Map<String,Integer>> map) { - // update the member map. - mMap.clear(); - mMap.putAll(map); - - // create the value to store in the preference store. - // see format definition in getPortList - StringBuilder sb = new StringBuilder(); - - 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) { - sb.append(appKey).append(':').append(port.intValue()).append(':'). - append(deviceKey).append('|'); - } - } - } - } - - String value = sb.toString(); - - // get the prefs store. - IPreferenceStore store = DdmsPlugin.getDefault().getPreferenceStore(); - - // and give it the new value. - store.setValue(PREFS_STATIC_PORT_LIST, value); - } -} diff --git a/eclipse/plugins/com.android.ide.eclipse.monitor/src/com/android/ide/eclipse/monitor/ddms/StaticPortConfigDialog.java b/eclipse/plugins/com.android.ide.eclipse.monitor/src/com/android/ide/eclipse/monitor/ddms/StaticPortConfigDialog.java deleted file mode 100644 index 456cdcf51..000000000 --- a/eclipse/plugins/com.android.ide.eclipse.monitor/src/com/android/ide/eclipse/monitor/ddms/StaticPortConfigDialog.java +++ /dev/null @@ -1,396 +0,0 @@ -/* - * 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); - } -} diff --git a/eclipse/plugins/com.android.ide.eclipse.monitor/src/com/android/ide/eclipse/monitor/ddms/StaticPortEditDialog.java b/eclipse/plugins/com.android.ide.eclipse.monitor/src/com/android/ide/eclipse/monitor/ddms/StaticPortEditDialog.java deleted file mode 100644 index a9b0cd45e..000000000 --- a/eclipse/plugins/com.android.ide.eclipse.monitor/src/com/android/ide/eclipse/monitor/ddms/StaticPortEditDialog.java +++ /dev/null @@ -1,334 +0,0 @@ -/* - * 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.ddmlib.IDevice; - -import org.eclipse.swt.SWT; -import org.eclipse.swt.events.ModifyEvent; -import org.eclipse.swt.events.ModifyListener; -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.Label; -import org.eclipse.swt.widgets.Listener; -import org.eclipse.swt.widgets.Shell; -import org.eclipse.swt.widgets.Text; - -import java.util.ArrayList; - -/** - * Small dialog box to edit a static port number. - */ -public class StaticPortEditDialog extends Dialog { - - private static final int DLG_WIDTH = 400; - private static final int DLG_HEIGHT = 200; - - private Shell mParent; - - private Shell mShell; - - private boolean mOk = false; - - private String mAppName; - - private String mPortNumber; - - private Button mOkButton; - - private Label mWarning; - - /** List of ports already in use */ - private ArrayList<Integer> mPorts; - - /** This is the port being edited. */ - private int mEditPort = -1; - private String mDeviceSn; - - /** - * Creates a dialog with empty fields. - * @param parent The parent Shell - * @param ports The list of already used port numbers. - */ - public StaticPortEditDialog(Shell parent, ArrayList<Integer> ports) { - super(parent, SWT.DIALOG_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL); - mPorts = ports; - mDeviceSn = IDevice.FIRST_EMULATOR_SN; - } - - /** - * Creates a dialog with predefined values. - * @param shell The parent shell - * @param ports The list of already used port numbers. - * @param oldDeviceSN the device serial number to display - * @param oldAppName The application name to display - * @param oldPortNumber The port number to display - */ - public StaticPortEditDialog(Shell shell, ArrayList<Integer> ports, - String oldDeviceSN, String oldAppName, String oldPortNumber) { - this(shell, ports); - - mDeviceSn = oldDeviceSN; - mAppName = oldAppName; - mPortNumber = oldPortNumber; - mEditPort = Integer.valueOf(mPortNumber); - } - - /** - * Opens the dialog. The method will return when the user closes the dialog - * somehow. - * - * @return true if ok was pressed, false if cancelled. - */ - public boolean open() { - createUI(); - - if (mParent == null || mShell == null) { - return false; - } - - 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.open(); - - Display display = mParent.getDisplay(); - while (!mShell.isDisposed()) { - if (!display.readAndDispatch()) - display.sleep(); - } - - return mOk; - } - - public String getDeviceSN() { - return mDeviceSn; - } - - public String getAppName() { - return mAppName; - } - - public int getPortNumber() { - return Integer.valueOf(mPortNumber); - } - - private void createUI() { - mParent = getParent(); - mShell = new Shell(mParent, getStyle()); - mShell.setText("Static Port"); - - mShell.setLayout(new GridLayout(1, false)); - - mShell.addListener(SWT.Close, new Listener() { - @Override - public void handleEvent(Event event) { - } - }); - - // center part with the edit field - Composite main = new Composite(mShell, SWT.NONE); - main.setLayoutData(new GridData(GridData.FILL_BOTH)); - main.setLayout(new GridLayout(2, false)); - - Label l0 = new Label(main, SWT.NONE); - l0.setText("Device Name:"); - - final Text deviceSNText = new Text(main, SWT.SINGLE | SWT.BORDER); - deviceSNText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); - if (mDeviceSn != null) { - deviceSNText.setText(mDeviceSn); - } - deviceSNText.addModifyListener(new ModifyListener() { - @Override - public void modifyText(ModifyEvent e) { - mDeviceSn = deviceSNText.getText().trim(); - validate(); - } - }); - - Label l = new Label(main, SWT.NONE); - l.setText("Application Name:"); - - final Text appNameText = new Text(main, SWT.SINGLE | SWT.BORDER); - if (mAppName != null) { - appNameText.setText(mAppName); - } - appNameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); - appNameText.addModifyListener(new ModifyListener() { - @Override - public void modifyText(ModifyEvent e) { - mAppName = appNameText.getText().trim(); - validate(); - } - }); - - Label l2 = new Label(main, SWT.NONE); - l2.setText("Debug Port:"); - - final Text debugPortText = new Text(main, SWT.SINGLE | SWT.BORDER); - if (mPortNumber != null) { - debugPortText.setText(mPortNumber); - } - debugPortText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); - debugPortText.addModifyListener(new ModifyListener() { - @Override - public void modifyText(ModifyEvent e) { - mPortNumber = debugPortText.getText().trim(); - validate(); - } - }); - - // warning label - Composite warningComp = new Composite(mShell, SWT.NONE); - warningComp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); - warningComp.setLayout(new GridLayout(1, true)); - - mWarning = new Label(warningComp, SWT.NONE); - mWarning.setText(""); - mWarning.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); - - // 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)); - - mOkButton = new Button(bottomComp, SWT.NONE); - mOkButton.setText("OK"); - mOkButton.addSelectionListener(new SelectionAdapter() { - @Override - public void widgetSelected(SelectionEvent e) { - mOk = true; - mShell.close(); - } - }); - mOkButton.setEnabled(false); - mShell.setDefaultButton(mOkButton); - - Button cancelButton = new Button(bottomComp, SWT.NONE); - cancelButton.setText("Cancel"); - cancelButton.addSelectionListener(new SelectionAdapter() { - @Override - public void widgetSelected(SelectionEvent e) { - mShell.close(); - } - }); - - validate(); - } - - /** - * Validates the content of the 2 text fields and enable/disable "ok", while - * setting up the warning/error message. - */ - private void validate() { - // first we reset the warning dialog. This allows us to latter - // display warnings. - mWarning.setText(""); //$NON-NLS-1$ - - // check the device name field is not empty - if (mDeviceSn == null || mDeviceSn.length() == 0) { - mWarning.setText("Device name missing."); - mOkButton.setEnabled(false); - return; - } - - // check the application name field is not empty - if (mAppName == null || mAppName.length() == 0) { - mWarning.setText("Application name missing."); - mOkButton.setEnabled(false); - return; - } - - String packageError = "Application name must be a valid Java package name."; - - // validate the package name as well. It must be a fully qualified - // java package. - String[] packageSegments = mAppName.split("\\."); //$NON-NLS-1$ - for (String p : packageSegments) { - if (p.matches("^[a-zA-Z][a-zA-Z0-9]*") == false) { //$NON-NLS-1$ - mWarning.setText(packageError); - mOkButton.setEnabled(false); - return; - } - - // lets also display a warning if the package contains upper case - // letters. - if (p.matches("^[a-z][a-z0-9]*") == false) { //$NON-NLS-1$ - mWarning.setText("Lower case is recommended for Java packages."); - } - } - - // the split will not detect the last char being a '.' - // so we test it manually - if (mAppName.charAt(mAppName.length()-1) == '.') { - mWarning.setText(packageError); - mOkButton.setEnabled(false); - return; - } - - // now we test the package name field is not empty. - if (mPortNumber == null || mPortNumber.length() == 0) { - mWarning.setText("Port Number missing."); - mOkButton.setEnabled(false); - return; - } - - // then we check it only contains digits. - if (mPortNumber.matches("[0-9]*") == false) { //$NON-NLS-1$ - mWarning.setText("Port Number invalid."); - mOkButton.setEnabled(false); - return; - } - - // get the int from the port number to validate - long port = Long.valueOf(mPortNumber); - if (port >= 32767) { - mOkButton.setEnabled(false); - return; - } - - // check if its in the list of already used ports - if (port != mEditPort) { - for (Integer i : mPorts) { - if (port == i.intValue()) { - mWarning.setText("Port already in use."); - mOkButton.setEnabled(false); - return; - } - } - } - - // at this point there's not error, so we enable the ok button. - mOkButton.setEnabled(true); - } -} |