1 19 20 package org.netbeans.swing.popupswitcher; 21 22 import javax.swing.event.TableModelEvent ; 23 import javax.swing.table.AbstractTableModel ; 24 import org.openide.util.Utilities; 25 26 33 class SwitcherTableModel extends AbstractTableModel { 34 35 39 private TableModelEvent event; 40 41 42 private int rows; 43 44 45 private int cols; 46 47 48 private SwitcherTableItem[] items; 49 50 54 SwitcherTableModel(SwitcherTableItem[] items, int rowHeight) { 55 this(items, rowHeight, Utilities.getUsableScreenBounds().height); 56 } 57 58 59 SwitcherTableModel(SwitcherTableItem[] items, int rowHeight, int tableHeight) { 60 super(); 61 this.items = items; 62 computeRowsAndCols(rowHeight, tableHeight); 63 } 64 65 private void computeRowsAndCols(int rowHeight, int tableHeight) { 66 int nOfItems = items.length; 68 if (nOfItems > 0) { int maxRowsPerCol = tableHeight / rowHeight; 71 int nOfColumns = (nOfItems / maxRowsPerCol); 72 if (nOfItems % maxRowsPerCol > 0) { 73 nOfColumns++; 74 } 75 int nOfRows = nOfItems / nOfColumns; 76 if (nOfItems % nOfColumns > 0) { 77 nOfRows++; 78 } 79 setRowsAndColumns(nOfRows, nOfColumns); 80 } else { 81 setRowsAndColumns(0, 0); 82 } 83 } 84 85 private void setRowsAndColumns(int rows, int cols) { 86 if ((this.rows != rows) || (this.cols != cols)) { 87 this.rows = rows; 88 this.cols = cols; 89 if (event == null) { 90 event = new TableModelEvent (this); 91 } 92 fireTableChanged(event); 93 } 94 } 95 96 public Class getColumnClass(int columnIndex) { 97 return SwitcherTableItem.class; 98 } 99 100 public String getColumnName(int columnIndex) { 101 return ""; 102 } 103 104 public boolean isCellEditable(int rowIndex, int columnIndex) { 105 return true; 106 } 107 108 public Object getValueAt(int rowIndex, int columnIndex) { 109 if ((rowIndex == -1) || (columnIndex == -1)) { 110 return null; 111 } 112 int docIdx = (columnIndex * getRowCount()) + rowIndex; 113 return (docIdx < items.length ? items[docIdx] : null); 114 } 115 116 public int getRowCount() { 117 return rows; 118 } 119 120 public int getColumnCount() { 121 return cols; 122 } 123 } 124 | Popular Tags |