001package jmri.jmrit.operations.locations.tools;
002
003import java.awt.Dimension;
004import java.awt.GridBagLayout;
005
006import javax.swing.*;
007
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011import jmri.jmrit.operations.OperationsFrame;
012import jmri.jmrit.operations.OperationsPanel;
013import jmri.jmrit.operations.OperationsXml;
014import jmri.jmrit.operations.locations.Track;
015import jmri.jmrit.operations.setup.Control;
016import jmri.jmrit.operations.setup.Setup;
017import jmri.jmrit.operations.trains.TrainCommon;
018
019public class TrackEditCommentsFrame extends OperationsFrame {
020
021    // text areas
022    JTextArea commentBothTextArea = new JTextArea(5, 100);
023    JTextArea commentPickupTextArea = new JTextArea(5, 100);
024    JTextArea commentSetoutTextArea = new JTextArea(5, 100);
025    
026    // scrollers
027    JScrollPane commentBothScroller = new JScrollPane(commentBothTextArea);
028    JScrollPane commentPickupScroller = new JScrollPane(commentPickupTextArea);
029    JScrollPane commentSetoutScroller = new JScrollPane(commentSetoutTextArea);
030    
031    // text color choosers
032    JColorChooser commentColorChooserBoth = new JColorChooser();
033    JColorChooser commentColorChooserPickup = new JColorChooser();
034    JColorChooser commentColorChooserSetout = new JColorChooser();
035
036    JButton saveButton = new JButton(Bundle.getMessage("ButtonSave"));
037    
038    JCheckBox printManifest = new JCheckBox(Bundle.getMessage("PrintManifest"));
039    JCheckBox printSwitchList = new JCheckBox(Bundle.getMessage("PrintSwitchList"));
040
041    Track _track;
042
043    public TrackEditCommentsFrame(Track track) {
044        super();
045        initComponents(track);
046    }
047
048    private void initComponents(Track track) {
049        if (track == null) {
050            log.debug("Track is null can't edit track comments");
051            return;
052        }
053        _track = track;
054        
055        // Layout the panel by rows
056        setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
057        
058        JPanel panelComments = new JPanel();
059        JScrollPane panelPane = new JScrollPane(panelComments);
060        panelComments.setLayout(new BoxLayout(panelComments, BoxLayout.Y_AXIS));
061        
062        panelPane.setBorder(BorderFactory.createTitledBorder(""));
063
064        JPanel pCb = new JPanel();
065        pCb.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("CommentBoth")));
066        pCb.setLayout(new GridBagLayout());
067        addItem(pCb, commentBothScroller, 1, 0);
068        
069        addItem(pCb, OperationsPanel.getColorChooserPanel(track.getCommentBothWithColor(), commentColorChooserBoth), 2, 0);
070
071        JPanel pCp = new JPanel();
072        pCp.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("CommentPickup")));
073        pCp.setLayout(new GridBagLayout());
074        addItem(pCp, commentPickupScroller, 1, 0);
075        
076        addItem(pCp, OperationsPanel.getColorChooserPanel(track.getCommentPickupWithColor(), commentColorChooserPickup), 2, 0);
077
078        JPanel pCs = new JPanel();
079        pCs.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("CommentSetout")));
080        pCs.setLayout(new GridBagLayout());
081        addItem(pCs, commentSetoutScroller, 1, 0);
082        
083        addItem(pCs, OperationsPanel.getColorChooserPanel(track.getCommentSetoutWithColor(), commentColorChooserSetout), 2, 0);
084
085        commentBothTextArea.setText(TrainCommon.getTextColorString(track.getCommentBothWithColor()));
086        commentPickupTextArea.setText(TrainCommon.getTextColorString(track.getCommentPickupWithColor()));
087        commentSetoutTextArea.setText(TrainCommon.getTextColorString(track.getCommentSetoutWithColor()));
088
089        JPanel pB = new JPanel();
090        pB.setLayout(new GridBagLayout());
091        addItem(pB, printManifest, 0, 0);
092        addItem(pB, printSwitchList, 1, 0);
093        addItem(pB, saveButton, 2, 0);
094        
095        printManifest.setSelected(track.isPrintManifestCommentEnabled());
096        printSwitchList.setSelected(track.isPrintSwitchListCommentEnabled());
097
098        panelComments.add(pCb);
099        panelComments.add(pCp);
100        panelComments.add(pCs);
101        
102        add(panelPane);
103        add(pB);
104
105        addButtonAction(saveButton);
106
107        setTitle(track.getName());
108        initMinimumSize(new Dimension(Control.panelWidth600, Control.panelHeight400));
109    }
110
111    // Buttons
112    @Override
113    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
114        if (ae.getSource() == saveButton) {
115            _track.setCommentBoth(TrainCommon.formatColorString(commentBothTextArea.getText(), commentColorChooserBoth.getColor()));
116            _track.setCommentPickup(TrainCommon.formatColorString(commentPickupTextArea.getText(), commentColorChooserPickup.getColor()));
117            _track.setCommentSetout(TrainCommon.formatColorString(commentSetoutTextArea.getText(), commentColorChooserSetout.getColor()));
118            _track.setPrintManifestCommentEnabled(printManifest.isSelected());
119            _track.setPrintSwitchListCommentEnabled(printSwitchList.isSelected());
120            // save location file
121            OperationsXml.save();
122            if (Setup.isCloseWindowOnSaveEnabled()) {
123                super.dispose();
124            }
125        }
126    }
127    
128    private final static Logger log = LoggerFactory.getLogger(TrackEditCommentsFrame.class
129            .getName());
130}