001package jmri.jmrix.openlcb.swing.tie; 002 003import java.awt.Font; 004import java.io.IOException; 005import java.util.ResourceBundle; 006 007import javax.swing.table.AbstractTableModel; 008import jmri.util.davidflanagan.OriginalHardcopyWriter; 009import org.slf4j.Logger; 010import org.slf4j.LoggerFactory; 011 012 013/** 014 * Table Model for access to producer info 015 * 016 * @author Bob Jacobsen 2008 017 * @since 2.3.7 018 */ 019public class ProducerTableModel extends AbstractTableModel { 020 021 static ResourceBundle rb = ResourceBundle.getBundle("jmri.jmrix.openlcb.swing.tie.TieBundle"); 022 023 public static final int USERNAME_COLUMN = 0; 024 public static final int NODE_COLUMN = 1; 025 public static final int NUMBER_COLUMN = 2; 026 final String[] columnName = new String[]{"User Name", "Node", "Event"}; 027 028 @Override 029 public String getColumnName(int c) { 030 return columnName[c]; 031 } 032 033 @Override 034 public Class<?> getColumnClass(int c) { 035 return String.class; 036 } 037 038 @Override 039 public boolean isCellEditable(int r, int c) { 040 return false; 041 } 042 043 @Override 044 public int getColumnCount() { 045 return columnName.length; 046 } 047 048 @Override 049 public int getRowCount() { 050 return dummy.length; 051 } 052 053 @Override 054 public Object getValueAt(int r, int c) { 055 return dummy[r][c]; // for testing 056 } 057 058 @Override 059 public void setValueAt(Object type, int r, int c) { 060 // nothing is stored here 061 } 062 063 final String[][] dummy = {{"East Lower Yard Button 1", "12", "1"}, // row then column 064 {"East Lower Yard Button 2", "12", "2"}, 065 {"East Lower Yard Button 3", "12", "3"}, 066 {"East Lower Yard Button 4", "12", "4"}, 067 {"East Lower Yard Button 5", "12", "5"}, 068 {"West Lower Yard Button 1", "14", "5"}, 069 {"West Lower Yard Button 2", "14", "4"}, 070 {"West Lower Yard Button 3", "14", "3"}, 071 {"West Lower Yard Button 4", "14", "2"}, 072 {"West Lower Yard Button 5", "14", "1"},}; 073 074 /** 075 * Method to print or print preview the assignment table. Printed in 076 * proportionately sized columns across the page with headings and vertical 077 * lines between each column. Data is word wrapped within a column. Can only 078 * handle 4 columns of data as strings. Adapted from routines in 079 * BeanTableDataModel.java by Bob Jacobsen and Dennis Miller 080 * @param w hard copy writer connection 081 * @param colWidth array of column widths 082 */ 083 public void printTable(OriginalHardcopyWriter w, int[] colWidth) { 084 // determine the column sizes - proportionately sized, with space between for lines 085 int[] columnSize = new int[4]; 086 int charPerLine = w.getCharactersPerLine(); 087 int tableLineWidth = 0; // table line width in characters 088 int totalColWidth = 0; 089 for (int j = 0; j < 4; j++) { 090 totalColWidth += colWidth[j]; 091 } 092 float ratio = ((float) charPerLine) / ((float) totalColWidth); 093 for (int j = 0; j < 4; j++) { 094 columnSize[j] = (int) Math.round(colWidth[j] * ratio - 1.); 095 tableLineWidth += (columnSize[j] + 1); 096 } 097 098 // Draw horizontal dividing line 099 w.write(w.getCurrentLineNumber(), 0, w.getCurrentLineNumber(), 100 tableLineWidth); 101 102 // print the column header labels 103 String[] columnStrings = new String[4]; 104 // Put each column header in the array 105 for (int i = 0; i < 4; i++) { 106 columnStrings[i] = this.getColumnName(i); 107 } 108 w.setFontStyle(Font.BOLD); 109 printColumns(w, columnStrings, columnSize); 110 w.setFontStyle(Font.PLAIN); 111 // draw horizontal line 112 w.write(w.getCurrentLineNumber(), 0, w.getCurrentLineNumber(), 113 tableLineWidth); 114 115 // now print each row of data 116 String[] spaces = new String[4]; 117 // create base strings the width of each of the columns 118 for (int k = 0; k < 4; k++) { 119 spaces[k] = ""; 120 for (int i = 0; i < columnSize[k]; i++) { 121 spaces[k] = spaces[k] + " "; 122 } 123 } 124 for (int i = 0; i < this.getRowCount(); i++) { 125 for (int j = 0; j < 4; j++) { 126 //check for special, null contents 127 if (this.getValueAt(i, j) == null) { 128 columnStrings[j] = spaces[j]; 129 } else { 130 columnStrings[j] = (String) this.getValueAt(i, j); 131 } 132 } 133 printColumns(w, columnStrings, columnSize); 134 // draw horizontal line 135 w.write(w.getCurrentLineNumber(), 0, w.getCurrentLineNumber(), 136 tableLineWidth); 137 } 138 w.close(); 139 } 140 141 protected void printColumns(OriginalHardcopyWriter w, String[] columnStrings, int[] columnSize) { 142 StringBuilder columnString = new StringBuilder(); 143 StringBuilder lineString = new StringBuilder(); 144 String[] spaces = new String[4]; 145 // create base strings the width of each of the columns 146 for (int k = 0; k < 4; k++) { 147 spaces[k] = ""; 148 for (int i = 0; i < columnSize[k]; i++) { 149 spaces[k] = spaces[k] + " "; 150 } 151 } 152 // loop through each column 153 boolean complete = false; 154 while (!complete) { 155 complete = true; 156 for (int i = 0; i < 4; i++) { 157 // if the column string is too wide cut it at word boundary (valid delimiters are space, - and _) 158 // use the initial part of the text,pad it with spaces and place the remainder back in the array 159 // for further processing on next line 160 // if column string isn't too wide, pad it to column width with spaces if needed 161 if (columnStrings[i].length() > columnSize[i]) { 162 // this column string will not fit on one line 163 boolean noWord = true; 164 for (int k = columnSize[i]; k >= 1; k--) { 165 if (columnStrings[i].startsWith(" ", k - 1) 166 || columnStrings[i].startsWith("-", k - 1) 167 || columnStrings[i].startsWith("_", k - 1)) { 168 columnString = new StringBuilder(columnStrings[i].substring(0, k)); 169 columnString.append(spaces[i].substring(columnStrings[i].substring(0, k).length())); 170 columnStrings[i] = columnStrings[i].substring(k); 171 noWord = false; 172 complete = false; 173 break; 174 } 175 } 176 if (noWord) { 177 columnString = new StringBuilder(columnStrings[i].substring(0, columnSize[i])); 178 columnStrings[i] = columnStrings[i].substring(columnSize[i]); 179 complete = false; 180 } 181 } else { 182 // this column string will fit on one line 183 columnString = new StringBuilder(columnStrings[i]); 184 columnString.append(spaces[i].substring(columnStrings[i].length())); 185 columnStrings[i] = ""; 186 } 187 lineString.append(columnString); 188 lineString.append(" "); 189 } 190 try { 191 w.write(lineString.toString()); 192 //write vertical dividing lines 193 int iLine = w.getCurrentLineNumber(); 194 for (int i = 0, k = 0; i < w.getCharactersPerLine(); k++) { 195 w.write(iLine, i, iLine + 1, i); 196 if (k < 4) { 197 i = i + columnSize[k] + 1; 198 } else { 199 i = w.getCharactersPerLine(); 200 } 201 } 202 w.write("\n"); 203 lineString = new StringBuilder(); 204 } catch (IOException e) { 205 log.warn("error during printing", e); 206 } 207 } 208 } 209 210 private final static Logger log = LoggerFactory.getLogger(ProducerTableModel.class); 211 212}