trace/traceviewer/com.nokia.traceviewer/src/com/nokia/traceviewer/engine/ConnectionChangedListener.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 changed listener
       
    17  *
       
    18  */
       
    19 package com.nokia.traceviewer.engine;
       
    20 
       
    21 import java.util.Collection;
       
    22 
       
    23 import org.eclipse.swt.widgets.Display;
       
    24 
       
    25 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
       
    26 import com.nokia.carbide.remoteconnections.interfaces.IConnection;
       
    27 import com.nokia.carbide.remoteconnections.interfaces.IConnectionTypeProvider;
       
    28 import com.nokia.carbide.remoteconnections.interfaces.IService;
       
    29 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener;
       
    30 import com.nokia.carbide.remoteconnections.internal.api.IConnection2;
       
    31 import com.nokia.traceviewer.TraceViewerPlugin;
       
    32 import com.nokia.traceviewer.action.ConnectAction;
       
    33 import com.nokia.traceviewer.engine.preferences.PreferenceConstants;
       
    34 import com.nokia.traceviewer.internal.api.TraceViewerAPI2Impl;
       
    35 
       
    36 /**
       
    37  * Connection changed listener
       
    38  */
       
    39 class ConnectionChangedListener implements IConnectionListener {
       
    40 
       
    41 	/*
       
    42 	 * (non-Javadoc)
       
    43 	 * 
       
    44 	 * @seecom.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.
       
    45 	 * IConnectionListener
       
    46 	 * #connectionAdded(com.nokia.carbide.remoteconnections.interfaces
       
    47 	 * .IConnection)
       
    48 	 */
       
    49 	public void connectionAdded(IConnection arg0) {
       
    50 	}
       
    51 
       
    52 	/*
       
    53 	 * (non-Javadoc)
       
    54 	 * 
       
    55 	 * @seecom.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.
       
    56 	 * IConnectionListener
       
    57 	 * #connectionRemoved(com.nokia.carbide.remoteconnections.
       
    58 	 * interfaces.IConnection)
       
    59 	 */
       
    60 	public void connectionRemoved(IConnection arg0) {
       
    61 	}
       
    62 
       
    63 	/*
       
    64 	 * (non-Javadoc)
       
    65 	 * 
       
    66 	 * @seecom.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.
       
    67 	 * IConnectionListener
       
    68 	 * #currentConnectionSet(com.nokia.carbide.remoteconnections
       
    69 	 * .interfaces.IConnection)
       
    70 	 */
       
    71 	public void currentConnectionSet(IConnection newConnection) {
       
    72 		if (ConnectionHelper.isCurrentConnectionSelected
       
    73 				&& newConnection != null
       
    74 				&& supportsTracingService(newConnection)) {
       
    75 
       
    76 			// Save new settings
       
    77 			if (TraceViewerGlobals.getTraceViewer().getConnection() == null
       
    78 					|| !TraceViewerGlobals.getTraceViewer().getConnection()
       
    79 							.isConnected()) {
       
    80 				saveNewSettings();
       
    81 
       
    82 				// Notify about connection preferences changed to listeners
       
    83 				TraceViewerAPI2Impl.notifyConnPrefsChanged(newConnection);
       
    84 
       
    85 				// If auto connect is on
       
    86 				if (TraceViewerPlugin
       
    87 						.getDefault()
       
    88 						.getPreferenceStore()
       
    89 						.getBoolean(
       
    90 								PreferenceConstants.AUTO_CONNECT_DYNAMIC_CONNECTIONS_CHECKBOX)) {
       
    91 
       
    92 					// IConnection2 object, dynamic connection and TraceViewer
       
    93 					// view exists
       
    94 					if (newConnection instanceof IConnection2) {
       
    95 						if (((IConnection2) newConnection).isDynamic()
       
    96 								&& TraceViewerGlobals.getTraceViewer()
       
    97 										.getView() != null) {
       
    98 							connect();
       
    99 						}
       
   100 					}
       
   101 				}
       
   102 			}
       
   103 		}
       
   104 	}
       
   105 
       
   106 	/**
       
   107 	 * Saves new settings
       
   108 	 */
       
   109 	private void saveNewSettings() {
       
   110 		// Change connect tooltip
       
   111 		final ConnectAction action = (ConnectAction) TraceViewerGlobals
       
   112 				.getTraceViewer().getView().getActionFactory()
       
   113 				.getConnectAction();
       
   114 
       
   115 		// UI thread
       
   116 		if (Display.getCurrent() != null) {
       
   117 			ConnectionHelper.saveConnectionSettingsToPreferenceStore(true);
       
   118 			action.changeConnectToolTip();
       
   119 
       
   120 			// Not in UI thread, sync
       
   121 		} else {
       
   122 			Display.getDefault().syncExec(new Runnable() {
       
   123 
       
   124 				/*
       
   125 				 * (non-Javadoc)
       
   126 				 * 
       
   127 				 * @see java.lang.Runnable#run()
       
   128 				 */
       
   129 				public void run() {
       
   130 					ConnectionHelper
       
   131 							.saveConnectionSettingsToPreferenceStore(true);
       
   132 					action.changeConnectToolTip();
       
   133 				}
       
   134 
       
   135 			});
       
   136 		}
       
   137 
       
   138 	}
       
   139 
       
   140 	/**
       
   141 	 * Connects
       
   142 	 */
       
   143 	private void connect() {
       
   144 
       
   145 		// Change connect tooltip
       
   146 		final ConnectAction action = (ConnectAction) TraceViewerGlobals
       
   147 				.getTraceViewer().getView().getActionFactory()
       
   148 				.getConnectAction();
       
   149 
       
   150 		// UI thread
       
   151 		if (Display.getCurrent() != null) {
       
   152 			action.run();
       
   153 
       
   154 			// Not in UI thread, sync
       
   155 		} else {
       
   156 			Display.getDefault().syncExec(new Runnable() {
       
   157 
       
   158 				/*
       
   159 				 * (non-Javadoc)
       
   160 				 * 
       
   161 				 * @see java.lang.Runnable#run()
       
   162 				 */
       
   163 				public void run() {
       
   164 					action.run();
       
   165 				}
       
   166 
       
   167 			});
       
   168 		}
       
   169 	}
       
   170 
       
   171 	/**
       
   172 	 * Checks if the connection supports tracing service
       
   173 	 * 
       
   174 	 * @param connection
       
   175 	 *            connection
       
   176 	 * @return true if the connection supports tracing service
       
   177 	 */
       
   178 	private boolean supportsTracingService(IConnection connection) {
       
   179 		boolean supports = false;
       
   180 		IConnectionTypeProvider provider = RemoteConnectionsActivator
       
   181 				.getConnectionTypeProvider();
       
   182 		Collection<IService> services = provider
       
   183 				.getCompatibleServices(connection.getConnectionType());
       
   184 		for (IService service : services) {
       
   185 			if (service.getIdentifier().equals(
       
   186 					"com.nokia.carbide.trk.support.service.TracingService")) { //$NON-NLS-1$	
       
   187 				supports = true;
       
   188 				break;
       
   189 			}
       
   190 		}
       
   191 
       
   192 		return supports;
       
   193 	}
       
   194 
       
   195 }