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.configureTable(listenerDataTable);
083        listenerDataModel.configEditColumn(listenerDataTable);
084        listenerDataModel.persistTable(listenerDataTable);
085        bufferDataModel.configureTable(bufferDataTable);
086        bufferDataModel.configEditColumn(bufferDataTable);
087        bufferDataModel.persistTable(bufferDataTable);
088        sourceDataModel.configureTable(sourceDataTable);
089        sourceDataModel.configEditColumn(sourceDataTable);
090        sourceDataModel.persistTable(sourceDataTable);
091
092        // general GUI config
093        this.setLayout(new BorderLayout());
094
095        // install items in GUI
096        audioTabs = new JTabbedPane();
097        audioTabs.addTab(rba.getString("LabelListener"), listenerDataScroll);
098        audioTabs.addTab(rba.getString("LabelBuffers"), bufferDataScroll);
099        audioTabs.addTab(rba.getString("LabelSources"), sourceDataScroll);
100        add(audioTabs, BorderLayout.CENTER);
101
102        bottomBox = new JPanel();
103        bottomBox.setLayout(new jmri.util.swing.WrapLayout(jmri.util.swing.WrapLayout.LEFT,20,5));
104        add(bottomBox, BorderLayout.SOUTH);
105
106        // set preferred scrolling options
107        listenerDataScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
108        listenerDataScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
109        bufferDataScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
110        bufferDataScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
111        sourceDataScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
112        sourceDataScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
113    }
114
115    public JMenuItem getPrintItem() {
116        JMenuItem printItem = new JMenuItem(Bundle.getMessage("PrintTable"));
117
118        printItem.addActionListener(new ActionListener() {
119            @Override
120            public void actionPerformed(ActionEvent e) {
121                try {
122                    MessageFormat footerFormat = new MessageFormat("Page {0,number}");
123                    listenerDataTable.print(JTable.PrintMode.FIT_WIDTH, new MessageFormat("Listener Table"), footerFormat);
124                    bufferDataTable.print(JTable.PrintMode.FIT_WIDTH, new MessageFormat("Buffer Table"), footerFormat);
125                    sourceDataTable.print(JTable.PrintMode.FIT_WIDTH, new MessageFormat("Source Table"), footerFormat);
126                } catch (java.awt.print.PrinterException e1) {
127                    log.warn("error printing: {}", e1, e1);
128                }
129            }
130        });
131        return printItem;
132    }
133
134    /**
135     * Add a component to the bottom box.
136     * @param comp {@link Component} to add
137     */
138    protected void addToBottomBox(Component comp) {
139        bottomBox.add(comp);
140    }
141
142    public void dispose() {
143        if (listenerDataModel != null) {
144            listenerDataModel.stopPersistingTable(listenerDataTable);
145            listenerDataModel.dispose();
146        }
147        listenerDataModel = null;
148        listenerDataTable = null;
149        listenerDataScroll = null;
150        if (bufferDataModel != null) {
151            bufferDataModel.stopPersistingTable(bufferDataTable);
152            bufferDataModel.dispose();
153        }
154        bufferDataModel = null;
155        bufferDataTable = null;
156        bufferDataScroll = null;
157        if (sourceDataModel != null) {
158            sourceDataModel.stopPersistingTable(sourceDataTable);
159            sourceDataModel.dispose();
160        }
161        sourceDataModel = null;
162        sourceDataTable = null;
163        sourceDataScroll = null;
164    }
165
166    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AudioTablePanel.class);
167
168}