001package jmri.jmrix.can.cbus.swing.nodeconfig;
002
003import java.awt.BorderLayout;
004import java.awt.event.ActionListener;
005import java.awt.event.KeyEvent;
006import java.awt.event.KeyListener;
007import javax.swing.JButton;
008import javax.swing.JPanel;
009import javax.swing.JScrollPane;
010import javax.swing.JTextArea;
011import jmri.jmrix.can.cbus.node.CbusNode;
012
013// import org.slf4j.Logger;
014// import org.slf4j.LoggerFactory;
015
016/**
017 *
018 * @author Steve Young Copyright (C) 2019
019 */
020public class CbusNodeUserCommentsPane extends CbusNodeConfigTab implements KeyListener {
021    
022    private JScrollPane eventScroll;
023    private ActionListener setNameListener;
024    private ActionListener resetCommentListener;
025    private JTextArea textFieldName;
026    private JButton saveTextButton;
027    private JButton resetTextButton;
028
029    /**
030     * Create a new instance of CbusNodeSetupPane.
031     * @param main the main NodeConfigToolPane this is a pane of.
032     */
033    protected CbusNodeUserCommentsPane( NodeConfigToolPane main ) {
034        super(main);
035        initPane();
036    }
037    
038    /**
039     * {@inheritDoc}
040     */
041    @Override
042    public String getTitle(){
043        return Bundle.getMessage("NodeComments"); // NOI18N
044    }
045
046    private void initPane() {
047        
048        initButtons();
049        
050        JPanel evMenuPane = new JPanel();
051        evMenuPane.add(saveTextButton);
052        evMenuPane.add(resetTextButton);
053
054        textFieldName = new JTextArea();
055        textFieldName.setMargin( new java.awt.Insets(10,10,10,10) );
056        textFieldName.addKeyListener(this);
057        eventScroll = new JScrollPane(textFieldName);
058        
059        add(evMenuPane, BorderLayout.PAGE_START);
060        add(eventScroll, BorderLayout.CENTER);
061        
062    }
063    
064    private void initButtons(){
065    
066        saveTextButton = new JButton(Bundle.getMessage("SaveCommentsButton")); // NOI18N
067        saveTextButton.setEnabled(false);
068        
069        resetTextButton = new JButton(Bundle.getMessage("ResetCommentButton")); // NOI18N
070        resetTextButton.setEnabled(false);
071        
072        setNameListener = ae -> {
073            saveOption();
074        };
075        saveTextButton.addActionListener(setNameListener);
076        
077        resetCommentListener = ae -> {
078            cancelOption();
079        };
080        resetTextButton.addActionListener(resetCommentListener);
081    
082    }
083    
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public void changedNode(CbusNode node){
089        textFieldName.setText( nodeOfInterest.getUserComment() );
090        resetCommentButtons();
091    }
092    
093    /**
094     * {@inheritDoc}
095     */
096    @Override
097    public void saveOption() {
098        nodeOfInterest.setUserComment(textFieldName.getText());
099        resetCommentButtons();
100    }
101
102    /** 
103     * Handle the key typed event from the text field.
104     * {@inheritDoc}
105     */
106    @Override
107    public void keyTyped(KeyEvent e) {
108        // resetCommentButtons();
109    }
110
111    /** 
112     * Handle the key-pressed event from the text field.
113     * {@inheritDoc}
114     */
115    @Override
116    public void keyPressed(KeyEvent e) {
117        // resetCommentButtons();
118    }
119
120    /** 
121     * Handle the key-released event from the text field.
122     * {@inheritDoc}
123     */
124    @Override
125    public void keyReleased(KeyEvent e) {
126        resetCommentButtons();
127    }
128    
129    private void resetCommentButtons() {
130        saveTextButton.setEnabled(!(nodeOfInterest.getUserComment().equals(textFieldName.getText())));
131        resetTextButton.setEnabled(!(nodeOfInterest.getUserComment().equals(textFieldName.getText())));
132    }
133    
134    public boolean areCommentsDirty(){
135        if (saveTextButton!=null) {
136            return saveTextButton.isEnabled();
137        }
138        return false;
139    }
140    
141    /**
142     * {@inheritDoc}
143     */
144    @Override
145    protected void cancelOption() {
146        textFieldName.setText( nodeOfInterest.getUserComment() );
147        resetCommentButtons();
148    }
149    
150    /**
151     * {@inheritDoc}
152     */
153    @Override
154    protected boolean getVetoBeingChanged(){
155        if (areCommentsDirty()) {
156            return getCancelSaveEditDialog(Bundle.getMessage("CommentsEditUnsaved"));
157        }
158        return false;
159    }
160    
161    // private final static Logger log = LoggerFactory.getLogger(CbusNodeUserCommentsPane.class);
162    
163}