001package jmri.jmrit.vsdecoder;
002
003import java.beans.PropertyChangeEvent;
004import org.jdom2.Element;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * Integer trigger.
010 *
011 * <hr>
012 * This file is part of JMRI.
013 * <p>
014 * JMRI is free software; you can redistribute it and/or modify it under
015 * the terms of version 2 of the GNU General Public License as published
016 * by the Free Software Foundation. See the "COPYING" file for a copy
017 * of this license.
018 * <p>
019 * JMRI is distributed in the hope that it will be useful, but WITHOUT
020 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
021 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
022 * for more details.
023 *
024 * @author Mark Underwood Copyright (C) 2011
025 */
026class IntTrigger extends Trigger {
027
028    int notch;
029    CompareType compare_type;
030
031    public IntTrigger(String name) {
032        this(name, 0, CompareType.EQ);
033    }
034
035    public IntTrigger(String name, int next, Trigger.CompareType ct) {
036        super(name);
037        this.setTriggerType(Trigger.TriggerType.INT);
038        notch = next;
039        compare_type = ct;
040    }
041
042    public void setMatchValue(int next) {
043        notch = next;
044    }
045
046    public int getMatchValue() {
047        return notch;
048    }
049
050    public void setCompareType(IntTrigger.CompareType ct) {
051        compare_type = ct;
052    }
053
054    public CompareType getCompareType() {
055        return compare_type;
056    }
057
058    @Override
059    public void propertyChange(PropertyChangeEvent event) {
060        int next;
061        boolean compare = false;
062
063        // Validate
064        // If no target, or not a name match, or no trigger, or no action
065        // then just return quickly.
066        // Careful: Takes advantage of "lazy OR" behavior
067        if (target == null) {
068            log.debug("Quit.  No target.");
069            return;
070        }
071        if (event.getPropertyName().equals(this.getEventName()) != true) {
072            log.debug("Quit. Event name mismatch event: {}, this:= {}", event.getPropertyName(), this.getEventName());
073            return;
074        }
075        if (this.getTriggerType() == TriggerType.NONE) {
076            log.debug("Quit.  TriggerType = NONE");
077            return;
078        }
079        if (this.getTargetAction() == TargetAction.NOTHING) {
080            log.debug("Quit.  TargetAction = NOTHING");
081            return;
082        }
083
084        // Compare
085        next = (Integer) event.getNewValue();
086        switch (compare_type) {
087            case GT:
088                compare = (next > notch);
089                break;
090            case LT:
091                compare = (next < notch);
092                break;
093            case GTE:
094                compare = (next >= notch);
095                break;
096            case LTE:
097                compare = (next <= notch);
098                break;
099            case EQ:
100            default:
101                compare = (next == notch);
102                break;
103        }
104
105        if (compare) {
106            this.callback.takeAction();
107        }
108    }
109
110    @Override
111    public void setXml(Element e) {
112        // Grab common stuff.
113        super.setXml(e);
114        // Only do this if this is a BoolTrigger type Element
115        if (e.getAttribute("type").getValue().equals("INT")) {
116            notch = Integer.parseInt(e.getChild("match").getValue());
117            compare_type = Trigger.CompareType.valueOf(e.getChild("compare-type").getValue().toUpperCase());
118        }
119    }
120
121    private final static Logger log = LoggerFactory.getLogger(IntTrigger.class);
122
123}