debuggercdi/com.nokia.carbide.trk.support/src/com/nokia/carbide/trk/support/connection/TCFConnectionFactory.java
branchC3_BUILDER_WORK
changeset 1657 03f5f8bf29b4
parent 1656 d1edeecb12af
parent 1655 e17ab99b8f30
child 1659 48d0d704d5e6
equal deleted inserted replaced
1656:d1edeecb12af 1657:03f5f8bf29b4
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.carbide.trk.support.connection;
       
    19 
       
    20 import java.util.Collection;
       
    21 import java.util.HashMap;
       
    22 import java.util.Map;
       
    23 
       
    24 import org.eclipse.jface.viewers.TableViewer;
       
    25 import org.eclipse.swt.widgets.Composite;
       
    26 
       
    27 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
       
    28 import com.nokia.carbide.remoteconnections.interfaces.IConnection;
       
    29 import com.nokia.carbide.remoteconnections.interfaces.IConnectionType;
       
    30 import com.nokia.carbide.remoteconnections.interfaces.IService;
       
    31 import com.nokia.carbide.remoteconnections.interfaces.IService2;
       
    32 
       
    33 public class TCFConnectionFactory extends TCPIPConnectionFactory {
       
    34 
       
    35 	private static final int MIN_DYN_PORT_NUM = 49152;
       
    36 	private static final int MAX_DYN_PORT_NUM = 65535;
       
    37 	private Map<String, Integer> serviceIdToPortMappings;
       
    38 	private TableViewer viewer;
       
    39 	
       
    40 	public TCFConnectionFactory(IConnectionType connectionType) {
       
    41 		super(connectionType);
       
    42 		serviceIdToPortMappings = new HashMap<String, Integer>();
       
    43 		initializePortMappings();
       
    44 	}
       
    45 
       
    46 	private void initializePortMappings() {
       
    47 		Collection<IService> services = RemoteConnectionsActivator.getConnectionTypeProvider().getCompatibleServices(connectionType);
       
    48 		for (IService service : services) {
       
    49 			IService2 service2 = service instanceof IService2 ? (IService2) service : null;
       
    50 			Integer defaultPort = null;
       
    51 			if (service2 != null) {
       
    52 				try {
       
    53 					String defaultPortString = service2.getDefaults().get(IP_PORT);
       
    54 					defaultPort = Integer.valueOf(defaultPortString);
       
    55 				} catch (NumberFormatException e) {
       
    56 					// if it doesn't parse as an int, we ignore it and provide a dynamic default
       
    57 				}
       
    58 			}
       
    59 			if (defaultPort != null)
       
    60 				serviceIdToPortMappings.put(service.getIdentifier(), defaultPort);
       
    61 		}
       
    62 		for (IService service : services) {
       
    63 			String identifier = service.getIdentifier();
       
    64 			if (!serviceIdToPortMappings.containsKey(identifier))
       
    65 				serviceIdToPortMappings.put(identifier, getUnusedDynamicDefault());
       
    66 		}
       
    67 		
       
    68 	}
       
    69 
       
    70 	private Integer getUnusedDynamicDefault() {
       
    71 		for (Integer val = MIN_DYN_PORT_NUM; val < MAX_DYN_PORT_NUM; val++) {
       
    72 			if (!serviceIdToPortMappings.containsValue(val))
       
    73 				return val;
       
    74 		}
       
    75 		return -1; // should never get here!!!
       
    76 	}
       
    77 
       
    78 	@Override
       
    79 	public IConnection createConnection(Map<String, String> settings) {
       
    80 		if (settings == null)
       
    81 			settings = getSettingsFromUI();
       
    82 		return new TCFConnection(connectionType, settings);
       
    83 	}
       
    84 
       
    85 	@Override
       
    86 	public void createEditingUI(Composite parent, IValidationErrorReporter errorReporter, Map<String, String> initialSettings) {
       
    87 		super.createEditingUI(parent, errorReporter, initialSettings);
       
    88 /*		updatePortMappings(initialSettings);
       
    89 		Label label = new Label(composite, SWT.NONE);
       
    90 		GridDataFactory.defaultsFor(label).span(2, 1).applyTo(label);
       
    91 		label.setText(Messages.getString("TCPIPPortMappingConnectionFactory.ViewerLabel")); //$NON-NLS-1$
       
    92 	
       
    93 		viewer = new TableViewer(composite, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
       
    94 		viewer.setContentProvider(new ArrayContentProvider());
       
    95 		
       
    96 		TableViewerColumn serviceColumn = new TableViewerColumn(viewer, SWT.LEFT);
       
    97 		serviceColumn.getColumn().setText(Messages.getString("TCPIPPortMappingConnectionFactory.ServiceHeader")); //$NON-NLS-1$
       
    98 		
       
    99 		TableViewerColumn portColumn = new TableViewerColumn(viewer, SWT.RIGHT);
       
   100 		portColumn.getColumn().setText(Messages.getString("TCPIPPortMappingConnectionFactory.PortHeader")); //$NON-NLS-1$
       
   101 		portColumn.setEditingSupport(new PortColumnEditingSupport(viewer));
       
   102 		
       
   103 		viewer.setLabelProvider(new TableLabelProvider());
       
   104 		viewer.setInput(serviceIdToPortMappings.keySet());
       
   105 		
       
   106 		Table table = viewer.getTable();
       
   107 		table.setHeaderVisible(true);
       
   108 		GridDataFactory.defaultsFor(table).span(2, 1).hint(SWT.DEFAULT, 60).grab(true, false).applyTo(table);
       
   109 		table.setToolTipText(Messages.getString("TCPIPPortMappingConnectionFactory.ViewerTooltip")); //$NON-NLS-1$
       
   110 		table.setData(UID, "TCPIPPortMappingConnectionFactory.table"); //$NON-NLS-1$
       
   111 		table.setData("viewer", viewer); //$NON-NLS-1$
       
   112 		packColumns();
       
   113 */	}
       
   114 
       
   115 	private Integer getValidValue(String value) {
       
   116 		try {
       
   117 			int intVal = Integer.parseInt(value);
       
   118 			if (intVal < 0)
       
   119 				return new Integer(0);
       
   120 			else if (intVal > MAX_DYN_PORT_NUM)
       
   121 				return new Integer(MAX_DYN_PORT_NUM);
       
   122 			else
       
   123 				return new Integer(intVal);
       
   124 		} catch (Exception e) {
       
   125 		}
       
   126 		return null;
       
   127 	}
       
   128 
       
   129 	@Override
       
   130 	public Map<String, String> getSettingsFromUI() {
       
   131 		Map<String, String> settings = super.getSettingsFromUI();
       
   132 		addMappings(settings);
       
   133 		return settings;
       
   134 	}
       
   135 
       
   136 	private void addMappings(Map<String, String> settings) {
       
   137 		for (String serviceId : serviceIdToPortMappings.keySet()) {
       
   138 			settings.put(serviceId, String.valueOf(serviceIdToPortMappings.get(serviceId).intValue()));
       
   139 		}
       
   140 		settings.remove(IP_PORT);
       
   141 	}
       
   142 
       
   143 }