| 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.priorspanel; |
| 27 |
|
| 28 |
import dr.app.beauti.BeautiFrame; |
| 29 |
import dr.app.beauti.BeautiPanel; |
| 30 |
import dr.app.beauti.components.hpm.HierarchicalModelComponentOptions; |
| 31 |
import dr.app.beauti.components.hpm.HierarchicalPhylogeneticModel; |
| 32 |
import dr.app.beauti.components.linkedparameters.LinkedParameter; |
| 33 |
import dr.app.beauti.components.linkedparameters.LinkedParameterComponentOptions; |
| 34 |
import dr.app.beauti.options.*; |
| 35 |
import dr.app.beauti.types.ClockType; |
| 36 |
import dr.app.beauti.types.PriorType; |
| 37 |
import dr.app.beauti.util.PanelUtils; |
| 38 |
import dr.app.gui.table.TableEditorStopper; |
| 39 |
import dr.util.NumberFormatter; |
| 40 |
import jam.framework.Exportable; |
| 41 |
import jam.table.TableRenderer; |
| 42 |
|
| 43 |
import javax.swing.*; |
| 44 |
import javax.swing.event.ListSelectionEvent; |
| 45 |
import javax.swing.event.ListSelectionListener; |
| 46 |
import javax.swing.plaf.BorderUIResource; |
| 47 |
import javax.swing.table.DefaultTableCellRenderer; |
| 48 |
import javax.swing.table.TableCellRenderer; |
| 49 |
import java.awt.*; |
| 50 |
import java.awt.event.ActionEvent; |
| 51 |
import java.awt.event.ActionListener; |
| 52 |
import java.util.ArrayList; |
| 53 |
import java.util.List; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public class PriorsPanel extends BeautiPanel implements Exportable { |
| 61 |
private static final long serialVersionUID = -2936049032365493416L; |
| 62 |
JScrollPane scrollPane = new JScrollPane(); |
| 63 |
JTable priorTable = null; |
| 64 |
PriorTableModel priorTableModel = null; |
| 65 |
JLabel messageLabel = new JLabel(); |
| 66 |
JButton linkButton = null; |
| 67 |
JButton hpmButton = null; |
| 68 |
JButton unlinkButton = null; |
| 69 |
|
| 70 |
JCheckBox classicPriorsCheck = null; |
| 71 |
|
| 72 |
public List<Parameter> parameters = new ArrayList<Parameter>(); |
| 73 |
|
| 74 |
BeautiFrame frame = null; |
| 75 |
BeautiOptions options = null; |
| 76 |
|
| 77 |
boolean hasUndefinedPrior = false; |
| 78 |
boolean hasImproperPrior = false; |
| 79 |
boolean hasRate = false; |
| 80 |
|
| 81 |
private final boolean isDefaultOnly; |
| 82 |
|
| 83 |
public PriorsPanel(BeautiFrame parent, boolean isDefaultOnly) { |
| 84 |
this.frame = parent; |
| 85 |
this.isDefaultOnly = isDefaultOnly; |
| 86 |
|
| 87 |
priorTableModel = new PriorTableModel(this); |
| 88 |
priorTable = new JTable(priorTableModel); |
| 89 |
|
| 90 |
priorTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); |
| 91 |
priorTable.getTableHeader().setReorderingAllowed(false); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
priorTable.getColumnModel().getColumn(0).setCellRenderer( |
| 96 |
new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4))); |
| 97 |
priorTable.getColumnModel().getColumn(0).setMinWidth(100); |
| 98 |
priorTable.getColumnModel().getColumn(0).setPreferredWidth(300); |
| 99 |
|
| 100 |
priorTable.getColumnModel().getColumn(1).setCellRenderer( |
| 101 |
new ButtonRenderer(SwingConstants.LEFT, new Insets(0, 8, 0, 8))); |
| 102 |
priorTable.getColumnModel().getColumn(1).setCellEditor( |
| 103 |
new ButtonEditor(SwingConstants.LEFT, new Insets(0, 8, 0, 8))); |
| 104 |
priorTable.getColumnModel().getColumn(1).setPreferredWidth(100); |
| 105 |
priorTable.getColumnModel().getColumn(1).setMinWidth(200); |
| 106 |
|
| 107 |
priorTable.getColumnModel().getColumn(2).setCellRenderer( |
| 108 |
new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4))); |
| 109 |
priorTable.getColumnModel().getColumn(2).setPreferredWidth(40); |
| 110 |
priorTable.getColumnModel().getColumn(2).setMinWidth(80); |
| 111 |
|
| 112 |
|
| 113 |
priorTable.getColumnModel().getColumn(3).setCellRenderer( |
| 114 |
new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4))); |
| 115 |
priorTable.getColumnModel().getColumn(3).setPreferredWidth(1000); |
| 116 |
|
| 117 |
TableEditorStopper.ensureEditingStopWhenTableLosesFocus(priorTable); |
| 118 |
|
| 119 |
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() { |
| 120 |
|
| 121 |
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, |
| 122 |
boolean hasFocus, |
| 123 |
int row, int column) |
| 124 |
{ |
| 125 |
|
| 126 |
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); |
| 127 |
|
| 128 |
Parameter parameter = parameters.get(row); |
| 129 |
|
| 130 |
if (isSelected) { |
| 131 |
setForeground(parameter.isLinked ? Color.LIGHT_GRAY : SystemColor.textHighlightText); |
| 132 |
} else { |
| 133 |
setForeground(parameter.isLinked ? Color.GRAY : SystemColor.controlText); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
return this; |
| 139 |
} |
| 140 |
}; |
| 141 |
priorTable.getColumnModel().getColumn(0).setCellRenderer(renderer); |
| 142 |
priorTable.getColumnModel().getColumn(2).setCellRenderer(renderer); |
| 143 |
priorTable.getColumnModel().getColumn(3).setCellRenderer(renderer); |
| 144 |
|
| 145 |
scrollPane = new JScrollPane(priorTable, |
| 146 |
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, |
| 147 |
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); |
| 148 |
|
| 149 |
scrollPane.setOpaque(false); |
| 150 |
|
| 151 |
Action setLinkedParametersAction = new AbstractAction("Link parameters together") { |
| 152 |
public void actionPerformed(ActionEvent actionEvent) { |
| 153 |
|
| 154 |
int[] rows = priorTable.getSelectedRows(); |
| 155 |
linkButtonPressed(rows); |
| 156 |
} |
| 157 |
}; |
| 158 |
|
| 159 |
linkButton = new JButton(setLinkedParametersAction); |
| 160 |
linkButton.setVisible(true); |
| 161 |
linkButton.setEnabled(false); |
| 162 |
linkButton.setToolTipText("<html>Join the selected parameters so their values are<br>" + |
| 163 |
"kept the same. This will create a new controlling<br>" + |
| 164 |
"parameter which can be given a prior.</html>"); |
| 165 |
|
| 166 |
Action setHierarchicalAction = new AbstractAction("Link parameters into a hierarchical model") { |
| 167 |
public void actionPerformed(ActionEvent actionEvent) { |
| 168 |
|
| 169 |
int[] rows = priorTable.getSelectedRows(); |
| 170 |
hierarchicalButtonPressed(rows); |
| 171 |
} |
| 172 |
}; |
| 173 |
|
| 174 |
hpmButton = new JButton(setHierarchicalAction); |
| 175 |
hpmButton.setVisible(true); |
| 176 |
hpmButton.setEnabled(false); |
| 177 |
hpmButton.setToolTipText(HierarchicalPhylogeneticModel.TIP_TOOL); |
| 178 |
|
| 179 |
Action setUnlinkAction = new AbstractAction("Unlink parameters") { |
| 180 |
public void actionPerformed(ActionEvent actionEvent) { |
| 181 |
|
| 182 |
int[] rows = priorTable.getSelectedRows(); |
| 183 |
unlinkButtonPressed(rows); |
| 184 |
} |
| 185 |
}; |
| 186 |
|
| 187 |
unlinkButton = new JButton(setUnlinkAction); |
| 188 |
unlinkButton.setVisible(true); |
| 189 |
unlinkButton.setEnabled(false); |
| 190 |
unlinkButton.setToolTipText("<html>Unlink parameters joined as a linked parameter<br>" + |
| 191 |
"or hierarchical model. Will delete the enclosing<br>" + |
| 192 |
"model.</html>"); |
| 193 |
|
| 194 |
messageLabel.setText(getMessage()); |
| 195 |
|
| 196 |
Action classicPriorsAction = new AbstractAction("Use classic priors/operators") { |
| 197 |
public void actionPerformed(ActionEvent actionEvent) { |
| 198 |
options.useClassicOperatorsAndPriors = classicPriorsCheck.isSelected(); |
| 199 |
priorTableModel.fireTableDataChanged(); |
| 200 |
frame.setAllOptions(); |
| 201 |
} |
| 202 |
}; |
| 203 |
|
| 204 |
classicPriorsCheck = new JCheckBox(classicPriorsAction); |
| 205 |
classicPriorsCheck.setVisible(true); |
| 206 |
classicPriorsCheck.setEnabled(true); |
| 207 |
classicPriorsCheck.setToolTipText("<html>Whether to use the older (v1.8 style) prior and operator combinations.</html>"); |
| 208 |
|
| 209 |
|
| 210 |
setOpaque(false); |
| 211 |
setLayout(new BorderLayout(0, 0)); |
| 212 |
setBorder(new BorderUIResource.EmptyBorderUIResource(new java.awt.Insets(12, 12, 12, 12))); |
| 213 |
|
| 214 |
if (!isDefaultOnly) { |
| 215 |
add(new JLabel("Priors for model parameters and statistics:"), BorderLayout.NORTH); |
| 216 |
} |
| 217 |
|
| 218 |
add(scrollPane, BorderLayout.CENTER); |
| 219 |
|
| 220 |
if (!isDefaultOnly) { |
| 221 |
JPanel southPanel = new JPanel(); |
| 222 |
southPanel.setLayout(new BorderLayout(0, 0)); |
| 223 |
JToolBar toolBar1 = new JToolBar(); |
| 224 |
toolBar1.setFloatable(false); |
| 225 |
toolBar1.setOpaque(false); |
| 226 |
toolBar1.setBorder(BorderFactory.createEmptyBorder()); |
| 227 |
toolBar1.setLayout(new BoxLayout(toolBar1, BoxLayout.X_AXIS)); |
| 228 |
|
| 229 |
PanelUtils.setupComponent(linkButton); |
| 230 |
toolBar1.add(linkButton); |
| 231 |
|
| 232 |
PanelUtils.setupComponent(hpmButton); |
| 233 |
toolBar1.add(hpmButton); |
| 234 |
|
| 235 |
toolBar1.addSeparator(); |
| 236 |
|
| 237 |
PanelUtils.setupComponent(unlinkButton); |
| 238 |
toolBar1.add(unlinkButton); |
| 239 |
|
| 240 |
southPanel.add(toolBar1, BorderLayout.NORTH); |
| 241 |
southPanel.add(messageLabel, BorderLayout.SOUTH); |
| 242 |
add(southPanel, BorderLayout.SOUTH); |
| 243 |
|
| 244 |
JToolBar toolBar2 = new JToolBar(); |
| 245 |
toolBar2.setFloatable(false); |
| 246 |
toolBar2.setOpaque(false); |
| 247 |
toolBar2.setBorder(BorderFactory.createEmptyBorder()); |
| 248 |
toolBar2.setLayout(new BoxLayout(toolBar2, BoxLayout.X_AXIS)); |
| 249 |
|
| 250 |
PanelUtils.setupComponent(classicPriorsCheck); |
| 251 |
toolBar2.add(classicPriorsCheck); |
| 252 |
|
| 253 |
add(toolBar2, BorderLayout.NORTH); |
| 254 |
} else { |
| 255 |
add(messageLabel, BorderLayout.SOUTH); |
| 256 |
} |
| 257 |
|
| 258 |
priorTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
| 259 |
public void valueChanged(ListSelectionEvent evt) { |
| 260 |
selectionChanged(); |
| 261 |
} |
| 262 |
}); |
| 263 |
|
| 264 |
} |
| 265 |
|
| 266 |
public void selectionChanged() { |
| 267 |
LinkedParameterComponentOptions comp = (LinkedParameterComponentOptions) |
| 268 |
options.getComponentOptions(LinkedParameterComponentOptions.class); |
| 269 |
|
| 270 |
int[] selRows = priorTable.getSelectedRows(); |
| 271 |
int jointPriorCount = 0; |
| 272 |
for (int i = 0; i < selRows.length; ++i) { |
| 273 |
Parameter parameter = parameters.get(selRows[i]); |
| 274 |
if (parameter.isLinked || comp.getLinkedParameterForArgument(parameter) != null) { |
| 275 |
jointPriorCount++; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
linkButton.setEnabled(selRows.length > 1); |
| 280 |
hpmButton.setEnabled(selRows.length > 1); |
| 281 |
unlinkButton.setEnabled(jointPriorCount > 0); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
public void setOptions(BeautiOptions options) { |
| 286 |
this.options = options; |
| 287 |
|
| 288 |
parameters = options.selectParameters(); |
| 289 |
|
| 290 |
messageLabel.setText(getMessage()); |
| 291 |
|
| 292 |
priorTableModel.fireTableDataChanged(); |
| 293 |
|
| 294 |
validate(); |
| 295 |
repaint(); |
| 296 |
} |
| 297 |
|
| 298 |
private String getMessage() { |
| 299 |
String message = "<html>"; |
| 300 |
if (isDefaultOnly) { |
| 301 |
message += "<ul><li>These priors listed above are still set to the default values " + |
| 302 |
"and need to be reviewed.</li>"; |
| 303 |
|
| 304 |
hasUndefinedPrior = false; |
| 305 |
hasImproperPrior = false; |
| 306 |
hasRate = false; |
| 307 |
|
| 308 |
for (Parameter param : parameters) { |
| 309 |
if (param.priorType == PriorType.UNDEFINED) { |
| 310 |
hasUndefinedPrior = true; |
| 311 |
} |
| 312 |
if (param.isPriorImproper()) { |
| 313 |
hasImproperPrior = true; |
| 314 |
} |
| 315 |
if (param.getBaseName().endsWith("clock.rate") || param.getBaseName().endsWith(ClockType.UCED_MEAN) |
| 316 |
|| param.getBaseName().endsWith(ClockType.UCLD_MEAN)) { |
| 317 |
hasRate = true; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
if (hasUndefinedPrior) { |
| 322 |
message += "<li><b><font color=\"#E42217\">These priors need to be defined by the user.</font></b></li>"; |
| 323 |
} |
| 324 |
if (hasImproperPrior) { |
| 325 |
message += "<li><b><font color=\"#B4B417\">Warning: one or more parameters have improper priors.</font></b></li>"; |
| 326 |
} |
| 327 |
if (hasRate) { |
| 328 |
message += "<li>" + |
| 329 |
"Priors on clock rates in particular should be proper such as a high variance lognormal or gamma distribution " + |
| 330 |
"with a mean appropriate for the organism and units of time employed.</li>"; |
| 331 |
} |
| 332 |
message += "</ul>"; |
| 333 |
|
| 334 |
} else { |
| 335 |
message += "* Marked parameters currently have a default prior distribution. " + |
| 336 |
"You should check that these are appropriate."; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
return message + "</html>"; |
| 341 |
} |
| 342 |
|
| 343 |
public void setParametersList(BeautiOptions options) { |
| 344 |
this.options = options; |
| 345 |
|
| 346 |
parameters.clear(); |
| 347 |
for (Parameter param : options.selectParameters()) { |
| 348 |
if (!param.isPriorEdited() || param.isPriorImproper()) { |
| 349 |
parameters.add(param); |
| 350 |
} |
| 351 |
} |
| 352 |
messageLabel.setText(getMessage()); |
| 353 |
|
| 354 |
if (continueButton != null) { |
| 355 |
continueButton.setEnabled(!hasUndefinedPrior); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
private PriorDialog priorDialog = null; |
| 360 |
private HierarchicalPriorDialog hierarchicalPriorDialog = null; |
| 361 |
private JointPriorDialog linkParameterDialog = null; |
| 362 |
|
| 363 |
private JButton continueButton = null; |
| 364 |
|
| 365 |
public void setContinueButton(JButton continueButton) { |
| 366 |
this.continueButton = continueButton; |
| 367 |
if (continueButton != null) { |
| 368 |
continueButton.setEnabled(!hasUndefinedPrior); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
private void linkButtonPressed(int[] rows) { |
| 373 |
Parameter firstParameter = parameters.get(rows[0]); |
| 374 |
|
| 375 |
|
| 376 |
List<Parameter> selectedParameters = new ArrayList<Parameter>(); |
| 377 |
boolean isLinked = false; |
| 378 |
for (int i = 0; i < rows.length; ++i) { |
| 379 |
|
| 380 |
Parameter parameter = parameters.get(rows[i]); |
| 381 |
if (parameter.isStatistic) { |
| 382 |
JOptionPane.showMessageDialog(frame, |
| 383 |
"Statistics cannot be linked together or\n" + |
| 384 |
"linked with parameter.", |
| 385 |
"Parameter linking error", |
| 386 |
JOptionPane.WARNING_MESSAGE); |
| 387 |
return; |
| 388 |
} |
| 389 |
if (parameter.truncationLower != firstParameter.truncationLower || |
| 390 |
parameter.truncationUpper != firstParameter.truncationUpper || |
| 391 |
options.getOperator(parameter).getOperatorType() != options.getOperator(firstParameter).getOperatorType()) { |
| 392 |
JOptionPane.showMessageDialog(frame, |
| 393 |
"Only parameters that share the same bounds\n" + |
| 394 |
"and have the same operator types can be linked.", |
| 395 |
"Parameter linking error", |
| 396 |
JOptionPane.WARNING_MESSAGE); |
| 397 |
return; |
| 398 |
} |
| 399 |
|
| 400 |
if (parameter.isLinked) { |
| 401 |
isLinked = true; |
| 402 |
} |
| 403 |
selectedParameters.add(parameters.get(rows[i])); |
| 404 |
} |
| 405 |
|
| 406 |
if (isLinked) { |
| 407 |
int option = JOptionPane.showConfirmDialog(this, |
| 408 |
"One or more selected parameters are already linked or\n" + |
| 409 |
"part of a hierarchical model.\n\n" + |
| 410 |
"Constructing a new prior will remove these parameter\n" + |
| 411 |
"from the original model. Continue?", |
| 412 |
"Link warning", |
| 413 |
JOptionPane.YES_NO_OPTION, |
| 414 |
JOptionPane.WARNING_MESSAGE); |
| 415 |
if (option == JOptionPane.NO_OPTION) { |
| 416 |
return; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
LinkedParameterComponentOptions comp = (LinkedParameterComponentOptions) |
| 421 |
options.getComponentOptions(LinkedParameterComponentOptions.class); |
| 422 |
|
| 423 |
LinkedParameter linkedParameter = comp.createLinkedParameter(null, selectedParameters); |
| 424 |
|
| 425 |
editLinkedParameter(linkedParameter, selectedParameters); |
| 426 |
} |
| 427 |
|
| 428 |
private void unlinkButtonPressed(int[] rows) { |
| 429 |
for (int i = 0; i < rows.length; ++i) { |
| 430 |
|
| 431 |
Parameter parameter = parameters.get(rows[i]); |
| 432 |
if (parameter.isLinked) { |
| 433 |
removeFromJointPrior(parameter); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
priorTableModel.fireTableDataChanged(); |
| 438 |
frame.setAllOptions(); |
| 439 |
} |
| 440 |
|
| 441 |
private void hierarchicalButtonPressed(int[] rows) { |
| 442 |
|
| 443 |
if (rows.length < 2) { |
| 444 |
JOptionPane.showMessageDialog(frame, |
| 445 |
"Fewer than two parameters selected.", |
| 446 |
"Parameter linking error", |
| 447 |
JOptionPane.WARNING_MESSAGE); |
| 448 |
return; |
| 449 |
} |
| 450 |
|
| 451 |
Double lowerBound = null; |
| 452 |
Double upperBound = null; |
| 453 |
|
| 454 |
List<Parameter> paramList = new ArrayList<Parameter>(); |
| 455 |
for (int i = 0; i < rows.length; ++i) { |
| 456 |
Parameter parameter = parameters.get(rows[i]); |
| 457 |
if (parameter.isStatistic) { |
| 458 |
JOptionPane.showMessageDialog(frame, |
| 459 |
"Statistics cannot be used in a hierarchical model.", |
| 460 |
"HPM parameter linking error", |
| 461 |
JOptionPane.WARNING_MESSAGE); |
| 462 |
return; |
| 463 |
} |
| 464 |
boolean sameBounds = true; |
| 465 |
if (lowerBound == null) { |
| 466 |
lowerBound = parameter.truncationLower; |
| 467 |
} else { |
| 468 |
if (lowerBound != parameter.truncationLower) { |
| 469 |
sameBounds = false; |
| 470 |
} |
| 471 |
} |
| 472 |
if (upperBound == null) { |
| 473 |
upperBound = parameter.truncationUpper; |
| 474 |
} else { |
| 475 |
if (upperBound != parameter.truncationUpper) { |
| 476 |
sameBounds = false; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
if (!sameBounds) { |
| 481 |
JOptionPane.showMessageDialog(frame, |
| 482 |
"Only parameters that share the same bounds\n" + |
| 483 |
"should be included in a HPM.", |
| 484 |
"HPM parameter link error", |
| 485 |
JOptionPane.WARNING_MESSAGE); |
| 486 |
return; |
| 487 |
} |
| 488 |
|
| 489 |
paramList.add(parameter); |
| 490 |
} |
| 491 |
|
| 492 |
if (hierarchicalPriorDialog != null) { |
| 493 |
|
| 494 |
HierarchicalModelComponentOptions comp = (HierarchicalModelComponentOptions) |
| 495 |
options.getComponentOptions(HierarchicalModelComponentOptions.class); |
| 496 |
boolean anyConflicts = false; |
| 497 |
for (Parameter parameter : paramList) { |
| 498 |
if (comp.isHierarchicalParameter(parameter)) { |
| 499 |
anyConflicts = true; |
| 500 |
break; |
| 501 |
} |
| 502 |
} |
| 503 |
if (anyConflicts) { |
| 504 |
int option = JOptionPane.showConfirmDialog(this, |
| 505 |
"At one selected parameter already exists in a HPM.\n" + |
| 506 |
"Constructing a new prior will remove these parameter\n" + |
| 507 |
"from the original model. Continue?", |
| 508 |
"HPM warning", |
| 509 |
JOptionPane.YES_NO_OPTION, |
| 510 |
JOptionPane.WARNING_MESSAGE); |
| 511 |
if (option == JOptionPane.NO_OPTION) { |
| 512 |
return; |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
if (hierarchicalPriorDialog == null) { |
| 518 |
hierarchicalPriorDialog = new HierarchicalPriorDialog(frame, options); |
| 519 |
} |
| 520 |
|
| 521 |
boolean done = false; |
| 522 |
|
| 523 |
while (!done) { |
| 524 |
int result = hierarchicalPriorDialog.showDialog(paramList); |
| 525 |
if (result == JOptionPane.OK_OPTION && hierarchicalPriorDialog.validateModelName()) { |
| 526 |
hierarchicalPriorDialog.getArguments(); |
| 527 |
done = true; |
| 528 |
} |
| 529 |
if (result == JOptionPane.CANCEL_OPTION) { |
| 530 |
return; |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
for (Parameter parameter : paramList) { |
| 536 |
HierarchicalModelComponentOptions comp = (HierarchicalModelComponentOptions) |
| 537 |
options.getComponentOptions(HierarchicalModelComponentOptions.class); |
| 538 |
if (comp.isHierarchicalParameter(parameter)) { |
| 539 |
comp.removeParameter(this, parameter, false); |
| 540 |
} |
| 541 |
|
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
hierarchicalPriorDialog.addHPM(paramList); |
| 546 |
|
| 547 |
for (Parameter parameter : paramList) { |
| 548 |
parameter.setPriorEdited(true); |
| 549 |
} |
| 550 |
priorTableModel.fireTableDataChanged(); |
| 551 |
} |
| 552 |
|
| 553 |
private void priorButtonPressed(int row) { |
| 554 |
Parameter parameter = parameters.get(row); |
| 555 |
|
| 556 |
LinkedParameterComponentOptions lpco = (LinkedParameterComponentOptions) |
| 557 |
options.getComponentOptions(LinkedParameterComponentOptions.class); |
| 558 |
|
| 559 |
HierarchicalModelComponentOptions hmco = (HierarchicalModelComponentOptions) |
| 560 |
options.getComponentOptions(HierarchicalModelComponentOptions.class); |
| 561 |
|
| 562 |
if (lpco.isArgumentParameter(parameter)) { |
| 563 |
LinkedParameter linkedParameter = lpco.getLinkedParameterForArgument(parameter); |
| 564 |
editLinkedParameter(linkedParameter, linkedParameter.getDependentParameterList()); |
| 565 |
} else { |
| 566 |
if (hmco.isHierarchicalParameter(parameter)) { |
| 567 |
int option = JOptionPane.showConfirmDialog(this, |
| 568 |
"This parameter forms part of an Hierarchical Model.\n" + |
| 569 |
"Selecting a new prior will remove the parameter from\n" + |
| 570 |
"this HPM.\nContinue?", |
| 571 |
"Hierarchical parameter warning", |
| 572 |
JOptionPane.YES_NO_OPTION, |
| 573 |
JOptionPane.WARNING_MESSAGE); |
| 574 |
if (option == JOptionPane.NO_OPTION) { |
| 575 |
return; |
| 576 |
} |
| 577 |
} |
| 578 |
if (parameter.isLinked) { |
| 579 |
int option = JOptionPane.showConfirmDialog(this, |
| 580 |
"Parameter linked to other parameter. Selecting a\n" + |
| 581 |
"new prior will remove the link.\nContinue?", |
| 582 |
"Linked parameter warning", |
| 583 |
JOptionPane.YES_NO_OPTION, |
| 584 |
JOptionPane.WARNING_MESSAGE); |
| 585 |
if (option == JOptionPane.NO_OPTION) { |
| 586 |
return; |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
editPrior(parameter); |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
private void editLinkedParameter(LinkedParameter linkedParameter, List<Parameter> selectedParameters) { |
| 595 |
Parameter sourceParameter = linkedParameter.getArgumentParameter(); |
| 596 |
Operator sourceOperator = linkedParameter.getArgumentOperator(); |
| 597 |
|
| 598 |
List<Parameter> compatibleParameters = new ArrayList<Parameter>(); |
| 599 |
for (Parameter parameter : parameters) { |
| 600 |
boolean isCompatible = true; |
| 601 |
if (parameter.isStatistic) { |
| 602 |
isCompatible = false; |
| 603 |
} |
| 604 |
if (parameter.truncationLower != sourceParameter.truncationLower) { |
| 605 |
isCompatible = false; |
| 606 |
} |
| 607 |
Operator operator = options.getOperator(parameter); |
| 608 |
if (operator == null || operator.getOperatorType() != sourceOperator.getOperatorType()) { |
| 609 |
isCompatible = false; |
| 610 |
} |
| 611 |
if (isCompatible) { |
| 612 |
compatibleParameters.add(parameter); |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
if (linkParameterDialog == null) { |
| 617 |
linkParameterDialog = new JointPriorDialog(frame, options); |
| 618 |
} |
| 619 |
|
| 620 |
linkParameterDialog.setLinkedParameter(linkedParameter); |
| 621 |
linkParameterDialog.setCompatibleParameterList(compatibleParameters); |
| 622 |
linkParameterDialog.setDependentParameterList(selectedParameters); |
| 623 |
|
| 624 |
boolean done = false; |
| 625 |
|
| 626 |
while (!done) { |
| 627 |
int result = linkParameterDialog.showDialog(); |
| 628 |
if (result == JOptionPane.OK_OPTION && linkParameterDialog.validateModelName()) { |
| 629 |
done = true; |
| 630 |
} |
| 631 |
if (result == JOptionPane.CANCEL_OPTION) { |
| 632 |
return; |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
linkedParameter.setName(linkParameterDialog.getName()); |
| 637 |
|
| 638 |
linkedParameter.linkDependentParameters(linkParameterDialog.getDependentParameterList()); |
| 639 |
|
| 640 |
linkParameterDialog.getArguments(linkedParameter.getArgumentParameter()); |
| 641 |
|
| 642 |
for (Parameter parameter : linkParameterDialog.getDependentParameterList()) { |
| 643 |
parameter.setPriorEdited(true); |
| 644 |
} |
| 645 |
|
| 646 |
priorTableModel.fireTableDataChanged(); |
| 647 |
frame.setAllOptions(); |
| 648 |
} |
| 649 |
|
| 650 |
private void editPrior(Parameter parameter) { |
| 651 |
if (priorDialog == null) { |
| 652 |
priorDialog = new PriorDialog(frame); |
| 653 |
} |
| 654 |
|
| 655 |
priorDialog.setParameter(parameter); |
| 656 |
|
| 657 |
int result; |
| 658 |
|
| 659 |
do { |
| 660 |
result = priorDialog.showDialog(); |
| 661 |
} while (result == JOptionPane.OK_OPTION && priorDialog.hasInvalidInput(true)); |
| 662 |
|
| 663 |
if (result == JOptionPane.OK_OPTION) { |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
removeFromJointPrior(parameter); |
| 668 |
|
| 669 |
|
| 670 |
priorDialog.getArguments(parameter); |
| 671 |
|
| 672 |
parameter.setPriorEdited(true); |
| 673 |
|
| 674 |
if (isDefaultOnly) { |
| 675 |
setParametersList(options); |
| 676 |
} |
| 677 |
|
| 678 |
if (parameter.getBaseName().endsWith("treeModel.rootHeight") || parameter.taxaId != null) { |
| 679 |
|
| 680 |
if (options.treeModelOptions.isNodeCalibrated(parameter)) { |
| 681 |
List<PartitionTreeModel> treeModels; |
| 682 |
treeModels = options.getPartitionTreeModels(options.getDataPartitions(parameter.getOptions())); |
| 683 |
|
| 684 |
for (PartitionTreeModel treeModel : treeModels) { |
| 685 |
treeModel.setNodeCalibrations(true); |
| 686 |
} |
| 687 |
frame.setAllOptions(); |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
priorTableModel.fireTableDataChanged(); |
| 692 |
frame.setAllOptions(); |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
private void removeFromJointPrior(Parameter parameter) { |
| 697 |
LinkedParameterComponentOptions comp = (LinkedParameterComponentOptions) |
| 698 |
options.getComponentOptions(LinkedParameterComponentOptions.class); |
| 699 |
|
| 700 |
LinkedParameter linkedParameter = comp.getLinkedParameter(parameter); |
| 701 |
if (linkedParameter != null) { |
| 702 |
comp.removeDependentParameter(parameter); |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
HierarchicalModelComponentOptions hmco = (HierarchicalModelComponentOptions) |
| 707 |
options.getComponentOptions(HierarchicalModelComponentOptions.class); |
| 708 |
if (hmco.isHierarchicalParameter(parameter)) { |
| 709 |
if (hmco.removeParameter(this, parameter, true) == JOptionPane.NO_OPTION) { |
| 710 |
|
| 711 |
System.err.println("Bailing out of modification"); |
| 712 |
return; |
| 713 |
} |
| 714 |
System.err.println("Parameter removed from an HPM"); |
| 715 |
} |
| 716 |
|
| 717 |
} |
| 718 |
|
| 719 |
public void getOptions(BeautiOptions options) { |
| 720 |
} |
| 721 |
|
| 722 |
public JComponent getExportableComponent() { |
| 723 |
return priorTable; |
| 724 |
} |
| 725 |
|
| 726 |
NumberFormatter formatter = new NumberFormatter(4); |
| 727 |
|
| 728 |
class DoubleRenderer extends TableRenderer { |
| 729 |
|
| 730 |
private static final long serialVersionUID = -2614341608257369805L; |
| 731 |
|
| 732 |
public DoubleRenderer(int alignment, Insets insets) { |
| 733 |
|
| 734 |
super(true, alignment, insets); |
| 735 |
} |
| 736 |
|
| 737 |
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, |
| 738 |
boolean hasFocus, int row, int column) { |
| 739 |
|
| 740 |
String s; |
| 741 |
if (((Double) value).isNaN()) { |
| 742 |
s = "random"; |
| 743 |
} else { |
| 744 |
s = formatter.format((Double) value); |
| 745 |
} |
| 746 |
return super.getTableCellRendererComponent(table, s, isSelected, hasFocus, row, column); |
| 747 |
|
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
public class ButtonRenderer extends JButton implements TableCellRenderer { |
| 752 |
protected Color undefinedColour = new Color(0xE4, 0x22, 0x17); |
| 753 |
protected Color improperColour = new Color(0xB4, 0xB4, 0x17); |
| 754 |
|
| 755 |
|
| 756 |
private static final long serialVersionUID = -2416184092883649169L; |
| 757 |
|
| 758 |
public ButtonRenderer(int alignment, Insets insets) { |
| 759 |
setOpaque(true); |
| 760 |
setHorizontalAlignment(alignment); |
| 761 |
setMargin(insets); |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
putClientProperty("JButton.buttonType", "tableHeader"); |
| 766 |
} |
| 767 |
|
| 768 |
public Component getTableCellRendererComponent(JTable table, Object value, |
| 769 |
boolean isSelected, boolean hasFocus, int row, int column) { |
| 770 |
setEnabled(table.isEnabled()); |
| 771 |
setFont(table.getFont()); |
| 772 |
if (isSelected) { |
| 773 |
|
| 774 |
setBackground(table.getSelectionBackground()); |
| 775 |
} else { |
| 776 |
|
| 777 |
setBackground(UIManager.getColor("Button.background")); |
| 778 |
} |
| 779 |
|
| 780 |
String text = (value == null) ? "" : value.toString(); |
| 781 |
if (text.toString().startsWith("?")) { |
| 782 |
setForeground(undefinedColour); |
| 783 |
} else if (text.toString().startsWith("!")) { |
| 784 |
setForeground(improperColour); |
| 785 |
} else { |
| 786 |
setForeground(UIManager.getColor("Button.foreground")); |
| 787 |
} |
| 788 |
|
| 789 |
setText(text); |
| 790 |
|
| 791 |
return this; |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
public class ButtonEditor extends DefaultCellEditor { |
| 796 |
|
| 797 |
private static final long serialVersionUID = 6372738480075411674L; |
| 798 |
protected JButton button; |
| 799 |
private String label; |
| 800 |
private boolean isPushed; |
| 801 |
private int row; |
| 802 |
|
| 803 |
public ButtonEditor(int alignment, Insets insets) { |
| 804 |
super(new JCheckBox()); |
| 805 |
button = new JButton(); |
| 806 |
button.setOpaque(true); |
| 807 |
button.setHorizontalAlignment(alignment); |
| 808 |
button.setMargin(insets); |
| 809 |
button.addActionListener(new ActionListener() { |
| 810 |
public void actionPerformed(ActionEvent e) { |
| 811 |
fireEditingStopped(); |
| 812 |
} |
| 813 |
}); |
| 814 |
|
| 815 |
|
| 816 |
button.putClientProperty("JButton.buttonType", "tableHeader"); |
| 817 |
} |
| 818 |
|
| 819 |
public Component getTableCellEditorComponent(JTable table, Object value, |
| 820 |
boolean isSelected, int row, int column) { |
| 821 |
button.setEnabled(table.isEnabled()); |
| 822 |
button.setFont(table.getFont()); |
| 823 |
if (isSelected) { |
| 824 |
|
| 825 |
button.setBackground(table.getSelectionBackground()); |
| 826 |
} else { |
| 827 |
|
| 828 |
button.setBackground(UIManager.getColor("Button.background")); |
| 829 |
} |
| 830 |
label = (value == null) ? "" : value.toString(); |
| 831 |
button.setText(label); |
| 832 |
isPushed = true; |
| 833 |
this.row = row; |
| 834 |
return button; |
| 835 |
} |
| 836 |
|
| 837 |
public Object getCellEditorValue() { |
| 838 |
if (isPushed) { |
| 839 |
priorButtonPressed(row); |
| 840 |
} |
| 841 |
isPushed = false; |
| 842 |
return label; |
| 843 |
} |
| 844 |
|
| 845 |
public boolean stopCellEditing() { |
| 846 |
isPushed = false; |
| 847 |
return super.stopCellEditing(); |
| 848 |
} |
| 849 |
|
| 850 |
protected void fireEditingStopped() { |
| 851 |
super.fireEditingStopped(); |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
|
| 856 |
} |