trace/traceviewer/com.nokia.traceviewer/src/com/nokia/traceviewer/engine/ConnectionHelper.java
changeset 11 5b9d4d8641ce
equal deleted inserted replaced
10:ed1c9f64298a 11:5b9d4d8641ce
       
     1 /*
       
     2  * Copyright (c) 2007-2010 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 "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  * Connection Helper class
       
    17  *
       
    18  */
       
    19 package com.nokia.traceviewer.engine;
       
    20 
       
    21 import org.eclipse.core.runtime.CoreException;
       
    22 import org.eclipse.jface.preference.IPreferenceNode;
       
    23 import org.eclipse.jface.preference.IPreferencePage;
       
    24 import org.eclipse.jface.preference.IPreferenceStore;
       
    25 import org.eclipse.jface.preference.PreferenceDialog;
       
    26 import org.eclipse.jface.preference.PreferenceManager;
       
    27 import org.eclipse.jface.preference.PreferenceNode;
       
    28 import org.eclipse.swt.widgets.Composite;
       
    29 import org.eclipse.ui.PlatformUI;
       
    30 
       
    31 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
       
    32 import com.nokia.carbide.remoteconnections.interfaces.IClientServiceSiteUI2;
       
    33 import com.nokia.carbide.remoteconnections.interfaces.IConnection;
       
    34 import com.nokia.carbide.remoteconnections.interfaces.IService;
       
    35 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.ISelectedConnectionInfo;
       
    36 import com.nokia.traceviewer.TraceViewerPlugin;
       
    37 import com.nokia.traceviewer.action.ConnectAction;
       
    38 import com.nokia.traceviewer.engine.preferences.PreferenceConstants;
       
    39 import com.nokia.traceviewer.engine.preferences.TraceViewerConnectionPreferencesPage;
       
    40 
       
    41 /**
       
    42  * Connection Helper class
       
    43  */
       
    44 public class ConnectionHelper {
       
    45 
       
    46 	/**
       
    47 	 * Current connection identifier
       
    48 	 */
       
    49 	public static String CURRENT_CONNECTION_ID = "com.nokia.carbide.remoteConnections.currentConnection"; //$NON-NLS-1$
       
    50 
       
    51 	/**
       
    52 	 * Client service UI
       
    53 	 */
       
    54 	private static IClientServiceSiteUI2 clientServiceUI = getClientServiceUI();
       
    55 
       
    56 	/**
       
    57 	 * Tells if "Current connection" is selected from TraceViewer preferences
       
    58 	 */
       
    59 	public static boolean isCurrentConnectionSelected;
       
    60 
       
    61 	static {
       
    62 
       
    63 		// Current connection is selected if ID is CURRENT_CONNECTION_ID or
       
    64 		// empty
       
    65 		String currConn = TraceViewerPlugin.getDefault().getPreferenceStore()
       
    66 				.getString(PreferenceConstants.SELECTED_CONNECTION_ID);
       
    67 		isCurrentConnectionSelected = currConn.equals(CURRENT_CONNECTION_ID)
       
    68 				|| currConn.equals(""); //$NON-NLS-1$
       
    69 	}
       
    70 
       
    71 	/**
       
    72 	 * The current connection ensured
       
    73 	 */
       
    74 	public static IConnection currentEnsuredConnection;
       
    75 
       
    76 	/**
       
    77 	 * Gets tracing service
       
    78 	 * 
       
    79 	 * @return tracing service
       
    80 	 */
       
    81 	public static IService getTracingService() {
       
    82 		IService service = RemoteConnectionsActivator
       
    83 				.getConnectionTypeProvider().findServiceByID(
       
    84 						"com.nokia.carbide.trk.support.service.TracingService"); //$NON-NLS-1$	
       
    85 		return service;
       
    86 	}
       
    87 
       
    88 	/**
       
    89 	 * Gets selected connection object
       
    90 	 * 
       
    91 	 * @return selected connection object
       
    92 	 */
       
    93 	public static IConnection getSelectedConnection() {
       
    94 		String selectedConnId = clientServiceUI.getSelectedConnection();
       
    95 
       
    96 		IConnection conn = null;
       
    97 
       
    98 		// Current connection selected
       
    99 		if (selectedConnId != null
       
   100 				&& selectedConnId.equals(CURRENT_CONNECTION_ID)) {
       
   101 			isCurrentConnectionSelected = true;
       
   102 		} else {
       
   103 			isCurrentConnectionSelected = false;
       
   104 			conn = getConnectionWithID(selectedConnId);
       
   105 
       
   106 			// If connection doesn't exist, create ClientUI and select the
       
   107 			// connection
       
   108 			if (conn == null) {
       
   109 				createClientUI();
       
   110 
       
   111 				// Select connection with ID
       
   112 				selectedConnId = TraceViewerPlugin.getDefault()
       
   113 						.getPreferenceStore().getString(
       
   114 								PreferenceConstants.SELECTED_CONNECTION_ID);
       
   115 				clientServiceUI.selectConnection(selectedConnId);
       
   116 				conn = getConnectionWithID(selectedConnId);
       
   117 			}
       
   118 		}
       
   119 
       
   120 		return conn;
       
   121 	}
       
   122 
       
   123 	/**
       
   124 	 * Gets connection with current ID
       
   125 	 * 
       
   126 	 * @return connection
       
   127 	 */
       
   128 	public static IConnection getConnectionWithCurrentID() {
       
   129 		String selectedConnId = TraceViewerPlugin.getDefault()
       
   130 				.getPreferenceStore().getString(
       
   131 						PreferenceConstants.SELECTED_CONNECTION_ID);
       
   132 		IConnection ret = getConnectionWithID(selectedConnId);
       
   133 
       
   134 		return ret;
       
   135 	}
       
   136 
       
   137 	/**
       
   138 	 * Gets connection with ID
       
   139 	 * 
       
   140 	 * @param id
       
   141 	 *            the ID
       
   142 	 * @return connection
       
   143 	 */
       
   144 	public static IConnection getConnectionWithID(String id) {
       
   145 		IConnection ret = RemoteConnectionsActivator.getConnectionsManager()
       
   146 				.findConnection(id);
       
   147 
       
   148 		return ret;
       
   149 	}
       
   150 
       
   151 	/**
       
   152 	 * Gets client service UI
       
   153 	 * 
       
   154 	 * @return client service UI
       
   155 	 */
       
   156 	public static IClientServiceSiteUI2 getClientServiceUI() {
       
   157 		IClientServiceSiteUI2 ui = null;
       
   158 		IService service = getTracingService();
       
   159 
       
   160 		if (service != null) {
       
   161 			ui = RemoteConnectionsActivator.getConnectionsManager()
       
   162 					.getClientSiteUI2(service);
       
   163 		}
       
   164 		return ui;
       
   165 	}
       
   166 
       
   167 	/**
       
   168 	 * Select connection from client UI with ID
       
   169 	 * 
       
   170 	 * @param id
       
   171 	 *            the ID
       
   172 	 */
       
   173 	public static void selectConnectionFromUIWithID(String id) {
       
   174 		if (clientServiceUI != null) {
       
   175 			clientServiceUI.selectConnection(id);
       
   176 		}
       
   177 	}
       
   178 
       
   179 	/**
       
   180 	 * Creates client service UI
       
   181 	 * 
       
   182 	 * @param composite
       
   183 	 *            composite
       
   184 	 * @return composite or null if client service UI couldn't be constructed
       
   185 	 */
       
   186 	public static Composite createClientServiceUI(Composite composite) {
       
   187 		Composite ret = null;
       
   188 		if (clientServiceUI != null) {
       
   189 			clientServiceUI.createComposite(composite);
       
   190 			ret = composite;
       
   191 		}
       
   192 		return ret;
       
   193 	}
       
   194 
       
   195 	/**
       
   196 	 * Creates Client UI
       
   197 	 */
       
   198 	private static void createClientUI() {
       
   199 		PreferenceManager mgr = new PreferenceManager();
       
   200 
       
   201 		// Create connection preference page
       
   202 		IPreferencePage connectionPage = new TraceViewerConnectionPreferencesPage();
       
   203 		IPreferenceNode connectionNode = new PreferenceNode("1", connectionPage); //$NON-NLS-1$
       
   204 		mgr.addToRoot(connectionNode);
       
   205 
       
   206 		// Create the dialog
       
   207 		PreferenceDialog dialog = new PreferenceDialog(PlatformUI
       
   208 				.getWorkbench().getDisplay().getActiveShell(), mgr);
       
   209 		dialog.create();
       
   210 	}
       
   211 
       
   212 	/**
       
   213 	 * Saves connection settings to preference store
       
   214 	 * 
       
   215 	 * @param ensureConnection
       
   216 	 *            if true, connection is ensured. It means that if the saved ID
       
   217 	 *            is not a ID of a "real" connection, we must find a real
       
   218 	 *            connection for this virtual ID
       
   219 	 * @return found IConnection object
       
   220 	 */
       
   221 	public static IConnection saveConnectionSettingsToPreferenceStore(
       
   222 			boolean ensureConnection) {
       
   223 
       
   224 		IPreferenceStore store = TraceViewerPlugin.getDefault()
       
   225 				.getPreferenceStore();
       
   226 
       
   227 		IConnection conn = null;
       
   228 
       
   229 		if (ensureConnection) {
       
   230 			String selectedConnId = clientServiceUI.getSelectedConnection();
       
   231 			if (selectedConnId == null && isCurrentConnectionSelected) {
       
   232 				selectedConnId = CURRENT_CONNECTION_ID;
       
   233 			} else if (selectedConnId == null) {
       
   234 				selectedConnId = store
       
   235 						.getString(PreferenceConstants.SELECTED_CONNECTION_ID);
       
   236 			}
       
   237 
       
   238 			try {
       
   239 				ISelectedConnectionInfo ret = RemoteConnectionsActivator
       
   240 						.getConnectionsManager().ensureConnection(
       
   241 								selectedConnId, getTracingService());
       
   242 				conn = ret.getConnection();
       
   243 			} catch (CoreException e) {
       
   244 				e.printStackTrace();
       
   245 			}
       
   246 
       
   247 		} else {
       
   248 			conn = ConnectionHelper.getSelectedConnection();
       
   249 		}
       
   250 
       
   251 		// Save the currently ensured connection object
       
   252 		currentEnsuredConnection = conn;
       
   253 
       
   254 		if (conn != null
       
   255 				&& PlatformUI.getWorkbench().getDisplay().getActiveShell() != null
       
   256 				&& !PlatformUI.getWorkbench().getDisplay().getActiveShell()
       
   257 						.isDisposed()) {
       
   258 
       
   259 			String connectionTypeId = conn.getConnectionType().getIdentifier();
       
   260 			boolean isMusti = connectionTypeId
       
   261 					.equals("com.nokia.carbide.trk.support.connection.TCPIPConnectionType"); //$NON-NLS-1$
       
   262 
       
   263 			boolean isPlatsim = connectionTypeId
       
   264 					.equals("com.nokia.carbide.trk.support.connection.PlatSimConnectionType"); //$NON-NLS-1$
       
   265 
       
   266 			// TCP / IP connection
       
   267 			if (isMusti || isPlatsim) {
       
   268 				String address = conn.getSettings().get("ipAddress"); //$NON-NLS-1$
       
   269 				String port = conn.getSettings().get("port"); //$NON-NLS-1$
       
   270 				int channel = PreferenceConstants.TCPIP_DEFAULT_CHANNEL;
       
   271 				if (isMusti) {
       
   272 					try {
       
   273 						channel = Integer.parseInt(conn.getSettings().get(
       
   274 								"mustiChannel")); //$NON-NLS-1$
       
   275 					} catch (NumberFormatException e) {
       
   276 						channel = 1;
       
   277 					}
       
   278 				}
       
   279 
       
   280 				// Save Connection type, IP address, port number and channel
       
   281 				store.setValue(PreferenceConstants.CONNECTION_TYPE,
       
   282 						PreferenceConstants.CONNECTION_TYPE_TCPIP);
       
   283 				store.setValue(PreferenceConstants.IP_ADDRESS, address);
       
   284 				store.setValue(PreferenceConstants.TCPIP_PORT, port);
       
   285 				store.setValue(PreferenceConstants.TCPIP_CHANNEL, channel);
       
   286 
       
   287 				// USB connection
       
   288 			} else if (connectionTypeId
       
   289 					.equals("com.nokia.carbide.trk.support.connection.USBConnectionType")) { //$NON-NLS-1$
       
   290 				String portNumStr = conn.getSettings().get("port"); //$NON-NLS-1$
       
   291 				int portNum = Integer.parseInt(portNumStr);
       
   292 
       
   293 				// Save Connection type and port number
       
   294 				store.setValue(PreferenceConstants.CONNECTION_TYPE,
       
   295 						PreferenceConstants.CONNECTION_TYPE_USB_SERIAL);
       
   296 				store
       
   297 						.setValue(PreferenceConstants.USB_SERIAL_COM_PORT,
       
   298 								portNum);
       
   299 
       
   300 			}
       
   301 
       
   302 			// Save selected connection name. Only save the identifier if we
       
   303 			// have not selected the "current connection"
       
   304 			store.setValue(PreferenceConstants.SELECTED_CONNECTION_NAME, conn
       
   305 					.getDisplayName());
       
   306 
       
   307 			if (!isCurrentConnectionSelected) {
       
   308 				store.setValue(PreferenceConstants.SELECTED_CONNECTION_ID, conn
       
   309 						.getIdentifier());
       
   310 			}
       
   311 
       
   312 			// Current connection selected
       
   313 		} else if (isCurrentConnectionSelected) {
       
   314 			store.setValue(PreferenceConstants.CONNECTION_TYPE,
       
   315 					CURRENT_CONNECTION_ID);
       
   316 			store.setValue(PreferenceConstants.SELECTED_CONNECTION_ID,
       
   317 					CURRENT_CONNECTION_ID);
       
   318 			store.setValue(PreferenceConstants.SELECTED_CONNECTION_NAME,
       
   319 					CURRENT_CONNECTION_ID);
       
   320 
       
   321 			// Couldn't find connection
       
   322 		} else {
       
   323 			store.setValue(PreferenceConstants.CONNECTION_TYPE, ""); //$NON-NLS-1$
       
   324 
       
   325 			// Resetting selected connection identifier
       
   326 			store.setValue(PreferenceConstants.SELECTED_CONNECTION_ID, ""); //$NON-NLS-1$
       
   327 			store.setValue(PreferenceConstants.SELECTED_CONNECTION_NAME, ""); //$NON-NLS-1$
       
   328 		}
       
   329 
       
   330 		// Change connection button tooltip
       
   331 		ConnectAction action = (ConnectAction) TraceViewerGlobals
       
   332 				.getTraceViewer().getView().getActionFactory()
       
   333 				.getConnectAction();
       
   334 		action.changeConnectToolTip();
       
   335 
       
   336 		return conn;
       
   337 	}
       
   338 }