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