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