001package jmri.jmrit.logixng.tools.swing;
002
003import java.awt.*;
004import java.awt.event.ActionEvent;
005
006import javax.swing.*;
007
008/**
009 * Show a dialog that lets the user edit a multiline comment
010 * 
011 * @author Daniel Bergqvist 2021
012 */
013public class EditCommentDialog {
014    
015    private static final int panelWidth = 500;
016    private static final int panelHeight = 500;
017    
018    private String _comment;
019    private JDialog _editCommentDialog = null;
020    private final JLabel _commentLabel = new JLabel(Bundle.getMessage("Comment") + ":"); 
021    private final JTextArea _commentTextArea = new JTextArea();
022    
023    
024    public EditCommentDialog() {
025    }
026    
027    public String showDialog(String comment) {
028        
029        _comment = comment;
030        
031        _commentTextArea.setText(comment);
032        
033        _editCommentDialog  = new JDialog(
034                (JDialog)null,
035                Bundle.getMessage("EditCommentDialogTitle"),
036                true);
037        
038        
039        Container contentPanel = _editCommentDialog.getContentPane();
040        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
041        
042        JPanel p;
043        p = new JPanel();
044        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
045        p.add(_commentLabel);
046        
047        JScrollPane commentScroller = new JScrollPane(_commentTextArea);
048        commentScroller.setPreferredSize(new Dimension(panelWidth, panelHeight));
049        p.add(commentScroller);
050        
051        contentPanel.add(p);
052        // set up message
053        JPanel panel3 = new JPanel();
054        panel3.setLayout(new BoxLayout(panel3, BoxLayout.Y_AXIS));
055        
056        contentPanel.add(panel3);
057
058        // set up create and cancel buttons
059        JPanel panel5 = new JPanel();
060        panel5.setLayout(new FlowLayout());
061        
062        // Cancel
063        JButton buttonCancel = new JButton(Bundle.getMessage("ButtonCancel"));    // NOI18N
064        panel5.add(buttonCancel);
065        buttonCancel.addActionListener((ActionEvent e) -> {
066            cancelPressed(null);
067        });
068//        cancel.setToolTipText(Bundle.getMessage("CancelLogixButtonHint"));      // NOI18N
069        buttonCancel.setToolTipText("CancelLogixButtonHint");      // NOI18N
070        
071        // OK
072        JButton buttonOK = new JButton(Bundle.getMessage("ButtonOK"));    // NOI18N
073        panel5.add(buttonOK);
074        buttonOK.addActionListener((ActionEvent e) -> {
075            okPressed(null);
076        });
077//        cancel.setToolTipText(Bundle.getMessage("CancelLogixButtonHint"));      // NOI18N
078        buttonOK.setToolTipText("CancelLogixButtonHint");      // NOI18N
079        
080        _editCommentDialog.addWindowListener(new java.awt.event.WindowAdapter() {
081            @Override
082            public void windowClosing(java.awt.event.WindowEvent e) {
083                cancelPressed(null);
084            }
085        });
086        
087        contentPanel.add(panel5);
088        
089        _editCommentDialog.setMinimumSize(new Dimension(panelWidth, panelHeight));
090        
091//        addLogixNGFrame.setLocationRelativeTo(component);
092        _editCommentDialog.setLocationRelativeTo(null);
093        _editCommentDialog.pack();
094        _editCommentDialog.setVisible(true);
095        
096        return _comment;
097    }
098    
099    final protected void cancelPressed(ActionEvent e) {
100        _editCommentDialog.setVisible(false);
101        _editCommentDialog.dispose();
102        _editCommentDialog = null;
103    }
104    
105    final protected void okPressed(ActionEvent e) {
106        if (_commentTextArea.getText().isEmpty()) {
107            _comment = null;
108        } else {
109            _comment = _commentTextArea.getText();
110        }
111        _editCommentDialog.setVisible(false);
112        _editCommentDialog.dispose();
113        _editCommentDialog = null;
114    }
115    
116}