Debian Codegraph @

src/dr/app/treestat/CharactersPanel.java

raw symbols SCIP
1 /*
2 * CharactersPanel.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.treestat;
27
28 import javax.swing.*;
29 import javax.swing.event.ListSelectionEvent;
30 import javax.swing.event.ListSelectionListener;
31 import javax.swing.table.AbstractTableModel;
32 import java.awt.*;
33 import java.awt.event.ActionEvent;
34 import java.util.ArrayList;
35
36 import dr.app.gui.table.TableSorter;
37 import jam.table.TableRenderer;
38 import jam.framework.Exportable;
39
40
41 public class CharactersPanel extends JPanel implements Exportable {
42
43 /**
44 *
45 */
46 private static final long serialVersionUID = 1063807543195481382L;
47 TreeStatFrame frame = null;
48 TreeStatData treeStatData = null;
49 TreeStatData.Character selectedCharacter = null;
50 TreeStatData.State selectedState = null;
51
52 JScrollPane scrollPane1 = new JScrollPane();
53 JTable charactersTable = null;
54 CharactersTableModel charactersTableModel = null;
55
56 JScrollPane scrollPane2 = new JScrollPane();
57 JTable statesTable = null;
58 StatesTableModel statesTableModel = null;
59
60 JScrollPane scrollPane3 = new JScrollPane();
61 JTable excludedTaxaTable = null;
62 TaxaTableModel excludedTaxaTableModel = null;
63
64 JScrollPane scrollPane4 = new JScrollPane();
65 JTable includedTaxaTable = null;
66 TaxaTableModel includedTaxaTableModel = null;
67
68
69 public CharactersPanel(TreeStatFrame frame, TreeStatData treeStatData) {
70
71 this.frame = frame;
72
73 setOpaque(false);
74
75 this.treeStatData = treeStatData;
76
77 Icon addIcon = null, removeIcon = null, includeIcon = null, excludeIcon = null;
78 try {
79 addIcon = new ImageIcon(dr.app.util.Utils.getImage(this, "images/add.png"));
80 removeIcon = new ImageIcon(dr.app.util.Utils.getImage(this, "images/minus.png"));
81 includeIcon = new ImageIcon(dr.app.util.Utils.getImage(this, "images/include.png"));
82 excludeIcon = new ImageIcon(dr.app.util.Utils.getImage(this, "images/exclude.png"));
83 } catch (Exception e) {
84 // do nothing
85 }
86
87 // Characters
88 charactersTableModel = new CharactersTableModel();
89 TableSorter sorter = new TableSorter(charactersTableModel);
90 charactersTable = new JTable(sorter);
91 sorter.addTableModelListener(charactersTable);
92
93 charactersTable.getColumnModel().getColumn(0).setCellRenderer(
94 new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
95
96 charactersTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
97 public void valueChanged(ListSelectionEvent evt) { charactersTableSelectionChanged(); }
98 });
99
100 scrollPane1 = new JScrollPane(charactersTable,
101 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
102 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
103
104 JPanel buttonPanel1 = createAddRemoveButtonPanel(addCharacterAction, addIcon, "Create a new character",
105 removeCharacterAction, removeIcon, "Remove a character",
106 javax.swing.BoxLayout.X_AXIS);
107
108 // States
109 statesTableModel = new StatesTableModel();
110 sorter = new TableSorter(statesTableModel);
111 statesTable = new JTable(sorter);
112 sorter.addTableModelListener(statesTable);
113
114 statesTable.getColumnModel().getColumn(0).setCellRenderer(
115 new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
116
117 statesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
118 public void valueChanged(ListSelectionEvent evt) { statesTableSelectionChanged(); }
119 });
120
121 scrollPane2 = new JScrollPane(statesTable,
122 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
123 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
124
125 JPanel buttonPanel2 = createAddRemoveButtonPanel(addStateAction, addIcon, "Create a new state",
126 removeStateAction, removeIcon, "Remove a state",
127 javax.swing.BoxLayout.X_AXIS);
128
129 // Excluded Taxon List
130 excludedTaxaTableModel = new TaxaTableModel(false);
131 sorter = new TableSorter(excludedTaxaTableModel);
132 excludedTaxaTable = new JTable(sorter);
133 sorter.addTableModelListener(excludedTaxaTable);
134
135 excludedTaxaTable.getColumnModel().getColumn(0).setCellRenderer(
136 new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
137
138 excludedTaxaTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
139 public void valueChanged(ListSelectionEvent evt) { excludedTaxaTableSelectionChanged(); }
140 });
141
142 scrollPane3 = new JScrollPane(excludedTaxaTable,
143 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
144 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
145
146 JPanel buttonPanel3 = createAddRemoveButtonPanel(includeTaxonAction, includeIcon, "Include selected taxa in the taxon set",
147 excludeTaxonAction, excludeIcon, "Exclude selected taxa from the taxon set",
148 javax.swing.BoxLayout.Y_AXIS);
149
150 // Included Taxon List
151 includedTaxaTableModel = new TaxaTableModel(true);
152 sorter = new TableSorter(includedTaxaTableModel);
153 includedTaxaTable = new JTable(sorter);
154 sorter.addTableModelListener(includedTaxaTable);
155
156 includedTaxaTable.getColumnModel().getColumn(0).setCellRenderer(
157 new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
158
159 includedTaxaTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
160 public void valueChanged(ListSelectionEvent evt) { includedTaxaTableSelectionChanged(); }
161 });
162
163 scrollPane4 = new JScrollPane(includedTaxaTable,
164 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
165 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
166
167
168 setLayout(new GridBagLayout());
169 GridBagConstraints c = new GridBagConstraints();
170
171 c.weightx = 0.333333;
172 c.weighty = 1;
173 c.fill = GridBagConstraints.BOTH;
174 c.anchor = GridBagConstraints.CENTER;
175 c.insets = new Insets(6,6,6,6);
176 c.gridx = 0;
177 c.gridy = 0;
178 c.gridwidth = 1;
179 c.gridheight = 3;
180 add(scrollPane1, c);
181
182 c.weightx = 0.333333;
183 c.weighty = 0;
184 c.fill = GridBagConstraints.NONE;
185 c.anchor = GridBagConstraints.LINE_START;
186 c.insets = new Insets(0,6,6,6);
187 c.gridx = 0;
188 c.gridy = 3;
189 c.gridwidth = 1;
190 c.gridheight = 1;
191 add(buttonPanel1, c);
192
193 c.weightx = 0.666664;
194 c.weighty = 0.333333;
195 c.fill = GridBagConstraints.BOTH;
196 c.anchor = GridBagConstraints.CENTER;
197 c.insets = new Insets(6,6,6,6);
198 c.gridx = 1;
199 c.gridy = 0;
200 c.gridwidth = 3;
201 c.gridheight = 1;
202 add(scrollPane2, c);
203
204 c.weightx = 0.666664;
205 c.weighty = 0;
206 c.fill = GridBagConstraints.NONE;
207 c.anchor = GridBagConstraints.LINE_START;
208 c.insets = new Insets(0,6,6,6);
209 c.gridx = 1;
210 c.gridy = 1;
211 c.gridwidth = 3;
212 c.gridheight = 1;
213 add(buttonPanel2, c);
214
215 c.weightx = 0.333333;
216 c.weighty = 0.666664;
217 c.fill = GridBagConstraints.BOTH;
218 c.anchor = GridBagConstraints.CENTER;
219 c.insets = new Insets(6,6,6,6);
220 c.gridx = 1;
221 c.gridy = 2;
222 c.gridwidth = 1;
223 c.gridheight = 1;
224 add(scrollPane3, c);
225
226 c.weightx = 0.0;
227 c.weighty = 0.666664;
228 c.fill = GridBagConstraints.NONE;
229 c.anchor = GridBagConstraints.CENTER;
230 c.insets = new Insets(0,0,0,0);
231 c.gridx = 2;
232 c.gridy = 2;
233 c.gridwidth = 1;
234 c.gridheight = 1;
235 add(buttonPanel3, c);
236
237 c.weightx = 0.333333;
238 c.weighty = 0.666664;
239 c.fill = GridBagConstraints.BOTH;
240 c.anchor = GridBagConstraints.CENTER;
241 c.insets = new Insets(6,6,6,6);
242 c.gridx = 3;
243 c.gridy = 2;
244 c.gridwidth = 1;
245 c.gridheight = 1;
246 add(scrollPane4, c);
247
248 setBorder(BorderFactory.createEmptyBorder(6,6,6,6));
249 }
250
251 JPanel createAddRemoveButtonPanel(Action addAction, Icon addIcon, String addToolTip,
252 Action removeAction, Icon removeIcon, String removeToolTip, int axis) {
253
254 JPanel buttonPanel = new JPanel();
255 buttonPanel.setLayout(new BoxLayout(buttonPanel, axis));
256 buttonPanel.setOpaque(false);
257 JButton addButton = new JButton(addAction);
258 if (addIcon != null) {
259 addButton.setIcon(addIcon);
260 addButton.setText(null);
261 }
262 addButton.setToolTipText(addToolTip);
263 addButton.putClientProperty("JButton.buttonType", "toolbar");
264 addButton.setOpaque(false);
265 addAction.setEnabled(false);
266
267 JButton removeButton = new JButton(removeAction);
268 if (removeIcon != null) {
269 removeButton.setIcon(removeIcon);
270 removeButton.setText(null);
271 }
272 removeButton.setToolTipText(removeToolTip);
273 removeButton.putClientProperty("JButton.buttonType", "toolbar");
274 removeButton.setOpaque(false);
275 removeAction.setEnabled(false);
276
277 buttonPanel.add(addButton);
278 buttonPanel.add(new JToolBar.Separator(new Dimension(6,6)));
279 buttonPanel.add(removeButton);
280
281 return buttonPanel;
282 }
283
284 public void dataChanged() {
285
286 addCharacterAction.setEnabled(treeStatData.allTaxa.size() > 0);
287
288 charactersTableModel.fireTableDataChanged();
289 statesTableModel.fireTableDataChanged();
290 includedTaxaTableModel.fireTableDataChanged();
291 excludedTaxaTableModel.fireTableDataChanged();
292 }
293
294 private void charactersTableSelectionChanged() {
295 if (charactersTable.getSelectedRowCount() == 0) {
296 selectedCharacter = null;
297 removeCharacterAction.setEnabled(false);
298 addStateAction.setEnabled(false);
299 } else {
300 selectedCharacter = treeStatData.characters.get(charactersTable.getSelectedRow());
301 removeCharacterAction.setEnabled(true);
302 addStateAction.setEnabled(true);
303 }
304 includedTaxaTableModel.fireTableDataChanged();
305 excludedTaxaTableModel.fireTableDataChanged();
306 }
307
308 private void statesTableSelectionChanged() {
309 if (statesTable.getSelectedRowCount() == 0) {
310 selectedState = null;
311 removeStateAction.setEnabled(false);
312 } else {
313 selectedCharacter = null;
314 removeStateAction.setEnabled(true);
315 }
316 includedTaxaTableModel.fireTableDataChanged();
317 excludedTaxaTableModel.fireTableDataChanged();
318 }
319
320 private void excludedTaxaTableSelectionChanged() {
321 if (excludedTaxaTable.getSelectedRowCount() == 0) {
322 includeTaxonAction.setEnabled(false);
323 } else {
324 includeTaxonAction.setEnabled(true);
325 }
326 }
327
328 private void includedTaxaTableSelectionChanged() {
329 if (includedTaxaTable.getSelectedRowCount() == 0) {
330 excludeTaxonAction.setEnabled(false);
331 } else {
332 excludeTaxonAction.setEnabled(true);
333 }
334 }
335
336 public JComponent getExportableComponent() {
337 return this;
338 }
339
340 Action addCharacterAction = new AbstractAction("Add") {
341
342 /**
343 *
344 */
345 private static final long serialVersionUID = -2222139644754040949L;
346
347 public void actionPerformed(ActionEvent ae) {
348 TreeStatData.Character character = new TreeStatData.Character();
349 character.name = "untitled";
350 character.states = new ArrayList<TreeStatData.State>();
351 treeStatData.characters.add(character);
352 dataChanged();
353
354 int sel = treeStatData.characters.size() - 1;
355 charactersTable.setRowSelectionInterval(sel, sel);
356 }
357 };
358
359 Action removeCharacterAction = new AbstractAction("Remove") {
360
361 /**
362 *
363 */
364 private static final long serialVersionUID = -6836455052115579291L;
365
366 public void actionPerformed(ActionEvent ae) {
367 int saved = charactersTable.getSelectedRow();
368 int row = charactersTable.getSelectedRow();
369 if (row != -1) {
370 treeStatData.characters.remove(row);
371 }
372 dataChanged();
373 if (saved >= treeStatData.characters.size()) saved = treeStatData.characters.size() - 1;
374 charactersTable.setRowSelectionInterval(saved, saved);
375 }
376 };
377
378 Action addStateAction = new AbstractAction("Add") {
379
380 /**
381 *
382 */
383 private static final long serialVersionUID = -2304872597649350237L;
384
385 public void actionPerformed(ActionEvent ae) {
386 }
387 };
388
389 Action removeStateAction = new AbstractAction("Remove") {
390
391 /**
392 *
393 */
394 private static final long serialVersionUID = -6390058458520173491L;
395
396 public void actionPerformed(ActionEvent ae) {
397 }
398 };
399
400 Action includeTaxonAction = new AbstractAction("->") {
401
402 /**
403 *
404 */
405 private static final long serialVersionUID = 4577920870740752531L;
406
407 public void actionPerformed(ActionEvent ae) {
408 int saved1 = charactersTable.getSelectedRow();
409 int saved2 = statesTable.getSelectedRow();
410 int[] rows = excludedTaxaTable.getSelectedRows();
411 ArrayList<String> exclList = new ArrayList<String>(treeStatData.allTaxa);
412 exclList.removeAll(selectedState.taxa);
413 for (int row : rows) {
414 selectedState.taxa.add(exclList.get(row));
415 }
416 dataChanged();
417 charactersTable.setRowSelectionInterval(saved1, saved1);
418 statesTable.setRowSelectionInterval(saved2, saved2);
419 }
420 };
421
422 Action excludeTaxonAction = new AbstractAction("<-") {
423
424 /**
425 *
426 */
427 private static final long serialVersionUID = 7911132810956409390L;
428
429 public void actionPerformed(ActionEvent ae) {
430 int saved1 = charactersTable.getSelectedRow();
431 int saved2 = statesTable.getSelectedRow();
432 int[] rows = includedTaxaTable.getSelectedRows();
433 for (int i = rows.length - 1; i >= 0 ; i--) {
434 selectedState.taxa.remove(rows[i]);
435 }
436 dataChanged();
437 charactersTable.setRowSelectionInterval(saved1, saved1);
438 statesTable.setRowSelectionInterval(saved2, saved2);
439 }
440 };
441
442 class CharactersTableModel extends AbstractTableModel {
443
444 /**
445 *
446 */
447 private static final long serialVersionUID = -3916166866378281436L;
448
449 public CharactersTableModel() {
450 }
451
452 public int getColumnCount() {
453 return 1;
454 }
455
456 public int getRowCount() {
457 return treeStatData.characters.size();
458 }
459
460 public Object getValueAt(int row, int col) {
461 return treeStatData.characters.get(row).name;
462 }
463
464 public void setValueAt(Object value, int row, int col) {
465 treeStatData.characters.get(row).name = (String)value;
466 }
467
468 public boolean isCellEditable(int row, int col) {
469 return true;
470 }
471
472 public String getColumnName(int column) {
473 return "Characters";
474 }
475
476 public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}
477 }
478
479 class StatesTableModel extends AbstractTableModel {
480
481 /**
482 *
483 */
484 private static final long serialVersionUID = -1912262346368463655L;
485 String[] columnNames = new String[] { "State", "Description" };
486
487 public StatesTableModel() {
488 }
489
490 public int getColumnCount() {
491 return 2;
492 }
493
494 public int getRowCount() {
495 if (selectedCharacter == null) return 0;
496 return selectedCharacter.states.size();
497 }
498
499 public Object getValueAt(int row, int col) {
500 final TreeStatData.State state = selectedCharacter.states.get(row);
501 if (col == 0) {
502 return state.name;
503 } else {
504 return state.description;
505 }
506 }
507
508 public void setValueAt(Object value, int row, int col) {
509 final TreeStatData.State state = selectedCharacter.states.get(row);
510 if (col == 0) {
511 state.name = (String)value;
512 } else {
513 state.description = (String)value;
514 }
515 }
516
517 public boolean isCellEditable(int row, int col) {
518 return true;
519 }
520
521 public String getColumnName(int col) {
522 return columnNames[col];
523 }
524
525 public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}
526 }
527
528 class TaxaTableModel extends AbstractTableModel {
529
530 /**
531 *
532 */
533 private static final long serialVersionUID = 2786966293840685962L;
534 boolean included;
535
536 public TaxaTableModel(boolean included) {
537 this.included = included;
538 }
539
540 public int getColumnCount() {
541 return 1;
542 }
543
544 public int getRowCount() {
545 if (selectedState == null || treeStatData.allTaxa == null) return 0;
546
547 if (included) {
548 return selectedState.taxa.size();
549 } else {
550 return treeStatData.allTaxa.size() - selectedState.taxa.size();
551 }
552 }
553
554 public Object getValueAt(int row, int col) {
555
556 if (included) {
557 return selectedState.taxa.get(row);
558 } else {
559 ArrayList<String> exclList = new ArrayList<String>(treeStatData.allTaxa);
560 exclList.removeAll(selectedState.taxa);
561 return exclList.get(row);
562 }
563 }
564
565 public boolean isCellEditable(int row, int col) {
566 return false;
567 }
568
569 public String getColumnName(int column) {
570 if (included) return "Included Taxa";
571 else return "Excluded Taxa";
572 }
573
574 public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}
575 }
576 }