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