001package jmri.jmrix.openlcb.swing;
002
003import java.awt.event.*;
004import javax.swing.*;
005
006import jmri.util.swing.*;
007import jmri.jmrix.can.CanSystemConnectionMemo;
008import jmri.jmrix.openlcb.OlcbAddress;
009
010import org.openlcb.swing.EventIdTextField;
011
012/**
013 * This is an entry field for Event IDs that also 
014 * accepts event names
015 *
016 * @author Bob Jacobsen  (C) 2024
017 */
018public class NamedEventIdTextField extends JTextField {
019        
020    public NamedEventIdTextField(CanSystemConnectionMemo memo) {
021        this(23, memo);  // heuristically-found default size
022    }
023
024    public NamedEventIdTextField(int size, CanSystemConnectionMemo memo) {
025        super(size);
026        this.memo = memo;
027        
028        // set up semi-automatic tool tip
029        addMouseListener(JmriMouseListener.adapt(new JmriMouseListener() {
030            @Override
031            public void mouseEntered(JmriMouseEvent arg0) {
032                // Generate dynamic tooltip text here
033                setToolTipText(makeToolTipText(getText()));
034            }
035
036            @Override public void mouseClicked(JmriMouseEvent e) {}
037            @Override public void mouseExited(JmriMouseEvent arg0) {}
038            @Override public void mousePressed(JmriMouseEvent arg0) {}
039            @Override public void mouseReleased(JmriMouseEvent arg0) {}
040        }));
041
042        // Add a focus listener to update the tooltip dynamically
043        addFocusListener(new FocusListener() {
044            @Override
045            public void focusGained(FocusEvent e) {
046                setToolTipText(makeToolTipText(getText()));
047            }
048
049            @Override public void focusLost(FocusEvent e) {}
050        });
051
052        EventIdTextField.configurePopUp(this);
053        
054    }
055
056    final CanSystemConnectionMemo memo;
057    
058    protected String makeToolTipText(String fieldText) {
059        var address = new OlcbAddress(fieldText, memo);
060        if (address.isFromName()) {
061            return fieldText+" is "+address.toDottedString();
062        }
063        return "Enter an event ID or event name";
064    }    
065    
066    // private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(NamedEventIdTextField.class);
067}