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 ConsumerTableModel 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 = {{"Turnout 21 Left", "11", "1"}, // row then column
064    {"Turnout 21 Right", "11", "2"},
065    {"Turnout 22 Right", "13", "1"},
066    {"Turnout 22 Left", "13", "2"},
067    {"Turnout 23 Right", "13", "3"},
068    {"Turnout 23 Left", "13", "4"},
069    {"Turnout 24 Right", "15", "1"},
070    {"Turnout 24 Left", "15", "2"}
071    };
072
073    /**
074     * Method to print or print preview the assignment table. Printed in
075     * proportionately sized columns across the page with headings and vertical
076     * lines between each column. Data is word wrapped within a column. Can only
077     * handle 4 columns of data as strings. Adapted from routines in
078     * BeanTableDataModel.java by Bob Jacobsen and Dennis Miller
079     * @param w hard copy writer connection
080     * @param colWidth array of column widths
081     */
082    public void printTable(OriginalHardcopyWriter w, int[] colWidth) {
083        // determine the column sizes - proportionately sized, with space between for lines
084        int[] columnSize = new int[4];
085        int charPerLine = w.getCharactersPerLine();
086        int tableLineWidth = 0;  // table line width in characters
087        int totalColWidth = 0;
088        for (int j = 0; j < 4; j++) {
089            totalColWidth += colWidth[j];
090        }
091        float ratio = ((float) charPerLine) / ((float) totalColWidth);
092        for (int j = 0; j < 4; j++) {
093            columnSize[j] = (int) Math.round(colWidth[j] * ratio - 1.);
094            tableLineWidth += (columnSize[j] + 1);
095        }
096
097        // Draw horizontal dividing line
098        w.write(w.getCurrentLineNumber(), 0, w.getCurrentLineNumber(),
099                tableLineWidth);
100
101        // print the column header labels
102        String[] columnStrings = new String[4];
103        // Put each column header in the array
104        for (int i = 0; i < 4; i++) {
105            columnStrings[i] = this.getColumnName(i);
106        }
107        w.setFontStyle(Font.BOLD);
108        printColumns(w, columnStrings, columnSize);
109        w.setFontStyle(Font.PLAIN);
110        // draw horizontal line
111        w.write(w.getCurrentLineNumber(), 0, w.getCurrentLineNumber(),
112                tableLineWidth);
113
114        // now print each row of data
115        String[] spaces = new String[4];
116        // create base strings the width of each of the columns
117        for (int k = 0; k < 4; k++) {
118            spaces[k] = "";
119            for (int i = 0; i < columnSize[k]; i++) {
120                spaces[k] = spaces[k] + " ";
121            }
122        }
123        for (int i = 0; i < this.getRowCount(); i++) {
124            for (int j = 0; j < 4; j++) {
125                //check for special, null contents
126                if (this.getValueAt(i, j) == null) {
127                    columnStrings[j] = spaces[j];
128                } else {
129                    columnStrings[j] = (String) this.getValueAt(i, j);
130                }
131            }
132            printColumns(w, columnStrings, columnSize);
133            // draw horizontal line
134            w.write(w.getCurrentLineNumber(), 0, w.getCurrentLineNumber(),
135                    tableLineWidth);
136        }
137        w.close();
138    }
139
140    protected void printColumns(OriginalHardcopyWriter w, String[] columnStrings, int[] columnSize) {
141        StringBuilder columnString = new StringBuilder();
142        StringBuilder lineString = new StringBuilder();
143        String[] spaces = new String[4];
144        // create base strings the width of each of the columns
145        for (int k = 0; k < 4; k++) {
146            spaces[k] = "";
147            for (int i = 0; i < columnSize[k]; i++) {
148                spaces[k] = spaces[k] + " ";
149            }
150        }
151        // loop through each column
152        boolean complete = false;
153        while (!complete) {
154            complete = true;
155            for (int i = 0; i < 4; i++) {
156                // if the column string is too wide cut it at word boundary (valid delimiters are space, - and _)
157                // use the initial part of the text,pad it with spaces and place the remainder back in the array
158                // for further processing on next line
159                // if column string isn't too wide, pad it to column width with spaces if needed
160                if (columnStrings[i].length() > columnSize[i]) {
161                    // this column string will not fit on one line
162                    boolean noWord = true;
163                    for (int k = columnSize[i]; k >= 1; k--) {
164                        if (columnStrings[i].startsWith(" ", k - 1)
165                                || columnStrings[i].startsWith("-", k - 1)
166                                || columnStrings[i].startsWith("_", k - 1)) {
167                            columnString = new StringBuilder(columnStrings[i].substring(0, k));
168                            columnString.append(spaces[i].substring(columnStrings[i].substring(0, k).length()));
169                            columnStrings[i] = columnStrings[i].substring(k);
170                            noWord = false;
171                            complete = false;
172                            break;
173                        }
174                    }
175                    if (noWord) {
176                        columnString = new StringBuilder(columnStrings[i].substring(0, columnSize[i]));
177                        columnStrings[i] = columnStrings[i].substring(columnSize[i]);
178                        complete = false;
179                    }
180                } else {
181                    // this column string will fit on one line
182                    columnString = new StringBuilder(columnStrings[i]);
183                    columnString.append(spaces[i].substring(columnStrings[i].length()));
184                    columnStrings[i] = "";
185                }
186                lineString.append(columnString);
187                lineString.append(" ");
188            }
189            try {
190                w.write(lineString.toString());
191                //write vertical dividing lines
192                int iLine = w.getCurrentLineNumber();
193                for (int i = 0, k = 0; i < w.getCharactersPerLine(); k++) {
194                    w.write(iLine, i, iLine + 1, i);
195                    if (k < 4) {
196                        i = i + columnSize[k] + 1;
197                    } else {
198                        i = w.getCharactersPerLine();
199                    }
200                }
201                w.write("\n");
202                lineString = new StringBuilder();
203            } catch (IOException e) {
204                log.warn("error during printing", e);
205            }
206        }
207    }
208
209    private final static Logger log = LoggerFactory.getLogger(ConsumerTableModel.class);
210
211}
212
213