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;
008
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012import jmri.util.davidflanagan.HardcopyWriter;
013
014/**
015 * Table Model for access to producer info
016 *
017 * @author Bob Jacobsen 2008
018  * @since 2.3.7
019 */
020public class ConsumerTableModel extends AbstractTableModel {
021
022    static ResourceBundle rb = ResourceBundle.getBundle("jmri.jmrix.openlcb.swing.tie.TieBundle");
023
024    public static final int USERNAME_COLUMN = 0;
025    public static final int NODE_COLUMN = 1;
026    public static final int NUMBER_COLUMN = 2;
027    final String[] columnName = new String[]{"User Name", "Node", "Event"};
028
029    @Override
030    public String getColumnName(int c) {
031        return columnName[c];
032    }
033
034    @Override
035    public Class<?> getColumnClass(int c) {
036        return String.class;
037    }
038
039    @Override
040    public boolean isCellEditable(int r, int c) {
041        return false;
042    }
043
044    @Override
045    public int getColumnCount() {
046        return columnName.length;
047    }
048
049    @Override
050    public int getRowCount() {
051        return dummy.length;
052    }
053
054    @Override
055    public Object getValueAt(int r, int c) {
056        return dummy[r][c];  // for testing
057    }
058
059    @Override
060    public void setValueAt(Object type, int r, int c) {
061        // nothing is stored here
062    }
063
064    final String[][] dummy = {{"Turnout 21 Left", "11", "1"}, // row then column
065    {"Turnout 21 Right", "11", "2"},
066    {"Turnout 22 Right", "13", "1"},
067    {"Turnout 22 Left", "13", "2"},
068    {"Turnout 23 Right", "13", "3"},
069    {"Turnout 23 Left", "13", "4"},
070    {"Turnout 24 Right", "15", "1"},
071    {"Turnout 24 Left", "15", "2"}
072    };
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(HardcopyWriter 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(HardcopyWriter 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(ConsumerTableModel.class);
211
212}
213
214