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