1 | package com.framsticks.gui.table; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.Collections; |
---|
5 | import java.util.List; |
---|
6 | |
---|
7 | import javax.swing.JCheckBox; |
---|
8 | import javax.swing.JComponent; |
---|
9 | import javax.swing.JTable; |
---|
10 | import javax.swing.UIManager; |
---|
11 | |
---|
12 | import org.apache.logging.log4j.Logger; |
---|
13 | import org.apache.logging.log4j.LogManager; |
---|
14 | |
---|
15 | import com.framsticks.gui.Frame; |
---|
16 | import com.framsticks.params.Access; |
---|
17 | import com.framsticks.params.ListAccess; |
---|
18 | import com.framsticks.params.Param; |
---|
19 | import com.framsticks.params.PrimitiveParam; |
---|
20 | import com.framsticks.params.types.ProcedureParam; |
---|
21 | import com.framsticks.util.lang.Casting; |
---|
22 | |
---|
23 | public class TableModel extends AbstractTableModel { |
---|
24 | |
---|
25 | private static final Logger log = LogManager.getLogger(TableModel.class); |
---|
26 | |
---|
27 | protected ListAccess access; |
---|
28 | protected Access elementAccess; |
---|
29 | protected final List<Column> columns = new ArrayList<>(); |
---|
30 | protected final ListPanel listPanel; |
---|
31 | protected JTable table; |
---|
32 | |
---|
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 | |
---|
51 | |
---|
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())); |
---|
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(); |
---|
64 | log.debug("attached {}", access); |
---|
65 | refreshAll(); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | @Override |
---|
70 | public Class<?> getColumnClass(int columnIndex) { |
---|
71 | // log.debug("returning column type {}", columnParams.get(columnIndex).getStorageType()); |
---|
72 | return columns.get(columnIndex).getColumnClass(); |
---|
73 | } |
---|
74 | |
---|
75 | @Override |
---|
76 | public int getColumnCount() { |
---|
77 | // log.debug("returning column count {}", columnParams.size()); |
---|
78 | return columns.size(); |
---|
79 | } |
---|
80 | |
---|
81 | @Override |
---|
82 | public String getColumnName(int columnIndex) { |
---|
83 | return columns.get(columnIndex).getParam().getName(); |
---|
84 | } |
---|
85 | |
---|
86 | @Override |
---|
87 | public int getRowCount() { |
---|
88 | // log.debug("returning row count {}", access.getParamCount()); |
---|
89 | return access == null ? 0 : access.getParamCount(); |
---|
90 | } |
---|
91 | |
---|
92 | @Override |
---|
93 | public Object getValueAt(int rowIndex, int columnIndex) { |
---|
94 | assert (rowIndex < access.getParamCount() && columnIndex < columns.size()); |
---|
95 | |
---|
96 | return columns.get(columnIndex).getValueAt(rowIndex); |
---|
97 | } |
---|
98 | |
---|
99 | @Override |
---|
100 | public boolean isCellEditable(int rowIndex, int columnIndex) { |
---|
101 | return columns.get(columnIndex).isEditable(); |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | @Override |
---|
107 | public void setValueAt(Object value, int rowIndex, int columnIndex) { |
---|
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 | } |
---|
115 | |
---|
116 | public void addColumn(Column column) { |
---|
117 | log.debug("added {}", column); |
---|
118 | columns.add(column); |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * @return the access |
---|
123 | */ |
---|
124 | public ListAccess getAccess() { |
---|
125 | return access; |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * @return the elementAccess |
---|
130 | */ |
---|
131 | public Access getElementAccess() { |
---|
132 | return elementAccess; |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * @return the columnParams |
---|
137 | */ |
---|
138 | public List<Column> getColumns() { |
---|
139 | return Collections.unmodifiableList(columns); |
---|
140 | } |
---|
141 | |
---|
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 | |
---|
169 | public Frame getFrame() { |
---|
170 | return listPanel.getFrame(); |
---|
171 | } |
---|
172 | |
---|
173 | } |
---|