1 package jspecview.js2d; 2 3 4 import javajs.api.GenericColor; 5 import javajs.util.CU; 6 import org.jmol.awtjs.swing.AbstractTableModel; 7 import org.jmol.awtjs.swing.TableColumn; 8 import javajs.util.BS; 9 import javajs.util.SB; 10 11 class DialogTableModel implements AbstractTableModel { 12 13 String[] columnNames; 14 Object[][] data; 15 boolean asString; 16 int[] widths; 17 private int thisCol; 18 private boolean tableCellAlignLeft; 19 DialogTableModel(String[] columnNames, Object[][] data, boolean asString, boolean tableCellAlignLeft )20 DialogTableModel(String[] columnNames, Object[][] data, boolean asString, boolean tableCellAlignLeft ) { 21 this.columnNames = columnNames; 22 this.data = data; 23 this.asString = asString; 24 this.widths = (data.length == 0 ? new int[0] : new int[data[0].length]); 25 this.tableCellAlignLeft = tableCellAlignLeft; 26 } 27 getColumnCount()28 public int getColumnCount() { 29 return columnNames.length; 30 } 31 getRowCount()32 public int getRowCount() { 33 return data.length; 34 } 35 getColumnName(int col)36 public String getColumnName(int col) { 37 return columnNames[col]; 38 } 39 getValueAt(int row, int col)40 public Object getValueAt(int row, int col) { 41 Object o = data[row][col]; 42 return (asString ? " " + o + " " : o); 43 } 44 45 @Override getColumn(int i)46 public TableColumn getColumn(int i) { 47 thisCol = i; 48 return this; 49 } 50 51 @Override setPreferredWidth(int n)52 public void setPreferredWidth(int n) { 53 widths[thisCol] = n; 54 } 55 56 @Override toHTML(SB sb, String id, BS selectedRows)57 public void toHTML(SB sb, String id, BS selectedRows) { 58 if (data == null || data[0] == null || data[0].length == 0) 59 return; 60 int nrows = data.length; 61 int ncols = columnNames.length; 62 for (int i = -1; i < nrows; i++) { 63 String rowid = id + "_" + i; 64 sb.append("\n<tr id='" + rowid + "' class='JTable_" + (i == -1 ? "header" : "row") + "' style='height:25px'>"); 65 for (int j = 0; j < ncols; j++) { 66 if (i == -1) 67 getCellHtml(sb, id + "_h" + j, i, j, columnNames[j], false); 68 else 69 getCellHtml(sb, rowid + "_" + j, i, j, data[i][j], selectedRows.get(i)); 70 } 71 sb.append("</tr>"); 72 } 73 } 74 getCellHtml(SB sb, String id, int iRow, int iCol, Object o, boolean isSelected)75 private void getCellHtml(SB sb, String id, int iRow, int iCol, Object o, boolean isSelected) { 76 String style = getCellStyle(id, iRow, iCol, o, isSelected); 77 sb.append("<td id='" + id + "'" + style 78 + " onclick=SwingController.click(this)>" + o + "</td>"); 79 } 80 81 /** 82 * @param id 83 * @param iRow 84 * @param iCol 85 * @param o 86 * @param isSelected 87 * @return CSS style attribute 88 */ getCellStyle(String id, int iRow, int iCol, Object o, boolean isSelected)89 private String getCellStyle(String id, int iRow, int iCol, Object o, boolean isSelected) { 90 String style = "padding:1px 1px 1px 1px"; 91 if (iRow < 0) { 92 style += ";font-weight:bold"; 93 } else { 94 if (o instanceof GenericColor) { 95 style += ";background-color:" 96 + CU.toCSSString((GenericColor) o); 97 } else { 98 if (asString) 99 o = " " + o + " "; 100 style += ";text-align:"; 101 if (tableCellAlignLeft) 102 style += "left"; 103 else if (iCol == 0) 104 style += "center"; 105 else 106 style += "right"; 107 style += ";border:" + (isSelected ? 3 : 1) + "px solid #000"; 108 } 109 } 110 return " style='" + style + "'"; 111 } 112 } 113