| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
package dr.app.beauti.priorspanel; |
| 33 |
|
| 34 |
import dr.app.beauti.components.hpm.HierarchicalModelComponentOptions; |
| 35 |
import dr.app.beauti.components.linkedparameters.LinkedParameter; |
| 36 |
import dr.app.beauti.options.BeautiOptions; |
| 37 |
import dr.app.beauti.options.Parameter; |
| 38 |
import dr.app.util.OSType; |
| 39 |
import jam.panels.ActionPanel; |
| 40 |
import jam.panels.OptionsPanel; |
| 41 |
|
| 42 |
import javax.swing.*; |
| 43 |
import javax.swing.border.EmptyBorder; |
| 44 |
import javax.swing.event.ListSelectionEvent; |
| 45 |
import javax.swing.event.ListSelectionListener; |
| 46 |
import javax.swing.plaf.BorderUIResource; |
| 47 |
import javax.swing.table.AbstractTableModel; |
| 48 |
import java.awt.*; |
| 49 |
import java.awt.event.ActionEvent; |
| 50 |
import java.util.*; |
| 51 |
import java.util.List; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public class JointPriorDialog implements AbstractPriorDialog { |
| 61 |
|
| 62 |
private static final int MINIMUM_TABLE_WIDTH = 120; |
| 63 |
private static final int MINIMUM_TABLE_HEIGHT = 160; |
| 64 |
private static final int PREFERRED_TABLE_WIDTH = 180; |
| 65 |
private static final int PREFERRED_TABLE_HEIGHT = 320; |
| 66 |
|
| 67 |
private JFrame frame; |
| 68 |
|
| 69 |
private final JTable parametersTable; |
| 70 |
private final ParametersTableModel parametersTableModel; |
| 71 |
|
| 72 |
private JTextField nameField = new JTextField(); |
| 73 |
private JPanel panel; |
| 74 |
|
| 75 |
private final PriorSettingsPanel priorSettingsPanel; |
| 76 |
|
| 77 |
private Parameter parameter; |
| 78 |
|
| 79 |
final private BeautiOptions options; |
| 80 |
|
| 81 |
private SelectParametersDialog selectParametersDialog = null; |
| 82 |
private List<Parameter> compatibleParameterList; |
| 83 |
private List<Parameter> dependentParameterList; |
| 84 |
|
| 85 |
public JointPriorDialog(JFrame frame, BeautiOptions options) { |
| 86 |
this.frame = frame; |
| 87 |
|
| 88 |
this.options = options; |
| 89 |
|
| 90 |
priorSettingsPanel = new PriorSettingsPanel(frame); |
| 91 |
|
| 92 |
nameField.setColumns(30); |
| 93 |
|
| 94 |
parametersTableModel = new ParametersTableModel(); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
parametersTable = new JTable(parametersTableModel); |
| 99 |
|
| 100 |
parametersTable.getTableHeader().setReorderingAllowed(false); |
| 101 |
parametersTable.getTableHeader().setResizingAllowed(false); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
parametersTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); |
| 106 |
parametersTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
| 107 |
public void valueChanged(ListSelectionEvent evt) { |
| 108 |
parametersSelectionChanged(); |
| 109 |
} |
| 110 |
}); |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
private void parametersSelectionChanged() { |
| 115 |
int selRow = parametersTable.getSelectedRow(); |
| 116 |
if (selRow >= 0) { |
| 117 |
removeParameterAction.setEnabled(true); |
| 118 |
} |
| 119 |
|
| 120 |
if (dependentParameterList.size() <= 0) { |
| 121 |
removeParameterAction.setEnabled(false); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
public boolean validateModelName() { |
| 126 |
return validateModelName(nameField.getText()); |
| 127 |
} |
| 128 |
|
| 129 |
private boolean validateModelName(String name) { |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
if (name.equals(parameter.getName())) { |
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
if (name.trim().length() == 0) { |
| 138 |
Toolkit.getDefaultToolkit().beep(); |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
if (parameterExists(name)) { |
| 144 |
JOptionPane.showMessageDialog(frame, |
| 145 |
"A parameter with this name already exists.", |
| 146 |
"Linked parameter error", |
| 147 |
JOptionPane.WARNING_MESSAGE); |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
if (modelExists(name)) { |
| 153 |
JOptionPane.showMessageDialog(frame, |
| 154 |
"A model with this name already exists.", |
| 155 |
"Linked parameter error", |
| 156 |
JOptionPane.WARNING_MESSAGE); |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
private boolean parameterExists(String name) { |
| 164 |
for (Parameter parameter : options.selectParameters()) { |
| 165 |
if (parameter.getName().equals(name)) { |
| 166 |
return true; |
| 167 |
} |
| 168 |
} |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
private boolean modelExists(String modelName) { |
| 173 |
HierarchicalModelComponentOptions comp = (HierarchicalModelComponentOptions) |
| 174 |
options.getComponentOptions(HierarchicalModelComponentOptions.class); |
| 175 |
return comp.modelExists(modelName); |
| 176 |
} |
| 177 |
|
| 178 |
public int showDialog() { |
| 179 |
panel = new JPanel(new GridBagLayout()); |
| 180 |
|
| 181 |
double lower = Double.NEGATIVE_INFINITY; |
| 182 |
double upper = Double.POSITIVE_INFINITY; |
| 183 |
|
| 184 |
if (parameter.isZeroOne) { |
| 185 |
lower = 0.0; |
| 186 |
upper = 1.0; |
| 187 |
} else if (parameter.isNonNegative) { |
| 188 |
lower = 0.0; |
| 189 |
} |
| 190 |
|
| 191 |
panel = new JPanel(new GridBagLayout()); |
| 192 |
|
| 193 |
setupComponents(); |
| 194 |
|
| 195 |
JOptionPane optionPane = new JOptionPane(panel, |
| 196 |
JOptionPane.PLAIN_MESSAGE, |
| 197 |
JOptionPane.OK_CANCEL_OPTION, |
| 198 |
null, |
| 199 |
null, |
| 200 |
null); |
| 201 |
optionPane.setBorder(new EmptyBorder(12, 12, 12, 12)); |
| 202 |
|
| 203 |
final JDialog dialog = optionPane.createDialog(frame, "Linked Parameter Setup"); |
| 204 |
|
| 205 |
priorSettingsPanel.setDialog(dialog); |
| 206 |
priorSettingsPanel.setParameter(parameter); |
| 207 |
|
| 208 |
if (OSType.isMac()) { |
| 209 |
dialog.setMinimumSize(new Dimension(dialog.getBounds().width, 300)); |
| 210 |
} else { |
| 211 |
Toolkit tk = Toolkit.getDefaultToolkit(); |
| 212 |
Dimension d = tk.getScreenSize(); |
| 213 |
if (d.height < 700 && panel.getHeight() > 450) { |
| 214 |
dialog.setSize(new Dimension(panel.getWidth() + 100, 550)); |
| 215 |
} else { |
| 216 |
|
| 217 |
dialog.setSize(new Dimension(panel.getWidth() + 100, panel.getHeight() + 100)); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
dialog.pack(); |
| 225 |
dialog.setResizable(true); |
| 226 |
dialog.setVisible(true); |
| 227 |
|
| 228 |
int result = JOptionPane.CANCEL_OPTION; |
| 229 |
Integer value = (Integer) optionPane.getValue(); |
| 230 |
if (value != null && value != -1) { |
| 231 |
result = value; |
| 232 |
} |
| 233 |
|
| 234 |
return result; |
| 235 |
} |
| 236 |
|
| 237 |
public String getName() { |
| 238 |
return nameField.getText(); |
| 239 |
} |
| 240 |
|
| 241 |
public List<Parameter> getDependentParameterList() { |
| 242 |
return dependentParameterList; |
| 243 |
} |
| 244 |
|
| 245 |
private void setupComponents() { |
| 246 |
panel.removeAll(); |
| 247 |
|
| 248 |
JScrollPane scrollPane1 = new JScrollPane(parametersTable, |
| 249 |
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, |
| 250 |
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); |
| 251 |
scrollPane1.setOpaque(false); |
| 252 |
|
| 253 |
ActionPanel actionPanel1 = new ActionPanel(false); |
| 254 |
actionPanel1.setAddAction(addParameterAction); |
| 255 |
actionPanel1.setRemoveAction(removeParameterAction); |
| 256 |
actionPanel1.setAddToolTipText("Use this button to add an existing parameter to the prior"); |
| 257 |
actionPanel1.setRemoveToolTipText("Use this button to remove a parameter from the prior"); |
| 258 |
|
| 259 |
removeParameterAction.setEnabled(false); |
| 260 |
|
| 261 |
JPanel controlPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT)); |
| 262 |
controlPanel1.setOpaque(false); |
| 263 |
controlPanel1.add(actionPanel1); |
| 264 |
|
| 265 |
JPanel panel1 = new JPanel(new BorderLayout(0, 0)); |
| 266 |
panel1.setOpaque(false); |
| 267 |
panel1.add(new JLabel("Linked parameters:"), BorderLayout.NORTH); |
| 268 |
panel1.add(scrollPane1, BorderLayout.CENTER); |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
panel1.setSize(new Dimension(PREFERRED_TABLE_WIDTH, PREFERRED_TABLE_HEIGHT)); |
| 274 |
panel1.setPreferredSize(new Dimension(PREFERRED_TABLE_WIDTH, PREFERRED_TABLE_HEIGHT)); |
| 275 |
panel1.setMinimumSize(new Dimension(MINIMUM_TABLE_WIDTH, MINIMUM_TABLE_HEIGHT)); |
| 276 |
|
| 277 |
OptionsPanel optionsPanel = new OptionsPanel(0,6); |
| 278 |
if (parameter.getName() != null) { |
| 279 |
nameField.setText(parameter.getName()); |
| 280 |
} else { |
| 281 |
nameField.setText("Untitled"); |
| 282 |
} |
| 283 |
optionsPanel.addComponentWithLabel("Unique Name: ", nameField); |
| 284 |
|
| 285 |
|
| 286 |
panel.setOpaque(false); |
| 287 |
panel.setBorder(new BorderUIResource.EmptyBorderUIResource(new Insets(12, 12, 12, 12))); |
| 288 |
|
| 289 |
GridBagConstraints c = new GridBagConstraints(); |
| 290 |
c.gridx = 0; |
| 291 |
c.gridy = 0; |
| 292 |
c.weightx = 0; |
| 293 |
c.weighty = 0; |
| 294 |
c.anchor = GridBagConstraints.PAGE_START; |
| 295 |
c.fill = GridBagConstraints.HORIZONTAL; |
| 296 |
c.gridwidth = 2; |
| 297 |
panel.add(optionsPanel, c); |
| 298 |
|
| 299 |
c.gridx = 0; |
| 300 |
c.gridy = 1; |
| 301 |
c.anchor = GridBagConstraints.PAGE_START; |
| 302 |
c.fill = GridBagConstraints.VERTICAL; |
| 303 |
c.gridwidth = 1; |
| 304 |
panel.add(panel1, c); |
| 305 |
|
| 306 |
c.gridx = 1; |
| 307 |
c.gridy = 1; |
| 308 |
c.weightx = 1; |
| 309 |
c.weighty = 1; |
| 310 |
c.fill = GridBagConstraints.BOTH; |
| 311 |
c.anchor = GridBagConstraints.PAGE_START; |
| 312 |
c.gridwidth = GridBagConstraints.REMAINDER; |
| 313 |
panel.add(priorSettingsPanel, c); |
| 314 |
|
| 315 |
} |
| 316 |
|
| 317 |
public void getArguments(Parameter parameter) { |
| 318 |
priorSettingsPanel.getArguments(parameter); |
| 319 |
} |
| 320 |
|
| 321 |
public boolean hasInvalidInput(boolean showError) { |
| 322 |
return priorSettingsPanel.hasInvalidInput(showError); |
| 323 |
} |
| 324 |
|
| 325 |
public boolean addParameter() { |
| 326 |
if (selectParametersDialog == null) { |
| 327 |
selectParametersDialog = new SelectParametersDialog(frame); |
| 328 |
} |
| 329 |
List<Parameter> availableParameters = new ArrayList<Parameter>(compatibleParameterList); |
| 330 |
availableParameters.removeAll(dependentParameterList); |
| 331 |
int result = selectParametersDialog.showDialog("Select parameter to add to this Linked Parameter", availableParameters); |
| 332 |
if (result == JOptionPane.OK_OPTION) { |
| 333 |
Parameter parameter = selectParametersDialog.getSelectedParameter(); |
| 334 |
dependentParameterList.add(parameter); |
| 335 |
parametersTableModel.fireTableDataChanged(); |
| 336 |
} else if (result == JOptionPane.CANCEL_OPTION) { |
| 337 |
return false; |
| 338 |
} |
| 339 |
|
| 340 |
return true; |
| 341 |
} |
| 342 |
|
| 343 |
private void removeSelectedParameters() { |
| 344 |
int[] selRows = parametersTable.getSelectedRows(); |
| 345 |
List<Parameter> parametersToRemove = new ArrayList<Parameter>(); |
| 346 |
for (int row : selRows) { |
| 347 |
parametersToRemove.add((Parameter)parametersTable.getValueAt(row, 0)); |
| 348 |
} |
| 349 |
removeParameters(parametersToRemove); |
| 350 |
} |
| 351 |
|
| 352 |
private void removeParameters(List<Parameter> parametersToRemove) { |
| 353 |
for (Parameter parameter : parametersToRemove) { |
| 354 |
dependentParameterList.remove(parameter); |
| 355 |
} |
| 356 |
parametersTableModel.fireTableDataChanged(); |
| 357 |
} |
| 358 |
|
| 359 |
private AddParameterAction addParameterAction = new AddParameterAction(); |
| 360 |
|
| 361 |
public void setLinkedParameter(LinkedParameter linkedParameter) { |
| 362 |
parameter = linkedParameter.getArgumentParameter(); |
| 363 |
} |
| 364 |
|
| 365 |
public void setDependentParameterList(List<Parameter> dependentParameterList) { |
| 366 |
this.dependentParameterList = dependentParameterList; |
| 367 |
} |
| 368 |
|
| 369 |
public void setCompatibleParameterList(List<Parameter> compatibleParameterList) { |
| 370 |
this.compatibleParameterList = compatibleParameterList; |
| 371 |
} |
| 372 |
|
| 373 |
public class AddParameterAction extends AbstractAction { |
| 374 |
|
| 375 |
public AddParameterAction() { |
| 376 |
super("Add parameter"); |
| 377 |
} |
| 378 |
|
| 379 |
public void actionPerformed(ActionEvent ae) { |
| 380 |
addParameter(); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
AbstractAction removeParameterAction = new AbstractAction() { |
| 385 |
public void actionPerformed(ActionEvent ae) { |
| 386 |
removeSelectedParameters(); |
| 387 |
} |
| 388 |
}; |
| 389 |
|
| 390 |
|
| 391 |
class ParametersTableModel extends AbstractTableModel { |
| 392 |
|
| 393 |
private static final long serialVersionUID = -6707994233020715574L; |
| 394 |
String[] columnNames = {"Parameter"}; |
| 395 |
|
| 396 |
public ParametersTableModel() { |
| 397 |
} |
| 398 |
|
| 399 |
public int getColumnCount() { |
| 400 |
return columnNames.length; |
| 401 |
} |
| 402 |
|
| 403 |
public int getRowCount() { |
| 404 |
if (dependentParameterList == null) { |
| 405 |
return 0; |
| 406 |
} |
| 407 |
return dependentParameterList.size(); |
| 408 |
} |
| 409 |
|
| 410 |
public Object getValueAt(int row, int col) { |
| 411 |
switch (col) { |
| 412 |
case 0: |
| 413 |
return dependentParameterList.get(row); |
| 414 |
} |
| 415 |
return null; |
| 416 |
} |
| 417 |
|
| 418 |
public String getColumnName(int column) { |
| 419 |
return columnNames[column]; |
| 420 |
} |
| 421 |
|
| 422 |
public Class getColumnClass(int c) { |
| 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 |
|
| 450 |
} |