annotate 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
rev   line source
f@0 1 /*
f@0 2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0 3
f@0 4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0 5
f@0 6 This program is free software: you can redistribute it and/or modify
f@0 7 it under the terms of the GNU General Public License as published by
f@0 8 the Free Software Foundation, either version 3 of the License, or
f@0 9 (at your option) any later version.
f@0 10
f@0 11 This program is distributed in the hope that it will be useful,
f@0 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0 14 GNU General Public License for more details.
f@0 15
f@0 16 You should have received a copy of the GNU General Public License
f@0 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
f@0 18 */
f@0 19
f@0 20 package uk.ac.qmul.eecs.ccmi.haptics;
f@0 21
f@0 22 import uk.ac.qmul.eecs.ccmi.utils.PreferencesService;
f@0 23
f@0 24 /**
f@0 25 *
f@0 26 * Creates an instance of a class implementing the Haptics interface. There can only be one instance of such
f@0 27 * class. Therefore the factory uses the singleton pattern to always return the same object
f@0 28 * after it is created.
f@0 29 *
f@0 30 */
f@0 31 public class HapticsFactory {
f@0 32 /**
f@0 33 * Creates a new instance of {@code Haptics}. If an Omni Haptic can be successfully initialised
f@0 34 * {@code Haptics} will handle the device, otherwise all the calls to the object will have no effect.
f@0 35 *
f@0 36 * @param listener an haptic commands listener to link to the {@code Haptics} instance.
f@0 37 */
f@0 38 public static void createInstance(HapticListenerThread listener) {
f@0 39 if(hapticsInstance != null)
f@0 40 throw new IllegalStateException("create instance must be called once only");
f@0 41
f@0 42 /* use the preferences service to pick the right device to use (the one the user selected *
f@0 43 * in their preferences). This allows to avoid loading the .dll file for the other device *
f@0 44 * that the user doesn't need, which would entail a waste of memory and, more important *
f@0 45 * a conflict between function names. */
f@0 46 String defaultDevice = PreferencesService.getInstance().get("haptic_device", TABLET_ID);
f@0 47
f@0 48 /* temporary set the preference to default: if the device crashes during init *
f@0 49 * the user will still be able to restart the diagram editor */
f@0 50 PreferencesService.getInstance().put("haptic_device", TABLET_ID);
f@0 51
f@0 52 if(PHANTOM_ID.equals(defaultDevice) || true){//FIXME
f@0 53 /* OmniHaptics first */
f@0 54 hapticsInstance = OmniHaptics.createInstance(listener);
f@0 55 if(hapticsInstance != null){//OmniHaptics instance successfully created: return
f@0 56 PreferencesService.getInstance().put("haptic_device", defaultDevice);
f@0 57 return;
f@0 58 }
f@0 59 }else if(FALCON_ID.equals(defaultDevice)){
f@0 60 /* Falcon first */
f@0 61 hapticsInstance = FalconHaptics.createInstance(listener);
f@0 62 if(hapticsInstance != null){ //FalconHaptics instance successfully created: return
f@0 63 PreferencesService.getInstance().put("haptic_device", defaultDevice);
f@0 64 return;
f@0 65 }
f@0 66 }
f@0 67
f@0 68 PreferencesService.getInstance().put("haptic_device", defaultDevice);
f@0 69 /* no devices available, stop the listener and go for the default */
f@0 70 if(listener.isAlive()){
f@0 71 listener.interrupt();
f@0 72 }
f@0 73 hapticsInstance = MouseHaptics.createInstance(listener.getNonRunnableListener());
f@0 74 if(hapticsInstance == null)
f@0 75 throw new RuntimeException();
f@0 76
f@0 77 }
f@0 78
f@0 79 public static Haptics getInstance(){
f@0 80 if(hapticsInstance == null){
f@0 81 throw new IllegalStateException("static method createInstance() must be called before getInstance()");
f@0 82 }
f@0 83 return hapticsInstance;
f@0 84 }
f@0 85
f@0 86 private static Haptics hapticsInstance;
f@0 87 public static final String PHANTOM_ID = "Phantom Omni";
f@0 88 public static final String FALCON_ID = "Falcon";
f@0 89 public static final String TABLET_ID = "Tablet/Mouse";
f@0 90 }