htiextension/com.nokia.s60tools.hticonnection/src/com/nokia/s60tools/hticonnection/preferences/HtiApiPreferences.java
author Matti Laitinen <matti.t.laitinen@nokia.com>
Thu, 04 Mar 2010 13:38:40 +0200
changeset 7 018264c44c89
parent 0 61163b28edca
permissions -rw-r--r--
HTI Connection v1.3.1 - Supporting Carbide 2.5

/*
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
*/

package com.nokia.s60tools.hticonnection.preferences;

import java.util.Collection;
import java.util.List;

import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
import com.nokia.carbide.remoteconnections.interfaces.IConnection;
import com.nokia.s60tools.hticonnection.HtiApiActivator;
import com.nokia.s60tools.hticonnection.core.HtiConnection;

/**
 * Helper class to use HTI API preferences. Use this class for accessing
 * HTI API preferences instead of accessing directly through
 * {@link org.eclipse.jface.util.IPropertyChangeListener.IPreferenceStore}.
 * 
 * However this class should be used via <code>IHtiApiPreferences</code>
 * class, that could be queried from <code>HtiApiActivator</code> with
 * the following method <code>HtiApiActivator.getPreferences()</code>.
 * @see
 */
public class HtiApiPreferences implements IHtiApiPreferences {

	/**
	 * The type of "current" type of connection.
	 */
	public static String SELECTION_ID_CURRENT = "com.nokia.carbide.remoteConnections.currentConnection";
	
	/* (non-Javadoc)
	 * @see com.nokia.s60tools.hticonnection.preferences.IHtiApiPreferences#initDefaultValues()
	 */
	public void initDefaultValues() {
		// COM port
		if(!HtiApiActivator.getPrefsStore().getDefaultString(HtiApiPreferenceConstants.CONNECTION_ID).
				equals(HtiApiPreferenceConstants.DEFAULT_CONNECTION_ID)){
			
			HtiApiActivator.getPrefsStore().setDefault(HtiApiPreferenceConstants.CONNECTION_ID, 
					HtiApiPreferenceConstants.DEFAULT_CONNECTION_ID);
		}
	}

	/* (non-Javadoc)
	 * @see com.nokia.s60tools.hticonnection.preferences.IHtiApiPreferences#getConnectionID()
	 */
	public String getConnectionID() {
		return HtiApiActivator.getPrefsStore().getString(
				HtiApiPreferenceConstants.CONNECTION_ID);
	}

	/* (non-Javadoc)
	 * @see com.nokia.s60tools.hticonnection.preferences.IHtiApiPreferences#setConnectionID(java.lang.String)
	 */
	public void setConnectionID(String connectionID) {
		HtiApiActivator.getPrefsStore().setValue(
				HtiApiPreferenceConstants.CONNECTION_ID, connectionID);
	}
	
	/* (non-Javadoc)
	 * @see com.nokia.s60tools.hticonnection.preferences.IHtiApiPreferences#getCurrentConnection()
	 */
	public IConnection getCurrentConnection() {
		// Initializing needed variables.
		String connID = getConnectionID();
		Collection<IConnection> connections = RemoteConnectionsActivator.getConnectionsManager().getConnections();
		
		// Checking for correct connection.
		for(IConnection conn : connections) {	
			if(conn.getIdentifier().equals(connID)) {
				return conn;
			}
		}
		
		// Connection set in preferences was not found. Checking if there is only one connection.

		List<IConnection> htiConnections = HtiConnection.getInstance().getHTIConnections();
		
		// Do not change default if it is "current connection".
		if (!HtiApiActivator.getPreferences().getConnectionID().equals(HtiApiPreferences.SELECTION_ID_CURRENT) &&
			htiConnections.size() == 1) {
			// Only one compatible connection was found. Using it as default.
			IConnection conn = htiConnections.get(0);
			HtiApiActivator.getPreferences().setConnectionID(conn.getIdentifier());
			return conn;
		}
		
		// Valid connection was not found.
		return null;
	}
}