001package jmri.jmrit.vsdecoder;
002
003import java.awt.event.ActionEvent;
004import java.awt.event.ActionListener;
005import java.beans.PropertyChangeEvent;
006import javax.swing.AbstractButton;
007import jmri.util.swing.JmriMouseEvent;
008import jmri.util.swing.JmriMouseListener;
009import org.jdom2.Element;
010
011/**
012 * Button trigger.
013 *
014 * <hr>
015 * This file is part of JMRI.
016 * <p>
017 * JMRI is free software; you can redistribute it and/or modify it under
018 * the terms of version 2 of the GNU General Public License as published
019 * by the Free Software Foundation. See the "COPYING" file for a copy
020 * of this license.
021 * <p>
022 * JMRI is distributed in the hope that it will be useful, but WITHOUT
023 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
024 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
025 * for more details.
026 *
027 * @author Mark Underwood Copyright (C) 2011
028 */
029public class ButtonTrigger extends Trigger implements ActionListener, JmriMouseListener {
030
031    enum ButtonAction {
032    }
033
034    boolean match_value;
035
036    public ButtonTrigger(String name) {
037        this(name, false);
038    }
039
040    public ButtonTrigger(String name, boolean bv) {
041        super(name);
042        this.setTriggerType(Trigger.TriggerType.BUTTON);
043        match_value = bv;
044    }
045
046    public void setMatchValue(boolean bv) {
047        match_value = bv;
048    }
049
050    public boolean getMatchValue() {
051        return match_value;
052    }
053
054    // Button action functions called directly from the enclosing SoundEvent.
055    public void mouseDown() {
056        log.debug("buttonTrigger {} mouseDown() called.", getName());
057        if (match_value) {
058            this.callback.takeAction();
059        }
060    }
061
062    public void mouseUp() {
063        log.debug("buttonTrigger {} mouseUp() called.", getName());
064        if (!match_value) {
065            this.callback.takeAction();
066        }
067    }
068
069    public void click(boolean v) {
070        log.debug("buttonTrigger {} click({}) called.", getName(), v);
071        if (v == match_value) {
072            this.callback.takeAction();
073        }
074    }
075
076    // PropertyChangeListener functions
077    @Override
078    public void propertyChange(PropertyChangeEvent event) {
079        // Button triggers respond to the button methods above, not to
080        // property change events.  Do nothing.
081    }
082
083    // ActionListener function(s)
084    @Override
085    public void actionPerformed(ActionEvent e) {
086        log.debug("ButtonTrigger.actionPerformed() {}", this.getName());
087        this.click(((AbstractButton) e.getSource()).isSelected());
088    }
089
090    // MouseListener functions
091    @Override
092    public void mousePressed(JmriMouseEvent e) {
093        log.debug("MouseListener.mousePressed() {}", this.getName());
094        this.mouseDown();
095    }
096
097    @Override
098    public void mouseReleased(JmriMouseEvent e) {
099        log.debug("MouseListener.mouseReleased() {}", this.getName());
100        this.mouseUp();
101    }
102
103    @Override
104    public void mouseEntered(JmriMouseEvent e) {
105    }
106
107    @Override
108    public void mouseExited(JmriMouseEvent e) {
109    }
110
111    @Override
112    public void mouseClicked(JmriMouseEvent e) {
113    }
114
115    @Override
116    public Element getXml() {
117        Element me = new Element("Trigger");
118
119        log.debug("Bool Trigger getXml():");
120        log.debug("  trigger_name = {}", this.getName());
121        log.debug("  event_name = {}", this.event_name);
122        log.debug("  target_name = {}", target.getName());
123        log.debug("  match = {}", Boolean.valueOf(match_value).toString());
124        log.debug("  action = {}", this.getTriggerType().toString());
125
126        me.setAttribute("name", this.getName());
127        me.setAttribute("type", "BOOLEAN");
128        me.addContent(new Element("event-name").addContent(event_name));
129        me.addContent(new Element("target-name").addContent(target.getName()));
130        me.addContent(new Element("match").addContent(Boolean.valueOf(match_value).toString()));
131        me.addContent(new Element("action").addContent(this.getTriggerType().toString()));
132
133        return me;
134    }
135
136    @Override
137    public void setXml(Element e) {
138        // Get common stuff
139        super.setXml(e);
140        // Only do this if this is a ButtonTrigger type Element
141        if (e.getAttribute("type").getValue().equals("BUTTON")) {
142            match_value = Boolean.parseBoolean(e.getChild("match").getValue());
143        }
144    }
145
146    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ButtonTrigger.class);
147
148}