001package jmri.jmrit.vsdecoder;
002
003import java.beans.PropertyChangeEvent;
004import org.jdom2.Element;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * Boolean 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 BoolTrigger extends Trigger {
027
028    boolean match_value;
029
030    public BoolTrigger(String name) {
031        this(name, false);
032    }
033
034    public BoolTrigger(String name, boolean bv) {
035        super(name);
036        this.setTriggerType(Trigger.TriggerType.BOOLEAN);
037        match_value = bv;
038    }
039
040    public void setMatchValue(boolean bv) {
041        match_value = bv;
042    }
043
044    public boolean getMatchValue() {
045        return match_value;
046    }
047
048    @Override
049    public void propertyChange(PropertyChangeEvent event) {
050        // Validate
051        // If no target, or not a name match, or no trigger, or no action
052        // then just return quickly.
053        // Careful: Takes advantage of "lazy OR" behavior
054        if (target == null) {
055            //log.debug("Quit.  No target.");
056            return;
057        }
058        if (!event.getPropertyName().equals(this.getEventName())) {
059            //log.debug("Quit. Event name mismatch event: {}, this: {}", event.getPropertyName(), this.getEventName());
060            return;
061        }
062        if (this.getTriggerType() == TriggerType.NONE) {
063            //log.debug("Quit.  TriggerType = NONE");
064            return;
065        }
066        if (this.getTargetAction() == TargetAction.NOTHING) {
067            //log.debug("Quit.  TargetAction = NOTHING");
068            return;
069        }
070
071        // Compare
072        if (match_value == (Boolean) event.getNewValue()) {
073            this.callback.takeAction();
074        }
075    }
076
077    @Override
078    public Element getXml() {
079        Element me = new Element("trigger");
080
081        log.debug("Bool Trigger getXml():");
082        log.debug("  trigger_name = {}", this.getName());
083        log.debug("  event_name = {}", this.event_name);
084        log.debug("  target_name = {}", target.getName());
085        log.debug("  match = {}", Boolean.valueOf(match_value).toString());
086        log.debug("  action = {}", this.getTriggerType().toString());
087
088        me.setAttribute("name", this.getName());
089        me.setAttribute("type", "BOOLEAN");
090        me.addContent(new Element("event-name").addContent(event_name));
091        me.addContent(new Element("target-name").addContent(target.getName()));
092        me.addContent(new Element("match").addContent(Boolean.valueOf(match_value).toString()));
093        me.addContent(new Element("action").addContent(this.getTriggerType().toString()));
094
095        return me;
096    }
097
098    @Override
099    public void setXml(Element e) {
100        // Get common stuff
101        super.setXml(e);
102        // Only do this if this is a BoolTrigger type Element
103        if (e.getAttribute("type").getValue().equals("BOOLEAN")) {
104            match_value = Boolean.parseBoolean(e.getChild("match").getValue());
105        }
106    }
107
108    private final static Logger log = LoggerFactory.getLogger(BoolTrigger.class);
109
110}