f@0: /* f@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool f@0: f@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) f@0: f@0: This program is free software: you can redistribute it and/or modify f@0: it under the terms of the GNU General Public License as published by f@0: the Free Software Foundation, either version 3 of the License, or f@0: (at your option) any later version. f@0: f@0: This program is distributed in the hope that it will be useful, f@0: but WITHOUT ANY WARRANTY; without even the implied warranty of f@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the f@0: GNU General Public License for more details. f@0: f@0: You should have received a copy of the GNU General Public License f@0: along with this program. If not, see . f@0: */ f@0: f@0: package uk.ac.qmul.eecs.ccmi.haptics; f@0: f@0: import uk.ac.qmul.eecs.ccmi.utils.PreferencesService; f@0: f@0: /** f@0: * f@0: * Creates an instance of a class implementing the Haptics interface. There can only be one instance of such f@0: * class. Therefore the factory uses the singleton pattern to always return the same object f@0: * after it is created. f@0: * f@0: */ f@0: public class HapticsFactory { f@0: /** f@0: * Creates a new instance of {@code Haptics}. If an Omni Haptic can be successfully initialised f@0: * {@code Haptics} will handle the device, otherwise all the calls to the object will have no effect. f@0: * f@0: * @param listener an haptic commands listener to link to the {@code Haptics} instance. f@0: */ f@0: public static void createInstance(HapticListenerThread listener) { f@0: if(hapticsInstance != null) f@0: throw new IllegalStateException("create instance must be called once only"); f@0: f@0: /* use the preferences service to pick the right device to use (the one the user selected * f@0: * in their preferences). This allows to avoid loading the .dll file for the other device * f@0: * that the user doesn't need, which would entail a waste of memory and, more important * f@0: * a conflict between function names. */ f@0: String defaultDevice = PreferencesService.getInstance().get("haptic_device", TABLET_ID); f@0: f@0: /* temporary set the preference to default: if the device crashes during init * f@0: * the user will still be able to restart the diagram editor */ f@0: PreferencesService.getInstance().put("haptic_device", TABLET_ID); f@0: f@0: if(PHANTOM_ID.equals(defaultDevice) || true){//FIXME f@0: /* OmniHaptics first */ f@0: hapticsInstance = OmniHaptics.createInstance(listener); f@0: if(hapticsInstance != null){//OmniHaptics instance successfully created: return f@0: PreferencesService.getInstance().put("haptic_device", defaultDevice); f@0: return; f@0: } f@0: }else if(FALCON_ID.equals(defaultDevice)){ f@0: /* Falcon first */ f@0: hapticsInstance = FalconHaptics.createInstance(listener); f@0: if(hapticsInstance != null){ //FalconHaptics instance successfully created: return f@0: PreferencesService.getInstance().put("haptic_device", defaultDevice); f@0: return; f@0: } f@0: } f@0: f@0: PreferencesService.getInstance().put("haptic_device", defaultDevice); f@0: /* no devices available, stop the listener and go for the default */ f@0: if(listener.isAlive()){ f@0: listener.interrupt(); f@0: } f@0: hapticsInstance = MouseHaptics.createInstance(listener.getNonRunnableListener()); f@0: if(hapticsInstance == null) f@0: throw new RuntimeException(); f@0: f@0: } f@0: f@0: public static Haptics getInstance(){ f@0: if(hapticsInstance == null){ f@0: throw new IllegalStateException("static method createInstance() must be called before getInstance()"); f@0: } f@0: return hapticsInstance; f@0: } f@0: f@0: private static Haptics hapticsInstance; f@0: public static final String PHANTOM_ID = "Phantom Omni"; f@0: public static final String FALCON_ID = "Falcon"; f@0: public static final String TABLET_ID = "Tablet/Mouse"; f@0: }