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 javax.swing.JComponent;
020import javax.swing.JToggleButton;
021import org.jdom2.Element;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025public class ToggleSoundEvent extends SoundEvent {
026
027    JToggleButton button;
028
029    Trigger t; // used in setXml as a temporary holder for creating the
030    // event listener class.
031    ButtonTrigger bt; // used in setupButtonAction() as a temporary holder
032    // for creating the button listeners.
033
034    public ToggleSoundEvent() {
035        this(null, null);
036    }
037
038    public ToggleSoundEvent(String n) {
039        this(n, n);
040    }
041
042    public ToggleSoundEvent(String n, String bl) {
043        super(n, bl);
044        button = null;
045    }
046
047    @Override
048    public boolean hasButton() {
049        if ((buttontype == ButtonType.NONE) || (buttontype == ButtonType.ENGINE) || (button == null)) {
050            return (false);
051        } else {
052            return (true);
053        }
054    }
055
056    public void setButton(JToggleButton b) {
057        button = b;
058    }
059
060    @Override
061    public JComponent getButton() {
062        return (button);
063    }
064
065    @Override
066    public void setButtonLabel(String bl) {
067        button.setText(bl);
068    }
069
070    @Override
071    public String getButtonLabel() {
072        return (button.getText());
073    }
074
075    @Override
076    protected ButtonTrigger setupButtonAction(Element te) {
077        bt = new ButtonTrigger(te.getAttributeValue("name"));
078        button_trigger_list.put(bt.getName(), bt);
079        log.debug("new ButtonTrigger {} name {} type {}", bt, bt.getName(), this.getButtonType());
080        if (bt != null) {
081            log.debug("name {} type {}", bt.getName(), this.getButtonType().toString());
082        }
083        if (button == null) {
084            log.error("BUTTON SHOULD NOT BE NULL");
085            return bt;
086        }
087        button.addActionListener(bt);
088        return (bt);
089    }
090
091    @Override
092    public Element getXml() {
093        Element me = new Element("SoundEvent");
094        me.setAttribute("name", name);
095        me.setAttribute("label", me.getText());
096        for (Trigger t : trigger_list.values()) {
097            me.addContent(t.getXml());
098        }
099
100        return (me);
101    }
102
103    @Override
104    public void setXml(Element el) {
105        this.setXml(el, null);
106    }
107
108    @Override
109    public void setXml(Element el, VSDFile vf) {
110
111        // Create the button first... (put this in constructor?)
112        button = new JToggleButton();
113
114        // Handle common stuff
115        super.setXml(el, vf);
116
117        // Get the SoundEvent's button type and create it.
118        button.setText(el.getAttributeValue("label"));
119
120        /*
121         for (ButtonTrigger bt : button_trigger_list.values()) {
122         log.debug("Button Trigger: " + bt.getName());
123         log.debug("  Target: " + bt.getTarget().getName());
124         log.debug("  Action: " + bt.getTargetAction().toString());
125         }
126
127         MouseListener [] listeners = button.getListeners(MouseListener.class);
128         for (MouseListener l : listeners) {
129         log.debug("Listener: " + l.toString());
130         }
131         */
132    }  // end setXml()
133
134    private static final Logger log = LoggerFactory.getLogger(ToggleSoundEvent.class);
135
136}