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