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        buttonCancel.setToolTipText(Bundle.getMessage("CancelCommentLogixNGButtonHint"));      // NOI18N
069        
070        // OK
071        JButton buttonOK = new JButton(Bundle.getMessage("ButtonOK"));    // NOI18N
072        panel5.add(buttonOK);
073        buttonOK.addActionListener((ActionEvent e) -> {
074            okPressed(null);
075        });
076        buttonOK.setToolTipText(Bundle.getMessage("EditButtonHint"));      // NOI18N
077        
078        _editCommentDialog.addWindowListener(new java.awt.event.WindowAdapter() {
079            @Override
080            public void windowClosing(java.awt.event.WindowEvent e) {
081                cancelPressed(null);
082            }
083        });
084        
085        contentPanel.add(panel5);
086        
087        _editCommentDialog.setMinimumSize(new Dimension(panelWidth, panelHeight));
088        
089//        addLogixNGFrame.setLocationRelativeTo(component);
090        _editCommentDialog.setLocationRelativeTo(null);
091        _editCommentDialog.pack();
092        _editCommentDialog.setVisible(true);
093        
094        return _comment;
095    }
096    
097    final protected void cancelPressed(ActionEvent e) {
098        _editCommentDialog.setVisible(false);
099        _editCommentDialog.dispose();
100        _editCommentDialog = null;
101    }
102    
103    final protected void okPressed(ActionEvent e) {
104        if (_commentTextArea.getText().isEmpty()) {
105            _comment = null;
106        } else {
107            _comment = _commentTextArea.getText();
108        }
109        _editCommentDialog.setVisible(false);
110        _editCommentDialog.dispose();
111        _editCommentDialog = null;
112    }
113    
114}