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