| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.app.bss; |
| 27 |
|
| 28 |
import jam.framework.Exportable; |
| 29 |
import jam.panels.ActionPanel; |
| 30 |
import jam.table.TableRenderer; |
| 31 |
|
| 32 |
import java.awt.BorderLayout; |
| 33 |
import java.awt.Insets; |
| 34 |
import java.awt.event.ActionEvent; |
| 35 |
|
| 36 |
import javax.swing.AbstractAction; |
| 37 |
import javax.swing.Action; |
| 38 |
import javax.swing.JComponent; |
| 39 |
import javax.swing.JPanel; |
| 40 |
import javax.swing.JScrollPane; |
| 41 |
import javax.swing.JTable; |
| 42 |
import javax.swing.SwingConstants; |
| 43 |
import javax.swing.event.TableModelEvent; |
| 44 |
import javax.swing.event.TableModelListener; |
| 45 |
import javax.swing.table.TableColumn; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
@SuppressWarnings("serial") |
| 53 |
public class PartitionsPanel extends JPanel implements Exportable { |
| 54 |
|
| 55 |
private PartitionDataList dataList = null; |
| 56 |
|
| 57 |
private JTable partitionTable = null; |
| 58 |
private PartitionTableModel partitionTableModel = null; |
| 59 |
private JTableComboBoxCellEditor jTableComboBoxCellEditor = null; |
| 60 |
|
| 61 |
private TableColumnHider hider; |
| 62 |
private JScrollPane scrollPane; |
| 63 |
private TableColumn column; |
| 64 |
private int columnIndex; |
| 65 |
private int partitionsCount; |
| 66 |
|
| 67 |
private Action addPartitionAction = new AbstractAction("+") { |
| 68 |
public void actionPerformed(ActionEvent ae) { |
| 69 |
|
| 70 |
|
| 71 |
partitionTableModel.addDefaultRow(); |
| 72 |
setPartitions(); |
| 73 |
|
| 74 |
} |
| 75 |
}; |
| 76 |
|
| 77 |
private Action removePartitionAction = new AbstractAction("-") { |
| 78 |
public void actionPerformed(ActionEvent ae) { |
| 79 |
if (partitionsCount > 1) { |
| 80 |
|
| 81 |
partitionTableModel.deleteRow(partitionsCount - 1); |
| 82 |
setPartitions(); |
| 83 |
|
| 84 |
} |
| 85 |
} |
| 86 |
}; |
| 87 |
|
| 88 |
public PartitionsPanel(PartitionDataList dataList) { |
| 89 |
|
| 90 |
this.dataList = dataList; |
| 91 |
|
| 92 |
partitionTable = new JTable(); |
| 93 |
partitionTable.getTableHeader().setReorderingAllowed(false); |
| 94 |
partitionTable.addMouseListener(new JTableButtonMouseListener( |
| 95 |
partitionTable)); |
| 96 |
|
| 97 |
partitionTableModel = new PartitionTableModel(this.dataList); |
| 98 |
partitionTableModel |
| 99 |
.addTableModelListener(new PartitionTableModelListener()); |
| 100 |
partitionTable.setModel(partitionTableModel); |
| 101 |
|
| 102 |
hider = new TableColumnHider(partitionTable); |
| 103 |
|
| 104 |
setLayout(new BorderLayout()); |
| 105 |
|
| 106 |
scrollPane = new JScrollPane(partitionTable, |
| 107 |
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, |
| 108 |
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); |
| 109 |
RowNumberTable rowNumberTable = new RowNumberTable(partitionTable); |
| 110 |
scrollPane.setRowHeaderView(rowNumberTable); |
| 111 |
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, |
| 112 |
rowNumberTable.getTableHeader()); |
| 113 |
|
| 114 |
scrollPane.getViewport().setOpaque(false); |
| 115 |
|
| 116 |
add(scrollPane, BorderLayout.CENTER); |
| 117 |
|
| 118 |
|
| 119 |
columnIndex = PartitionTableModel.DATA_INDEX; |
| 120 |
column = partitionTable.getColumnModel().getColumn( |
| 121 |
columnIndex); |
| 122 |
column.setCellRenderer(new JTableComboBoxCellRenderer(columnIndex)); |
| 123 |
jTableComboBoxCellEditor = new JTableComboBoxCellEditor(this.dataList); |
| 124 |
column.setCellEditor(jTableComboBoxCellEditor); |
| 125 |
column.setMinWidth(100); |
| 126 |
|
| 127 |
column = partitionTable.getColumnModel().getColumn( |
| 128 |
PartitionTableModel.FROM_INDEX); |
| 129 |
column.setCellRenderer( |
| 130 |
new TableRenderer(SwingConstants.LEFT, new Insets(0, 2, |
| 131 |
0, 2))); |
| 132 |
column.setPreferredWidth(80); |
| 133 |
|
| 134 |
column = partitionTable.getColumnModel().getColumn( |
| 135 |
PartitionTableModel.TO_INDEX); |
| 136 |
column.setCellRenderer( |
| 137 |
new TableRenderer(SwingConstants.LEFT, new Insets(0, 2, |
| 138 |
0, 2))); |
| 139 |
column.setPreferredWidth(80); |
| 140 |
|
| 141 |
column = partitionTable.getColumnModel().getColumn( |
| 142 |
PartitionTableModel.EVERY_INDEX); |
| 143 |
column.setCellRenderer( |
| 144 |
new TableRenderer(SwingConstants.LEFT, new Insets(0, 2, |
| 145 |
0, 2))); |
| 146 |
column.setPreferredWidth(80); |
| 147 |
|
| 148 |
columnIndex = PartitionTableModel.DATA_TYPE_INDEX; |
| 149 |
column = partitionTable.getColumnModel().getColumn( |
| 150 |
columnIndex); |
| 151 |
column.setCellEditor(new JTableComboBoxCellEditor(this.dataList)); |
| 152 |
column.setCellRenderer(new JTableComboBoxCellRenderer(columnIndex)); |
| 153 |
|
| 154 |
column = partitionTable.getColumnModel().getColumn( |
| 155 |
PartitionTableModel.DEMOGRAPHIC_MODEL_INDEX); |
| 156 |
column.setCellRenderer(new JTableButtonCellRenderer()); |
| 157 |
column.setCellEditor(new JTableButtonCellEditor()); |
| 158 |
|
| 159 |
column = partitionTable.getColumnModel().getColumn( |
| 160 |
PartitionTableModel.BRANCH_SUBSTITUTION_MODEL_INDEX); |
| 161 |
column.setCellRenderer(new JTableButtonCellRenderer()); |
| 162 |
column.setCellEditor(new JTableButtonCellEditor()); |
| 163 |
|
| 164 |
column = partitionTable.getColumnModel().getColumn( |
| 165 |
PartitionTableModel.FREQUENCY_MODEL_INDEX); |
| 166 |
column.setCellRenderer(new JTableButtonCellRenderer()); |
| 167 |
column.setCellEditor(new JTableButtonCellEditor()); |
| 168 |
|
| 169 |
column = partitionTable.getColumnModel().getColumn( |
| 170 |
PartitionTableModel.SITE_RATE_MODEL_INDEX); |
| 171 |
column.setCellRenderer(new JTableButtonCellRenderer()); |
| 172 |
column.setCellEditor(new JTableButtonCellEditor()); |
| 173 |
|
| 174 |
column = partitionTable.getColumnModel().getColumn( |
| 175 |
PartitionTableModel.CLOCK_RATE_MODEL_INDEX); |
| 176 |
column.setCellRenderer(new JTableButtonCellRenderer()); |
| 177 |
column.setCellEditor(new JTableButtonCellEditor()); |
| 178 |
|
| 179 |
String toolTipText = "Leave this field empty to start from a random root sequence."; |
| 180 |
column = partitionTable.getColumnModel().getColumn( |
| 181 |
PartitionTableModel.ROOT_SEQUENCE_INDEX); |
| 182 |
JTableButtonCellRenderer jTableButtonCellRenderer = new JTableButtonCellRenderer(); |
| 183 |
jTableButtonCellRenderer.setColumnToolTipText(toolTipText); |
| 184 |
column.setCellRenderer(jTableButtonCellRenderer); |
| 185 |
column.setCellEditor(new JTableButtonCellEditor()); |
| 186 |
|
| 187 |
ActionPanel actionPanel = new ActionPanel(false); |
| 188 |
actionPanel.setAddAction(addPartitionAction); |
| 189 |
actionPanel.setRemoveAction(removePartitionAction); |
| 190 |
add(actionPanel, BorderLayout.SOUTH); |
| 191 |
|
| 192 |
setPartitions(); |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
private void setPartitions() { |
| 197 |
|
| 198 |
partitionsCount = dataList.size(); |
| 199 |
|
| 200 |
addPartitionAction.setEnabled(true); |
| 201 |
if (partitionsCount == 1) { |
| 202 |
removePartitionAction.setEnabled(false); |
| 203 |
} else { |
| 204 |
removePartitionAction.setEnabled(true); |
| 205 |
} |
| 206 |
|
| 207 |
ColumnResizer.adjustColumnPreferredWidths(partitionTable); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
private class PartitionTableModelListener implements TableModelListener { |
| 212 |
|
| 213 |
public void tableChanged(TableModelEvent ev) { |
| 214 |
|
| 215 |
if (ev.getType() == TableModelEvent.UPDATE) { |
| 216 |
int row = ev.getFirstRow(); |
| 217 |
int column = ev.getColumn(); |
| 218 |
|
| 219 |
if (column == PartitionTableModel.DATA_INDEX) { |
| 220 |
|
| 221 |
TreesTableRecord value = (TreesTableRecord) partitionTableModel.getValueAt(row, |
| 222 |
column); |
| 223 |
|
| 224 |
dataList.get(row).record = (TreesTableRecord) value; |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
public void hideTreeColumn() { |
| 236 |
hider.hide(PartitionTableModel.COLUMN_NAMES[PartitionTableModel.DATA_INDEX]); |
| 237 |
} |
| 238 |
|
| 239 |
public void showTreeColumn() { |
| 240 |
hider.show(PartitionTableModel.COLUMN_NAMES[PartitionTableModel.DATA_INDEX]); |
| 241 |
} |
| 242 |
|
| 243 |
public JComponent getExportableComponent() { |
| 244 |
return this; |
| 245 |
} |
| 246 |
|
| 247 |
public void updatePartitionTable(PartitionDataList dataList) { |
| 248 |
partitionTableModel.setDataList(dataList); |
| 249 |
setDataList(dataList); |
| 250 |
setPartitions(); |
| 251 |
jTableComboBoxCellEditor.setDataList(dataList); |
| 252 |
partitionTableModel.fireTableDataChanged(); |
| 253 |
} |
| 254 |
|
| 255 |
public void setDataList(PartitionDataList dataList) { |
| 256 |
this.dataList = dataList; |
| 257 |
} |
| 258 |
|
| 259 |
} |