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