001package jmri.jmrit.beantable;
002
003import java.awt.BorderLayout;
004import java.awt.Component;
005import java.awt.event.ActionEvent;
006import java.awt.event.ActionListener;
007import java.text.MessageFormat;
008import java.util.ResourceBundle;
009
010import javax.swing.*;
011import javax.swing.table.TableRowSorter;
012
013import jmri.jmrit.beantable.AudioTableAction.AudioTableDataModel;
014import jmri.swing.RowSorterUtil;
015import jmri.util.swing.XTableColumnModel;
016
017/**
018 *
019 * <hr>
020 * This file is part of JMRI.
021 * <p>
022 * JMRI is free software; you can redistribute it and/or modify it under the
023 * terms of version 2 of the GNU General Public License as published by the Free
024 * Software Foundation. See the "COPYING" file for a copy of this license.
025 * <p>
026 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
027 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
028 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
029 *
030 * @author Bob Jacobsen Copyright (C) 2003
031 * @author Matthew Harris copyright (c) 2009
032 */
033public class AudioTablePanel extends JPanel {
034
035    private AudioTableDataModel listenerDataModel;
036    private AudioTableDataModel bufferDataModel;
037    private AudioTableDataModel sourceDataModel;
038    private JTable listenerDataTable;
039    private JTable bufferDataTable;
040    private JTable sourceDataTable;
041    private JScrollPane listenerDataScroll;
042    private JScrollPane bufferDataScroll;
043    private JScrollPane sourceDataScroll;
044    private final JTabbedPane audioTabs;
045    private final JPanel bottomBox; // panel at bottom for extra buttons etc
046
047    private static final ResourceBundle rba = ResourceBundle.getBundle("jmri.jmrit.audio.swing.AudioTableBundle");
048
049    public AudioTablePanel(AudioTableDataModel listenerModel,
050            AudioTableDataModel bufferModel,
051            AudioTableDataModel sourceModel,
052            String helpTarget) {
053
054        super();
055        listenerDataModel = listenerModel;
056        TableRowSorter<AudioTableDataModel> sorter = new TableRowSorter<>(listenerDataModel);
057
058        // use NamedBean's built-in Comparator interface for sorting the system name column
059        RowSorterUtil.setSortOrder(sorter, AudioTableDataModel.SYSNAMECOL, SortOrder.ASCENDING);
060        listenerDataTable = listenerDataModel.makeJTable(listenerDataModel.getMasterClassName(), listenerDataModel, sorter);
061        listenerDataScroll = new JScrollPane(listenerDataTable);
062        listenerDataTable.setColumnModel(new XTableColumnModel());
063        listenerDataTable.createDefaultColumnsFromModel();
064
065        bufferDataModel = bufferModel;
066        sorter = new TableRowSorter<>(bufferDataModel);
067        RowSorterUtil.setSortOrder(sorter, AudioTableDataModel.SYSNAMECOL, SortOrder.ASCENDING);
068        bufferDataTable = bufferDataModel.makeJTable(bufferDataModel.getMasterClassName(), bufferDataModel, sorter);
069        bufferDataScroll = new JScrollPane(bufferDataTable);
070        bufferDataTable.setColumnModel(new XTableColumnModel());
071        bufferDataTable.createDefaultColumnsFromModel();
072
073        sourceDataModel = sourceModel;
074        sorter = new TableRowSorter<>(sourceDataModel);
075        RowSorterUtil.setSortOrder(sorter, AudioTableDataModel.SYSNAMECOL, SortOrder.ASCENDING);
076        sourceDataTable = sourceDataModel.makeJTable(sourceDataModel.getMasterClassName(), sourceDataModel, sorter);
077        sourceDataScroll = new JScrollPane(sourceDataTable);
078        sourceDataTable.setColumnModel(new XTableColumnModel());
079        sourceDataTable.createDefaultColumnsFromModel();
080
081        // configure items for GUI
082        listenerDataModel.configEditColumn(listenerDataTable);
083        listenerDataModel.configureTable(listenerDataTable); // and persists
084
085        bufferDataModel.configEditColumn(bufferDataTable);
086        bufferDataModel.configureTable(bufferDataTable); // and persists
087
088        sourceDataModel.configEditColumn(sourceDataTable);
089        sourceDataModel.configureTable(sourceDataTable); // and persists
090
091        // general GUI config
092        this.setLayout(new BorderLayout());
093
094        // install items in GUI
095        audioTabs = new JTabbedPane();
096        audioTabs.addTab(rba.getString("LabelListener"), listenerDataScroll);
097        audioTabs.addTab(rba.getString("LabelBuffers"), bufferDataScroll);
098        audioTabs.addTab(rba.getString("LabelSources"), sourceDataScroll);
099        add(audioTabs, BorderLayout.CENTER);
100
101        bottomBox = new JPanel();
102        bottomBox.setLayout(new jmri.util.swing.WrapLayout(jmri.util.swing.WrapLayout.LEFT,20,5));
103        add(bottomBox, BorderLayout.SOUTH);
104
105        // set preferred scrolling options
106        listenerDataScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
107        listenerDataScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
108        bufferDataScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
109        bufferDataScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
110        sourceDataScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
111        sourceDataScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
112    }
113
114    public JMenuItem getPrintItem() {
115        JMenuItem printItem = new JMenuItem(Bundle.getMessage("PrintTable"));
116
117        printItem.addActionListener(new ActionListener() {
118            @Override
119            public void actionPerformed(ActionEvent e) {
120                try {
121                    MessageFormat footerFormat = new MessageFormat("Page {0,number}");
122                    listenerDataTable.print(JTable.PrintMode.FIT_WIDTH, new MessageFormat("Listener Table"), footerFormat);
123                    bufferDataTable.print(JTable.PrintMode.FIT_WIDTH, new MessageFormat("Buffer Table"), footerFormat);
124                    sourceDataTable.print(JTable.PrintMode.FIT_WIDTH, new MessageFormat("Source Table"), footerFormat);
125                } catch (java.awt.print.PrinterException e1) {
126                    log.warn("error printing: {}", e1, e1);
127                }
128            }
129        });
130        return printItem;
131    }
132
133    /**
134     * Add a component to the bottom box.
135     * @param comp {@link Component} to add
136     */
137    protected void addToBottomBox(Component comp) {
138        bottomBox.add(comp);
139    }
140
141    public void dispose() {
142        if (listenerDataModel != null) {
143            listenerDataModel.stopPersistingTable(listenerDataTable);
144            listenerDataModel.dispose();
145        }
146        listenerDataModel = null;
147        listenerDataTable = null;
148        listenerDataScroll = null;
149        if (bufferDataModel != null) {
150            bufferDataModel.stopPersistingTable(bufferDataTable);
151            bufferDataModel.dispose();
152        }
153        bufferDataModel = null;
154        bufferDataTable = null;
155        bufferDataScroll = null;
156        if (sourceDataModel != null) {
157            sourceDataModel.stopPersistingTable(sourceDataTable);
158            sourceDataModel.dispose();
159        }
160        sourceDataModel = null;
161        sourceDataTable = null;
162        sourceDataScroll = null;
163    }
164
165    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AudioTablePanel.class);
166
167}