Debian Codegraph @

src/dr/app/beauti/clockmodelspanel/ClockModelsPanel.java

raw symbols SCIP
1 /*
2 * ClockModelsPanel.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.beauti.clockmodelspanel;
27
28 import dr.app.beauti.BeautiFrame;
29 import dr.app.beauti.BeautiPanel;
30 import dr.app.beauti.options.*;
31 import dr.app.beauti.util.PanelUtils;
32 import dr.app.gui.components.RealNumberField;
33 import dr.app.gui.table.TableEditorStopper;
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.ActionEvent;
47 import java.util.ArrayList;
48 import java.util.HashMap;
49 import java.util.Map;
50 import java.util.logging.Logger;
51
52 /**
53 * @author Andrew Rambaut
54 * @version $Id: ModelPanel.java,v 1.17 2006/09/05 13:29:34 rambaut Exp $
55 */
56 public class ClockModelsPanel extends BeautiPanel implements Exportable {
57
58 public final static boolean DEBUG = false;
59
60 private static final long serialVersionUID = 2778103564318492601L;
61
62 private static final int MINIMUM_TABLE_WIDTH = 140;
63
64 private JTable modelTable = null;
65 private ModelTableModel modelTableModel = null;
66 private BeautiOptions options = null;
67
68 JPanel modelPanelParent;
69 PartitionClockModel currentModel = null;
70 Map<PartitionClockModel, PartitionClockModelPanel> modelPanels = new HashMap<PartitionClockModel, PartitionClockModelPanel>();
71 TitledBorder modelBorder;
72
73 JCheckBox fixedMeanRateCheck = new JCheckBox("Fix mean rate of molecular clock model to: ");
74 RealNumberField meanRateField = new RealNumberField(Double.MIN_VALUE, Double.MAX_VALUE);
75
76 BeautiFrame frame = null;
77 // CreateModelDialog createModelDialog = null;
78 boolean settingOptions = false;
79
80 private CloneModelDialog cloneModelDialog = null;
81
82 CloneModelsAction cloneModelsAction = new CloneModelsAction();
83
84 public ClockModelsPanel(BeautiFrame parent) {
85
86 super();
87
88 this.frame = parent;
89
90 modelTableModel = new ModelTableModel();
91 modelTable = new JTable(modelTableModel);
92
93 modelTable.getTableHeader().setReorderingAllowed(false);
94 modelTable.getTableHeader().setResizingAllowed(false);
95 // modelTable.getTableHeader().setDefaultRenderer(
96 // new HeaderRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
97
98 final TableColumnModel model = modelTable.getColumnModel();
99 final TableColumn tableColumn0 = model.getColumn(0);
100 tableColumn0.setCellRenderer(new ModelsTableCellRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
101
102 TableEditorStopper.ensureEditingStopWhenTableLosesFocus(modelTable);
103
104 modelTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
105 modelTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
106 public void valueChanged(ListSelectionEvent evt) {
107 selectionChanged();
108 }
109 });
110
111 JScrollPane scrollPane = new JScrollPane(modelTable,
112 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
113 scrollPane.setOpaque(false);
114
115 // ActionPanel actionPanel1 = new ActionPanel(false);
116 // actionPanel1.setAddAction(addModelAction);
117 // actionPanel1.setRemoveAction(removeModelAction);
118
119 JPanel controlPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
120 controlPanel1.setOpaque(false);
121 // controlPanel1.add(actionPanel1);
122
123 JPanel panel = new JPanel(new BorderLayout(0, 0));
124 panel.setOpaque(false);
125 panel.add(scrollPane, BorderLayout.CENTER);
126 // panel.add(controlPanel1, BorderLayout.SOUTH);
127 panel.setMinimumSize(new Dimension(MINIMUM_TABLE_WIDTH, 0));
128
129 JToolBar toolBar = new JToolBar();
130 toolBar.setFloatable(false);
131 toolBar.setOpaque(false);
132 toolBar.setBorder(BorderFactory.createEmptyBorder());
133
134 toolBar.setLayout(new FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));
135 JButton button = new JButton(cloneModelsAction);
136 PanelUtils.setupComponent(button);
137 toolBar.add(button);
138 panel.add(toolBar, BorderLayout.SOUTH);
139
140 modelPanelParent = new JPanel(new FlowLayout(FlowLayout.CENTER));
141 modelPanelParent.setOpaque(false);
142 modelBorder = new TitledBorder(null, "Clock Model", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.ABOVE_TOP);
143 modelPanelParent.setBorder(modelBorder);
144
145 setCurrentModel(null);
146
147 JScrollPane scrollPane2 = new JScrollPane(modelPanelParent, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
148 scrollPane2.setOpaque(false);
149 scrollPane2.setBorder(null);
150 scrollPane2.getViewport().setOpaque(false);
151
152 JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel, scrollPane2);
153 splitPane.setDividerLocation(MINIMUM_TABLE_WIDTH);
154 splitPane.setContinuousLayout(true);
155 splitPane.setBorder(BorderFactory.createEmptyBorder());
156 splitPane.setOpaque(false);
157
158 setOpaque(false);
159 setBorder(new BorderUIResource.EmptyBorderUIResource(new Insets(12, 12, 12, 12)));
160 setLayout(new BorderLayout(0, 0));
161 add(splitPane, BorderLayout.CENTER);
162 }
163
164 private void resetPanel() {
165 if (!options.hasData()) {
166 currentModel = null;
167 modelPanels.clear();
168 modelPanelParent.removeAll();
169 modelBorder.setTitle("Clock Model");
170
171 return;
172 }
173 }
174
175 public void setOptions(BeautiOptions options) {
176
177 if (DEBUG) {
178 Logger.getLogger("dr.app.beauti").info("ModelsPanel.setOptions");
179 }
180
181 this.options = options;
182
183 resetPanel();
184
185 settingOptions = true;
186
187 int selRow = modelTable.getSelectedRow();
188 modelTableModel.fireTableDataChanged();
189 if (options.getPartitionSubstitutionModels().size() > 0) {
190 if (selRow < 0) {
191 selRow = 0;
192 }
193 modelTable.getSelectionModel().setSelectionInterval(selRow, selRow);
194 }
195
196 if (currentModel == null && options.getPartitionClockModels().size() > 0) {
197 modelTable.getSelectionModel().setSelectionInterval(0, 0);
198 }
199
200 settingOptions = false;
201
202 validate();
203 repaint();
204 }
205
206 public void getOptions(BeautiOptions options) {
207 if (settingOptions) return;
208
209 // options.clockModelOptions.setMeanRelativeRate(meanRateField.getValue());
210 }
211
212
213 private void fireModelsChanged() {
214 // options.updatePartitionAllLinks();
215 frame.setDirty();
216 }
217
218 private void selectionChanged() {
219 if (modelTable.getSelectedRowCount() == 1) {
220 int selRow = modelTable.getSelectedRow();
221
222 if (selRow >= options.getPartitionClockModels().size()) {
223 selRow = 0;
224 modelTable.getSelectionModel().setSelectionInterval(selRow, selRow);
225 }
226
227 if (selRow >= 0) {
228 setCurrentModel(options.getPartitionClockModels().get(selRow));
229 // frame.modelSelectionChanged(!isUsed(selRow));
230 }
231 } else {
232 setCurrentModels(getSelectedModels());
233 }
234 }
235
236 private java.util.List<PartitionClockModel> getSelectedModels() {
237 java.util.List<PartitionClockModel> models = new ArrayList<PartitionClockModel>();
238
239 for (int row : modelTable.getSelectedRows()) {
240 models.add(options.getPartitionClockModels().get(row));
241 }
242 if (models.size() == 0) {
243 models.addAll(options.getPartitionClockModels());
244 }
245
246 return models;
247 }
248
249 /**
250 * Sets the current model that this model panel is displaying
251 *
252 * @param model the new model to display
253 */
254 private void setCurrentModel(PartitionClockModel model) {
255 modelPanelParent.removeAll();
256
257 currentModel = model;
258
259 if (currentModel != null) {
260 PartitionClockModelPanel panel = setPanelSettings(model);
261 modelPanelParent.add(panel);
262
263 } else {
264
265 }
266
267 cloneModelsAction.setEnabled(true);
268
269 updateBorder();
270 }
271
272 private PartitionClockModelPanel setPanelSettings(PartitionClockModel model) {
273 PartitionClockModelPanel panel = modelPanels.get(currentModel);
274 if (panel == null) {
275 panel = new PartitionClockModelPanel(model);
276 modelPanels.put(model, panel);
277 }
278
279 panel.setOptions();
280 return panel;
281 }
282
283 private void setCurrentModels(java.util.List<PartitionClockModel> models) {
284 modelPanelParent.removeAll();
285
286 currentModel = null;
287
288 updateBorder();
289 cloneModelsAction.setEnabled(true);
290
291 repaint();
292 }
293
294 private void updateBorder() {
295 if (currentModel != null) {
296 modelBorder.setTitle("Clock Model - " + currentModel.getName());
297 } else {
298 modelBorder.setTitle("Multiple clock models selected");
299 }
300 repaint();
301 }
302
303 private boolean isUsed(int row) {
304 PartitionClockModel model = options.getPartitionClockModels().get(row);
305 for (AbstractPartitionData partition : options.dataPartitions) {
306 if (partition.getPartitionClockModel() == model) {
307 return true;
308 }
309 }
310 return false;
311 }
312
313
314 private void cloneModelSettings() {
315 if (cloneModelDialog == null) {
316 cloneModelDialog = new CloneModelDialog(frame);
317 }
318
319
320 java.util.List<PartitionClockModel> sourceModels = new ArrayList<PartitionClockModel>();
321
322 for (PartitionClockModel model : options.getPartitionClockModels()) {
323 sourceModels.add(model);
324 }
325
326 int result = cloneModelDialog.showDialog(sourceModels);
327
328 if (result == -1 || result == JOptionPane.CANCEL_OPTION) {
329 return;
330 }
331
332 PartitionClockModel sourceModel = cloneModelDialog.getSourceModel();
333 for (PartitionClockModel model : getSelectedModels()) {
334 if (!model.equals(sourceModel)) {
335 model.copyFrom(sourceModel);
336 }
337 }
338
339 selectionChanged();
340
341 }
342
343 public JComponent getExportableComponent() {
344 return this;
345 }
346
347 class ModelTableModel extends AbstractTableModel {
348
349 private static final long serialVersionUID = -6707994233020715574L;
350 String[] columnNames = {"Clock Model"};
351
352 public ModelTableModel() {
353 }
354
355 public int getColumnCount() {
356 return columnNames.length;
357 }
358
359 public int getRowCount() {
360 if (options == null) return 0;
361 return options.getPartitionClockModels().size();
362 }
363
364 public Object getValueAt(int row, int col) {
365 PartitionClockModel model = options.getPartitionClockModels().get(row);
366 switch (col) {
367 case 0:
368 return model.getName();
369 default:
370 throw new IllegalArgumentException("unknown column, " + col);
371 }
372 }
373
374 public boolean isCellEditable(int row, int col) {
375 return false;
376 }
377
378 public void setValueAt(Object value, int row, int col) {
379 String name = ((String) value).trim();
380 if (name.length() > 0) {
381 PartitionClockModel model = options.getPartitionClockModels().get(row);
382 model.setName(name); //TODO: update every same model in diff PD?
383 updateBorder();
384 fireModelsChanged();
385 }
386 }
387
388 public String getColumnName(int column) {
389 return columnNames[column];
390 }
391
392 public Class getColumnClass(int c) {
393 if (getRowCount() == 0) {
394 return Object.class;
395 }
396 return getValueAt(0, c).getClass();
397 }
398
399 public String toString() {
400 StringBuffer buffer = new StringBuffer();
401
402 buffer.append(getColumnName(0));
403 for (int j = 1; j < getColumnCount(); j++) {
404 buffer.append("\t");
405 buffer.append(getColumnName(j));
406 }
407 buffer.append("\n");
408
409 for (int i = 0; i < getRowCount(); i++) {
410 buffer.append(getValueAt(i, 0));
411 for (int j = 1; j < getColumnCount(); j++) {
412 buffer.append("\t");
413 buffer.append(getValueAt(i, j));
414 }
415 buffer.append("\n");
416 }
417
418 return buffer.toString();
419 }
420 }
421
422
423 class ModelsTableCellRenderer extends TableRenderer {
424
425 public ModelsTableCellRenderer(int alignment, Insets insets) {
426 super(alignment, insets);
427 }
428
429 public Component getTableCellRendererComponent(JTable aTable,
430 Object value,
431 boolean aIsSelected,
432 boolean aHasFocus,
433 int aRow, int aColumn) {
434
435 if (value == null) return this;
436
437 Component renderer = super.getTableCellRendererComponent(aTable,
438 value,
439 aIsSelected,
440 aHasFocus,
441 aRow, aColumn);
442
443 if (!isUsed(aRow))
444 renderer.setForeground(Color.gray);
445 else
446 renderer.setForeground(Color.black);
447 return this;
448 }
449
450 }
451
452 public class CloneModelsAction extends AbstractAction {
453 public CloneModelsAction() {
454 super("Clone Settings...");
455 setToolTipText("Use this tool to copy settings to selected models");
456 }
457
458 public void actionPerformed(ActionEvent ae) {
459 cloneModelSettings();
460 }
461 }
462
463
464 }