htiextension/com.nokia.s60tools.hticonnection/src/com/nokia/s60tools/hticonnection/core/HtiConnection.java
author Matti Laitinen <matti.t.laitinen@nokia.com>
Thu, 04 Mar 2010 13:38:40 +0200
changeset 7 018264c44c89
parent 0 61163b28edca
child 9 e67492608de0
permissions -rw-r--r--
HTI Connection v1.3.1 - Supporting Carbide 2.5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     1
/*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     3
* All rights reserved.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     4
* This component and the accompanying materials are made available
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     6
* which accompanies this distribution, and is available
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     8
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     9
* Initial Contributors:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    11
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    12
* Contributors:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    13
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    14
* Description:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    15
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    16
*/
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    17
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    18
package com.nokia.s60tools.hticonnection.core;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    19
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    20
import java.util.ArrayList;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    21
import java.util.Collection;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    22
import java.util.List;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    23
import java.util.concurrent.ExecutionException;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    24
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
    25
import org.eclipse.core.runtime.CoreException;
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    26
import org.eclipse.swt.SWT;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    27
import org.eclipse.swt.widgets.Display;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    28
import org.eclipse.ui.PartInitException;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    29
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    30
import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    31
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    32
import com.nokia.carbide.remoteconnections.interfaces.IConnection;
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
    33
import com.nokia.carbide.remoteconnections.interfaces.IService;
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    34
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    35
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus.EStatus;
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
    36
import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener;
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
    37
import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.ISelectedConnectionInfo;
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
    38
import com.nokia.carbide.remoteconnections.internal.api.IConnection2;
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
    39
import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus;
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
    40
import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatusChangedListener;
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
    41
import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus.EConnectionStatus;
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    42
import com.nokia.s60tools.hticonnection.HtiApiActivator;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    43
import com.nokia.s60tools.hticonnection.actions.OpenPreferencePageAction;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    44
import com.nokia.s60tools.hticonnection.connection.HTIConnectedService;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    45
import com.nokia.s60tools.hticonnection.connection.HTIConnectionStatus;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    46
import com.nokia.s60tools.hticonnection.connection.HTIService;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    47
import com.nokia.s60tools.hticonnection.gateway.DataGatewayManager;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    48
import com.nokia.s60tools.hticonnection.listener.HtiConnectionManager;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    49
import com.nokia.s60tools.hticonnection.preferences.HtiApiPreferenceConstants;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    50
import com.nokia.s60tools.hticonnection.preferences.HtiApiPreferencePage;
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
    51
import com.nokia.s60tools.hticonnection.preferences.HtiApiPreferences;
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    52
import com.nokia.s60tools.hticonnection.resources.Messages;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    53
import com.nokia.s60tools.hticonnection.services.HTIVersion;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    54
import com.nokia.s60tools.hticonnection.ui.dialogs.ErrorDialogWithHelp;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    55
import com.nokia.s60tools.hticonnection.ui.dialogs.HtiApiMessageBox;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    56
import com.nokia.s60tools.hticonnection.ui.views.main.MainView;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    57
import com.nokia.s60tools.util.cmdline.UnsupportedOSException;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    58
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    59
/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    60
 * This class manages current connection and holds it status information.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    61
 */
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
    62
public class HtiConnection {
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    63
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    64
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    65
	 * Status of current connection. Used to device if new requests should
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    66
	 * be accepted. Connection is in shutdown status when plug-in is started.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    67
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    68
	private ConnectionStatus currentStatus = ConnectionStatus.SHUTDOWN;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    69
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    70
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    71
	 * Thread is used to test connection when connection has been started.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    72
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    73
	private ConnectionCheckerThread connectionChecker = null;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    74
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    75
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    76
	 * Instance of this singleton class.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    77
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    78
	private static HtiConnection instance = null;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    79
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    80
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    81
	 * HTI version to which HTI Connection is currently connected.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    82
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    83
	private HTIVersion version = null;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    84
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    85
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    86
	 * Currently used connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    87
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    88
	private IConnection currentConnection = null;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    89
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    90
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    91
	 * Stores instance of data gateway, that is used for communication between HTI API and device.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    92
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    93
	private DataGatewayManager gatewayManager = null;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    94
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    95
	/**
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
    96
	 * Listener for listening changes in connections.
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
    97
	 */
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
    98
	private IConnectionListener connectionListener = null;
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
    99
	
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   100
	/**
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   101
	 * Listener for listening changes in current connection status.
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   102
	 */
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   103
	private IConnectionStatusChangedListener currentConnectionStatusListener = null;
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   104
	
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   105
	/**
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   106
	 * To point right connection when IConnectionStatusChangedListener is added and removed.
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   107
	 */
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   108
	private IConnection currentlyOpenConnection = null;
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   109
	
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   110
	/**
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   111
	 * Enumeration for status of the current connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   112
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   113
	public enum ConnectionStatus {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   114
		SHUTDOWN, // Connection has been shut down.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   115
		TESTING,  // Testing connection. Used when testing connection from preferences.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   116
		CONNECTING, //  Connection is just started or there have been problems in connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   117
		CONNECTED // Connection is working.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   118
	};
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   119
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   120
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   121
	 * Private constructor to prevent creating new instances.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   122
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   123
	private HtiConnection(){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   124
		gatewayManager = new DataGatewayManager();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   125
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   126
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   127
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   128
	 * Only one instance can exist at one time.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   129
	 * @return Current instance.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   130
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   131
	public static synchronized HtiConnection getInstance(){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   132
		if( instance == null ){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   133
			instance = new HtiConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   134
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   135
		return instance;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   136
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   137
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   138
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   139
	 * Initializes listeners and settings.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   140
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   141
	public void init() {
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   142
		// Add listeners
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   143
		// Create "current connection" listener
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   144
		connectionListener = new ConnectionChangedListener();
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   145
		RemoteConnectionsActivator.getConnectionsManager().addConnectionListener(connectionListener);
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   146
		
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   147
		currentConnection = HtiApiActivator.getPreferences().getCurrentConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   148
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   149
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   150
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   151
	 * Stops thread that checks if connection is up.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   152
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   153
	public void stop() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   154
		setConnectionStatus(ConnectionStatus.SHUTDOWN);
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   155
		// Remove listeners
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   156
		RemoteConnectionsActivator.getConnectionsManager().removeConnectionListener(connectionListener);
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   157
		
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   158
		connectionChecker = null;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   159
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   160
		if(gatewayManager != null){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   161
			stopConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   162
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   163
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   164
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   165
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   166
	 * Starts connection with current settings.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   167
	 * @param isTesting True if connection is started only to test connection settings.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   168
	 * @return True if connection was started successfully. False otherwise.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   169
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   170
	public boolean startConnection(boolean isTesting) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   171
		IConnection connection = getCurrentConnection(); 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   172
		if(gatewayManager.isReady()) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   173
			// Old gateway needs to be stopped before starting new.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   174
			stopConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   175
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   176
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   177
		if(connection == null) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   178
			String msg;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   179
			List<IConnection> htiConns = getHTIConnections();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   180
			if(htiConns.size() > 0) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   181
				msg = new String(Messages.getString("HtiConnection.ConnectionNotSelected_ErrMsg")); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   182
			} else {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   183
				msg = new String(Messages.getString("HtiConnection.NoConnections_ErrMsg")); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   184
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   185
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   186
			ErrorDialogRunnable runnable = new ErrorDialogRunnable(msg);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   187
			Display.getDefault().asyncExec(runnable);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   188
			return false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   189
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   190
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   191
		// Connection status needs to be set before starting gateway, so that connection is set to use.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   192
		ConnectionStatus status = (isTesting) ? ConnectionStatus.TESTING : ConnectionStatus.CONNECTING;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   193
		setConnectionStatus(status);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   194
		
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   195
		boolean result = startGateway(connection, isTesting);
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   196
		
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   197
		if (result) {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   198
			currentlyOpenConnection = connection;
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   199
			// Add status listener to the connection
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   200
			if (currentlyOpenConnection instanceof IConnection2) {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   201
				currentConnectionStatusListener = new ConnectionStatusListener();
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   202
				((IConnection2) currentlyOpenConnection)
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   203
						.addStatusChangedListener(currentConnectionStatusListener);
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   204
			}
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   205
		}
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   206
		
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   207
		return result;
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   208
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   209
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   210
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   211
	 * Starts datagateway with current settings.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   212
	 * @param isTesting True if datagateway is started only to test connection settings.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   213
	 * @return True if datagateway was started successfully. False otherwise.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   214
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   215
	private boolean startGateway(IConnection connection, boolean isTesting) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   216
		try {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   217
			boolean started = false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   218
			// Connection status needs to be checked here, because setting connection status can take a while,
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   219
			// when connection is set in to use. User could have stopped the connection during that time.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   220
			if(getConnectionStatus() != ConnectionStatus.SHUTDOWN) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   221
				// Connection string needs to be updated, before starting connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   222
				// Otherwise old information might be shown.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   223
				started = gatewayManager.startGateway(connection, isTesting);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   224
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   225
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   226
			if(started && !isTesting) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   227
				// Connection needs to be tested after datagateway has been started.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   228
				connectionChecker = new ConnectionCheckerThread();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   229
				connectionChecker.start();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   230
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   231
			else if(!started) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   232
				setConnectionStatus(ConnectionStatus.SHUTDOWN);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   233
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   234
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   235
			return started;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   236
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   237
		} catch (UnsupportedOSException e) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   238
			Runnable runnable = new Runnable() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   239
				public void run() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   240
					// Only Windows operating system is supported.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   241
					String errMsg = Messages.getString("HtiConnection.FailedToStartGateway_ErrMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   242
					HtiApiMessageBox msgBox = new HtiApiMessageBox(errMsg, SWT.ICON_ERROR | SWT.OK);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   243
					msgBox.open();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   244
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   245
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   246
			};
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   247
			// Messagebox needs to be opened from UI thread.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   248
			Display.getDefault().asyncExec(runnable);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   249
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   250
			return false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   251
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   252
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   253
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   254
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   255
	 * Stops datagateway.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   256
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   257
	public void stopConnection() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   258
		setConnectionStatus(ConnectionStatus.SHUTDOWN);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   259
		gatewayManager.stopGateway();
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   260
		
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   261
		// Remove status listener from the connection
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   262
		if ((currentlyOpenConnection instanceof IConnection2) &&
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   263
			(currentConnectionStatusListener != null)) {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   264
			((IConnection2) currentlyOpenConnection)
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   265
					.removeStatusChangedListener(currentConnectionStatusListener);
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   266
		}
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   267
		currentlyOpenConnection = null;
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   268
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   269
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   270
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   271
	 * Tests given connection. Shuts down existing connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   272
	 * This method is synchronized, because there can come multiple test
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   273
	 * request at the same time from Remote Connections.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   274
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   275
	public synchronized IStatus testConnection(IConnection testConnection) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   276
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   277
		IConnection origConn = getCurrentConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   278
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   279
		EStatus testStatus = EStatus.UNKNOWN;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   280
		String shortDesc = Messages.getString("HtiConnection.NotConnected_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   281
		String longDesc = Messages.getString("HtiConnection.ConnectionFailed_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   282
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   283
		try {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   284
			// Existing gateway needs to be stopped.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   285
			stopConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   286
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   287
			// New connection should be defined.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   288
			setCurrentConnection(testConnection, true);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   289
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   290
			// Gateway can be started now.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   291
			if(startConnection(true)) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   292
				if(RequestQueueManager.getInstance().testConnection()) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   293
					HTIVersion version = getHTIVersion();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   294
					testStatus = EStatus.UP;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   295
					shortDesc = Messages.getString("HtiConnection.Connected_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   296
					longDesc = Messages.getString("HtiConnection.Connected_LongStatusMsg") + version.toString(); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   297
				} else {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   298
					testStatus = EStatus.DOWN;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   299
					shortDesc = Messages.getString("HtiConnection.NotConnected_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   300
					longDesc = Messages.getString("HtiConnection.ConnectionFailed_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   301
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   302
			} else {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   303
				testStatus = EStatus.DOWN;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   304
				shortDesc = Messages.getString("HtiConnection.NotConnected_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   305
				longDesc = Messages.getString("HtiConnection.FailedToStartGateway_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   306
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   307
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   308
		} catch (Exception e) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   309
			if (e.getMessage().equals("com.nokia.HTI.HTIException: HTI NOT INITIALIZED")) { //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   310
				// HTI agent is not responding
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   311
				testStatus = EStatus.DOWN;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   312
				shortDesc = Messages.getString("HtiConnection.NotConnected_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   313
				longDesc = Messages.getString("HtiConnection.HtiNotResponding_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   314
			} else {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   315
				// Connection could not be created
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   316
				testStatus = EStatus.DOWN;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   317
				shortDesc = Messages.getString("HtiConnection.NotConnected_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   318
				longDesc = Messages.getString("HtiConnection.ConnectionFailed_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   319
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   320
		} finally {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   321
			stopConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   322
			setCurrentConnection(origConn, false);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   323
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   324
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   325
		return new HTIConnectionStatus(null, testStatus, shortDesc, longDesc);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   326
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   327
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   328
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   329
	 * Set HTI version to which HTI Connection is currently connected.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   330
	 * @param version HTI version to which HTI Connection is currently connected.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   331
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   332
	public void setHTIVersion(HTIVersion version) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   333
		this.version = version;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   334
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   335
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   336
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   337
	 * Get HTI version to which HTI Connection is currently connected.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   338
	 * @return HTI version to which HTI Connection is currently connected,
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   339
	 * or null if connection is down.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   340
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   341
	public HTIVersion getHTIVersion() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   342
		return version;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   343
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   344
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   345
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   346
	 * Returns all connections which support connection types that HTI Connection can use. 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   347
	 * @return List of connections that support connection types that HTI Connection can use.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   348
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   349
	public List<IConnection> getHTIConnections() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   350
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   351
		// Getting needed variables.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   352
		Collection<IConnection> connections = RemoteConnectionsActivator.getConnectionsManager().getConnections();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   353
		Collection<String> connectionTypes = HTIService.getCompatibleConnectionTypeIds();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   354
		List<IConnection> htiConnections = new ArrayList<IConnection>();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   355
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   356
		// Going through all connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   357
		for(IConnection conn : connections) {	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   358
			if(connectionTypes.contains(conn.getConnectionType().getIdentifier())) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   359
				htiConnections.add(conn);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   360
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   361
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   362
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   363
		return htiConnections;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   364
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   365
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   366
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   367
	 * Returns current status of connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   368
	 * @return Current status of connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   369
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   370
	public ConnectionStatus getConnectionStatus() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   371
		synchronized(currentStatus) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   372
			return currentStatus;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   373
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   374
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   375
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   376
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   377
	 * Updates connection status in views description.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   378
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   379
	public void updateConnectionStatus() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   380
		Runnable updater = new ConnectionStatusUpdater(getCurrentConnection());
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   381
		// Updating connection status asynchronously in default UI thread.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   382
		Display.getDefault().asyncExec(updater);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   383
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   384
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   385
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   386
	 * Sets current status of connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   387
	 * @param currentStatus The currentStatus to set.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   388
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   389
	public void setConnectionStatus(ConnectionStatus newStatus) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   390
		// Flagging status change information.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   391
		boolean connectionStarted = false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   392
		boolean connectionTerminated = false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   393
		boolean connectionUp = false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   394
		boolean connectionDown = false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   395
		boolean needsUpdate = false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   396
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   397
		// Checking new status and updating current status.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   398
		synchronized(currentStatus) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   399
			if (newStatus == ConnectionStatus.SHUTDOWN
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   400
					&& currentStatus != ConnectionStatus.SHUTDOWN) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   401
				// Shutting down the connection and connection tester.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   402
				connectionChecker = null;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   403
				connectionDown = true;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   404
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   405
			else if (newStatus == ConnectionStatus.CONNECTED
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   406
					&& currentStatus != ConnectionStatus.CONNECTED) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   407
				connectionUp = true;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   408
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   409
			else if (newStatus != ConnectionStatus.CONNECTED
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   410
					&& currentStatus == ConnectionStatus.CONNECTED) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   411
				connectionDown = true;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   412
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   413
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   414
			// Checking if it is needed to start/stop using current connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   415
			if (newStatus != ConnectionStatus.SHUTDOWN
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   416
					&& currentStatus == ConnectionStatus.SHUTDOWN) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   417
				connectionStarted = true;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   418
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   419
			else if (newStatus == ConnectionStatus.SHUTDOWN
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   420
					&& currentStatus != ConnectionStatus.SHUTDOWN) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   421
				connectionTerminated = true;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   422
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   423
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   424
			needsUpdate = (currentStatus != newStatus);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   425
			currentStatus = newStatus;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   426
		} // End synchronized
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   427
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   428
		// Updating and informing outside of the synchronized block to prevent
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   429
		// synchronization slowing down connection status queries.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   430
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   431
		if(needsUpdate) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   432
			updateConnectionStatus();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   433
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   434
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   435
		if(connectionUp) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   436
			// Inform listeners of starting the connection
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   437
			HtiConnectionManager.getInstance().informConnectionStarted();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   438
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   439
		else if(connectionDown) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   440
			// Inform listeners of connection termination
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   441
			HtiConnectionManager.getInstance().informConnectionTerminated();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   442
			setHTIVersion(null);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   443
			RequestQueueManager.getInstance().cancelRequestsInQueue();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   444
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   445
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   446
		if(connectionStarted) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   447
			// Connection needs to be set as used, before starting datagateway.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   448
			// This must be done as last operation, because useConnection can take some time and
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   449
			// it is good to update other components first to prevent information lagging behind.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   450
			IConnection conn = getCurrentConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   451
			if(conn != null) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   452
				conn.useConnection(true);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   453
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   454
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   455
		else if(connectionTerminated) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   456
			// Connection is not needed anymore.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   457
			IConnection conn = getCurrentConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   458
			if(conn != null) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   459
				conn.useConnection(false);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   460
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   461
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   462
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   463
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   464
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   465
	 * Shows message to user if exception is caused by communication problems.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   466
	 * This method also changes connection state to CONNECTNG, so service won't accept further requests.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   467
	 * @param exception Exception to be checked and reported.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   468
	 * @return Returns true if error was reported and connection is set to CONNECTING state, false otherwise.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   469
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   470
	public boolean reportConnectionError(ExecutionException exception) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   471
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   472
		String msg;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   473
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   474
		// Checking if there are problems with connection and handling problems.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   475
		if (exception.getMessage().equals("com.nokia.HTI.HTIException: HTI NOT INITIALIZED")) { //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   476
			msg = Messages.getString("HtiConnection.HtiNotResponding_MsgBoxMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   477
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   478
		else if( exception.getMessage().equals("java.net.ConnectException: Connection refused: connect")) { //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   479
			msg = Messages.getString("HtiConnection.ConnectionFailed_MsgBoxMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   480
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   481
		else {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   482
			// HTI was initialized correctly and connection was established.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   483
			// So this shouldn't be connection issue and no need to change to CONNECTING state.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   484
			return false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   485
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   486
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   487
		if(HtiConnection.getInstance().getConnectionStatus() != ConnectionStatus.SHUTDOWN) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   488
			// Setting connection status to testing state here to prevent extra error messages.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   489
			HtiConnection.getInstance().setConnectionStatus(ConnectionStatus.CONNECTING);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   490
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   491
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   492
		ErrorDialogRunnable runnable = new ErrorDialogRunnable(msg);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   493
		Display.getDefault().asyncExec(runnable);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   494
		return true;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   495
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   496
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   497
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   498
	 * Sets currently used connection. Saves connection to preferences, if it isn't a test connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   499
	 * @param currentConnection Connection to be set.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   500
	 * @param isTestConnection True if connection is set for test purposes. False otherwise.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   501
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   502
	public synchronized void setCurrentConnection(IConnection connection, boolean isTestConnection) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   503
		if(currentConnection != null && currentConnection != connection) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   504
			// Connection can be set as not used, because HTI Connection uses only currentConnection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   505
			currentConnection.useConnection(false);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   506
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   507
		this.currentConnection = connection;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   508
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   509
		if(!isTestConnection) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   510
			// ID of normal connection need to be saved to preferences so that it will be used later as default.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   511
			String connectionID = (currentConnection == null) ? HtiApiPreferenceConstants.DEFAULT_CONNECTION_ID 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   512
					: currentConnection.getIdentifier();
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   513
						
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   514
			// Set only if NOT already set as same or NOT set as "current connection".
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   515
			if ((!connectionID.equals(HtiApiActivator.getPreferences().getConnectionID())) &&
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   516
				(!HtiApiPreferences.SELECTION_ID_CURRENT.equals(HtiApiActivator.getPreferences().getConnectionID()))) {
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   517
				HtiApiActivator.getPreferences().setConnectionID(connectionID);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   518
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   519
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   520
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   521
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   522
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   523
	 * Gets currently used connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   524
	 * @return Currently used connection, or <code>null</code> if connection has not been set.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   525
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   526
	public synchronized IConnection getCurrentConnection() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   527
		return currentConnection;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   528
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   529
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   530
	/**
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   531
	 * Call when connection changes.
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   532
	 * @param newConnection new connection
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   533
	 * @throws CoreException
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   534
	 */
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   535
	public void changeConnection(String newConnection) throws CoreException {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   536
			
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   537
		String currentConnectionId = null;
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   538
		if (getCurrentConnection() != null) currentConnectionId = getCurrentConnection().getIdentifier();
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   539
		boolean isConnected = getConnectionStatus() != ConnectionStatus.SHUTDOWN;
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   540
		boolean isNewConnection = (newConnection != null) && !(newConnection.equals(currentConnectionId)); 
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   541
		
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   542
		// Ensure that connection is ok.
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   543
		IService service = RemoteConnectionsActivator.getConnectionTypeProvider().findServiceByID(HTIService.ID);
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   544
		ISelectedConnectionInfo connectionInfo;
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   545
		connectionInfo = RemoteConnectionsActivator.getConnectionsManager().ensureConnection(newConnection, service);
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   546
		IConnection connection = connectionInfo.getConnection();
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   547
		
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   548
		if (isConnected && isNewConnection) {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   549
			// Connection has been changed when connection is started.
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   550
			restartDataGateway(connection, false);
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   551
		}
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   552
		else if(!isConnected || currentConnection == null) {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   553
			// Connection isn't active or connections have been deleted. New connection can be set. 
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   554
			setCurrentConnection(connection, false);
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   555
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   556
	}
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   557
	
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   558
	/**
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   559
	 * Call when connection changes.
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   560
	 * @param newConnection new connection
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   561
	 */
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   562
	public void changeConnection(IConnection newConnection) {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   563
		boolean isConnected = getConnectionStatus() != ConnectionStatus.SHUTDOWN;
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   564
		boolean isNewConnection = 
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   565
			(newConnection != null) && 
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   566
			!(newConnection.equals(getCurrentConnection())); 
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   567
		
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   568
		if (isConnected && isNewConnection) {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   569
			// Connection has been changed when connection is started.
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   570
			restartDataGateway(newConnection, false);
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   571
		}
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   572
		else if(!isConnected || currentConnection == null) {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   573
			// Connection isn't active or connections have been deleted. New connection can be set. 
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   574
			setCurrentConnection(newConnection, false);
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   575
		}
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   576
	}
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   577
	
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   578
	/**
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   579
	 * Restarts Datagateway with settings from preference store.
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   580
	 * @param isTesting True if testing connection and not trying to establish permanent connection.
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   581
	 */
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   582
	public boolean restartDataGateway(IConnection conn, boolean isTesting) {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   583
		stopConnection();
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   584
		setCurrentConnection(conn, false);
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   585
		return startConnection(isTesting);
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   586
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   587
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   588
	//
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   589
	// Private classes.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   590
	//
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   591
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   592
	/**
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   593
	 * Listener for listening changes in current connection status.
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   594
	 */
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   595
	private class ConnectionStatusListener implements IConnectionStatusChangedListener {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   596
		
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   597
		public void statusChanged(IConnectionStatus status) {		
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   598
			// Stop connection when current connection device is removed.
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   599
			if (status.getEConnectionStatus().equals(EConnectionStatus.IN_USE_DISCONNECTED)) {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   600
				stopConnection();
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   601
				setCurrentConnection(null, false);
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   602
			}
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   603
		}
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   604
	}
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   605
	
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   606
	/**
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   607
	 * Listener for listening changes in connections.
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   608
	 */
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   609
	private class ConnectionChangedListener implements IConnectionListener {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   610
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   611
		public void connectionAdded(IConnection connection) {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   612
			// Do nothing.
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   613
		}
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   614
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   615
		public void connectionRemoved(IConnection connection) {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   616
			// Initializing needed variables.
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   617
			IConnection currConn = getCurrentConnection();
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   618
			if(currConn == null) {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   619
				// Nothing to do, if no connection is selected.
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   620
				return;
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   621
			}
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   622
			
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   623
			// Checking if current connection was the removed one.
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   624
			if (currConn.getIdentifier().equals(connection.getIdentifier())) {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   625
				//If so, remove it and stop connection.
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   626
				stopConnection();
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   627
				setCurrentConnection(null, false);
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   628
			}		
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   629
		}
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   630
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   631
		public void currentConnectionSet(IConnection connection) {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   632
			if (HtiApiActivator.getPreferences().getConnectionID().equals(HtiApiPreferences.SELECTION_ID_CURRENT)) {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   633
				changeConnection(connection);
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   634
			}
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   635
		}
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   636
		
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   637
	};
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   638
	
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   639
	/**
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   640
	 * Thread that tests connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   641
	 * Connection needs to be tested when new connection has been started
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   642
	 * or there has been problems with current connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   643
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   644
	private class ConnectionCheckerThread extends Thread {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   645
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   646
		/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   647
		 * Time in ms that is waited between testing the connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   648
		 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   649
		private static final long TEST_DELAY = 1000;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   650
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   651
		/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   652
		 * Keeps track for first connection try. This is needed if there
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   653
		 * happens connection error when testing connection first time.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   654
		 * On other times connection failure information comes from outside
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   655
		 * of this class.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   656
		 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   657
		private boolean firstConnection = true;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   658
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   659
		/* (non-Javadoc)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   660
		 * @see java.lang.Thread#run()
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   661
		 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   662
		public void run() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   663
			Thread thisThread = Thread.currentThread();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   664
			while(thisThread == connectionChecker) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   665
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   666
				if(getConnectionStatus() == ConnectionStatus.CONNECTING) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   667
					testConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   668
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   669
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   670
				try {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   671
					Thread.sleep(TEST_DELAY);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   672
				} catch (InterruptedException e) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   673
					// Not needed to handle. Just continuing polling in this case.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   674
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   675
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   676
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   677
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   678
		/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   679
		 * Tests if current connection works and modifies connection status
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   680
		 * in HtiApiActivator class if needed.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   681
		 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   682
		private void testConnection() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   683
			try {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   684
				boolean connectionUp = RequestQueueManager.getInstance().testConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   685
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   686
				if(connectionUp) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   687
					setConnectionStatus(ConnectionStatus.CONNECTED);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   688
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   689
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   690
			} catch (ExecutionException e) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   691
				if(firstConnection && getConnectionStatus() != ConnectionStatus.SHUTDOWN) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   692
					// Other than first connection errors are reported from RequestQueueManager.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   693
					// This handles only first connection problem when trying to connect to the device.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   694
					reportConnectionError(e);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   695
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   696
			} catch (Exception e) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   697
				// Catching other errors that doesn't need any specific actions.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   698
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   699
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   700
			firstConnection = false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   701
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   702
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   703
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   704
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   705
	 * Updates connection status and buttons in UI.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   706
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   707
	private class ConnectionStatusUpdater implements Runnable {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   708
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   709
		/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   710
		 * Connection that needs status update.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   711
		 */
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   712
		private final IConnection connectionToUpdate;
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   713
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   714
		/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   715
		 * Constructor.
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   716
		 * @param connectionToUpdate Connection that needs status update.
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   717
		 */
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   718
		public ConnectionStatusUpdater(IConnection connectionToUpdate) {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   719
			this.connectionToUpdate = connectionToUpdate;
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   720
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   721
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   722
		/* (non-Javadoc)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   723
		 * @see java.lang.Runnable#run()
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   724
		 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   725
		public void run() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   726
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   727
			// Connection status needs to be updated in connected service.
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   728
			if(connectionToUpdate != null) {
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   729
				Collection<IConnectedService> services = RemoteConnectionsActivator.getConnectionsManager().getConnectedServices(connectionToUpdate);
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   730
				// Collection can be null if getting services for test connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   731
				if(services != null) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   732
					for(IConnectedService service : services) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   733
						if(service instanceof HTIConnectedService) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   734
							((HTIConnectedService)service).refreshStatus();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   735
						}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   736
					}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   737
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   738
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   739
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   740
			try {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   741
				MainView view = MainView.getViewInstance(false);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   742
				view.updateActionButtonStates();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   743
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   744
				// Updating connection description.
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   745
				String displayName = "";
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   746
				if(connectionToUpdate != null){
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   747
					displayName = connectionToUpdate.getDisplayName();
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   748
				}
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   749
				
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   750
				switch (getConnectionStatus()) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   751
				case CONNECTED:
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   752
					view.updateDescription(Messages.getString("HtiConnection.Connected_ToolBar_Msg") + displayName); //$NON-NLS-1$
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   753
					break;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   754
				case CONNECTING:
7
018264c44c89 HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents: 0
diff changeset
   755
					view.updateDescription(Messages.getString("HtiConnection.Connecting_ToolBar_Msg") + displayName); //$NON-NLS-1$
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   756
					break;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   757
				case SHUTDOWN:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   758
					view.updateDescription(Messages.getString("HtiConnection.NotConnected_ToolBar_Msg")); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   759
					break;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   760
				case TESTING:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   761
					view.updateDescription(Messages.getString("HtiConnection.Testing_ToolBar_Msg")); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   762
					break;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   763
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   764
			} catch (PartInitException e) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   765
				// No need to handle. Description doesn't need update if part is not visible.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   766
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   767
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   768
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   769
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   770
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   771
	 * Runnable that opens error message box.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   772
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   773
	private class ErrorDialogRunnable implements Runnable {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   774
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   775
		/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   776
		 * Error message.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   777
		 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   778
		private final String errorMessage;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   779
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   780
		/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   781
		 * Constructor.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   782
		 * @param errorMessage Message to be shown in dialog.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   783
		 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   784
		public ErrorDialogRunnable(String errorMessage) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   785
			this.errorMessage = errorMessage;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   786
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   787
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   788
		/* (non-Javadoc)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   789
		 * @see java.lang.Runnable#run()
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   790
		 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   791
		public void run() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   792
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   793
			// No need to show error message if preferences page is already created.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   794
			if(HtiApiPreferencePage.isCreated()) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   795
				// Showing plain error message if preferences page is open.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   796
				ErrorDialogWithHelp msgBox = new ErrorDialogWithHelp(errorMessage, SWT.OK);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   797
				msgBox.open();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   798
			} else {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   799
				// Asking if user wants to manage preferences.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   800
				String msgWithQuestionStr = errorMessage + Messages.getString("HtiConnection.ManageConnections_ErrMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   801
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   802
				ErrorDialogWithHelp msgBox = new ErrorDialogWithHelp(msgWithQuestionStr, SWT.YES | SWT.NO);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   803
				int result = msgBox.open();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   804
				if(result == SWT.YES && !HtiApiPreferencePage.isCreated()){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   805
					// Opening preferences if Yes is selected.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   806
					OpenPreferencePageAction openPreferencesAction = new OpenPreferencePageAction();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   807
					openPreferencesAction.run();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   808
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   809
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   810
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   811
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   812
}