001package jmri.jmrit.vsdecoder; 002 003import javax.swing.JButton; 004import javax.swing.JComponent; 005import jmri.util.swing.JmriMouseListener; 006import org.jdom2.Element; 007import org.slf4j.Logger; 008import org.slf4j.LoggerFactory; 009 010/** 011 * Momentary Sound Event. 012 * 013 * <hr> 014 * This file is part of JMRI. 015 * <p> 016 * JMRI is free software; you can redistribute it and/or modify it under 017 * the terms of version 2 of the GNU General Public License as published 018 * by the Free Software Foundation. See the "COPYING" file for a copy 019 * of this license. 020 * <p> 021 * JMRI is distributed in the hope that it will be useful, but WITHOUT 022 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 023 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 024 * for more details. 025 * 026 * @author Mark Underwood Copyright (C) 2011 027 */ 028public class MomentarySoundEvent extends SoundEvent { 029 030 JButton button; 031 032 Trigger t; // used in setXml as a temporary holder for creating the 033 // event listener class. 034 ButtonTrigger bt; // used in setupButtonAction() as a temporary holder 035 // for creating the button listeners. 036 037 public MomentarySoundEvent() { 038 this(null, null); 039 } 040 041 public MomentarySoundEvent(String n) { 042 this(n, n); 043 } 044 045 public MomentarySoundEvent(String n, String bl) { 046 super(n, bl); 047 button = null; 048 } 049 050 @Override 051 public boolean hasButton() { 052 if ((buttontype == ButtonType.NONE) || (buttontype == ButtonType.ENGINE) || (button == null)) { 053 return false; 054 } else { 055 return true; 056 } 057 } 058 059 public void setButton(JButton b) { 060 button = b; 061 } 062 063 @Override 064 public JComponent getButton() { 065 return button; 066 } 067 068 @Override 069 public void setButtonLabel(String bl) { 070 button.setText(bl); 071 } 072 073 @Override 074 public String getButtonLabel() { 075 return button.getText(); 076 } 077 078 @Override 079 protected ButtonTrigger setupButtonAction(Element te) { 080 bt = new ButtonTrigger(te.getAttributeValue("name")); 081 button_trigger_list.put(bt.getName(), bt); 082 log.debug("new ButtonTrigger: {}, type: {}", bt.getName(), buttontype.toString()); 083 button.addMouseListener(JmriMouseListener.adapt(bt)); 084 return bt; // cast OK since we just instantiated it up above. 085 } 086 087 @Override 088 public Element getXml() { 089 Element me = new Element("SoundEvent"); 090 me.setAttribute("name", name); 091 me.setAttribute("label", me.getText()); 092 for (Trigger t : trigger_list.values()) { 093 me.addContent(t.getXml()); 094 } 095 096 return me; 097 } 098 099 @Override 100 public void setXml(Element el) { 101 this.setXml(el, null); 102 } 103 104 @Override 105 public void setXml(Element el, VSDFile vf) { 106 107 // Create the button first (put this in constructor?) 108 button = new JButton(); 109 110 // Handle common stuff. 111 super.setXml(el, vf); 112 113 // We know it's momentary, or this class wouldn't have been constructed. 114 button.setText(el.getAttributeValue("label")); 115 116 /* 117 for (ButtonTrigger bt : button_trigger_list.values()) { 118 log.debug("Button Trigger: " + bt.getName()); 119 if (bt.getTarget() != null) 120 log.debug(" Target: " + bt.getTarget().getName()); 121 else 122 log.debug(" Target: null"); 123 if (bt.getTargetAction() != null) 124 log.debug(" Action: " + bt.getTargetAction().toString()); 125 else 126 log.debug(" Target Action: null"); 127 128 } 129 */ 130 131 /* 132 MouseListener [] listeners = button.getListeners(MouseListener.class); 133 for (MouseListener l : listeners) { 134 log.debug("Listener: " + l.toString()); 135 } 136 */ 137 } // end setXml() 138 139 private static final Logger log = LoggerFactory.getLogger(MomentarySoundEvent.class); 140 141}