001package jmri.jmrit.beantable.oblock; 002 003import java.beans.PropertyChangeEvent; 004import java.beans.PropertyChangeListener; 005import java.util.Arrays; 006import java.util.List; 007import javax.swing.*; 008import javax.swing.table.AbstractTableModel; 009import jmri.jmrit.logix.OBlock; 010import jmri.jmrit.logix.Portal; 011import jmri.util.NamedBeanUserNameComparator; 012 013import org.slf4j.Logger; 014import org.slf4j.LoggerFactory; 015 016/** 017 * GUI to define Portal-Block-Portal combos for OBlocks. 018 * No differences between _desktop and _tabbed code, so _tabbed not stored. 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 Pete Cressman (C) 2010 031 */ 032public class BlockPortalTableModel extends AbstractTableModel implements PropertyChangeListener { 033 034 public static final int BLOCK_NAME_COLUMN = 0; 035 public static final int PORTAL_NAME_COLUMN = 1; 036 public static final int OPPOSING_BLOCK_NAME = 2; 037 public static final int NUMCOLS = 3; 038 039 OBlockTableModel _oBlockModel; 040 041 public BlockPortalTableModel(OBlockTableModel oBlockModel) { 042 super(); 043 _oBlockModel = oBlockModel; 044 } 045 046 @Override 047 public int getColumnCount() { 048 return NUMCOLS; 049 } 050 051 @Override 052 public int getRowCount() { 053 int count = 0; 054 List<OBlock> list = _oBlockModel.getBeanList(); 055 for (OBlock oBlock : list) { 056 count += oBlock.getPortals().size(); 057 } 058 return count; // no temprow for edit, so no need for -1 for _tabbed 059 } 060 061 @Override 062 public String getColumnName(int col) { 063 switch (col) { 064 case BLOCK_NAME_COLUMN: 065 return Bundle.getMessage("BlockName"); 066 case PORTAL_NAME_COLUMN: 067 return Bundle.getMessage("PortalName"); 068 case OPPOSING_BLOCK_NAME: 069 return Bundle.getMessage("OppBlockName"); 070 default: 071 log.warn("Unhandled column name: {}", col); 072 break; 073 } 074 return ""; 075 } 076 077 @Override 078 public Object getValueAt(int row, int col) { 079 List<OBlock> list = _oBlockModel.getBeanList(); 080 if (list.size() > 0) { 081 int count = 0; 082 int idx = 0; // accumulated row count 083 OBlock block; 084 OBlock[] array = new OBlock[list.size()]; 085 array = list.toArray(array); 086 Arrays.sort(array, new NamedBeanUserNameComparator<>()); 087 while (count <= row) { 088 count += array[idx++].getPortals().size(); 089 } 090 block = array[--idx]; 091 idx = row - (count - block.getPortals().size()); 092 if (col == BLOCK_NAME_COLUMN) { 093 if (idx == 0) { 094 return block.getDisplayName(); 095 } 096 return ""; 097 } 098 if (col == PORTAL_NAME_COLUMN) { 099 return block.getPortals().get(idx).getName(); 100 } 101 if (col == OPPOSING_BLOCK_NAME) { 102 Portal portal = block.getPortals().get(idx); 103 OBlock oppBlock = portal.getOpposingBlock(block); 104 if (oppBlock != null) { 105 return oppBlock.getDisplayName(); 106 } 107 } 108 } 109 return null; 110 } 111 112 @Override 113 public void setValueAt(Object value, int row, int col) { 114 } 115 116 @Override 117 public boolean isCellEditable(int row, int col) { 118 return false; 119 } 120 121 @Override 122 public Class<?> getColumnClass(int col) { 123 return String.class; 124 } 125 126 public int getPreferredWidth(int col) { 127 return new JTextField(20).getPreferredSize().width; 128 } 129 130 @Override 131 public void propertyChange(PropertyChangeEvent e) { 132 String property = e.getPropertyName(); 133 if (property.equals("length") || property.equals("UserName") || 134 property.equals("portalCount") ) { 135 fireTableDataChanged(); 136 } 137 } 138 139 private final static Logger log = LoggerFactory.getLogger(BlockPortalTableModel.class); 140 141}