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