view java/src/uk/ac/qmul/eecs/ccmi/haptics/HapticsFactory.java @ 1:e3935c01cde2 tip

moved license of PdPersistenceManager to the beginning of the file
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 19:52:03 +0100
parents 78b7fc5391a2
children
line wrap: on
line source
/*  
 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
  
 Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/  

package uk.ac.qmul.eecs.ccmi.haptics;

import uk.ac.qmul.eecs.ccmi.utils.PreferencesService;

/**
 *
 * Creates an instance of a class implementing the Haptics interface. There can only be one instance of such 
 * class. Therefore the factory uses the singleton pattern to always return the same object 
 * after it is created.
 *
 */
public class HapticsFactory {
	/**
	 * Creates a new instance of {@code Haptics}. If an Omni Haptic can be successfully initialised
	 * {@code Haptics} will handle the device, otherwise all the calls to the object will have no effect.
	 *  
	 * @param listener an haptic commands listener to link to the {@code Haptics} instance.
	 */
	public static void createInstance(HapticListenerThread listener) {
		if(hapticsInstance != null)
			throw new IllegalStateException("create instance must be called once only");
		
		/* use the preferences service to pick the right device to use (the one the user selected *
		 * in their preferences). This allows to avoid loading the .dll file for the other device *
		 * that the user doesn't need, which would entail a waste of memory and, more important   *
		 * a conflict between function names.                                                     */
		String defaultDevice = PreferencesService.getInstance().get("haptic_device", TABLET_ID);
		
		/* temporary set the preference to default: if the device crashes during init * 
		 * the user will still be able to restart the diagram editor                  */
		PreferencesService.getInstance().put("haptic_device", TABLET_ID);
		
		if(PHANTOM_ID.equals(defaultDevice) || true){//FIXME
			/* OmniHaptics first */
			hapticsInstance = OmniHaptics.createInstance(listener);
			if(hapticsInstance != null){//OmniHaptics instance successfully created: return
				PreferencesService.getInstance().put("haptic_device", defaultDevice);
				return;
			}
		}else if(FALCON_ID.equals(defaultDevice)){ 
			/* Falcon first  */
			hapticsInstance = FalconHaptics.createInstance(listener);
			if(hapticsInstance != null){ //FalconHaptics instance successfully created: return
				PreferencesService.getInstance().put("haptic_device", defaultDevice);
				return;
			}
		}
		
		PreferencesService.getInstance().put("haptic_device", defaultDevice);
		/* no devices available, stop the listener and go for the default */
		if(listener.isAlive()){
			listener.interrupt();
		}
		hapticsInstance = MouseHaptics.createInstance(listener.getNonRunnableListener());
		if(hapticsInstance == null)
			throw new RuntimeException();
		
	}
	
	public static Haptics getInstance(){
		if(hapticsInstance == null){ 
			throw new IllegalStateException("static method createInstance() must be called before getInstance()");
		}
		return hapticsInstance;
	}
	
	private static Haptics hapticsInstance;
	public static final String PHANTOM_ID = "Phantom Omni";
	public static final String FALCON_ID = "Falcon";
	public static final String TABLET_ID = "Tablet/Mouse";
}