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