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.awt.event.ActionEvent;
020import java.awt.event.ActionListener;
021import java.awt.event.MouseEvent;
022import java.awt.event.MouseListener;
023import java.beans.PropertyChangeEvent;
024import javax.swing.AbstractButton;
025import org.jdom2.Element;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029public class ButtonTrigger extends Trigger implements ActionListener, MouseListener {
030
031    enum ButtonAction {
032    }
033
034    boolean match_value;
035    boolean state;
036
037    public ButtonTrigger(String name) {
038        this(name, false);
039    }
040
041    public ButtonTrigger(String name, boolean bv) {
042        super(name);
043        this.setTriggerType(Trigger.TriggerType.BUTTON);
044        match_value = bv;
045    }
046
047    public void setMatchValue(boolean bv) {
048        match_value = bv;
049    }
050
051    public boolean getMatchValue() {
052        return (match_value);
053    }
054
055    // Button action functions called directly from the enclosing SoundEvent.
056    public void mouseDown() {
057        log.debug("buttonTrigger {} mouseDown() called.", getName());
058        if (match_value) {
059            this.callback.takeAction();
060        }
061    }
062
063    public void mouseUp() {
064        log.debug("buttonTrigger {} mouseUp() called.", getName());
065        if (!match_value) {
066            this.callback.takeAction();
067        }
068    }
069
070    public void click(boolean v) {
071        log.debug("buttonTrigger {} click( {}) called.", getName(), v);
072        if (v == match_value) {
073            this.callback.takeAction();
074        }
075    }
076
077    // PropertyChangeListener functions
078    @Override
079    public void propertyChange(PropertyChangeEvent event) {
080        // Button triggers respond to the button methods above, not to
081        // property change events.  Do nothing.
082    }
083
084    // ActionListener function(s)
085    @Override
086    public void actionPerformed(ActionEvent e) {
087        log.debug("ButtonTrigger.actionPerformed() {}", this.getName());
088        this.click(((AbstractButton) e.getSource()).isSelected());
089    }
090
091    // MouseListener functions
092    @Override
093    public void mousePressed(MouseEvent e) {
094        log.debug("MouseListener.mousePressed() {}", this.getName());
095        this.mouseDown();
096    }
097
098    @Override
099    public void mouseReleased(MouseEvent e) {
100        log.debug("MouseListener.mouseReleased() {}", this.getName());
101        this.mouseUp();
102    }
103
104    @Override
105    public void mouseEntered(MouseEvent e) {
106    }
107
108    @Override
109    public void mouseExited(MouseEvent e) {
110    }
111
112    @Override
113    public void mouseClicked(MouseEvent e) {
114    }
115
116    @Override
117    public Element getXml() {
118        Element me = new Element("Trigger");
119
120        log.debug("Bool Trigger getXml():");
121        log.debug("  trigger_name = {}", this.getName());
122        log.debug("  event_name = {}", this.event_name);
123        log.debug("  target_name = {}", target.getName());
124        log.debug("  match = {}", Boolean.valueOf(match_value).toString());
125        log.debug("  action = {}", this.getTriggerType().toString());
126
127        me.setAttribute("name", this.getName());
128        me.setAttribute("type", "BOOLEAN");
129        me.addContent(new Element("event-name").addContent(event_name));
130        me.addContent(new Element("target-name").addContent(target.getName()));
131        me.addContent(new Element("match").addContent(Boolean.valueOf(match_value).toString()));
132        me.addContent(new Element("action").addContent(this.getTriggerType().toString()));
133
134        return (me);
135    }
136
137    @Override
138    public void setXml(Element e) {
139        // Get common stuff
140        super.setXml(e);
141        // Only do this if this is a ButtonTrigger type Element
142        if (e.getAttribute("type").getValue().equals("BUTTON")) {
143            match_value = Boolean.parseBoolean(e.getChild("match").getValue());
144        }
145    }
146
147    private static final Logger log = LoggerFactory.getLogger(ButtonTrigger.class);
148
149}