Debian Codegraph @

src/dr/app/beauti/sitemodelspanel/SiteModelsPanel.java

raw symbols SCIP
1 /*
2 * SiteModelsPanel.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.sitemodelspanel;
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.beauti.options.PartitionSubstitutionModel;
33 import dr.app.beauti.util.PanelUtils;
34 import dr.app.gui.table.TableEditorStopper;
35 import dr.evolution.datatype.DataType;
36 import jam.framework.Exportable;
37 import jam.table.TableRenderer;
38
39 import javax.swing.*;
40 import javax.swing.border.TitledBorder;
41 import javax.swing.event.ListSelectionEvent;
42 import javax.swing.event.ListSelectionListener;
43 import javax.swing.plaf.BorderUIResource;
44 import javax.swing.table.AbstractTableModel;
45 import javax.swing.table.TableColumn;
46 import javax.swing.table.TableColumnModel;
47 import java.awt.*;
48 import java.awt.event.ActionEvent;
49 import java.util.*;
50 import java.util.List;
51 import java.util.logging.Logger;
52
53 /**
54 * @author Andrew Rambaut
55 * @author Alexei Drummond
56 * @version $Id: ModelPanel.java,v 1.17 2006/09/05 13:29:34 rambaut Exp $
57 */
58 public class SiteModelsPanel extends BeautiPanel implements Exportable {
59
60 public final static boolean DEBUG = false;
61
62 private static final long serialVersionUID = 2778103564318492601L;
63
64 private static final int MINIMUM_TABLE_WIDTH = 140;
65
66 private JTable modelTable = null;
67 private ModelTableModel modelTableModel = null;
68 private BeautiOptions options = null;
69
70 private JPanel modelPanelParent;
71 private PartitionSubstitutionModel currentModel = null;
72 private Map<PartitionSubstitutionModel, PartitionModelPanel> modelPanels = new HashMap<PartitionSubstitutionModel, PartitionModelPanel>();
73 private TitledBorder modelBorder;
74 private CloneModelDialog cloneModelDialog = null;
75
76 private final BeautiFrame frame;
77 // CreateModelDialog createModelDialog = null;
78 boolean settingOptions = false;
79
80 CloneModelsAction cloneModelsAction = new CloneModelsAction();
81
82 public SiteModelsPanel(final BeautiFrame frame, Action removeModelAction) {
83
84 super();
85
86 this.frame = frame;
87
88 modelTableModel = new ModelTableModel();
89 modelTable = new JTable(modelTableModel);
90
91 modelTable.getTableHeader().setReorderingAllowed(false);
92 modelTable.getTableHeader().setResizingAllowed(false);
93 // modelTable.getTableHeader().setDefaultRenderer(
94 // new HeaderRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
95
96 final TableColumnModel model = modelTable.getColumnModel();
97 final TableColumn tableColumn0 = model.getColumn(0);
98 tableColumn0.setCellRenderer(new ModelsTableCellRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
99
100 TableEditorStopper.ensureEditingStopWhenTableLosesFocus(modelTable);
101
102 modelTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
103 modelTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
104 public void valueChanged(ListSelectionEvent evt) {
105 selectionChanged();
106 }
107 });
108
109 JScrollPane scrollPane = new JScrollPane(modelTable,
110 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
111 scrollPane.setOpaque(false);
112
113 // ActionPanel actionPanel1 = new ActionPanel(false);
114 // actionPanel1.setAddAction(addModelAction);
115 // actionPanel1.setRemoveAction(removeModelAction);
116
117 JPanel controlPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
118 controlPanel1.setOpaque(false);
119 // controlPanel1.add(actionPanel1);
120
121 JPanel panel = new JPanel(new BorderLayout(0, 0));
122 panel.setOpaque(false);
123 panel.add(scrollPane, BorderLayout.CENTER);
124 // panel.add(controlPanel1, BorderLayout.SOUTH);
125 panel.setMinimumSize(new Dimension(MINIMUM_TABLE_WIDTH, 0));
126
127 JToolBar toolBar = new JToolBar();
128 toolBar.setFloatable(false);
129 toolBar.setOpaque(false);
130 toolBar.setBorder(BorderFactory.createEmptyBorder());
131
132 toolBar.setLayout(new FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));
133 JButton button = new JButton(cloneModelsAction);
134 PanelUtils.setupComponent(button);
135 toolBar.add(button);
136 panel.add(toolBar, BorderLayout.SOUTH);
137
138 modelPanelParent = new JPanel(new FlowLayout(FlowLayout.CENTER));
139 modelPanelParent.setOpaque(false);
140 modelBorder = new TitledBorder(null, "Substitution Model", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.ABOVE_TOP);
141 modelPanelParent.setBorder(modelBorder);
142
143 setCurrentModel(null);
144
145 JScrollPane scrollPane2 = new JScrollPane(modelPanelParent, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
146 scrollPane2.setOpaque(false);
147 scrollPane2.setBorder(null);
148 scrollPane2.getViewport().setOpaque(false);
149
150 JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel, scrollPane2);
151 splitPane.setDividerLocation(MINIMUM_TABLE_WIDTH);
152 splitPane.setContinuousLayout(true);
153 splitPane.setBorder(BorderFactory.createEmptyBorder());
154 splitPane.setOpaque(false);
155
156 setOpaque(false);
157 setBorder(new BorderUIResource.EmptyBorderUIResource(new Insets(12, 12, 12, 12)));
158 setLayout(new BorderLayout(0, 0));
159 add(splitPane, BorderLayout.CENTER);
160 }
161
162 private void resetPanel() {
163 if (!options.hasData()) {
164 currentModel = null;
165 modelPanels.clear();
166 modelPanelParent.removeAll();
167 modelBorder.setTitle("Substitution Model");
168
169 // if (currentDiscreteTraitOption != null) {
170 // this.remove(d_splitPane);
171 // currentDiscreteTraitOption = null;
172 // }
173 return;
174 }
175 }
176
177 public void setOptions(BeautiOptions options) {
178
179 if (DEBUG) {
180 Logger.getLogger("dr.app.beauti").info("ModelsPanel.setOptions");
181 }
182
183 this.options = options;
184
185 resetPanel();
186
187 settingOptions = true;
188
189 int selRow = modelTable.getSelectedRow();
190 modelTableModel.fireTableDataChanged();
191 if (options.getPartitionSubstitutionModels().size() > 0) {
192 if (selRow < 0) {
193 selRow = 0;
194 }
195 modelTable.getSelectionModel().setSelectionInterval(selRow, selRow);
196 }
197
198 if (currentModel == null && options.getPartitionSubstitutionModels().size() > 0) {
199 modelTable.getSelectionModel().setSelectionInterval(0, 0);
200 }
201
202 settingOptions = false;
203
204 validate();
205 repaint();
206 }
207
208 public void getOptions(BeautiOptions options) {
209 }
210
211 private void fireModelsChanged() {
212 // options.updatePartitionAllLinks();
213 frame.setDirty();
214 }
215
216 private void selectionChanged() {
217 if (modelTable.getSelectedRowCount() == 1) {
218 int selRow = modelTable.getSelectedRow();
219
220 if (selRow >= options.getPartitionSubstitutionModels().size()) {
221 selRow = 0;
222 modelTable.getSelectionModel().setSelectionInterval(selRow, selRow);
223 }
224
225 if (selRow >= 0) {
226 setCurrentModel(options.getPartitionSubstitutionModels().get(selRow));
227 // frame.modelSelectionChanged(!isUsed(selRow));
228 }
229 } else {
230 setCurrentModels(getSelectedModels());
231 }
232 }
233
234 private List<PartitionSubstitutionModel> getSelectedModels() {
235 java.util.List<PartitionSubstitutionModel> models = new ArrayList<PartitionSubstitutionModel>();
236
237 for (int row : modelTable.getSelectedRows()) {
238 models.add(options.getPartitionSubstitutionModels().get(row));
239 }
240 if (models.size() == 0) {
241 models.addAll(options.getPartitionSubstitutionModels());
242 }
243
244 return models;
245 }
246
247 /**
248 * Sets the current model that this model panel is displaying
249 *
250 * @param model the new model to display
251 */
252 private void setCurrentModel(PartitionSubstitutionModel model) {
253 modelPanelParent.removeAll();
254
255 currentModel = model;
256
257 if (currentModel != null) {
258 PartitionModelPanel panel = modelPanels.get(currentModel);
259 if (panel == null) {
260 panel = new PartitionModelPanel(frame, currentModel);
261 modelPanels.put(currentModel, panel);
262 }
263
264 panel.setOptions();
265 modelPanelParent.add(panel);
266
267 } else {
268
269 }
270
271 cloneModelsAction.setEnabled(true);
272
273 updateBorder();
274 }
275
276 private void setCurrentModels(List<PartitionSubstitutionModel> models) {
277 modelPanelParent.removeAll();
278
279 currentModel = null;
280
281 Set<DataType> dataTypes = new HashSet<DataType>();
282 for (PartitionSubstitutionModel model : models) {
283 dataTypes.add(model.getDataType());
284 }
285
286 if (dataTypes.size() == 1) {
287 DataType dataType = dataTypes.iterator().next();
288 modelBorder.setTitle("Multiple " + dataType.getName() + " substitution models selected");
289 } else {
290 modelBorder.setTitle("Multiple mixed type substitution models selected");
291 }
292
293 cloneModelsAction.setEnabled(dataTypes.size() == 1);
294
295 repaint();
296 }
297
298 private void updateBorder() {
299
300 if (currentModel != null) {
301 modelBorder.setTitle(currentModel.getDataType().getName() + " Substitution Model - " + currentModel.getName());
302 } else {
303 modelBorder.setTitle("Multiple substitution models selected");
304 }
305 repaint();
306 }
307
308 private void cloneModelSettings() {
309 if (cloneModelDialog == null) {
310 cloneModelDialog = new CloneModelDialog(frame);
311 }
312
313 Set<DataType> dataTypes = new HashSet<DataType>();
314 for (PartitionSubstitutionModel model : getSelectedModels()) {
315 dataTypes.add(model.getDataType());
316 }
317
318 if (dataTypes.size() == 1) {
319 DataType dataType = dataTypes.iterator().next();
320
321 List<PartitionSubstitutionModel> sourceModels = new ArrayList<PartitionSubstitutionModel>();
322
323 for (PartitionSubstitutionModel model : options.getPartitionSubstitutionModels()) {
324 if (model.getDataType().equals(dataType)) {
325 sourceModels.add(model);
326 }
327 }
328
329 int result = cloneModelDialog.showDialog(sourceModels);
330
331 if (result == -1 || result == JOptionPane.CANCEL_OPTION) {
332 return;
333 }
334
335 PartitionSubstitutionModel sourceModel = cloneModelDialog.getSourceModel();
336 for (PartitionSubstitutionModel model : getSelectedModels()) {
337 if (!model.equals(sourceModel)) {
338 model.copyFrom(sourceModel);
339 }
340 }
341
342 repaint();
343
344 }
345 }
346
347 private boolean isUsed(int row) {
348 PartitionSubstitutionModel model = options.getPartitionSubstitutionModels().get(row);
349 for (AbstractPartitionData partition : options.dataPartitions) {
350 if (partition.getPartitionSubstitutionModel() == model) {
351 return true;
352 }
353 }
354 return false;
355 }
356
357 public JComponent getExportableComponent() {
358 return this;
359 }
360
361 class ModelTableModel extends AbstractTableModel {
362
363 /**
364 *
365 */
366 private static final long serialVersionUID = -6707994233020715574L;
367 String[] columnNames = {"Substitution Model"};
368
369 public ModelTableModel() {
370 }
371
372 public int getColumnCount() {
373 return columnNames.length;
374 }
375
376 public int getRowCount() {
377 if (options == null) return 0;
378 return options.getPartitionSubstitutionModels().size();
379 }
380
381 public Object getValueAt(int row, int col) {
382 PartitionSubstitutionModel model = options.getPartitionSubstitutionModels().get(row);
383 switch (col) {
384 case 0:
385 return model.getName();
386 default:
387 throw new IllegalArgumentException("unknown column, " + col);
388 }
389 }
390
391 public boolean isCellEditable(int row, int col) {
392 return false;
393 }
394
395 public void setValueAt(Object value, int row, int col) {
396 String name = ((String) value).trim();
397 if (name.length() > 0) {
398 PartitionSubstitutionModel model = options.getPartitionSubstitutionModels().get(row);
399 model.setName(name); //TODO: update every same model in diff PD?
400 updateBorder();
401 fireModelsChanged();
402 }
403 }
404
405 public String getColumnName(int column) {
406 return columnNames[column];
407 }
408
409 public Class getColumnClass(int c) {
410 if (getRowCount() == 0) {
411 return Object.class;
412 }
413 return getValueAt(0, c).getClass();
414 }
415
416 public String toString() {
417 StringBuffer buffer = new StringBuffer();
418
419 buffer.append(getColumnName(0));
420 for (int j = 1; j < getColumnCount(); j++) {
421 buffer.append("\t");
422 buffer.append(getColumnName(j));
423 }
424 buffer.append("\n");
425
426 for (int i = 0; i < getRowCount(); i++) {
427 buffer.append(getValueAt(i, 0));
428 for (int j = 1; j < getColumnCount(); j++) {
429 buffer.append("\t");
430 buffer.append(getValueAt(i, j));
431 }
432 buffer.append("\n");
433 }
434
435 return buffer.toString();
436 }
437 }
438
439 class ModelsTableCellRenderer extends TableRenderer {
440
441 public ModelsTableCellRenderer(int alignment, Insets insets) {
442 super(alignment, insets);
443 }
444
445 public Component getTableCellRendererComponent(JTable aTable,
446 Object value,
447 boolean aIsSelected,
448 boolean aHasFocus,
449 int aRow, int aColumn) {
450
451 if (value == null) return this;
452
453 Component renderer = super.getTableCellRendererComponent(aTable,
454 value,
455 aIsSelected,
456 aHasFocus,
457 aRow, aColumn);
458
459 if (!isUsed(aRow))
460 renderer.setForeground(Color.gray);
461 else
462 renderer.setForeground(Color.black);
463 return this;
464 }
465
466 }
467
468 public class CloneModelsAction extends AbstractAction {
469 public CloneModelsAction() {
470 super("Clone Settings...");
471 setToolTipText("Use this tool to copy settings to selected models");
472 }
473
474 public void actionPerformed(ActionEvent ae) {
475 cloneModelSettings();
476 }
477 }
478
479
480 }