| 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.treespanel; |
| 27 |
|
| 28 |
import dr.app.beauti.BeautiFrame; |
| 29 |
import dr.app.beauti.BeautiPanel; |
| 30 |
import dr.app.beauti.options.*; |
| 31 |
import dr.app.beauti.types.TreePriorType; |
| 32 |
import dr.app.gui.table.TableEditorStopper; |
| 33 |
import dr.evolution.datatype.Microsatellite; |
| 34 |
import jam.framework.Exportable; |
| 35 |
import jam.table.TableRenderer; |
| 36 |
|
| 37 |
import javax.swing.*; |
| 38 |
import javax.swing.border.TitledBorder; |
| 39 |
import javax.swing.event.ListSelectionEvent; |
| 40 |
import javax.swing.event.ListSelectionListener; |
| 41 |
import javax.swing.plaf.BorderUIResource; |
| 42 |
import javax.swing.table.AbstractTableModel; |
| 43 |
import javax.swing.table.TableColumn; |
| 44 |
import javax.swing.table.TableColumnModel; |
| 45 |
import java.awt.*; |
| 46 |
import java.awt.event.ItemEvent; |
| 47 |
import java.awt.event.ItemListener; |
| 48 |
import java.util.HashMap; |
| 49 |
import java.util.List; |
| 50 |
import java.util.Map; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public class TreesPanel extends BeautiPanel implements Exportable { |
| 59 |
|
| 60 |
public final static boolean DEBUG = false; |
| 61 |
|
| 62 |
private static final long serialVersionUID = 2778103564318492601L; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private BeautiFrame frame = null; |
| 71 |
public BeautiOptions options = null; |
| 72 |
|
| 73 |
private JTable treesTable = null; |
| 74 |
private TreesTableModel treesTableModel = null; |
| 75 |
|
| 76 |
|
| 77 |
private boolean settingOptions = false; |
| 78 |
|
| 79 |
|
| 80 |
public JCheckBox linkTreePriorCheck = new JCheckBox("Link tree prior for all trees"); |
| 81 |
|
| 82 |
JPanel treeModelPanelParent; |
| 83 |
public PartitionTreeModel currentTreeModel = null; |
| 84 |
TitledBorder treeModelBorder; |
| 85 |
Map<PartitionTreeModel, PartitionTreeModelPanel> treeModelPanels = new HashMap<PartitionTreeModel, PartitionTreeModelPanel>(); |
| 86 |
JPanel treePriorPanelParent; |
| 87 |
TitledBorder treePriorBorder; |
| 88 |
Map<PartitionTreePrior, PartitionTreePriorPanel> treePriorPanels = new HashMap<PartitionTreePrior, PartitionTreePriorPanel>(); |
| 89 |
|
| 90 |
public TreesPanel(BeautiFrame parent, Action removeTreeAction) { |
| 91 |
super(); |
| 92 |
this.frame = parent; |
| 93 |
|
| 94 |
treesTableModel = new TreesTableModel(); |
| 95 |
treesTable = new JTable(treesTableModel); |
| 96 |
|
| 97 |
treesTable.getTableHeader().setReorderingAllowed(false); |
| 98 |
treesTable.getTableHeader().setResizingAllowed(false); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
final TableColumnModel model = treesTable.getColumnModel(); |
| 103 |
final TableColumn tableColumn0 = model.getColumn(0); |
| 104 |
tableColumn0.setCellRenderer(new ModelsTableCellRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4))); |
| 105 |
|
| 106 |
TableEditorStopper.ensureEditingStopWhenTableLosesFocus(treesTable); |
| 107 |
|
| 108 |
treesTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
| 109 |
treesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
| 110 |
public void valueChanged(ListSelectionEvent evt) { |
| 111 |
selectionChanged(); |
| 112 |
} |
| 113 |
}); |
| 114 |
|
| 115 |
JScrollPane scrollPane = new JScrollPane(treesTable, |
| 116 |
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, |
| 117 |
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
| 118 |
scrollPane.setOpaque(false); |
| 119 |
|
| 120 |
JPanel controlPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT)); |
| 121 |
controlPanel1.setOpaque(false); |
| 122 |
|
| 123 |
setCurrentModelAndPrior(null); |
| 124 |
|
| 125 |
JPanel panel1 = new JPanel(new BorderLayout(0, 0)); |
| 126 |
panel1.setOpaque(false); |
| 127 |
panel1.add(scrollPane, BorderLayout.CENTER); |
| 128 |
|
| 129 |
treeModelPanelParent = new JPanel(new FlowLayout(FlowLayout.LEFT)); |
| 130 |
treeModelPanelParent.setOpaque(false); |
| 131 |
treeModelBorder = new TitledBorder(null, "Tree Model", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.ABOVE_TOP); |
| 132 |
treeModelPanelParent.setBorder(treeModelBorder); |
| 133 |
|
| 134 |
JScrollPane scrollPane2 = new JScrollPane(treeModelPanelParent, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
| 135 |
scrollPane2.setOpaque(false); |
| 136 |
scrollPane2.setBorder(null); |
| 137 |
scrollPane2.getViewport().setOpaque(false); |
| 138 |
|
| 139 |
treePriorPanelParent = new JPanel(new FlowLayout(FlowLayout.LEFT)); |
| 140 |
treePriorPanelParent.setOpaque(false); |
| 141 |
treePriorBorder = new TitledBorder(null, "Tree Prior", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.ABOVE_TOP); |
| 142 |
treePriorPanelParent.setBorder(treePriorBorder); |
| 143 |
|
| 144 |
JPanel panel3 = new JPanel(new FlowLayout(FlowLayout.LEFT)); |
| 145 |
panel3.setOpaque(false); |
| 146 |
linkTreePriorCheck.setEnabled(false); |
| 147 |
linkTreePriorCheck.setSelected(true); |
| 148 |
linkTreePriorCheck.addItemListener(new ItemListener() { |
| 149 |
public void itemStateChanged(ItemEvent ev) { |
| 150 |
updateShareSameTreePriorChanged(); |
| 151 |
} |
| 152 |
}); |
| 153 |
linkTreePriorCheck.setToolTipText("Decide whether to use one tree prior for all trees"); |
| 154 |
panel3.add(linkTreePriorCheck); |
| 155 |
|
| 156 |
JPanel panel4 = new JPanel(new BorderLayout()); |
| 157 |
panel4.setOpaque(false); |
| 158 |
panel4.add(treePriorPanelParent, BorderLayout.NORTH); |
| 159 |
panel4.add(scrollPane2, BorderLayout.CENTER); |
| 160 |
|
| 161 |
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel1, panel4); |
| 162 |
splitPane.setDividerLocation(180); |
| 163 |
splitPane.setContinuousLayout(true); |
| 164 |
splitPane.setBorder(BorderFactory.createEmptyBorder()); |
| 165 |
splitPane.setOpaque(false); |
| 166 |
|
| 167 |
setOpaque(false); |
| 168 |
setLayout(new BorderLayout(0, 0)); |
| 169 |
setBorder(new BorderUIResource.EmptyBorderUIResource(new Insets(12, 12, 12, 12))); |
| 170 |
|
| 171 |
add(panel3, BorderLayout.NORTH); |
| 172 |
add(splitPane, BorderLayout.CENTER); |
| 173 |
} |
| 174 |
|
| 175 |
public void updateShareSameTreePriorChanged() { |
| 176 |
if (linkTreePriorCheck.isSelected()) { |
| 177 |
options.linkTreePriors(currentTreeModel.getPartitionTreePrior()); |
| 178 |
} else { |
| 179 |
options.unLinkTreePriors(currentTreeModel); |
| 180 |
} |
| 181 |
updateTreePriorBorder(); |
| 182 |
|
| 183 |
fireTreePriorsChanged(); |
| 184 |
} |
| 185 |
|
| 186 |
private void updateTreePriorBorder() { |
| 187 |
if (options.isShareSameTreePrior()) { |
| 188 |
treePriorBorder.setTitle("Tree prior shared by all tree models"); |
| 189 |
} else { |
| 190 |
treePriorBorder.setTitle("Tree Prior - " + currentTreeModel.getPartitionTreePrior().getName()); |
| 191 |
} |
| 192 |
repaint(); |
| 193 |
} |
| 194 |
|
| 195 |
public void fireTreePriorsChanged() { |
| 196 |
|
| 197 |
frame.setDirty(); |
| 198 |
} |
| 199 |
|
| 200 |
private void selectionChanged() { |
| 201 |
int selRow = treesTable.getSelectedRow(); |
| 202 |
|
| 203 |
if (selRow >= options.getPartitionTreeModels().size()) { |
| 204 |
selRow = 0; |
| 205 |
treesTable.getSelectionModel().setSelectionInterval(selRow, selRow); |
| 206 |
} |
| 207 |
|
| 208 |
if (selRow >= 0) { |
| 209 |
PartitionTreeModel ptm = options.getPartitionTreeModels().get(selRow); |
| 210 |
setCurrentModelAndPrior(ptm); |
| 211 |
|
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
private void setCurrentModelAndPrior(PartitionTreeModel model) { |
| 221 |
|
| 222 |
if (model != null) { |
| 223 |
if (currentTreeModel != null) treeModelPanelParent.removeAll(); |
| 224 |
|
| 225 |
PartitionTreeModelPanel panel = treeModelPanels.get(model); |
| 226 |
if (panel == null) { |
| 227 |
panel = new PartitionTreeModelPanel(frame, model, options); |
| 228 |
treeModelPanels.put(model, panel); |
| 229 |
} |
| 230 |
|
| 231 |
currentTreeModel = model; |
| 232 |
treeModelBorder.setTitle("Tree Model - " + model.getName()); |
| 233 |
treeModelPanelParent.add(panel); |
| 234 |
|
| 235 |
|
| 236 |
if (model.getPartitionTreePrior() != null) treePriorPanelParent.removeAll(); |
| 237 |
|
| 238 |
PartitionTreePrior prior = model.getPartitionTreePrior(); |
| 239 |
|
| 240 |
PartitionTreePriorPanel panel1 = treePriorPanels.get(prior); |
| 241 |
if (panel1 == null) { |
| 242 |
panel1 = new PartitionTreePriorPanel(prior, this); |
| 243 |
treePriorPanels.put(prior, panel1); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
updateTreePriorBorder(); |
| 248 |
treePriorPanelParent.add(panel1); |
| 249 |
|
| 250 |
repaint(); |
| 251 |
} else { |
| 252 |
|
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
private void resetPanel() { |
| 257 |
if (!options.hasData()) { |
| 258 |
currentTreeModel = null; |
| 259 |
treeModelPanels.clear(); |
| 260 |
|
| 261 |
treePriorPanels.clear(); |
| 262 |
|
| 263 |
treeModelPanelParent.removeAll(); |
| 264 |
treeModelBorder.setTitle("Tree Model"); |
| 265 |
treePriorPanelParent.removeAll(); |
| 266 |
treePriorBorder.setTitle("Tree Prior"); |
| 267 |
|
| 268 |
return; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
public void updateLinkTreePriorEnablility() { |
| 273 |
boolean selected = !(options.getPartitionTreeModels().size() < 2 |
| 274 |
|| options.contains(Microsatellite.INSTANCE)); |
| 275 |
|
| 276 |
linkTreePriorCheck.setEnabled(selected); |
| 277 |
} |
| 278 |
|
| 279 |
public void setOptions(BeautiOptions options) { |
| 280 |
this.options = options; |
| 281 |
|
| 282 |
resetPanel(); |
| 283 |
|
| 284 |
settingOptions = true; |
| 285 |
|
| 286 |
updateLinkTreePriorEnablility(); |
| 287 |
linkTreePriorCheck.setSelected(options.isShareSameTreePrior()); |
| 288 |
|
| 289 |
for (PartitionTreeModel model : options.getPartitionTreeModels()) { |
| 290 |
if (model != null && treeModelPanels.get(model) != null) treeModelPanels.get(model).setupPanel(); |
| 291 |
} |
| 292 |
|
| 293 |
for (PartitionTreePrior prior : options.getPartitionTreePriors()) { |
| 294 |
PartitionTreePriorPanel ptpp = treePriorPanels.get(prior); |
| 295 |
if (ptpp != null) { |
| 296 |
ptpp.setTreePriorChoices(options.getPartitionTreeModels().size() > 1, |
| 297 |
options.useTipDates); |
| 298 |
|
| 299 |
ptpp.setOptions(); |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
ptpp.repaint(); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
settingOptions = false; |
| 309 |
|
| 310 |
int selRow = treesTable.getSelectedRow(); |
| 311 |
treesTableModel.fireTableDataChanged(); |
| 312 |
if (options.getPartitionTreeModels().size() > 0) { |
| 313 |
if (selRow < 0) { |
| 314 |
selRow = 0; |
| 315 |
} |
| 316 |
treesTable.getSelectionModel().setSelectionInterval(selRow, selRow); |
| 317 |
} |
| 318 |
|
| 319 |
if (currentTreeModel == null && options.getPartitionTreeModels().size() > 0) { |
| 320 |
treesTable.getSelectionModel().setSelectionInterval(0, 0); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
validate(); |
| 326 |
repaint(); |
| 327 |
} |
| 328 |
|
| 329 |
public void getOptions(BeautiOptions options) { |
| 330 |
if (settingOptions) return; |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
for (PartitionTreePrior prior : options.getPartitionTreePriors()) { |
| 339 |
PartitionTreePriorPanel ptpp = treePriorPanels.get(prior); |
| 340 |
if (ptpp != null) { |
| 341 |
ptpp.getOptions(); |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
public BeautiFrame getFrame() { |
| 347 |
return frame; |
| 348 |
} |
| 349 |
|
| 350 |
public JComponent getExportableComponent() { |
| 351 |
|
| 352 |
return this; |
| 353 |
} |
| 354 |
|
| 355 |
private boolean isUsed(int row) { |
| 356 |
PartitionTreeModel model = options.getPartitionTreeModels().get(row); |
| 357 |
for (AbstractPartitionData partition : options.dataPartitions) { |
| 358 |
if (partition.getPartitionTreeModel() == model) { |
| 359 |
return true; |
| 360 |
} |
| 361 |
} |
| 362 |
return false; |
| 363 |
} |
| 364 |
|
| 365 |
class TreesTableModel extends AbstractTableModel { |
| 366 |
|
| 367 |
private static final long serialVersionUID = -6707994233020715574L; |
| 368 |
String[] columnNames = {"Trees"}; |
| 369 |
|
| 370 |
public TreesTableModel() { |
| 371 |
} |
| 372 |
|
| 373 |
public int getColumnCount() { |
| 374 |
return columnNames.length; |
| 375 |
} |
| 376 |
|
| 377 |
public int getRowCount() { |
| 378 |
if (options == null) return 0; |
| 379 |
return options.getPartitionTreeModels().size(); |
| 380 |
} |
| 381 |
|
| 382 |
public Object getValueAt(int row, int col) { |
| 383 |
PartitionTreeModel model = options.getPartitionTreeModels().get(row); |
| 384 |
switch (col) { |
| 385 |
case 0: |
| 386 |
return model.getName(); |
| 387 |
default: |
| 388 |
throw new IllegalArgumentException("unknown column, " + col); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
public void setValueAt(Object aValue, int row, int col) { |
| 393 |
|
| 394 |
switch (col) { |
| 395 |
case 0: |
| 396 |
String name = ((String) aValue).trim(); |
| 397 |
if (name.length() > 0) { |
| 398 |
PartitionTreeModel model = options.getPartitionTreeModels().get(row); |
| 399 |
model.setName(name); |
| 400 |
|
| 401 |
PartitionTreePrior prior = model.getPartitionTreePrior(); |
| 402 |
prior.setName(name); |
| 403 |
fireTreePriorsChanged(); |
| 404 |
} |
| 405 |
break; |
| 406 |
default: |
| 407 |
throw new IllegalArgumentException("unknown column, " + col); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
public boolean isCellEditable(int row, int col) { |
| 412 |
return false; |
| 413 |
} |
| 414 |
|
| 415 |
public String getColumnName(int column) { |
| 416 |
return columnNames[column]; |
| 417 |
} |
| 418 |
|
| 419 |
public Class getColumnClass(int c) { |
| 420 |
if (getRowCount() == 0) { |
| 421 |
return Object.class; |
| 422 |
} |
| 423 |
return getValueAt(0, c).getClass(); |
| 424 |
} |
| 425 |
|
| 426 |
public String toString() { |
| 427 |
StringBuffer buffer = new StringBuffer(); |
| 428 |
|
| 429 |
buffer.append(getColumnName(0)); |
| 430 |
for (int j = 1; j < getColumnCount(); j++) { |
| 431 |
buffer.append("\t"); |
| 432 |
buffer.append(getColumnName(j)); |
| 433 |
} |
| 434 |
buffer.append("\n"); |
| 435 |
|
| 436 |
for (int i = 0; i < getRowCount(); i++) { |
| 437 |
buffer.append(getValueAt(i, 0)); |
| 438 |
for (int j = 1; j < getColumnCount(); j++) { |
| 439 |
buffer.append("\t"); |
| 440 |
buffer.append(getValueAt(i, j)); |
| 441 |
} |
| 442 |
buffer.append("\n"); |
| 443 |
} |
| 444 |
|
| 445 |
return buffer.toString(); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
class ModelsTableCellRenderer extends TableRenderer { |
| 450 |
|
| 451 |
public ModelsTableCellRenderer(int alignment, Insets insets) { |
| 452 |
super(alignment, insets); |
| 453 |
} |
| 454 |
|
| 455 |
public Component getTableCellRendererComponent(JTable aTable, |
| 456 |
Object value, |
| 457 |
boolean aIsSelected, |
| 458 |
boolean aHasFocus, |
| 459 |
int aRow, int aColumn) { |
| 460 |
|
| 461 |
if (value == null) return this; |
| 462 |
|
| 463 |
Component renderer = super.getTableCellRendererComponent(aTable, |
| 464 |
value, |
| 465 |
aIsSelected, |
| 466 |
aHasFocus, |
| 467 |
aRow, aColumn); |
| 468 |
|
| 469 |
if (!isUsed(aRow)) |
| 470 |
renderer.setForeground(Color.gray); |
| 471 |
else |
| 472 |
renderer.setForeground(Color.black); |
| 473 |
return this; |
| 474 |
} |
| 475 |
|
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
} |