[99] | 1 | package com.framsticks.gui.table; |
---|
| 2 | |
---|
| 3 | import java.util.ArrayList; |
---|
| 4 | import java.util.Collections; |
---|
| 5 | import java.util.List; |
---|
| 6 | |
---|
[100] | 7 | import javax.swing.JCheckBox; |
---|
| 8 | import javax.swing.JComponent; |
---|
| 9 | import javax.swing.JTable; |
---|
| 10 | import javax.swing.UIManager; |
---|
[99] | 11 | |
---|
[100] | 12 | import org.apache.logging.log4j.Logger; |
---|
| 13 | import org.apache.logging.log4j.LogManager; |
---|
[99] | 14 | |
---|
[102] | 15 | import com.framsticks.gui.Frame; |
---|
[100] | 16 | import com.framsticks.params.Access; |
---|
[99] | 17 | import com.framsticks.params.ListAccess; |
---|
[100] | 18 | import com.framsticks.params.Param; |
---|
[99] | 19 | import com.framsticks.params.PrimitiveParam; |
---|
[100] | 20 | import com.framsticks.params.types.ProcedureParam; |
---|
[99] | 21 | import com.framsticks.util.lang.Casting; |
---|
| 22 | |
---|
[101] | 23 | public class TableModel extends AbstractTableModel { |
---|
[99] | 24 | |
---|
[100] | 25 | private static final Logger log = LogManager.getLogger(TableModel.class); |
---|
[99] | 26 | |
---|
| 27 | protected ListAccess access; |
---|
[100] | 28 | protected Access elementAccess; |
---|
| 29 | protected final List<Column> columns = new ArrayList<>(); |
---|
| 30 | protected final ListPanel listPanel; |
---|
| 31 | protected JTable table; |
---|
[99] | 32 | |
---|
[100] | 33 | /** |
---|
| 34 | * @param listPanel |
---|
| 35 | */ |
---|
| 36 | public TableModel(ListPanel listPanel) { |
---|
| 37 | this.listPanel = listPanel; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * @return the listPanel |
---|
| 42 | */ |
---|
| 43 | public ListPanel getListPanel() { |
---|
| 44 | return listPanel; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | public JTable getTable() { |
---|
| 48 | return listPanel.getTable(); |
---|
| 49 | } |
---|
| 50 | |
---|
[99] | 51 | |
---|
[100] | 52 | /** |
---|
| 53 | * @param table the table to set |
---|
| 54 | */ |
---|
| 55 | public void setupTable() { |
---|
| 56 | getTable().setDefaultRenderer(ProcedureParam.class, new ProcedureRenderer()); |
---|
| 57 | getTable().setDefaultEditor(ProcedureParam.class, new ProcedureEditor(new JCheckBox())); |
---|
[99] | 58 | } |
---|
| 59 | |
---|
| 60 | public void attachSource(ListAccess access) { |
---|
| 61 | this.access = Casting.assertCast(ListAccess.class, access.cloneAccess().select(access.getSelected())); |
---|
| 62 | |
---|
| 63 | this.elementAccess = this.access.getElementAccess().cloneAccess(); |
---|
[100] | 64 | log.debug("attached {}", access); |
---|
[99] | 65 | refreshAll(); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | @Override |
---|
| 70 | public Class<?> getColumnClass(int columnIndex) { |
---|
[100] | 71 | // log.debug("returning column type {}", columnParams.get(columnIndex).getStorageType()); |
---|
| 72 | return columns.get(columnIndex).getColumnClass(); |
---|
[99] | 73 | } |
---|
| 74 | |
---|
| 75 | @Override |
---|
| 76 | public int getColumnCount() { |
---|
[100] | 77 | // log.debug("returning column count {}", columnParams.size()); |
---|
| 78 | return columns.size(); |
---|
[99] | 79 | } |
---|
| 80 | |
---|
| 81 | @Override |
---|
| 82 | public String getColumnName(int columnIndex) { |
---|
[100] | 83 | return columns.get(columnIndex).getParam().getName(); |
---|
[99] | 84 | } |
---|
| 85 | |
---|
| 86 | @Override |
---|
| 87 | public int getRowCount() { |
---|
[100] | 88 | // log.debug("returning row count {}", access.getParamCount()); |
---|
| 89 | return access == null ? 0 : access.getParamCount(); |
---|
[99] | 90 | } |
---|
| 91 | |
---|
| 92 | @Override |
---|
| 93 | public Object getValueAt(int rowIndex, int columnIndex) { |
---|
[100] | 94 | assert (rowIndex < access.getParamCount() && columnIndex < columns.size()); |
---|
[99] | 95 | |
---|
[100] | 96 | return columns.get(columnIndex).getValueAt(rowIndex); |
---|
[99] | 97 | } |
---|
| 98 | |
---|
| 99 | @Override |
---|
| 100 | public boolean isCellEditable(int rowIndex, int columnIndex) { |
---|
[100] | 101 | return columns.get(columnIndex).isEditable(); |
---|
[99] | 102 | } |
---|
| 103 | |
---|
| 104 | |
---|
[100] | 105 | |
---|
[99] | 106 | @Override |
---|
| 107 | public void setValueAt(Object value, int rowIndex, int columnIndex) { |
---|
[100] | 108 | assert (rowIndex < access.getParamCount() && columnIndex < columns.size()); |
---|
| 109 | // Object object = getObjectAt(rowIndex, columnIndex); |
---|
| 110 | // if (object == null) { |
---|
| 111 | // return; |
---|
| 112 | // } |
---|
| 113 | columns.get(columnIndex).setValueAt(rowIndex, value); |
---|
| 114 | } |
---|
[99] | 115 | |
---|
[100] | 116 | public void addColumn(Column column) { |
---|
| 117 | log.debug("added {}", column); |
---|
| 118 | columns.add(column); |
---|
[99] | 119 | } |
---|
| 120 | |
---|
[100] | 121 | /** |
---|
| 122 | * @return the access |
---|
| 123 | */ |
---|
| 124 | public ListAccess getAccess() { |
---|
| 125 | return access; |
---|
[99] | 126 | } |
---|
| 127 | |
---|
| 128 | /** |
---|
[100] | 129 | * @return the elementAccess |
---|
| 130 | */ |
---|
| 131 | public Access getElementAccess() { |
---|
| 132 | return elementAccess; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /** |
---|
[99] | 136 | * @return the columnParams |
---|
| 137 | */ |
---|
[100] | 138 | public List<Column> getColumns() { |
---|
| 139 | return Collections.unmodifiableList(columns); |
---|
[99] | 140 | } |
---|
| 141 | |
---|
[100] | 142 | |
---|
| 143 | public boolean addColumnIfSupported(Param param) { |
---|
| 144 | if (param instanceof PrimitiveParam) { |
---|
| 145 | addColumn(new PrimitiveColumn((PrimitiveParam<?>) param, this)); |
---|
| 146 | return true; |
---|
| 147 | } |
---|
| 148 | if (param instanceof ProcedureParam) { |
---|
| 149 | if (((ProcedureParam) param).getArgumentsType().size() == 0) { |
---|
| 150 | addColumn(new ProcedureColumn((ProcedureParam) param, this)); |
---|
| 151 | return true; |
---|
| 152 | } |
---|
| 153 | return false; |
---|
| 154 | } |
---|
| 155 | return false; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | public static void transferCellAppeariance(JTable table, JComponent component, boolean isSelected) { |
---|
| 159 | if (isSelected) { |
---|
| 160 | component.setForeground(table.getSelectionForeground()); |
---|
| 161 | component.setBackground(table.getSelectionBackground()); |
---|
| 162 | } else { |
---|
| 163 | component.setForeground(table.getForeground()); |
---|
| 164 | component.setBackground(UIManager.getColor("Button.background")); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | } |
---|
| 168 | |
---|
[102] | 169 | public Frame getFrame() { |
---|
| 170 | return listPanel.getFrame(); |
---|
| 171 | } |
---|
| 172 | |
---|
[99] | 173 | } |
---|