| 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.beauti.ancestralstatespanel; |
| 27 |
|
| 28 |
import dr.app.beauti.BeautiFrame; |
| 29 |
import dr.app.beauti.BeautiPanel; |
| 30 |
import dr.app.beauti.options.AbstractPartitionData; |
| 31 |
import dr.app.beauti.options.BeautiOptions; |
| 32 |
import dr.app.gui.table.TableEditorStopper; |
| 33 |
import dr.evolution.datatype.DataType; |
| 34 |
import jam.framework.Exportable; |
| 35 |
|
| 36 |
import javax.swing.*; |
| 37 |
import javax.swing.border.TitledBorder; |
| 38 |
import javax.swing.event.ListSelectionEvent; |
| 39 |
import javax.swing.event.ListSelectionListener; |
| 40 |
import javax.swing.plaf.BorderUIResource; |
| 41 |
import javax.swing.table.*; |
| 42 |
import java.awt.*; |
| 43 |
import java.util.HashMap; |
| 44 |
import java.util.Map; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class AncestralStatesPanel extends BeautiPanel implements Exportable { |
| 52 |
|
| 53 |
private static final long serialVersionUID = 2778103564318492601L; |
| 54 |
|
| 55 |
private static final int MINIMUM_TABLE_WIDTH = 140; |
| 56 |
|
| 57 |
JTable partitionTable = null; |
| 58 |
PartitionTableModel partitionTableModel = null; |
| 59 |
BeautiOptions options = null; |
| 60 |
|
| 61 |
JPanel optionsPanelParent; |
| 62 |
AbstractPartitionData currentPartition = null; |
| 63 |
Map<AbstractPartitionData, AncestralStatesOptionsPanel> optionsPanels = new HashMap<AbstractPartitionData, AncestralStatesOptionsPanel>(); |
| 64 |
TitledBorder optionsBorder; |
| 65 |
|
| 66 |
BeautiFrame frame = null; |
| 67 |
boolean settingOptions = false; |
| 68 |
|
| 69 |
public AncestralStatesPanel(BeautiFrame parent) { |
| 70 |
|
| 71 |
super(); |
| 72 |
|
| 73 |
this.frame = parent; |
| 74 |
|
| 75 |
partitionTableModel = new PartitionTableModel(); |
| 76 |
partitionTable = new JTable(partitionTableModel); |
| 77 |
|
| 78 |
partitionTable.getTableHeader().setReorderingAllowed(false); |
| 79 |
partitionTable.getTableHeader().setResizingAllowed(false); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
final TableColumnModel model = partitionTable.getColumnModel(); |
| 84 |
final TableColumn tableColumn0 = model.getColumn(0); |
| 85 |
|
| 86 |
TableEditorStopper.ensureEditingStopWhenTableLosesFocus(partitionTable); |
| 87 |
|
| 88 |
partitionTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
| 89 |
partitionTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
| 90 |
public void valueChanged(ListSelectionEvent evt) { |
| 91 |
selectionChanged(); |
| 92 |
} |
| 93 |
}); |
| 94 |
|
| 95 |
JScrollPane scrollPane = new JScrollPane(partitionTable, |
| 96 |
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); |
| 97 |
scrollPane.setOpaque(false); |
| 98 |
|
| 99 |
JPanel panel = new JPanel(new BorderLayout(0, 0)); |
| 100 |
panel.setOpaque(false); |
| 101 |
panel.add(scrollPane, BorderLayout.CENTER); |
| 102 |
panel.setMinimumSize(new Dimension(MINIMUM_TABLE_WIDTH, 0)); |
| 103 |
|
| 104 |
optionsPanelParent = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
| 105 |
optionsPanelParent.setOpaque(false); |
| 106 |
optionsBorder = new TitledBorder(null, "Ancestral state options:", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.ABOVE_TOP); |
| 107 |
optionsPanelParent.setBorder(optionsBorder); |
| 108 |
|
| 109 |
setCurrentPartition(null); |
| 110 |
|
| 111 |
JScrollPane scrollPane2 = new JScrollPane(optionsPanelParent, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
| 112 |
scrollPane2.setOpaque(false); |
| 113 |
scrollPane2.setBorder(null); |
| 114 |
scrollPane2.getViewport().setOpaque(false); |
| 115 |
|
| 116 |
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel, scrollPane2); |
| 117 |
splitPane.setDividerLocation(MINIMUM_TABLE_WIDTH); |
| 118 |
splitPane.setContinuousLayout(true); |
| 119 |
splitPane.setBorder(BorderFactory.createEmptyBorder()); |
| 120 |
splitPane.setOpaque(false); |
| 121 |
|
| 122 |
setOpaque(false); |
| 123 |
setBorder(new BorderUIResource.EmptyBorderUIResource(new Insets(12, 12, 12, 12))); |
| 124 |
setLayout(new BorderLayout(0, 0)); |
| 125 |
add(splitPane, BorderLayout.CENTER); |
| 126 |
} |
| 127 |
|
| 128 |
private void resetPanel() { |
| 129 |
if (!options.hasData()) { |
| 130 |
currentPartition = null; |
| 131 |
optionsPanels.clear(); |
| 132 |
optionsPanelParent.removeAll(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
public void setOptions(BeautiOptions options) { |
| 137 |
|
| 138 |
this.options = options; |
| 139 |
|
| 140 |
resetPanel(); |
| 141 |
|
| 142 |
settingOptions = true; |
| 143 |
|
| 144 |
int selRow = partitionTable.getSelectedRow(); |
| 145 |
partitionTableModel.fireTableDataChanged(); |
| 146 |
if (options.getDataPartitions().size() > 0) { |
| 147 |
if (selRow < 0) { |
| 148 |
selRow = 0; |
| 149 |
} |
| 150 |
partitionTable.getSelectionModel().setSelectionInterval(selRow, selRow); |
| 151 |
|
| 152 |
setCurrentPartition(options.getDataPartitions().get(selRow)); |
| 153 |
} |
| 154 |
|
| 155 |
AncestralStatesOptionsPanel panel = optionsPanels.get(currentPartition); |
| 156 |
if (panel != null) { |
| 157 |
panel.setupPanel(); |
| 158 |
} |
| 159 |
|
| 160 |
settingOptions = false; |
| 161 |
|
| 162 |
validate(); |
| 163 |
repaint(); |
| 164 |
} |
| 165 |
|
| 166 |
public void getOptions(BeautiOptions options) { |
| 167 |
} |
| 168 |
|
| 169 |
public void fireModelChanged() { |
| 170 |
frame.setDirty(); |
| 171 |
} |
| 172 |
|
| 173 |
private void selectionChanged() { |
| 174 |
if (settingOptions) return; |
| 175 |
|
| 176 |
int selRow = partitionTable.getSelectedRow(); |
| 177 |
|
| 178 |
if (selRow >= options.getDataPartitions().size()) { |
| 179 |
selRow = 0; |
| 180 |
partitionTable.getSelectionModel().setSelectionInterval(selRow, selRow); |
| 181 |
} |
| 182 |
|
| 183 |
if (selRow >= 0) { |
| 184 |
setCurrentPartition(options.getDataPartitions().get(selRow)); |
| 185 |
|
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
private void setCurrentPartition(AbstractPartitionData partition) { |
| 195 |
if (partition != null) { |
| 196 |
if (currentPartition != null) optionsPanelParent.removeAll(); |
| 197 |
|
| 198 |
AncestralStatesOptionsPanel panel = optionsPanels.get(partition); |
| 199 |
if (panel == null) { |
| 200 |
panel = new AncestralStatesOptionsPanel(this, options, partition); |
| 201 |
optionsPanels.put(partition, panel); |
| 202 |
} |
| 203 |
|
| 204 |
currentPartition = partition; |
| 205 |
|
| 206 |
panel.setupPanel(); |
| 207 |
|
| 208 |
optionsPanelParent.add(panel); |
| 209 |
|
| 210 |
updateBorder(); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
private void updateBorder() { |
| 215 |
|
| 216 |
String title; |
| 217 |
|
| 218 |
switch (currentPartition.getDataType().getType()) { |
| 219 |
case DataType.NUCLEOTIDES: |
| 220 |
title = "Nucleotide"; |
| 221 |
break; |
| 222 |
case DataType.AMINO_ACIDS: |
| 223 |
title = "Amino Acid"; |
| 224 |
break; |
| 225 |
case DataType.TWO_STATES: |
| 226 |
title = "Binary"; |
| 227 |
break; |
| 228 |
case DataType.GENERAL: |
| 229 |
title = "Discrete Traits"; |
| 230 |
break; |
| 231 |
case DataType.CONTINUOUS: |
| 232 |
title = "Continuous Traits"; |
| 233 |
break; |
| 234 |
case DataType.MICRO_SAT: |
| 235 |
title = "Microsatellite"; |
| 236 |
break; |
| 237 |
default: |
| 238 |
throw new IllegalArgumentException("Unsupported data type"); |
| 239 |
|
| 240 |
} |
| 241 |
optionsBorder.setTitle(title + " Partition - " + currentPartition.getName()); |
| 242 |
repaint(); |
| 243 |
} |
| 244 |
|
| 245 |
public JComponent getExportableComponent() { |
| 246 |
return this; |
| 247 |
} |
| 248 |
|
| 249 |
class PartitionTableModel extends AbstractTableModel { |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
private static final long serialVersionUID = -6707994233020715574L; |
| 255 |
String[] columnNames = {"Partition"}; |
| 256 |
|
| 257 |
public PartitionTableModel() { |
| 258 |
} |
| 259 |
|
| 260 |
public int getColumnCount() { |
| 261 |
return columnNames.length; |
| 262 |
} |
| 263 |
|
| 264 |
public int getRowCount() { |
| 265 |
if (options == null) return 0; |
| 266 |
return options.getDataPartitions().size(); |
| 267 |
} |
| 268 |
|
| 269 |
public Object getValueAt(int row, int col) { |
| 270 |
AbstractPartitionData partition = options.getDataPartitions().get(row); |
| 271 |
switch (col) { |
| 272 |
case 0: |
| 273 |
return partition.getName(); |
| 274 |
default: |
| 275 |
throw new IllegalArgumentException("unknown column, " + col); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
public boolean isCellEditable(int row, int col) { |
| 280 |
return false; |
| 281 |
} |
| 282 |
|
| 283 |
public String getColumnName(int column) { |
| 284 |
return columnNames[column]; |
| 285 |
} |
| 286 |
|
| 287 |
public Class getColumnClass(int c) { |
| 288 |
if (getRowCount() == 0) { |
| 289 |
return Object.class; |
| 290 |
} |
| 291 |
return getValueAt(0, c).getClass(); |
| 292 |
} |
| 293 |
|
| 294 |
public String toString() { |
| 295 |
StringBuffer buffer = new StringBuffer(); |
| 296 |
|
| 297 |
buffer.append(getColumnName(0)); |
| 298 |
for (int j = 1; j < getColumnCount(); j++) { |
| 299 |
buffer.append("\t"); |
| 300 |
buffer.append(getColumnName(j)); |
| 301 |
} |
| 302 |
buffer.append("\n"); |
| 303 |
|
| 304 |
for (int i = 0; i < getRowCount(); i++) { |
| 305 |
buffer.append(getValueAt(i, 0)); |
| 306 |
for (int j = 1; j < getColumnCount(); j++) { |
| 307 |
buffer.append("\t"); |
| 308 |
buffer.append(getValueAt(i, j)); |
| 309 |
} |
| 310 |
buffer.append("\n"); |
| 311 |
} |
| 312 |
|
| 313 |
return buffer.toString(); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
} |