debuggercdi/com.nokia.carbide.trk.support/src/com/nokia/carbide/trk/support/status/ConnectionStatusReconciler.java
author Ed Swartz <ed.swartz@nokia.com>
Thu, 11 Feb 2010 10:41:44 -0600
changeset 924 657e34ac671f
parent 848 679300bad088
child 961 8c95e3acb374
permissions -rw-r--r--
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
     1
/**
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
     2
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
     3
* All rights reserved.
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
     4
* This component and the accompanying materials are made available
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
     6
* which accompanies this distribution, and is available
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
     8
*
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
     9
* Initial Contributors:
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    11
*
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    12
* Contributors:
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    13
*
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    14
* Description: 
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    15
*
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    16
*/
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    17
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    18
package com.nokia.carbide.trk.support.status;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    19
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    20
import java.util.ArrayList;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    21
import java.util.List;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    22
924
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
    23
import org.eclipse.swt.widgets.Display;
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
    24
import org.eclipse.swt.widgets.Shell;
848
679300bad088 Show connections view when new connection is added
dadubrow
parents: 770
diff changeset
    25
import org.eclipse.ui.PartInitException;
679300bad088 Show connections view when new connection is added
dadubrow
parents: 770
diff changeset
    26
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    27
import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    28
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    29
import com.nokia.carbide.remoteconnections.interfaces.IConnection;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    30
import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    31
import com.nokia.carbide.remoteconnections.interfaces.AbstractConnection.ConnectionStatus;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    32
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    33
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatusChangedListener;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    34
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus.EStatus;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    35
import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    36
import com.nokia.carbide.remoteconnections.internal.api.IConnection2;
763
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
    37
import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus;
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    38
import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus.EConnectionStatus;
749
22f0d10fce10 Externalize strings
dadubrow
parents: 748
diff changeset
    39
import com.nokia.carbide.trk.support.Messages;
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    40
import com.nokia.carbide.trk.support.connection.USBConnectionType;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    41
import com.nokia.carbide.trk.support.service.TRKConnectedService;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    42
import com.nokia.carbide.trk.support.service.TracingConnectedService;
848
679300bad088 Show connections view when new connection is added
dadubrow
parents: 770
diff changeset
    43
import com.nokia.cpp.internal.api.utils.ui.RunRunnableWhenWorkbenchVisibleJob;
679300bad088 Show connections view when new connection is added
dadubrow
parents: 770
diff changeset
    44
import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils;
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    45
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    46
/**
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    47
 * A singleton object that manages the device status of dynamic connections
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    48
 * based on the status of the TRK and Tracing services.
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    49
 */
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    50
public class ConnectionStatusReconciler {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    51
	
848
679300bad088 Show connections view when new connection is added
dadubrow
parents: 770
diff changeset
    52
	private static final String CONNECTIONS_VIEW_ID = 
679300bad088 Show connections view when new connection is added
dadubrow
parents: 770
diff changeset
    53
		"com.nokia.carbide.remoteconnections.view.ConnectionsView"; //$NON-NLS-1$
679300bad088 Show connections view when new connection is added
dadubrow
parents: 770
diff changeset
    54
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    55
	private class ConnectionListener implements IConnectionListener {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    56
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    57
		public void connectionAdded(IConnection connection) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    58
			addConnection(connection);
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    59
		}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    60
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    61
		public void connectionRemoved(IConnection connection) {
764
603d6ca035e8 updated set and reset current depending on ready state
dadubrow
parents: 763
diff changeset
    62
			if (connection.equals(userSetCurrentConnection))
603d6ca035e8 updated set and reset current depending on ready state
dadubrow
parents: 763
diff changeset
    63
				userSetCurrentConnection = null;
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    64
			removeConnection(connection);
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    65
		}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    66
		
764
603d6ca035e8 updated set and reset current depending on ready state
dadubrow
parents: 763
diff changeset
    67
		public void currentConnectionSet(IConnection connection) {
603d6ca035e8 updated set and reset current depending on ready state
dadubrow
parents: 763
diff changeset
    68
			if (connection != null && !connection.equals(reconcilerSetCurrentConnection))
603d6ca035e8 updated set and reset current depending on ready state
dadubrow
parents: 763
diff changeset
    69
				userSetCurrentConnection = connection;
603d6ca035e8 updated set and reset current depending on ready state
dadubrow
parents: 763
diff changeset
    70
		}
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    71
		
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    72
	}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    73
	
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    74
	private class ServiceStatusListener implements IStatusChangedListener {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    75
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    76
		public void statusChanged(IStatus status) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    77
			handleServiceStatusChange(status);
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    78
		}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    79
		
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    80
	}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    81
	
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    82
	private static ConnectionStatusReconciler instance;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    83
	private IConnectionsManager manager;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    84
	private IConnectionListener connectionListener;
763
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
    85
	private List<IConnection> handledConnections;
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    86
	private ServiceStatusListener serviceStatusListener;
764
603d6ca035e8 updated set and reset current depending on ready state
dadubrow
parents: 763
diff changeset
    87
	private IConnection reconcilerSetCurrentConnection;
603d6ca035e8 updated set and reset current depending on ready state
dadubrow
parents: 763
diff changeset
    88
	private IConnection userSetCurrentConnection;
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    89
	
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    90
	private ConnectionStatusReconciler() {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    91
		connectionListener = new ConnectionListener();
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    92
		manager = RemoteConnectionsActivator.getConnectionsManager();
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    93
		manager.addConnectionListener(connectionListener);
763
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
    94
		handledConnections = new ArrayList<IConnection>();
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    95
		serviceStatusListener = new ServiceStatusListener();
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    96
	}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    97
	
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    98
	public static ConnectionStatusReconciler getInstance() {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
    99
		if (instance == null)
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   100
			instance = new ConnectionStatusReconciler();
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   101
		
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   102
		return instance;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   103
	}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   104
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   105
	public void dispose() {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   106
		manager.removeConnectionListener(connectionListener);
763
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   107
		for (IConnection connection : new ArrayList<IConnection>(handledConnections)) {
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   108
			removeConnection(connection);
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   109
		}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   110
	}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   111
752
a0609ea7401a add sys trk checking for usb only and use with reconciler
dadubrow
parents: 750
diff changeset
   112
	private boolean isDynamic(IConnection connection) {
a0609ea7401a add sys trk checking for usb only and use with reconciler
dadubrow
parents: 750
diff changeset
   113
		return connection instanceof IConnection2 && ((IConnection2) connection).isDynamic();
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   114
	}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   115
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   116
	private boolean isSysTRK(TRKConnectedService service) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   117
		String value = service.getProperties().get(TRKConnectedService.PROP_SYS_TRK);
752
a0609ea7401a add sys trk checking for usb only and use with reconciler
dadubrow
parents: 750
diff changeset
   118
		return Boolean.parseBoolean(value);
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   119
	}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   120
	
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   121
	private void addConnection(IConnection connection) {
763
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   122
		handledConnections.add(connection);
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   123
		for (IConnectedService service : manager.getConnectedServices(connection)) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   124
			if (service instanceof TRKConnectedService ||
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   125
					service instanceof TracingConnectedService) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   126
				service.addStatusChangedListener(serviceStatusListener);
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   127
			}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   128
		}
848
679300bad088 Show connections view when new connection is added
dadubrow
parents: 770
diff changeset
   129
		showConnectionsView();
679300bad088 Show connections view when new connection is added
dadubrow
parents: 770
diff changeset
   130
	}
679300bad088 Show connections view when new connection is added
dadubrow
parents: 770
diff changeset
   131
679300bad088 Show connections view when new connection is added
dadubrow
parents: 770
diff changeset
   132
	private void showConnectionsView() {
924
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   133
		// avoid deadlock if this called as a result of a launch sequence issuing a "select connection" dialog
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   134
		Shell shell = WorkbenchUtils.getActiveShell();
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   135
		if (shell == null || !shell.isVisible()) {
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   136
			RunRunnableWhenWorkbenchVisibleJob.start(new Runnable() {
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   137
				public void run() {
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   138
					// try to show the connections view to start service testers
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   139
					try {
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   140
						WorkbenchUtils.getView(CONNECTIONS_VIEW_ID);
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   141
					} catch (PartInitException e) {
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   142
					}
848
679300bad088 Show connections view when new connection is added
dadubrow
parents: 770
diff changeset
   143
				}
924
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   144
			});
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   145
		} else {
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   146
			Display.getDefault().asyncExec(new Runnable() {
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   147
				public void run() {
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   148
					try {
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   149
						WorkbenchUtils.getView(CONNECTIONS_VIEW_ID);
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   150
					} catch (PartInitException e) {
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   151
					}
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   152
				}
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   153
			});
657e34ac671f Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
Ed Swartz <ed.swartz@nokia.com>
parents: 848
diff changeset
   154
		}
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   155
	}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   156
	
763
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   157
	private void reconcileAsCurrent(IConnection connection) {
764
603d6ca035e8 updated set and reset current depending on ready state
dadubrow
parents: 763
diff changeset
   158
		if (canBeSetToCurrent(connection)) {
770
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   159
			if (isReady(connection)) { // set as current
764
603d6ca035e8 updated set and reset current depending on ready state
dadubrow
parents: 763
diff changeset
   160
				reconcilerSetCurrentConnection = connection;
603d6ca035e8 updated set and reset current depending on ready state
dadubrow
parents: 763
diff changeset
   161
				manager.setCurrentConnection(connection);
770
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   162
			} else if (isNotReady(connection) && connection.equals(manager.getCurrentConnection())) { 
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   163
				// unset current or set something else current
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   164
				if (isDynamic(connection) && userSetCurrentConnection != null) {
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   165
					manager.setCurrentConnection(userSetCurrentConnection);
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   166
				}
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   167
				else {
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   168
					// look for some other existing connection that is ready
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   169
					for (IConnection c : manager.getConnections()) {
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   170
						if (canBeSetToCurrent(c) && isReady(c)) {
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   171
							reconcilerSetCurrentConnection = connection;
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   172
							manager.setCurrentConnection(connection);
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   173
							return;
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   174
						}
764
603d6ca035e8 updated set and reset current depending on ready state
dadubrow
parents: 763
diff changeset
   175
					}
770
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   176
					// set to no current connection
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   177
					manager.setCurrentConnection(null);
764
603d6ca035e8 updated set and reset current depending on ready state
dadubrow
parents: 763
diff changeset
   178
				}
603d6ca035e8 updated set and reset current depending on ready state
dadubrow
parents: 763
diff changeset
   179
			}
763
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   180
		}
758
c3cf3704d340 Make setting default part of reconciler responsibility
dadubrow
parents: 752
diff changeset
   181
	}
c3cf3704d340 Make setting default part of reconciler responsibility
dadubrow
parents: 752
diff changeset
   182
763
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   183
	private boolean isReady(IConnection connection) {
770
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   184
		return equalsConnectionStatus(connection, EConnectionStatus.READY);
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   185
	}
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   186
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   187
	private boolean isNotReady(IConnection connection) {
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   188
		return equalsConnectionStatus(connection, EConnectionStatus.NOT_READY);
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   189
	}
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   190
	
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   191
	private boolean equalsConnectionStatus(IConnection connection, EConnectionStatus status) {
763
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   192
		if (connection instanceof IConnection2) {
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   193
			IConnectionStatus connectionStatus = ((IConnection2) connection).getStatus();
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   194
			if (connectionStatus != null)
770
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   195
				return connectionStatus.getEConnectionStatus().equals(status);
763
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   196
		}
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   197
		return false;
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   198
	}
770
cb7e68ca1ef4 don't unset current when in-use
dadubrow
parents: 764
diff changeset
   199
	
763
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   200
	private boolean canBeSetToCurrent(IConnection connection) {
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   201
		// USB connections for now
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   202
		return USBConnectionType.ID.equals(connection.getConnectionType().getIdentifier());
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   203
	}
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   204
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   205
	private void reconcileStatus(IConnection connection) {
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   206
		if (!isDynamic(connection)) // don't set status for user generated connections
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   207
			return;
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   208
		
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   209
		boolean isSysTRK = false;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   210
		EStatus trkStatus = EStatus.UNKNOWN;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   211
		EStatus traceStatus = EStatus.UNKNOWN;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   212
		for (IConnectedService service : manager.getConnectedServices(connection)) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   213
			if (service instanceof TRKConnectedService) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   214
				isSysTRK = isSysTRK((TRKConnectedService) service);
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   215
				trkStatus = service.getStatus().getEStatus();
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   216
			}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   217
			if (service instanceof TracingConnectedService) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   218
				traceStatus = service.getStatus().getEStatus();
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   219
			}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   220
		}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   221
		setConnectionStatus((IConnection2) connection, isSysTRK, trkStatus, traceStatus);
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   222
	}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   223
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   224
	private void setConnectionStatus(IConnection2 connection, boolean isSysTRK, EStatus trkStatus, EStatus traceStatus) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   225
		// use trk status
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   226
		EConnectionStatus connectionStatus = service2ConnectionStatus(trkStatus);
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   227
		// if sys trk, tracing also used
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   228
		if (isSysTRK && connectionStatus.equals(EConnectionStatus.READY)) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   229
			connectionStatus = service2ConnectionStatus(traceStatus);
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   230
		}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   231
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   232
		String shortDesc = getShortDescriptionForStatus(connectionStatus);
749
22f0d10fce10 Externalize strings
dadubrow
parents: 748
diff changeset
   233
		StringBuilder longDesc = new StringBuilder(Messages.getString("ConnectionStatusReconciler_TRKServicePrefix")); //$NON-NLS-1$
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   234
		longDesc.append(getServiceStatusString(trkStatus));
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   235
		if (isSysTRK) {
749
22f0d10fce10 Externalize strings
dadubrow
parents: 748
diff changeset
   236
			longDesc.append(Messages.getString("ConnectionStatusReconciler_TracingServicePrefix")); //$NON-NLS-1$
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   237
			longDesc.append(getServiceStatusString(traceStatus));
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   238
		}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   239
		
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   240
		connection.setStatus(new ConnectionStatus(connectionStatus, shortDesc, longDesc.toString()));
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   241
	}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   242
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   243
	private String getShortDescriptionForStatus(EConnectionStatus connectionStatus) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   244
		switch (connectionStatus) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   245
			case READY:
749
22f0d10fce10 Externalize strings
dadubrow
parents: 748
diff changeset
   246
				return Messages.getString("ConnectionStatusReconciler_ReadyLabel"); //$NON-NLS-1$
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   247
			case NOT_READY:
749
22f0d10fce10 Externalize strings
dadubrow
parents: 748
diff changeset
   248
				return Messages.getString("ConnectionStatusReconciler_NotReadyLabel"); //$NON-NLS-1$
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   249
			case IN_USE:
749
22f0d10fce10 Externalize strings
dadubrow
parents: 748
diff changeset
   250
				return Messages.getString("ConnectionStatusReconciler_InUseLabel"); //$NON-NLS-1$
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   251
			case IN_USE_DISCONNECTED:
749
22f0d10fce10 Externalize strings
dadubrow
parents: 748
diff changeset
   252
				return Messages.getString("ConnectionStatusReconciler_DisconnectedLabel"); //$NON-NLS-1$
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   253
			}
749
22f0d10fce10 Externalize strings
dadubrow
parents: 748
diff changeset
   254
		return ""; //$NON-NLS-1$
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   255
	}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   256
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   257
	private String getServiceStatusString(EStatus status) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   258
		switch (status) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   259
			case UP:
749
22f0d10fce10 Externalize strings
dadubrow
parents: 748
diff changeset
   260
				return Messages.getString("ConnectionStatusReconciler_availableLabel"); //$NON-NLS-1$
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   261
			case DOWN:
749
22f0d10fce10 Externalize strings
dadubrow
parents: 748
diff changeset
   262
				return Messages.getString("ConnectionStatusReconciler_unavailableLabel"); //$NON-NLS-1$
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   263
			case IN_USE:
749
22f0d10fce10 Externalize strings
dadubrow
parents: 748
diff changeset
   264
				return Messages.getString("ConnectionStatusReconciler_inUseLabel_lower"); //$NON-NLS-1$
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   265
		}
749
22f0d10fce10 Externalize strings
dadubrow
parents: 748
diff changeset
   266
		return ""; //$NON-NLS-1$
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   267
	}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   268
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   269
	private EConnectionStatus service2ConnectionStatus(EStatus serviceStatus) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   270
		switch (serviceStatus) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   271
			case UP:
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   272
				return EConnectionStatus.READY;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   273
			case DOWN:
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   274
				return EConnectionStatus.NOT_READY;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   275
			case IN_USE:
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   276
				return EConnectionStatus.IN_USE;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   277
		}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   278
		return EConnectionStatus.NONE;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   279
	}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   280
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   281
	private void removeConnection(IConnection connection) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   282
		handledConnections.remove(connection);
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   283
	}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   284
763
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   285
	private IConnection findConnection(IConnectedService cs) {
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   286
		for (IConnection connection : handledConnections) {
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   287
			for (IConnectedService connectedService : manager.getConnectedServices(connection)) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   288
				if (cs.equals(connectedService))
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   289
					return connection;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   290
			}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   291
		}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   292
		return null;
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   293
	}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   294
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   295
	public void handleServiceStatusChange(IStatus status) {
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   296
		IConnectedService service = status.getConnectedService();
763
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   297
		IConnection connection = findConnection(service);
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   298
		if (connection instanceof IConnection2) {
751f5182b866 Refactor API and related from default connection to current + update heuristics
dadubrow
parents: 758
diff changeset
   299
			reconcileStatus((IConnection2) connection);
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   300
		}
764
603d6ca035e8 updated set and reset current depending on ready state
dadubrow
parents: 763
diff changeset
   301
		if (connection != null)
603d6ca035e8 updated set and reset current depending on ready state
dadubrow
parents: 763
diff changeset
   302
			reconcileAsCurrent(connection);
748
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   303
	}
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   304
7bd40a2d0a18 Add connection status reconciliation + fixes + tweaks
dadubrow
parents:
diff changeset
   305
}