htiextension/com.nokia.s60tools.hticonnection/src/com/nokia/s60tools/hticonnection/core/HtiConnection.java
author dpodwall
Tue, 12 Jan 2010 13:17:53 -0600
changeset 0 61163b28edca
child 7 018264c44c89
permissions -rw-r--r--
initial EPL conversion
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
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    25
import org.eclipse.swt.SWT;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    26
import org.eclipse.swt.widgets.Display;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    27
import org.eclipse.ui.PartInitException;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    28
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    29
import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    30
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    31
import com.nokia.carbide.remoteconnections.interfaces.IConnection;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    32
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    33
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus.EStatus;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    34
import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    35
import com.nokia.s60tools.hticonnection.HtiApiActivator;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    36
import com.nokia.s60tools.hticonnection.actions.OpenPreferencePageAction;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    37
import com.nokia.s60tools.hticonnection.connection.HTIConnectedService;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    38
import com.nokia.s60tools.hticonnection.connection.HTIConnectionStatus;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    39
import com.nokia.s60tools.hticonnection.connection.HTIService;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    40
import com.nokia.s60tools.hticonnection.gateway.DataGatewayManager;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    41
import com.nokia.s60tools.hticonnection.listener.HtiConnectionManager;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    42
import com.nokia.s60tools.hticonnection.preferences.HtiApiPreferenceConstants;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    43
import com.nokia.s60tools.hticonnection.preferences.HtiApiPreferencePage;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    44
import com.nokia.s60tools.hticonnection.resources.Messages;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    45
import com.nokia.s60tools.hticonnection.services.HTIVersion;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    46
import com.nokia.s60tools.hticonnection.ui.dialogs.ErrorDialogWithHelp;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    47
import com.nokia.s60tools.hticonnection.ui.dialogs.HtiApiMessageBox;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    48
import com.nokia.s60tools.hticonnection.ui.views.main.MainView;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    49
import com.nokia.s60tools.util.cmdline.UnsupportedOSException;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    50
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    51
/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    52
 * This class manages current connection and holds it status information.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    53
 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    54
public class HtiConnection implements IConnectionsManagerListener {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    55
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    56
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    57
	 * Status of current connection. Used to device if new requests should
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    58
	 * be accepted. Connection is in shutdown status when plug-in is started.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    59
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    60
	private ConnectionStatus currentStatus = ConnectionStatus.SHUTDOWN;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    61
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    62
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    63
	 * Thread is used to test connection when connection has been started.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    64
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    65
	private ConnectionCheckerThread connectionChecker = null;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    66
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    67
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    68
	 * Instance of this singleton class.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    69
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    70
	private static HtiConnection instance = null;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    71
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    72
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    73
	 * HTI version to which HTI Connection is currently connected.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    74
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    75
	private HTIVersion version = null;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    76
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    77
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    78
	 * Currently used connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    79
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    80
	private IConnection currentConnection = null;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    81
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    82
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    83
	 * Stores instance of data gateway, that is used for communication between HTI API and device.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    84
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    85
	private DataGatewayManager gatewayManager = null;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    86
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    87
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    88
	 * Enumeration for status of the current connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    89
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    90
	public enum ConnectionStatus {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    91
		SHUTDOWN, // Connection has been shut down.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    92
		TESTING,  // Testing connection. Used when testing connection from preferences.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    93
		CONNECTING, //  Connection is just started or there have been problems in connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    94
		CONNECTED // Connection is working.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    95
	};
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    96
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    97
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    98
	 * Private constructor to prevent creating new instances.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    99
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   100
	private HtiConnection(){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   101
		gatewayManager = new DataGatewayManager();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   102
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   103
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   104
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   105
	 * Only one instance can exist at one time.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   106
	 * @return Current instance.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   107
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   108
	public static synchronized HtiConnection getInstance(){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   109
		if( instance == null ){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   110
			instance = new HtiConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   111
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   112
		return instance;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   113
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   114
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   115
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   116
	 * Initializes listeners and settings.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   117
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   118
	public void init() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   119
		RemoteConnectionsActivator.getConnectionsManager().addConnectionStoreChangedListener(this);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   120
		currentConnection = HtiApiActivator.getPreferences().getCurrentConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   121
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   122
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   123
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   124
	 * Stops thread that checks if connection is up.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   125
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   126
	public void stop() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   127
		setConnectionStatus(ConnectionStatus.SHUTDOWN);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   128
		RemoteConnectionsActivator.getConnectionsManager().removeConnectionStoreChangedListener(this);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   129
		connectionChecker = null;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   130
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   131
		if(gatewayManager != null){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   132
			stopConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   133
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   134
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   135
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   136
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   137
	 * Starts connection with current settings.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   138
	 * @param isTesting True if connection is started only to test connection settings.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   139
	 * @return True if connection was started successfully. False otherwise.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   140
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   141
	public boolean startConnection(boolean isTesting) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   142
		IConnection connection = getCurrentConnection(); 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   143
		if(gatewayManager.isReady()) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   144
			// Old gateway needs to be stopped before starting new.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   145
			stopConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   146
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   147
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   148
		if(connection == null) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   149
			String msg;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   150
			List<IConnection> htiConns = getHTIConnections();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   151
			if(htiConns.size() > 0) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   152
				msg = new String(Messages.getString("HtiConnection.ConnectionNotSelected_ErrMsg")); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   153
			} else {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   154
				msg = new String(Messages.getString("HtiConnection.NoConnections_ErrMsg")); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   155
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   156
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   157
			ErrorDialogRunnable runnable = new ErrorDialogRunnable(msg);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   158
			Display.getDefault().asyncExec(runnable);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   159
			return false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   160
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   161
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   162
		// Connection status needs to be set before starting gateway, so that connection is set to use.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   163
		ConnectionStatus status = (isTesting) ? ConnectionStatus.TESTING : ConnectionStatus.CONNECTING;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   164
		setConnectionStatus(status);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   165
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   166
		return startGateway(connection, isTesting);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   167
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   168
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   169
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   170
	 * Starts datagateway with current settings.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   171
	 * @param isTesting True if datagateway is started only to test connection settings.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   172
	 * @return True if datagateway was started successfully. False otherwise.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   173
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   174
	private boolean startGateway(IConnection connection, boolean isTesting) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   175
		try {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   176
			boolean started = false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   177
			// Connection status needs to be checked here, because setting connection status can take a while,
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   178
			// when connection is set in to use. User could have stopped the connection during that time.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   179
			if(getConnectionStatus() != ConnectionStatus.SHUTDOWN) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   180
				// Connection string needs to be updated, before starting connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   181
				// Otherwise old information might be shown.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   182
				started = gatewayManager.startGateway(connection, isTesting);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   183
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   184
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   185
			if(started && !isTesting) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   186
				// Connection needs to be tested after datagateway has been started.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   187
				connectionChecker = new ConnectionCheckerThread();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   188
				connectionChecker.start();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   189
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   190
			else if(!started) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   191
				setConnectionStatus(ConnectionStatus.SHUTDOWN);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   192
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   193
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   194
			return started;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   195
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   196
		} catch (UnsupportedOSException e) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   197
			Runnable runnable = new Runnable() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   198
				public void run() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   199
					// Only Windows operating system is supported.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   200
					String errMsg = Messages.getString("HtiConnection.FailedToStartGateway_ErrMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   201
					HtiApiMessageBox msgBox = new HtiApiMessageBox(errMsg, SWT.ICON_ERROR | SWT.OK);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   202
					msgBox.open();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   203
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   204
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   205
			};
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   206
			// Messagebox needs to be opened from UI thread.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   207
			Display.getDefault().asyncExec(runnable);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   208
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   209
			return false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   210
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   211
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   212
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   213
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   214
	 * Stops datagateway.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   215
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   216
	public void stopConnection() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   217
		setConnectionStatus(ConnectionStatus.SHUTDOWN);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   218
		gatewayManager.stopGateway();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   219
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   220
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   221
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   222
	 * Tests given connection. Shuts down existing connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   223
	 * This method is synchronized, because there can come multiple test
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   224
	 * request at the same time from Remote Connections.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   225
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   226
	public synchronized IStatus testConnection(IConnection testConnection) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   227
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   228
		IConnection origConn = getCurrentConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   229
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   230
		EStatus testStatus = EStatus.UNKNOWN;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   231
		String shortDesc = Messages.getString("HtiConnection.NotConnected_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   232
		String longDesc = Messages.getString("HtiConnection.ConnectionFailed_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   233
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   234
		try {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   235
			// Existing gateway needs to be stopped.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   236
			stopConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   237
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   238
			// New connection should be defined.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   239
			setCurrentConnection(testConnection, true);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   240
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   241
			// Gateway can be started now.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   242
			if(startConnection(true)) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   243
				if(RequestQueueManager.getInstance().testConnection()) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   244
					HTIVersion version = getHTIVersion();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   245
					testStatus = EStatus.UP;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   246
					shortDesc = Messages.getString("HtiConnection.Connected_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   247
					longDesc = Messages.getString("HtiConnection.Connected_LongStatusMsg") + version.toString(); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   248
				} else {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   249
					testStatus = EStatus.DOWN;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   250
					shortDesc = Messages.getString("HtiConnection.NotConnected_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   251
					longDesc = Messages.getString("HtiConnection.ConnectionFailed_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   252
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   253
			} else {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   254
				testStatus = EStatus.DOWN;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   255
				shortDesc = Messages.getString("HtiConnection.NotConnected_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   256
				longDesc = Messages.getString("HtiConnection.FailedToStartGateway_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   257
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   258
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   259
		} catch (Exception e) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   260
			if (e.getMessage().equals("com.nokia.HTI.HTIException: HTI NOT INITIALIZED")) { //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   261
				// HTI agent is not responding
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   262
				testStatus = EStatus.DOWN;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   263
				shortDesc = Messages.getString("HtiConnection.NotConnected_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   264
				longDesc = Messages.getString("HtiConnection.HtiNotResponding_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   265
			} else {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   266
				// Connection could not be created
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   267
				testStatus = EStatus.DOWN;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   268
				shortDesc = Messages.getString("HtiConnection.NotConnected_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   269
				longDesc = Messages.getString("HtiConnection.ConnectionFailed_StatusMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   270
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   271
		} finally {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   272
			stopConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   273
			setCurrentConnection(origConn, false);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   274
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   275
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   276
		return new HTIConnectionStatus(null, testStatus, shortDesc, longDesc);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   277
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   278
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   279
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   280
	 * Set HTI version to which HTI Connection is currently connected.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   281
	 * @param version HTI version to which HTI Connection is currently connected.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   282
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   283
	public void setHTIVersion(HTIVersion version) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   284
		this.version = version;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   285
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   286
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   287
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   288
	 * Get HTI version to which HTI Connection is currently connected.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   289
	 * @return HTI version to which HTI Connection is currently connected,
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   290
	 * or null if connection is down.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   291
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   292
	public HTIVersion getHTIVersion() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   293
		return version;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   294
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   295
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   296
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   297
	 * Returns all connections which support connection types that HTI Connection can use. 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   298
	 * @return List of connections that support connection types that HTI Connection can use.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   299
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   300
	public List<IConnection> getHTIConnections() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   301
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   302
		// Getting needed variables.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   303
		Collection<IConnection> connections = RemoteConnectionsActivator.getConnectionsManager().getConnections();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   304
		Collection<String> connectionTypes = HTIService.getCompatibleConnectionTypeIds();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   305
		List<IConnection> htiConnections = new ArrayList<IConnection>();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   306
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   307
		// Going through all connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   308
		for(IConnection conn : connections) {	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   309
			if(connectionTypes.contains(conn.getConnectionType().getIdentifier())) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   310
				htiConnections.add(conn);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   311
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   312
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   313
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   314
		return htiConnections;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   315
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   316
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   317
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   318
	 * Returns current status of connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   319
	 * @return Current status of connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   320
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   321
	public ConnectionStatus getConnectionStatus() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   322
		synchronized(currentStatus) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   323
			return currentStatus;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   324
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   325
	}
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
	 * Updates connection status in views description.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   329
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   330
	public void updateConnectionStatus() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   331
		Runnable updater = new ConnectionStatusUpdater(getCurrentConnection());
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   332
		// Updating connection status asynchronously in default UI thread.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   333
		Display.getDefault().asyncExec(updater);
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
	 * Sets current status of connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   338
	 * @param currentStatus The currentStatus to set.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   339
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   340
	public void setConnectionStatus(ConnectionStatus newStatus) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   341
		// Flagging status change information.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   342
		boolean connectionStarted = false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   343
		boolean connectionTerminated = false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   344
		boolean connectionUp = false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   345
		boolean connectionDown = false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   346
		boolean needsUpdate = false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   347
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   348
		// Checking new status and updating current status.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   349
		synchronized(currentStatus) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   350
			if (newStatus == ConnectionStatus.SHUTDOWN
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   351
					&& currentStatus != ConnectionStatus.SHUTDOWN) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   352
				// Shutting down the connection and connection tester.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   353
				connectionChecker = null;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   354
				connectionDown = true;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   355
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   356
			else if (newStatus == ConnectionStatus.CONNECTED
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   357
					&& currentStatus != ConnectionStatus.CONNECTED) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   358
				connectionUp = true;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   359
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   360
			else if (newStatus != ConnectionStatus.CONNECTED
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   361
					&& currentStatus == ConnectionStatus.CONNECTED) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   362
				connectionDown = true;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   363
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   364
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   365
			// Checking if it is needed to start/stop using current connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   366
			if (newStatus != ConnectionStatus.SHUTDOWN
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   367
					&& currentStatus == ConnectionStatus.SHUTDOWN) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   368
				connectionStarted = true;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   369
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   370
			else if (newStatus == ConnectionStatus.SHUTDOWN
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   371
					&& currentStatus != ConnectionStatus.SHUTDOWN) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   372
				connectionTerminated = true;
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
			needsUpdate = (currentStatus != newStatus);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   376
			currentStatus = newStatus;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   377
		} // End synchronized
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   378
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   379
		// Updating and informing outside of the synchronized block to prevent
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   380
		// synchronization slowing down connection status queries.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   381
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   382
		if(needsUpdate) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   383
			updateConnectionStatus();
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
		if(connectionUp) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   387
			// Inform listeners of starting the connection
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   388
			HtiConnectionManager.getInstance().informConnectionStarted();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   389
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   390
		else if(connectionDown) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   391
			// Inform listeners of connection termination
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   392
			HtiConnectionManager.getInstance().informConnectionTerminated();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   393
			setHTIVersion(null);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   394
			RequestQueueManager.getInstance().cancelRequestsInQueue();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   395
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   396
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   397
		if(connectionStarted) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   398
			// Connection needs to be set as used, before starting datagateway.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   399
			// This must be done as last operation, because useConnection can take some time and
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   400
			// it is good to update other components first to prevent information lagging behind.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   401
			IConnection conn = getCurrentConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   402
			if(conn != null) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   403
				conn.useConnection(true);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   404
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   405
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   406
		else if(connectionTerminated) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   407
			// Connection is not needed anymore.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   408
			IConnection conn = getCurrentConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   409
			if(conn != null) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   410
				conn.useConnection(false);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   411
			}
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
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   415
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   416
	 * Shows message to user if exception is caused by communication problems.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   417
	 * This method also changes connection state to CONNECTNG, so service won't accept further requests.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   418
	 * @param exception Exception to be checked and reported.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   419
	 * @return Returns true if error was reported and connection is set to CONNECTING state, false otherwise.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   420
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   421
	public boolean reportConnectionError(ExecutionException exception) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   422
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   423
		String msg;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   424
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   425
		// Checking if there are problems with connection and handling problems.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   426
		if (exception.getMessage().equals("com.nokia.HTI.HTIException: HTI NOT INITIALIZED")) { //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   427
			msg = Messages.getString("HtiConnection.HtiNotResponding_MsgBoxMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   428
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   429
		else if( exception.getMessage().equals("java.net.ConnectException: Connection refused: connect")) { //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   430
			msg = Messages.getString("HtiConnection.ConnectionFailed_MsgBoxMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   431
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   432
		else {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   433
			// HTI was initialized correctly and connection was established.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   434
			// So this shouldn't be connection issue and no need to change to CONNECTING state.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   435
			return false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   436
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   437
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   438
		if(HtiConnection.getInstance().getConnectionStatus() != ConnectionStatus.SHUTDOWN) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   439
			// Setting connection status to testing state here to prevent extra error messages.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   440
			HtiConnection.getInstance().setConnectionStatus(ConnectionStatus.CONNECTING);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   441
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   442
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   443
		ErrorDialogRunnable runnable = new ErrorDialogRunnable(msg);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   444
		Display.getDefault().asyncExec(runnable);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   445
		return true;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   446
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   447
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   448
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   449
	 * Sets currently used connection. Saves connection to preferences, if it isn't a test connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   450
	 * @param currentConnection Connection to be set.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   451
	 * @param isTestConnection True if connection is set for test purposes. False otherwise.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   452
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   453
	public synchronized void setCurrentConnection(IConnection connection, boolean isTestConnection) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   454
		if(currentConnection != null && currentConnection != connection) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   455
			// Connection can be set as not used, because HTI Connection uses only currentConnection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   456
			currentConnection.useConnection(false);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   457
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   458
		this.currentConnection = connection;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   459
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   460
		if(!isTestConnection) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   461
			// 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
   462
			String connectionID = (currentConnection == null) ? HtiApiPreferenceConstants.DEFAULT_CONNECTION_ID 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   463
					: currentConnection.getIdentifier();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   464
			if (!connectionID.equals(HtiApiActivator.getPreferences().getConnectionID())) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   465
				HtiApiActivator.getPreferences().setConnectionID(connectionID);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   466
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   467
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   468
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   469
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   470
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   471
	 * Gets currently used connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   472
	 * @return Currently used connection, or <code>null</code> if connection has not been set.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   473
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   474
	public synchronized IConnection getCurrentConnection() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   475
		return currentConnection;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   476
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   477
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   478
	/* (non-Javadoc)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   479
	 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener#connectionStoreChanged()
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   480
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   481
	public void connectionStoreChanged() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   482
		// Initializing needed variables.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   483
		IConnection currConn = getCurrentConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   484
		if(currConn == null) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   485
			// Nothing to do, if no connection is selected.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   486
			return;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   487
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   488
		String connID = currConn.getIdentifier();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   489
		Collection<IConnection> connections = RemoteConnectionsActivator.getConnectionsManager().getConnections();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   490
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   491
		// Checking if current connection still exists.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   492
		boolean isFound = false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   493
		for(IConnection conn : connections) {	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   494
			if(conn.getIdentifier().equals(connID)) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   495
				isFound = true;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   496
				break;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   497
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   498
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   499
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   500
		if(!isFound) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   501
			// Connection doesn't exist anymore. Setting current connection as null.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   502
			setCurrentConnection(null, false);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   503
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   504
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   505
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   506
	/* (non-Javadoc)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   507
	 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener#displayChanged()
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   508
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   509
	public void displayChanged() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   510
		// Not implemented.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   511
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   512
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   513
	//
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   514
	// Private classes.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   515
	//
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   516
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   517
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   518
	 * Thread that tests connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   519
	 * Connection needs to be tested when new connection has been started
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   520
	 * or there has been problems with current connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   521
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   522
	private class ConnectionCheckerThread extends Thread {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   523
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   524
		/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   525
		 * Time in ms that is waited between testing the connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   526
		 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   527
		private static final long TEST_DELAY = 1000;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   528
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   529
		/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   530
		 * Keeps track for first connection try. This is needed if there
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   531
		 * happens connection error when testing connection first time.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   532
		 * On other times connection failure information comes from outside
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   533
		 * of this class.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   534
		 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   535
		private boolean firstConnection = true;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   536
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   537
		/* (non-Javadoc)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   538
		 * @see java.lang.Thread#run()
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   539
		 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   540
		public void run() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   541
			Thread thisThread = Thread.currentThread();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   542
			while(thisThread == connectionChecker) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   543
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   544
				if(getConnectionStatus() == ConnectionStatus.CONNECTING) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   545
					testConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   546
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   547
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   548
				try {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   549
					Thread.sleep(TEST_DELAY);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   550
				} catch (InterruptedException e) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   551
					// Not needed to handle. Just continuing polling in this case.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   552
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   553
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   554
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   555
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   556
		/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   557
		 * Tests if current connection works and modifies connection status
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   558
		 * in HtiApiActivator class if needed.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   559
		 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   560
		private void testConnection() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   561
			try {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   562
				boolean connectionUp = RequestQueueManager.getInstance().testConnection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   563
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   564
				if(connectionUp) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   565
					setConnectionStatus(ConnectionStatus.CONNECTED);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   566
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   567
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   568
			} catch (ExecutionException e) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   569
				if(firstConnection && getConnectionStatus() != ConnectionStatus.SHUTDOWN) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   570
					// Other than first connection errors are reported from RequestQueueManager.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   571
					// This handles only first connection problem when trying to connect to the device.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   572
					reportConnectionError(e);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   573
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   574
			} catch (Exception e) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   575
				// Catching other errors that doesn't need any specific actions.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   576
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   577
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   578
			firstConnection = false;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   579
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   580
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   581
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   582
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   583
	 * Updates connection status and buttons in UI.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   584
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   585
	private class ConnectionStatusUpdater implements Runnable {
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
		 * Connection that needs status update.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   589
		 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   590
		private final IConnection onnectionToUpdate;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   591
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   592
		/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   593
		 * Constructor.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   594
		 * @param onnectionToUpdate Connection that needs status update.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   595
		 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   596
		public ConnectionStatusUpdater(IConnection onnectionToUpdate) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   597
			this.onnectionToUpdate = onnectionToUpdate;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   598
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   599
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   600
		/* (non-Javadoc)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   601
		 * @see java.lang.Runnable#run()
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   602
		 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   603
		public void run() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   604
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   605
			// Connection status needs to be updated in connected service.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   606
			if(onnectionToUpdate != null) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   607
				Collection<IConnectedService> services = RemoteConnectionsActivator.getConnectionsManager().getConnectedServices(onnectionToUpdate);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   608
				// Collection can be null if getting services for test connection.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   609
				if(services != null) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   610
					for(IConnectedService service : services) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   611
						if(service instanceof HTIConnectedService) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   612
							((HTIConnectedService)service).refreshStatus();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   613
						}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   614
					}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   615
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   616
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   617
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   618
			try {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   619
				MainView view = MainView.getViewInstance(false);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   620
				view.updateActionButtonStates();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   621
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   622
				// Updating connection description.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   623
				switch (getConnectionStatus()) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   624
				case CONNECTED:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   625
					view.updateDescription(Messages.getString("HtiConnection.Connected_ToolBar_Msg") + onnectionToUpdate.getDisplayName()); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   626
					break;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   627
				case CONNECTING:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   628
					view.updateDescription(Messages.getString("HtiConnection.Connecting_ToolBar_Msg") + onnectionToUpdate.getDisplayName()); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   629
					break;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   630
				case SHUTDOWN:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   631
					view.updateDescription(Messages.getString("HtiConnection.NotConnected_ToolBar_Msg")); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   632
					break;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   633
				case TESTING:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   634
					view.updateDescription(Messages.getString("HtiConnection.Testing_ToolBar_Msg")); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   635
					break;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   636
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   637
			} catch (PartInitException e) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   638
				// No need to handle. Description doesn't need update if part is not visible.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   639
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   640
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   641
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   642
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   643
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   644
	 * Runnable that opens error message box.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   645
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   646
	private class ErrorDialogRunnable implements Runnable {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   647
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   648
		/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   649
		 * Error message.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   650
		 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   651
		private final String errorMessage;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   652
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   653
		/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   654
		 * Constructor.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   655
		 * @param errorMessage Message to be shown in dialog.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   656
		 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   657
		public ErrorDialogRunnable(String errorMessage) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   658
			this.errorMessage = errorMessage;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   659
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   660
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   661
		/* (non-Javadoc)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   662
		 * @see java.lang.Runnable#run()
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   663
		 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   664
		public void run() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   665
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   666
			// No need to show error message if preferences page is already created.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   667
			if(HtiApiPreferencePage.isCreated()) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   668
				// Showing plain error message if preferences page is open.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   669
				ErrorDialogWithHelp msgBox = new ErrorDialogWithHelp(errorMessage, SWT.OK);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   670
				msgBox.open();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   671
			} else {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   672
				// Asking if user wants to manage preferences.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   673
				String msgWithQuestionStr = errorMessage + Messages.getString("HtiConnection.ManageConnections_ErrMsg"); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   674
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   675
				ErrorDialogWithHelp msgBox = new ErrorDialogWithHelp(msgWithQuestionStr, SWT.YES | SWT.NO);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   676
				int result = msgBox.open();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   677
				if(result == SWT.YES && !HtiApiPreferencePage.isCreated()){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   678
					// Opening preferences if Yes is selected.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   679
					OpenPreferencePageAction openPreferencesAction = new OpenPreferencePageAction();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   680
					openPreferencesAction.run();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   681
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   682
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   683
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   684
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   685
}