001package jmri.jmrit.throttle.list; 002 003import java.util.*; 004 005import javax.swing.table.AbstractTableModel; 006 007import jmri.*; 008import jmri.jmrit.throttle.SimpleThrottleWindow; 009import jmri.jmrit.throttle.ThrottleFrameManager; 010import jmri.jmrit.throttle.ThrottleWindow; 011import jmri.jmrit.throttle.implementation.SimpleThrottlePanel; 012import jmri.jmrit.throttle.implementation.ThrottleFrame; 013import jmri.jmrit.throttle.interfaces.ThrottleControllerUI; 014import jmri.jmrit.throttle.interfaces.ThrottleControllersUIContainer; 015 016/** 017 * A TableModel to display active Throttles in a summary table 018 * (see ThrottlesListPanel) 019 * 020 * <hr> 021 * This file is part of JMRI. 022 * <p> 023 * JMRI is free software; you can redistribute it and/or modify it under the 024 * terms of version 2 of the GNU General Public License as published by the Free 025 * Software Foundation. See the "COPYING" file for a copy of this license. 026 * <p> 027 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY 028 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 029 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 030 * 031 * @author Lionel Jeanson - 2011-2026 032 * 033 */ 034 035public class ThrottlesTableModel extends AbstractTableModel implements java.beans.PropertyChangeListener, ConsistListListener { 036 037 // The JMRI Swing throttles manager 038 private final ThrottleFrameManager throttleFrameManager = InstanceManager.getDefault(ThrottleFrameManager.class); 039 040 @Override 041 public int getRowCount() { 042 int max = 0; 043 Iterator<ThrottleControllersUIContainer> twi = throttleFrameManager.iterator(); 044 while (twi.hasNext()) { 045 max = Math.max(max, twi.next().getNbThrottlesControllers()); 046 } 047 return max; 048 } 049 050 @Override 051 public int getColumnCount() { 052 return throttleFrameManager.getNbThrottleControllersContainers(); 053 } 054 055 @Override 056 public ThrottleControllerUI getValueAt(int row_tf, int col_tw) { 057 ThrottleControllersUIContainer tw = throttleFrameManager.getThrottleControllersContainerAt(col_tw); 058 if (tw == null) { 059 return null; 060 } 061 //log.debug("{} selected", tw.getThrottleFrameAt(row_tf).getTitle()); 062 return tw.getThrottleControllerAt(row_tf); 063 } 064 065 public boolean moveThrottleController(ThrottleControllerUI tcui, int row, int column) { 066 if (tcui instanceof ThrottleFrame) { 067 return moveThrottleController((ThrottleFrame) tcui, row, column); 068 } else if (tcui instanceof SimpleThrottlePanel) { 069 return moveThrottleController((SimpleThrottlePanel) tcui, row, column); 070 } 071 return false; 072 } 073 074 private boolean moveThrottleController(ThrottleFrame tcui, int row_tf, int col_tw ) { 075 ThrottleControllersUIContainer destination = throttleFrameManager.getThrottleControllersContainerAt(col_tw); 076 ThrottleControllersUIContainer source = tcui.getThrottleControllersContainer(); 077 source.removeThrottleController(tcui); 078 if (destination instanceof ThrottleWindow) { 079 tcui.setThrottleControllersContainer(destination); 080 destination.addThrottleControllerAt(tcui, row_tf); 081 } 082 if (destination instanceof SimpleThrottleWindow) { 083 // create a new window with the same address as the one provided in parameter 084 InstanceManager.getDefault(ThrottleFrameManager.class).createSimpleThrottleFrame(tcui.getAddress()); 085 } 086 fireTableStructureChanged(); 087 return true; 088 } 089 090 private boolean moveThrottleController(SimpleThrottlePanel tcui, int row_tf, int col_tw ) { 091 ThrottleControllersUIContainer destination = throttleFrameManager.getThrottleControllersContainerAt(col_tw); 092 ThrottleControllersUIContainer source = tcui.getThrottleControllersContainer(); 093 if (destination instanceof ThrottleWindow) { 094 ThrottleFrame tf = new ThrottleFrame( (ThrottleWindow) destination); 095 tf.setAddress(tcui.getAddress()); 096 destination.addThrottleControllerAt(tf, row_tf); 097 source.dispose(); 098 } 099 // nothing to do for destination instanceof SimpleThrottleWindow 100 fireTableStructureChanged(); 101 return true; 102 } 103 104 public ThrottleControllersUIContainer getThrottleControllersContainerAt( int col_tw) { 105 return throttleFrameManager.getThrottleControllersContainerAt(col_tw); 106 } 107 108 @Override 109 public void propertyChange(java.beans.PropertyChangeEvent e) { 110 if (e.getPropertyName().equals("ThrottleFrameChanged")) { 111 fireTableDataChanged(); 112 } else if (e.getPropertyName().startsWith("ThrottleFrame")) { 113 fireTableStructureChanged(); 114 } 115 } 116 117 @Override 118 public void notifyConsistListChanged() { 119 fireTableDataChanged(); 120 } 121 122// private static final Logger log = LoggerFactory.getLogger(ThrottlesTableModel.class); 123}