001package jmri.jmrix.can.cbus.swing.eventtable; 002 003import java.awt.BorderLayout; 004import java.awt.event.ActionEvent; 005import java.util.ArrayList; 006import java.util.List; 007 008import javax.swing.*; 009 010import jmri.InstanceManager; 011import jmri.UserPreferencesManager; 012import jmri.jmrix.can.CanSystemConnectionMemo; 013import jmri.jmrix.can.cbus.CbusConfigurationManager; 014import jmri.jmrix.can.cbus.CbusPreferences; 015import jmri.jmrix.can.cbus.eventtable.*; 016import jmri.jmrix.can.cbus.swing.*; 017import jmri.swing.JmriJTablePersistenceManager; 018import jmri.util.swing.XTableColumnModel; 019import jmri.util.swing.StayOpenCheckBoxItem; 020import jmri.util.table.JTableToCsvAction; 021import jmri.util.table.JTableWithColumnToolTips; 022 023// import org.slf4j.Logger; 024// import org.slf4j.LoggerFactory; 025 026/** 027 * Pane providing a CBUS event table. 028 * 029 * @author Andrew Crosland (C) 2009 030 * @author Kevin Dickerson (C) 2012 031 * @author Steve Young (C) 2018 032 * @see CbusEventTableDataModel 033 * 034 * @since 2.99.2 035 */ 036public class CbusEventTablePane extends jmri.jmrix.can.swing.CanPanel { 037 038 protected CbusEventTableDataModel eventModel; 039 protected JTable eventTable; 040 041 private final UserPreferencesManager p; 042 043 private CbusCreateBeanPane newBeanPanel; 044 protected CbusNewEventPane neweventcontainer; 045 private CbusSendEventPane sendPane; 046 protected JPanel filterpanel; 047 048 public CbusEventTable cbEvTable; 049 050 protected final JTextField filterText = new JTextField("",8); 051 protected JButton clearfilterButton; 052 053 protected final JMenu evColMenu = new JMenu(Bundle.getMessage("evColMenuName")); // NOI18N 054 protected final JMenu evStatMenu = new JMenu(Bundle.getMessage("evStatMenuName")); // NOI18N 055 protected final JMenu evJmMenu = new JMenu(Bundle.getMessage("latestEvCols")); // NOI18N 056 protected final JMenu buttonMenu = new JMenu(Bundle.getMessage("buttonCols")); // NOI18N 057 058 private final StayOpenCheckBoxItem showfilterpanel; 059 private final StayOpenCheckBoxItem shownewevent; 060 private final StayOpenCheckBoxItem showNewBeanPanel; 061 private final StayOpenCheckBoxItem showSendEventPanel; 062 private final StayOpenCheckBoxItem saveRestoreEventTableItem; 063 064 private final XTableColumnModel tcm; 065 066 public CbusEventTablePane() { 067 super(); 068 tcm = new XTableColumnModel(); 069 showNewBeanPanel = new StayOpenCheckBoxItem(Bundle.getMessage("NewTsl")); // NOI18N 070 shownewevent = new StayOpenCheckBoxItem((Bundle.getMessage("NewEvent"))); // NOI18N 071 showfilterpanel = new StayOpenCheckBoxItem(Bundle.getMessage("FilterSurround")); // NOI18N 072 showSendEventPanel = new StayOpenCheckBoxItem(Bundle.getMessage("ButtonSendEvent")); // NOI18N 073 saveRestoreEventTableItem = new StayOpenCheckBoxItem(Bundle.getMessage("SaveEvSession")); // NOI18N 074 p = InstanceManager.getDefault(UserPreferencesManager.class); 075 } 076 077 @Override 078 public void initComponents(CanSystemConnectionMemo memo) { 079 super.initComponents(memo); 080 081 CbusPreferences preferences = memo.get(CbusPreferences.class); 082 083 saveRestoreEventTableItem.setSelected( preferences.getSaveRestoreEventTable() ); 084 saveRestoreEventTableItem.addActionListener((ActionEvent e) -> 085 preferences.setSaveRestoreEventTable(saveRestoreEventTableItem.isSelected()) ); 086 087 eventModel = memo.get(CbusConfigurationManager.class).provide(CbusEventTableDataModel.class); 088 089 eventTable = new JTableWithColumnToolTips(eventModel, CbusEventTableDataModel.CBUS_EV_TABLE_COL_TOOLTIPS); 090 091 // Use XTableColumnModel so we can control which columns are visible 092 eventTable.setColumnModel(tcm); 093 094 init(); 095 } 096 097 public final void init() { 098 099 setLayout(new BorderLayout()); 100 101 JPanel _toppanelcontainer = new JPanel(); 102 _toppanelcontainer.setLayout(new BoxLayout(_toppanelcontainer, BoxLayout.X_AXIS)); 103 104 JPanel _topToppanelcontainer = new JPanel(); 105 _topToppanelcontainer.setLayout(new BoxLayout(_topToppanelcontainer, BoxLayout.Y_AXIS)); 106 107 neweventcontainer = new CbusNewEventPane(this); 108 cbEvTable = new CbusEventTable(this); 109 newBeanPanel = new CbusCreateBeanPane(this); 110 sendPane = new CbusSendEventPane(this); 111 112 _toppanelcontainer.add(newBeanPanel); 113 _toppanelcontainer.add(getFilterPanel()); 114 _toppanelcontainer.add(neweventcontainer); 115 _topToppanelcontainer.add(_toppanelcontainer); 116 _topToppanelcontainer.add(newBeanPanel); 117 _topToppanelcontainer.add(sendPane); 118 add(_topToppanelcontainer, BorderLayout.PAGE_START); 119 120 add(cbEvTable, BorderLayout.CENTER); 121 setVisible(true); 122 setPanesVisibleFromSettings(); 123 cbEvTable.tableChanged(null); 124 125 } 126 127 private void setPanesVisibleFromSettings() { 128 129 if ( p.getSimplePreferenceState(this.getClass().getName() + ".Notfirstrun") ) { // NOI18N 130 showfilterpanel.setSelected(p.getSimplePreferenceState(getClass().getName() + ".Showfilterpanel")); // NOI18N 131 shownewevent.setSelected(p.getSimplePreferenceState(getClass().getName() + ".Shownewevent")); // NOI18N 132 showNewBeanPanel.setSelected(p.getSimplePreferenceState(getClass().getName() + ".ShowNewBeanPanel")); // NOI18N 133 showSendEventPanel.setSelected(p.getSimplePreferenceState(getClass().getName() + ".ShowSendEventPanel")); // NOI18N 134 135 filterpanel.setVisible(p.getSimplePreferenceState(getClass().getName() + ".Showfilterpanel")); // NOI18N 136 neweventcontainer.setVisible(p.getSimplePreferenceState(getClass().getName() + ".Shownewevent")); // NOI18N 137 newBeanPanel.setVisible(p.getSimplePreferenceState(getClass().getName() + ".ShowNewBeanPanel")); // NOI18N 138 sendPane.setVisible(p.getSimplePreferenceState(getClass().getName() + ".ShowSendEventPanel")); // NOI18N 139 140 } else { 141 // set virgin load view 142 showfilterpanel.setSelected(true); 143 shownewevent.setSelected(true); 144 showNewBeanPanel.setSelected(false); 145 showSendEventPanel.setSelected(false); 146 newBeanPanel.setVisible(false); 147 sendPane.setVisible(false); 148 } 149 } 150 151 private JPanel getFilterPanel(){ 152 153 filterpanel = new JPanel(); 154 filterpanel.setBorder(BorderFactory.createTitledBorder( 155 BorderFactory.createEtchedBorder(), Bundle.getMessage("FilterSurround"))); // NOI18N 156 157 clearfilterButton = new JButton(Bundle.getMessage("ClearFilter")); // NOI18N 158 clearfilterButton.setEnabled(false); 159 clearfilterButton.addActionListener((ActionEvent e) -> filterText.setText("") ); 160 161 filterText.setMaximumSize( filterText.getPreferredSize() ); 162 163 filterpanel.add(filterText); 164 filterpanel.add(clearfilterButton); 165 166 cbEvTable.addFilterListener(filterText); 167 168 return filterpanel; 169 } 170 171 /** 172 * {@inheritDoc} 173 */ 174 @Override 175 public String getHelpTarget() { 176 return "package.jmri.jmrix.can.cbus.swing.eventtable.EventTablePane"; // NOI18N 177 } 178 179 /** 180 * Creates a Menu List 181 * <p> 182 * File - Print, Print Preview, Save, SaveAs csv 183 * Display - show / hide Create new event pane, show/hide bottom feedback pane 184 */ 185 @Override 186 public List<JMenu> getMenus() { 187 List<JMenu> menuList = new ArrayList<>(); 188 189 menuList.add(getFileMenu()); 190 menuList.add(getDisplayMenu()); 191 menuList.add(evColMenu); 192 menuList.add(evJmMenu); 193 menuList.add(buttonMenu); 194 menuList.add(evStatMenu); 195 return menuList; 196 } 197 198 private JMenu getFileMenu(){ 199 200 JMenu fileMenu = new JMenu(Bundle.getMessage("MenuFile")); // NOI18N 201 202 fileMenu.add(saveRestoreEventTableItem); 203 fileMenu.add( new JSeparator() ); 204 fileMenu.add(new JTableToCsvAction(Bundle.getMessage("ExportCsvAll"), 205 null, eventModel, "myevents.csv", CbusEventTableDataModel.BUTTON_COLUMNS)); // NOI18N 206 fileMenu.add(new JTableToCsvAction(Bundle.getMessage("ExportCsvView"), 207 eventTable, eventModel, "myevents.csv", CbusEventTableDataModel.BUTTON_COLUMNS)); // NOI18N 208 209 // add print menu items 210 fileMenu.add( new JSeparator() ); 211 fileMenu.add(new CbusEventTablePrintAction(Bundle.getMessage("PrintTable"),eventModel,getTitle(),false)); // NOI18N 212 fileMenu.add(new CbusEventTablePrintAction(Bundle.getMessage("PreviewTable"),eventModel,getTitle(),true)); // NOI18N 213 return fileMenu; 214 215 } 216 217 private JMenu getDisplayMenu(){ 218 219 JMenu displayMenu = new JMenu(Bundle.getMessage("Display")); // NOI18N 220 221 showNewBeanPanel.addActionListener((ActionEvent e) -> 222 newBeanPanel.setVisible(showNewBeanPanel.isSelected()) ); 223 224 shownewevent.addActionListener((ActionEvent e) -> 225 neweventcontainer.setVisible(shownewevent.isSelected()) ); 226 227 showfilterpanel.addActionListener((ActionEvent e) -> 228 filterpanel.setVisible(showfilterpanel.isSelected()) ); 229 230 showSendEventPanel.addActionListener((ActionEvent e) -> 231 sendPane.setVisible(showSendEventPanel.isSelected()) ); 232 233 displayMenu.add(showfilterpanel); 234 displayMenu.add(shownewevent); 235 displayMenu.add(showNewBeanPanel); 236 displayMenu.add(showSendEventPanel); 237 238 return displayMenu; 239 240 } 241 242 /** 243 * {@inheritDoc} 244 */ 245 @Override 246 public String getTitle() { 247 return prependConnToString(Bundle.getMessage("EventTableTitle")); // NOI18N 248 } 249 250 /** 251 * {@inheritDoc} 252 */ 253 @Override 254 public void dispose() { 255 256 if(cbEvTable != null) { 257 cbEvTable.dispose(); 258 } 259 InstanceManager.getOptionalDefault(JmriJTablePersistenceManager.class).ifPresent((tpm) -> { 260 tpm.stopPersisting(eventTable); 261 }); 262 263 p.setSimplePreferenceState(getClass().getName() + ".Notfirstrun", true); // NOI18N 264 p.setSimplePreferenceState(getClass().getName() + ".Showfilterpanel", showfilterpanel.isSelected()); // NOI18N 265 p.setSimplePreferenceState(getClass().getName() + ".Shownewevent", shownewevent.isSelected()); // NOI18N 266 p.setSimplePreferenceState(getClass().getName() + ".ShowNewBeanPanel", showNewBeanPanel.isSelected()); // NOI18N 267 p.setSimplePreferenceState(getClass().getName() + ".ShowSendEventPanel", showSendEventPanel.isSelected()); // NOI18N 268 super.dispose(); 269 } 270 271 /** 272 * Nested class to create one of these using old-style defaults. 273 * Used as a startup action 274 */ 275 static public class Default extends jmri.jmrix.can.swing.CanNamedPaneAction { 276 277 public Default() { 278 super(Bundle.getMessage("MenuItemEventTable"), 279 new jmri.util.swing.sdi.JmriJFrameInterface(), 280 CbusEventTablePane.class.getName(), 281 jmri.InstanceManager.getDefault(CanSystemConnectionMemo.class)); 282 } 283 } 284 285 // private final static Logger log = LoggerFactory.getLogger(CbusEventTablePane.class); 286 287}