001package jmri.jmrix.jinput;
002
003import java.util.HashMap;
004import javax.swing.tree.DefaultMutableTreeNode;
005import jmri.InstanceManager;
006import jmri.JmriException;
007import jmri.Sensor;
008import net.java.games.input.Component;
009import net.java.games.input.Controller;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * UsbNode represents the USB controllers or component.
015 * <p>
016 * Generally accessed via the TreeModel.
017 * <p>
018 * Can be connected to a JMRI Sensor or Memory.
019 *
020 * @author Bob Jacobsen Copyright 2008
021 */
022public class UsbNode extends DefaultMutableTreeNode {
023
024    String name;
025    Controller controller;
026    Component component;
027
028    UsbNode(String name, Controller controller, Component component) {
029        super(name);
030        this.name = name;
031        this.controller = controller;
032        this.component = component;
033    }
034
035    @Override
036    public int hashCode() {
037        if (controller == null) {
038            return super.hashCode();
039        } else {
040            return controller.hashCode();
041        }
042    }
043
044    public Controller getController() {
045        return controller;
046    }
047
048    public Component getComponent() {
049        return component;
050    }
051
052    @Override
053    public boolean equals(Object a) {
054        boolean result = false;
055        if ((a != null) && (a instanceof UsbNode)) {
056            UsbNode usbNode = (UsbNode) a;
057            result = (name.equals(usbNode.name)
058                    && (controller == usbNode.controller)
059                    && (component == usbNode.component));
060        }
061        return result;
062    }
063
064    public void setValue(float val) {
065        this.val = val;
066        // if attached, set value
067        try {
068            if ((attachedSensor != null) && (!attachedSensor.equals(""))) {
069                InstanceManager.sensorManagerInstance()
070                        .provideSensor(attachedSensor).setKnownState(
071                        val > 0.0 ? Sensor.ACTIVE : Sensor.INACTIVE);
072            }
073        } catch (IllegalArgumentException | JmriException e1) {
074            log.error("Can't set sensor", e1);
075        }
076        try {
077            if ((attachedMemory != null) && (!attachedMemory.equals(""))) {
078                InstanceManager.memoryManagerInstance()
079                        .provideMemory(attachedMemory).setValue("" + val);
080            }
081        } catch (IllegalArgumentException e2) {
082            log.error("Can't set memory", e2);
083        }
084    }
085
086    public float getValue() {
087        return val;
088    }
089    float val = -1;
090
091    String attachedSensor = null;
092
093    public void setAttachedSensor(String sensor) {
094        attachedSensor = sensor;
095    }
096
097    public String getAttachedSensor() {
098        return attachedSensor;
099    }
100
101    String attachedMemory = null;
102
103    public void setAttachedMemory(String memory) {
104        attachedMemory = memory;
105    }
106
107    public String getAttachedMemory() {
108        return attachedMemory;
109    }
110
111    public String getName() {
112        return name;
113    }
114
115    /**
116     * Get a specific node. This is used instead of a constructor to ensure that
117     * node objects for a given USB object are unique.
118     *
119     * @param name       the node name
120     * @param controller the input controller
121     * @param component  the input component
122     * @return the node, either an existing node with the same controller or
123     *         component, or newly created
124     */
125    static public UsbNode getNode(String name, Controller controller, Component component) {
126        Object key = (component != null) ? component : controller;
127        UsbNode result = NODES.get(key);
128        if (result == null) {
129            result = new UsbNode(name, controller, component);
130            NODES.put(key, result);
131        }
132        return result;
133    }
134
135    private static final HashMap<Object, UsbNode> NODES = new HashMap<>();
136
137    private final static Logger log = LoggerFactory.getLogger(UsbNode.class);
138}