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