001package jmri.jmrit.vsdecoder;
002
003import java.beans.PropertyChangeEvent;
004import java.beans.PropertyChangeListener;
005import org.jdom2.Element;
006
007/**
008 * Superclass for all VSD trigger types.
009 *
010 * <hr>
011 * This file is part of JMRI.
012 * <p>
013 * JMRI is free software; you can redistribute it and/or modify it under
014 * the terms of version 2 of the GNU General Public License as published
015 * by the Free Software Foundation. See the "COPYING" file for a copy
016 * of this license.
017 * <p>
018 * JMRI is distributed in the hope that it will be useful, but WITHOUT
019 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
020 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
021 *
022 * @author Mark Underwood Copyright (C) 2011
023 */
024abstract public class Trigger implements PropertyChangeListener {
025
026    static public enum TriggerType {
027
028        BUTTON, BOOLEAN, STRING, NONE, NOTCH, INT, FLOAT, THROTTLE
029    }
030
031    static public enum TargetAction {
032
033        PLAY, LOOP, STOP, FADEIN, FADEOUT, NOTCH, CHANGE, NOTHING, STOP_AT_ZERO
034    }
035
036    static public enum CompareType {
037
038        EQ, GT, LT, GTE, LTE
039    }
040
041    static public enum CompareValueType {
042
043        INT, FLOAT
044    }
045
046    String trigger_name; // Name for the trigger object
047    String event_name;   // event to respond to
048    String target_name;  // target to act on
049
050    VSDSound target;     // sound to work on
051    private TargetAction target_action; // action to take
052    private TriggerType trigger_type;
053    TriggerListener callback;
054
055    public Trigger(String name) {
056        trigger_name = name;
057        event_name = "";
058        target = null;
059        target_action = TargetAction.NOTHING;
060        trigger_type = TriggerType.NONE;
061    }
062
063    @Override
064    abstract public void propertyChange(PropertyChangeEvent event);
065
066    // JavaBean set/get functions
067    public void setName(String tn) {
068        trigger_name = tn;
069    }
070
071    public String getName() {
072        return trigger_name;
073    }
074
075    public void setEventName(String en) {
076        event_name = en;
077    }
078
079    public String getEventName() {
080        return event_name;
081    }
082
083    public void setTarget(VSDSound tgt) {
084        target = tgt;
085    }
086
087    public VSDSound getTarget() {
088        return target;
089    }
090
091    public void setTargetName(String tn) {
092        target_name = tn;
093    }
094
095    public String getTargetName() {
096        return target_name;
097    }
098
099    public void setTargetAction(Trigger.TargetAction ta) {
100        target_action = ta;
101    }
102
103    public Trigger.TargetAction getTargetAction() {
104        return target_action;
105    }
106
107    public void setTriggerType(Trigger.TriggerType ta) {
108        trigger_type = ta;
109    }
110
111    public Trigger.TriggerType getTriggerType() {
112        return trigger_type;
113    }
114
115    public void setCallback(TriggerListener cb) {
116        callback = cb;
117    }
118
119    public TriggerListener getCallback() {
120        return callback;
121    }
122
123    public Element getXml() {
124        Element me = new Element("Trigger");
125        me.setAttribute("name", trigger_name);
126        me.setAttribute("type", "empty");
127        // do something, eventually...
128        return me;
129    }
130
131    public void setXml(Element e) {
132        // Grab XML content that's common to all Triggers
133        trigger_name = e.getAttributeValue("name");
134        event_name = e.getChild("event-name").getValue();
135        target_name = e.getChild("target-name").getValue();
136        if (e.getChild("action") != null) {
137            try {
138                this.setTargetAction(Trigger.TargetAction.valueOf(e.getChild("action").getValue()));
139            } catch (IllegalArgumentException iea) {
140                this.setTargetAction(Trigger.TargetAction.NOTHING);
141            }
142        }
143    }
144
145}