001package jmri.jmrix.loconet.soundloader;
002
003import java.io.File;
004import java.io.IOException;
005import javax.swing.BoxLayout;
006import javax.swing.JLabel;
007import javax.swing.JPanel;
008import javax.swing.JScrollPane;
009import javax.swing.JSeparator;
010import javax.swing.JTable;
011import javax.swing.JTextField;
012import javax.swing.SortOrder;
013import javax.swing.table.TableRowSorter;
014import jmri.jmrix.loconet.spjfile.SpjFile;
015import jmri.swing.RowSorterUtil;
016import org.slf4j.Logger;
017import org.slf4j.LoggerFactory;
018
019/**
020 * Pane for editing Digitrax SPJ files.
021 *
022 * @author Bob Jacobsen Copyright (C) 2006, 2010
023 */
024public class EditorFilePane extends javax.swing.JPanel {
025
026    // GUI member declarations
027    SpjFile file;
028    EditorTableDataModel dataModel;
029
030    public EditorFilePane(File name) {
031        // open and save file
032        try {
033            file = new SpjFile(name);
034            file.read();
035        } catch (IOException e) {
036            log.error("Exception reading file", e);
037            return;
038        }
039
040        // start to configure GUI
041        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
042
043        // create and include table
044        dataModel = new EditorTableDataModel(file);
045
046        JTable dataTable = new JTable(dataModel);
047        JScrollPane dataScroll = new JScrollPane(dataTable);
048
049        // set default sort order
050        TableRowSorter<EditorTableDataModel> sorter = new TableRowSorter<>(dataModel);
051        RowSorterUtil.setSortOrder(sorter, EditorTableDataModel.HEADERCOL, SortOrder.ASCENDING);
052
053        // configure items for GUI
054        dataModel.configureTable(dataTable);
055
056        add(dataScroll);
057
058        // some stuff at bottom for now
059        add(new JSeparator());
060        JPanel bottom = new JPanel();
061        bottom.setLayout(new BoxLayout(bottom, BoxLayout.Y_AXIS));
062
063        JPanel p1 = new JPanel();
064        p1.add(new JLabel(Bundle.getMessage("LabelSize")));
065        JTextField t1 = new JTextField(12);
066        t1.setEditable(false);
067        p1.add(t1);
068
069        bottom.add(p1);
070        add(bottom);
071    }
072
073    public void saveFile(String name) throws java.io.IOException {
074        file.save(name);
075    }
076
077    /**
078     * Get rid of any held resources
079     */
080    void dispose() {
081        file.dispose();
082        file = null;  // not for GC, this flags need to reinit
083    }
084
085    private final static Logger log = LoggerFactory.getLogger(EditorFilePane.class);
086
087}