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.JButton;
020import javax.swing.JComponent;
021import org.jdom2.Element;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025public class MomentarySoundEvent extends SoundEvent {
026
027    JButton 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 MomentarySoundEvent() {
035        this(null, null);
036    }
037
038    public MomentarySoundEvent(String n) {
039        this(n, n);
040    }
041
042    public MomentarySoundEvent(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(JButton 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 {} type {}", bt.getName(), buttontype.toString());
080        button.addMouseListener(bt);
081        return (bt);  // cast OK since we just instantiated it up above.
082    }
083
084    @Override
085    public Element getXml() {
086        Element me = new Element("SoundEvent");
087        me.setAttribute("name", name);
088        me.setAttribute("label", me.getText());
089        for (Trigger t : trigger_list.values()) {
090            me.addContent(t.getXml());
091        }
092
093        return (me);
094    }
095
096    @Override
097    public void setXml(Element el) {
098        this.setXml(el, null);
099    }
100
101    @Override
102    public void setXml(Element el, VSDFile vf) {
103
104        // Create the button first (put this in constructor?)
105        button = new JButton();
106
107        // Handle common stuff.
108        super.setXml(el, vf);
109
110        // We know it's momentary, or this class wouldn't have been constructed.
111        button.setText(el.getAttributeValue("label"));
112
113        /*
114         for (ButtonTrigger bt : button_trigger_list.values()) {
115         log.debug("Button Trigger: " + bt.getName());
116         if (bt.getTarget() != null)
117         log.debug("  Target: " + bt.getTarget().getName());
118         else
119         log.debug("  Target: null");
120         if (bt.getTargetAction() != null)
121         log.debug("  Action: " + bt.getTargetAction().toString());
122         else
123         log.debug("  Target Action: null");
124
125         } 
126         */
127
128        /*
129         MouseListener [] listeners = button.getListeners(MouseListener.class);
130         for (MouseListener l : listeners) {
131         log.debug("Listener: " + l.toString());
132         }
133         */
134    }  // end setXml()
135
136    private static final Logger log = LoggerFactory.getLogger(MomentarySoundEvent.class);
137
138}