debuggercdi/com.nokia.carbide.trk.support/src/com/nokia/carbide/trk/support/connection/TCPIPPortMappingConnectionFactory.java
changeset 164 88b7eb94b003
child 165 cffbaf033837
equal deleted inserted replaced
163:b25acbfc406a 164:88b7eb94b003
       
     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 com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
       
    21 import com.nokia.carbide.remoteconnections.interfaces.*;
       
    22 
       
    23 import org.eclipse.jface.layout.GridDataFactory;
       
    24 import org.eclipse.jface.viewers.*;
       
    25 import org.eclipse.swt.SWT;
       
    26 import org.eclipse.swt.graphics.Image;
       
    27 import org.eclipse.swt.widgets.*;
       
    28 
       
    29 import java.util.*;
       
    30 import java.util.List;
       
    31 
       
    32 public class TCPIPPortMappingConnectionFactory extends TCPIPConnectionFactory {
       
    33 
       
    34 	private final class PortColumnEditingSupport extends EditingSupport {
       
    35 		private TextCellEditor editor;
       
    36 
       
    37 		private PortColumnEditingSupport(ColumnViewer viewer) {
       
    38 			super(viewer);
       
    39 			editor = new TextCellEditor((Composite) viewer.getControl(), SWT.NONE);
       
    40 		}
       
    41 
       
    42 		@Override
       
    43 		protected boolean canEdit(Object element) {
       
    44 			return true;
       
    45 		}
       
    46 
       
    47 		@Override
       
    48 		protected CellEditor getCellEditor(Object element) {
       
    49 			return editor;
       
    50 		}
       
    51 
       
    52 		@Override
       
    53 		protected Object getValue(Object element) {
       
    54 			return serviceIdToPortMappings.get(element.toString()).toString();
       
    55 		}
       
    56 
       
    57 		@Override
       
    58 		protected void setValue(Object element, Object value) {
       
    59 			Integer integer = getValidValue((String) value);
       
    60 			if (integer != null) {
       
    61 				serviceIdToPortMappings.put(element.toString(), integer);
       
    62 				viewer.refresh(true);
       
    63 				packColumns();
       
    64 			}
       
    65 		}
       
    66 
       
    67 	}
       
    68 
       
    69 	private final class TableLabelProvider implements ITableLabelProvider {
       
    70 		public Image getColumnImage(Object element, int columnIndex) {
       
    71 			return null;
       
    72 		}
       
    73 
       
    74 		public String getColumnText(Object element, int columnIndex) {
       
    75 			String serviceId = element.toString();
       
    76 			if (columnIndex == SERVICE_COLUMN) {
       
    77 				IService service = 
       
    78 					RemoteConnectionsActivator.getConnectionTypeProvider().findServiceByID(serviceId);
       
    79 				return service.getDisplayName();
       
    80 			}
       
    81 			else if (columnIndex == PORT_COLUMN) {
       
    82 				return String.valueOf(serviceIdToPortMappings.get(serviceId).intValue());
       
    83 			}
       
    84 			return null;
       
    85 		}
       
    86 
       
    87 		public void addListener(ILabelProviderListener listener) {
       
    88 		}
       
    89 
       
    90 		public void dispose() {
       
    91 		}
       
    92 
       
    93 		public boolean isLabelProperty(Object element, String property) {
       
    94 			return false;
       
    95 		}
       
    96 
       
    97 		public void removeListener(ILabelProviderListener listener) {
       
    98 		}
       
    99 	}
       
   100 
       
   101 	private static final int MIN_DYN_PORT_NUM = 49152;
       
   102 	private static final int MAX_DYN_PORT_NUM = 65535;
       
   103 	private static final int SERVICE_COLUMN = 0;
       
   104 	private static final int PORT_COLUMN = 1;
       
   105 	private Map<String, Integer> serviceIdToPortMappings;
       
   106 	private TableViewer viewer;
       
   107 	
       
   108 	public TCPIPPortMappingConnectionFactory(IConnectionType connectionType) {
       
   109 		super(connectionType);
       
   110 		serviceIdToPortMappings = new HashMap<String, Integer>();
       
   111 		initializePortMappings();
       
   112 	}
       
   113 
       
   114 	private void initializePortMappings() {
       
   115 		Collection<IService> services = RemoteConnectionsActivator.getConnectionTypeProvider().getCompatibleServices(connectionType);
       
   116 		for (IService service : services) {
       
   117 			IService2 service2 = service instanceof IService2 ? (IService2) service : null;
       
   118 			Integer defaultPort = null;
       
   119 			if (service2 != null) {
       
   120 				try {
       
   121 					String defaultPortString = service2.getDefaults().get(IP_PORT);
       
   122 					defaultPort = Integer.valueOf(defaultPortString);
       
   123 				} catch (NumberFormatException e) {
       
   124 					// if it doesn't parse as an int, we ignore it and provide a dynamic default
       
   125 				}
       
   126 			}
       
   127 			if (defaultPort != null)
       
   128 				serviceIdToPortMappings.put(service.getIdentifier(), defaultPort);
       
   129 		}
       
   130 		for (IService service : services) {
       
   131 			String identifier = service.getIdentifier();
       
   132 			if (!serviceIdToPortMappings.containsKey(identifier))
       
   133 				serviceIdToPortMappings.put(identifier, getUnusedDynamicDefault());
       
   134 		}
       
   135 		
       
   136 	}
       
   137 
       
   138 	private Integer getUnusedDynamicDefault() {
       
   139 		for (Integer val = MIN_DYN_PORT_NUM; val < MAX_DYN_PORT_NUM; val++) {
       
   140 			if (!serviceIdToPortMappings.containsValue(val))
       
   141 				return val;
       
   142 		}
       
   143 		return -1; // should never get here!!!
       
   144 	}
       
   145 
       
   146 	@Override
       
   147 	public IConnection createConnection(Map<String, String> settings) {
       
   148 		if (settings == null)
       
   149 			settings = getSettingsFromUI();
       
   150 		return new TCPIPPortMappingConnection(connectionType, settings);
       
   151 	}
       
   152 
       
   153 	@Override
       
   154 	public void createEditingUI(Composite parent, IValidationErrorReporter errorReporter, Map<String, String> initialSettings) {
       
   155 		super.createEditingUI(parent, errorReporter, initialSettings);
       
   156 		updatePortMappings(initialSettings);
       
   157 		Label label = new Label(composite, SWT.NONE);
       
   158 		GridDataFactory.defaultsFor(label).span(2, 1).applyTo(label);
       
   159 		label.setText("Service to port mappings");
       
   160 	
       
   161 		viewer = new TableViewer(composite, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
       
   162 		viewer.setContentProvider(new ArrayContentProvider());
       
   163 		
       
   164 		TableViewerColumn serviceColumn = new TableViewerColumn(viewer, SWT.LEFT);
       
   165 		serviceColumn.getColumn().setText("Service");
       
   166 		
       
   167 		TableViewerColumn portColumn = new TableViewerColumn(viewer, SWT.RIGHT);
       
   168 		portColumn.getColumn().setText("Port");
       
   169 		portColumn.setEditingSupport(new PortColumnEditingSupport(viewer));
       
   170 		
       
   171 		viewer.setLabelProvider(new TableLabelProvider());
       
   172 		viewer.setInput(serviceIdToPortMappings.keySet());
       
   173 		
       
   174 		Table table = viewer.getTable();
       
   175 		table.setHeaderVisible(true);
       
   176 		GridDataFactory.defaultsFor(table).span(2, 1).hint(SWT.DEFAULT, 60).grab(true, false).applyTo(table);
       
   177 		table.setToolTipText("Service to port mappings for this connection. Port settings are user editable");
       
   178 		table.setData(UID, "TCPIPPortMappingConnectionFactory.table"); //$NON-NLS-1$
       
   179 		table.setData("viewer", viewer);
       
   180 		packColumns();
       
   181 	}
       
   182 
       
   183 	private Integer getValidValue(String value) {
       
   184 		try {
       
   185 			int intVal = Integer.parseInt(value);
       
   186 			if (intVal < 0)
       
   187 				return new Integer(0);
       
   188 			else if (intVal > MAX_DYN_PORT_NUM)
       
   189 				return new Integer(MAX_DYN_PORT_NUM);
       
   190 			else
       
   191 				return new Integer(intVal);
       
   192 		} catch (Exception e) {
       
   193 		}
       
   194 		return null;
       
   195 	}
       
   196 
       
   197 	private void updatePortMappings(Map<String, String> initialSettings) {
       
   198 		if (initialSettings != null) {
       
   199 			List<String> serviceIds = new ArrayList<String>(serviceIdToPortMappings.keySet());
       
   200 			for (String serviceId : serviceIds) {
       
   201 				String value = initialSettings.get(serviceId);
       
   202 				Integer validValue = getValidValue(value);
       
   203 				if (validValue != null)
       
   204 					serviceIdToPortMappings.put(serviceId, validValue);
       
   205 			}
       
   206 		}
       
   207 		
       
   208 	}
       
   209 
       
   210 	@Override
       
   211 	public Map<String, String> getSettingsFromUI() {
       
   212 		Map<String, String> settings = super.getSettingsFromUI();
       
   213 		addMappings(settings);
       
   214 		return settings;
       
   215 	}
       
   216 
       
   217 	private void addMappings(Map<String, String> settings) {
       
   218 		for (String serviceId : serviceIdToPortMappings.keySet()) {
       
   219 			settings.put(serviceId, String.valueOf(serviceIdToPortMappings.get(serviceId).intValue()));
       
   220 		}
       
   221 		settings.remove(IP_PORT);
       
   222 	}
       
   223 
       
   224 	private void packColumns() {
       
   225 		TableColumn[] columns = viewer.getTable().getColumns();
       
   226 		for (TableColumn tableColumn : columns) {
       
   227 			tableColumn.pack();
       
   228 		}
       
   229 	}
       
   230 }