Debian Codegraph @

src/dr/app/beauti/traitspanel/TraitsPanel.java

raw symbols SCIP
1 /*
2 * TraitsPanel.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.traitspanel;
27
28 import dr.app.beauti.BeautiFrame;
29 import dr.app.beauti.BeautiPanel;
30 import dr.app.beauti.ComboBoxRenderer;
31 import dr.app.beauti.datapanel.DataPanel;
32 import dr.app.beauti.options.BeautiOptions;
33 import dr.app.beauti.options.TraitData;
34 import dr.app.beauti.options.TraitGuesser;
35 import dr.app.beauti.util.PanelUtils;
36 import dr.app.gui.table.TableEditorStopper;
37 import dr.app.gui.table.TableSorter;
38 import dr.evolution.util.Taxa;
39 import dr.evolution.util.Taxon;
40 import jam.framework.Exportable;
41 import jam.panels.ActionPanel;
42 import jam.table.TableRenderer;
43
44 import javax.swing.*;
45 import javax.swing.event.ListSelectionEvent;
46 import javax.swing.event.ListSelectionListener;
47 import javax.swing.plaf.BorderUIResource;
48 import javax.swing.table.AbstractTableModel;
49 import javax.swing.table.TableColumn;
50 import java.awt.*;
51 import java.awt.event.ActionEvent;
52 import java.util.ArrayList;
53
54 /**
55 * @author Andrew Rambaut
56 * @version $Id: DataPanel.java,v 1.17 2006/09/05 13:29:34 rambaut Exp $
57 */
58 public class TraitsPanel extends BeautiPanel implements Exportable {
59
60 private static final long serialVersionUID = 5283922195494563924L;
61
62 private static final int MINIMUM_TABLE_WIDTH = 140;
63
64 private static final String ADD_TRAITS_TOOLTIP = "<html>Define a new trait for the current taxa</html>";
65 private static final String IMPORT_TRAITS_TOOLTIP = "<html>Import one or more traits for these taxa from a tab-delimited<br>" +
66 "file. Taxa should be in the first column and the trait names<br>" +
67 "in the first row</html>";
68 private static final String GUESS_TRAIT_VALUES_TOOLTIP = "<html>This attempts to extract values for this trait that are<br>" +
69 "encoded in the names of the selected taxa.</html>";
70 private static final String SET_TRAIT_VALUES_TOOLTIP = "<html>This sets values for this trait for all<br>" +
71 "the selected taxa.</html>";
72 private static final String CLEAR_TRAIT_VALUES_TOOLTIP = "<html>This clears all the values currently assigned to taxa for<br>" +
73 "this trait.</html>";
74 private static final String CREATE_TRAIT_PARTITIONS_TOOLTIP = "<html>Create a data partition for the selected traits.</html>";
75
76 public final JTable traitsTable;
77 private final TraitsTableModel traitsTableModel;
78
79 private final JTable dataTable;
80 private final DataTableModel dataTableModel;
81
82 private final BeautiFrame frame;
83 private final DataPanel dataPanel;
84
85 private BeautiOptions options = null;
86
87 private TraitData currentTrait = null; // current trait
88
89 private CreateTraitDialog createTraitDialog = null;
90 private GuessTraitDialog guessTraitDialog = null;
91 private TraitValueDialog traitValueDialog = null;
92
93 AddTraitAction addTraitAction = new AddTraitAction();
94 Action importTraitsAction;
95 CreateTraitPartitionAction createTraitPartitionAction = new CreateTraitPartitionAction();
96 GuessTraitsAction guessTraitsAction = new GuessTraitsAction();
97 SetValueAction setValueAction = new SetValueAction();
98
99 public TraitsPanel(BeautiFrame parent, DataPanel dataPanel, Action importTraitsAction) {
100
101 this.frame = parent;
102 this.dataPanel = dataPanel;
103
104 traitsTableModel = new TraitsTableModel();
105 // TableSorter sorter = new TableSorter(traitsTableModel);
106 // predictorsTable = new JTable(sorter);
107 // sorter.setTableHeader(predictorsTable.getTableHeader());
108 traitsTable = new JTable(traitsTableModel);
109
110 traitsTable.getTableHeader().setReorderingAllowed(false);
111 traitsTable.getTableHeader().setResizingAllowed(false);
112 // predictorsTable.getTableHeader().setDefaultRenderer(
113 // new HeaderRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
114
115 TableColumn col = traitsTable.getColumnModel().getColumn(1);
116 ComboBoxRenderer comboBoxRenderer = new ComboBoxRenderer(TraitData.TraitType.values());
117 comboBoxRenderer.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
118 col.setCellRenderer(comboBoxRenderer);
119
120 TableEditorStopper.ensureEditingStopWhenTableLosesFocus(traitsTable);
121
122 traitsTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
123 traitsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
124 public void valueChanged(ListSelectionEvent evt) {
125 traitSelectionChanged();
126 // dataTableModel.fireTableDataChanged();
127 }
128 });
129
130 JScrollPane scrollPane1 = new JScrollPane(traitsTable,
131 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
132 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
133 scrollPane1.setOpaque(false);
134
135 dataTableModel = new DataTableModel();
136 TableSorter sorter = new TableSorter(dataTableModel);
137 dataTable = new JTable(sorter);
138 sorter.setTableHeader(dataTable.getTableHeader());
139
140 dataTable.getTableHeader().setReorderingAllowed(false);
141 // dataTable.getTableHeader().setDefaultRenderer(
142 // new HeaderRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
143
144 dataTable.getColumnModel().getColumn(0).setCellRenderer(
145 new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
146 dataTable.getColumnModel().getColumn(0).setPreferredWidth(80);
147
148 col = dataTable.getColumnModel().getColumn(1);
149 comboBoxRenderer = new ComboBoxRenderer();
150 comboBoxRenderer.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
151 col.setCellRenderer(comboBoxRenderer);
152
153 TableEditorStopper.ensureEditingStopWhenTableLosesFocus(dataTable);
154
155 // dataTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
156 // public void valueChanged(ListSelectionEvent evt) {
157 // traitSelectionChanged();
158 // }
159 // });
160
161 JScrollPane scrollPane2 = new JScrollPane(dataTable,
162 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
163 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
164 scrollPane2.setOpaque(false);
165
166 JToolBar toolBar1 = new JToolBar();
167 toolBar1.setFloatable(false);
168 toolBar1.setOpaque(false);
169 toolBar1.setBorder(BorderFactory.createEmptyBorder());
170
171 toolBar1.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
172
173 JButton button;
174
175 button = new JButton(addTraitAction);
176 PanelUtils.setupComponent(button);
177 button.setToolTipText(ADD_TRAITS_TOOLTIP);
178 toolBar1.add(button);
179
180 this.importTraitsAction = importTraitsAction;
181 button = new JButton(importTraitsAction);
182 PanelUtils.setupComponent(button);
183 button.setToolTipText(IMPORT_TRAITS_TOOLTIP);
184 toolBar1.add(button);
185
186 toolBar1.add(new JToolBar.Separator(new Dimension(12, 12)));
187
188 button = new JButton(guessTraitsAction);
189 PanelUtils.setupComponent(button);
190 button.setToolTipText(GUESS_TRAIT_VALUES_TOOLTIP);
191 toolBar1.add(button);
192
193 button = new JButton(setValueAction);
194 PanelUtils.setupComponent(button);
195 button.setToolTipText(SET_TRAIT_VALUES_TOOLTIP);
196 toolBar1.add(button);
197
198 // Don't see the need for a clear values button
199 // button = new JButton(new ClearTraitAction());
200 // PanelUtils.setupComponent(button);
201 // button.setToolTipText(CLEAR_TRAIT_VALUES_TOOLTIP);
202 // toolBar1.add(button);
203 button = new JButton(createTraitPartitionAction);
204 PanelUtils.setupComponent(button);
205 button.setToolTipText(CREATE_TRAIT_PARTITIONS_TOOLTIP);
206 toolBar1.add(button);
207
208 ActionPanel actionPanel1 = new ActionPanel(false);
209 actionPanel1.setAddAction(addTraitAction);
210 actionPanel1.setRemoveAction(removeTraitAction);
211 actionPanel1.setAddToolTipText(ADD_TRAITS_TOOLTIP);
212
213 removeTraitAction.setEnabled(false);
214
215 JPanel controlPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
216 controlPanel1.setOpaque(false);
217 controlPanel1.add(actionPanel1);
218
219 JPanel panel1 = new JPanel(new BorderLayout(0, 0));
220 panel1.setOpaque(false);
221 panel1.add(scrollPane1, BorderLayout.CENTER);
222 panel1.add(controlPanel1, BorderLayout.SOUTH);
223 panel1.setMinimumSize(new Dimension(MINIMUM_TABLE_WIDTH, 0));
224
225 JPanel panel2 = new JPanel(new BorderLayout(0, 0));
226 panel2.setOpaque(false);
227 panel2.add(toolBar1, BorderLayout.NORTH);
228 panel2.add(scrollPane2, BorderLayout.CENTER);
229
230 JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
231 panel1, panel2);
232 splitPane.setDividerLocation(MINIMUM_TABLE_WIDTH);
233 splitPane.setContinuousLayout(true);
234 splitPane.setBorder(BorderFactory.createEmptyBorder());
235 splitPane.setOpaque(false);
236
237 setOpaque(false);
238 setBorder(new BorderUIResource.EmptyBorderUIResource(new Insets(12, 12, 12, 12)));
239 setLayout(new BorderLayout(0, 0));
240 add(splitPane, BorderLayout.CENTER);
241 add(toolBar1, BorderLayout.NORTH);
242
243 }
244
245 public void setOptions(BeautiOptions options) {
246 this.options = options;
247
248 updateButtons();
249
250 // int selRow = predictorsTable.getSelectedRow();
251 // traitsTableModel.fireTableDataChanged();
252 //
253 // if (selRow < 0) {
254 // selRow = 0;
255 // }
256 // predictorsTable.getSelectionModel().setSelectionInterval(selRow, selRow);
257
258
259 // if (selectedTrait == null) {
260 // predictorsTable.getSelectionModel().setSelectionInterval(0, 0);
261 // }
262
263 traitsTableModel.fireTableDataChanged();
264 dataTableModel.fireTableDataChanged();
265
266 validate();
267 repaint();
268 }
269
270 public void getOptions(BeautiOptions options) {
271 // int selRow = predictorsTable.getSelectedRow();
272 // if (selRow >= 0 && options.traitsOptions.selecetedTraits.size() > 0) {
273 // selectedTrait = options.traitsOptions.selecetedTraits.get(selRow);
274 // }
275 // options.datesUnits = unitsCombo.getSelectedIndex();
276 // options.datesDirection = directionCombo.getSelectedIndex();
277 }
278
279 public JComponent getExportableComponent() {
280 return dataTable;
281 }
282
283 public void fireTraitsChanged() {
284 if (currentTrait != null) {
285 // if (currentTrait.getName().equalsIgnoreCase(TraitData.Traits.TRAIT_SPECIES.toString())) {
286 // frame.setupStarBEAST();
287 // } else
288 if (currentTrait != null && currentTrait.getTraitType() == TraitData.TraitType.DISCRETE) {
289 frame.updateDiscreteTraitAnalysis();
290 }
291
292 // if (selRow > 0) {
293 // predictorsTable.getSelectionModel().setSelectionInterval(selRow-1, selRow-1);
294 // } else if (selRow == 0 && options.traitsOptions.traits.size() > 0) { // options.traitsOptions.traits.size() after remove
295 // predictorsTable.getSelectionModel().setSelectionInterval(0, 0);
296 // }
297
298 traitsTableModel.fireTableDataChanged();
299 // options.updatePartitionAllLinks();
300 frame.setDirty();
301 }
302 }
303
304 private void traitSelectionChanged() {
305 int selRow = traitsTable.getSelectedRow();
306 if (selRow >= 0) {
307 currentTrait = options.traits.get(selRow);
308 // predictorsTable.getSelectionModel().setSelectionInterval(selRow, selRow);
309 removeTraitAction.setEnabled(true);
310 // } else {
311 // currentTrait = null;
312 // removeTraitAction.setEnabled(false);
313 }
314
315 if (options.traits.size() <= 0) {
316 currentTrait = null;
317 removeTraitAction.setEnabled(false);
318 }
319 dataTableModel.fireTableDataChanged();
320 // traitsTableModel.fireTableDataChanged();
321 }
322
323 private void updateButtons() { //TODO: better to merge updateButtons() fireTraitsChanged() traitSelectionChanged() into one
324 boolean hasData = options.hasData();
325
326 addTraitAction.setEnabled(hasData);
327 importTraitsAction.setEnabled(hasData);
328 createTraitPartitionAction.setEnabled(hasData && options.traits.size() > 0);
329 guessTraitsAction.setEnabled(hasData && options.traits.size() > 0);
330 setValueAction.setEnabled(hasData && options.traits.size() > 0);
331 }
332
333 public void clearTraitValues(String traitName) {
334 options.clearTraitValues(traitName);
335
336 dataTableModel.fireTableDataChanged();
337 }
338
339 public void guessTrait() {
340 if (options.taxonList == null) { // validation of check empty taxonList
341 return;
342 }
343
344 if (currentTrait == null) {
345 if (!addTrait()) {
346 return; // if addTrait() cancel then false
347 }
348 }
349 int result;
350 do {
351 TraitGuesser currentTraitGuesser = new TraitGuesser(currentTrait);
352 if (guessTraitDialog == null) {
353 guessTraitDialog = new GuessTraitDialog(frame);
354 }
355 guessTraitDialog.setDescription("Extract values for trait '" + currentTrait + "' from taxa labels");
356 result = guessTraitDialog.showDialog();
357
358 if (result == -1 || result == JOptionPane.CANCEL_OPTION) {
359 return;
360 }
361
362 guessTraitDialog.setupGuesserFromDialog(currentTraitGuesser);
363
364 try {
365 int[] selRows = dataTable.getSelectedRows();
366 if (selRows.length > 0) {
367 Taxa selectedTaxa = new Taxa();
368
369 for (int row : selRows) {
370 Taxon taxon = (Taxon) dataTable.getValueAt(row, 0);
371 selectedTaxa.addTaxon(taxon);
372 }
373 currentTraitGuesser.guessTrait(selectedTaxa);
374 } else {
375 currentTraitGuesser.guessTrait(options.taxonList);
376 }
377
378 } catch (IllegalArgumentException iae) {
379 JOptionPane.showMessageDialog(this, iae.getMessage(), "Unable to guess trait value", JOptionPane.ERROR_MESSAGE);
380 result = -1;
381 }
382
383 dataTableModel.fireTableDataChanged();
384 } while (result < 0);
385 }
386
387 public void setTraitValue() {
388 if (options.taxonList == null) { // validation of check empty taxonList
389 return;
390 }
391
392 int result;
393 do {
394 if (traitValueDialog == null) {
395 traitValueDialog = new TraitValueDialog(frame);
396 }
397
398 int[] selRows = dataTable.getSelectedRows();
399
400 if (selRows.length > 0) {
401 traitValueDialog.setDescription("Set values for trait '" + currentTrait + "' for selected taxa");
402 } else {
403 traitValueDialog.setDescription("Set values for trait '" + currentTrait + "' for all taxa");
404 }
405
406 result = traitValueDialog.showDialog();
407
408 if (result == -1 || result == JOptionPane.CANCEL_OPTION) {
409 return;
410 }
411
412 // currentTrait.guessTrait = true; // ?? no use?
413 String value = traitValueDialog.getTraitValue();
414
415 try {
416 if (selRows.length > 0) {
417 for (int row : selRows) {
418 Taxon taxon = (Taxon) dataTable.getValueAt(row, 0);
419 taxon.setAttribute(currentTrait.getName(), value);
420 }
421 } else {
422 for (Taxon taxon : options.taxonList) {
423 taxon.setAttribute(currentTrait.getName(), value);
424 }
425 }
426
427 } catch (IllegalArgumentException iae) {
428 JOptionPane.showMessageDialog(this, iae.getMessage(), "Unable to guess trait value", JOptionPane.ERROR_MESSAGE);
429 result = -1;
430 }
431
432 dataTableModel.fireTableDataChanged();
433 } while (result < 0);
434 }
435
436 public boolean addTrait() {
437 return addTrait("Untitled");
438 }
439
440 public boolean addTrait(String traitName) {
441 return addTrait(null, traitName);
442 }
443
444 public boolean addTrait(String message, String traitName) {
445 if (createTraitDialog == null) {
446 createTraitDialog = new CreateTraitDialog(frame);
447 }
448
449 createTraitDialog.setTraitName(traitName);
450 createTraitDialog.setMessage(message);
451
452 int result = createTraitDialog.showDialog();
453 if (result == JOptionPane.OK_OPTION) {
454 frame.tabbedPane.setSelectedComponent(this);
455
456 String name = createTraitDialog.getName();
457 TraitData.TraitType type = createTraitDialog.getType();
458 TraitData newTrait = new TraitData(options, name, "", type);
459 currentTrait = newTrait;
460
461 // The createTraitDialog will have already checked if the
462 // user is overwriting an existing trait
463 addTrait(newTrait);
464
465 if (createTraitDialog.createTraitPartition()) {
466 options.createPartitionForTraits(name, newTrait);
467 }
468
469 fireTraitsChanged();
470 updateButtons();
471
472 } else if (result == CreateTraitDialog.OK_IMPORT) {
473 boolean done = frame.doImportTraits();
474 if (done) {
475 updateButtons();
476 }
477 return done;
478 } else if (result == JOptionPane.CANCEL_OPTION) {
479 return false;
480 }
481
482 return true;
483 }
484
485 public void createTraitPartition() {
486 int[] selRows = traitsTable.getSelectedRows();
487 java.util.List<TraitData> traits = new ArrayList<TraitData>();
488 int discreteCount = 0;
489 int continuousCount = 0;
490 for (int row : selRows) {
491 TraitData trait = options.traits.get(row);
492 traits.add(trait);
493
494 if (trait.getTraitType() == TraitData.TraitType.DISCRETE) {
495 discreteCount ++;
496 }
497 if (trait.getTraitType() == TraitData.TraitType.CONTINUOUS) {
498 continuousCount ++;
499 }
500 }
501
502 boolean success = false;
503 if (discreteCount > 0) {
504 if (continuousCount > 0) {
505 JOptionPane.showMessageDialog(TraitsPanel.this, "Don't mix discrete and continuous traits when creating partition(s).", "Mixed Trait Types", JOptionPane.ERROR_MESSAGE);
506 return;
507 }
508
509 // with discrete traits, create a separate partition for each
510 for (TraitData trait : traits) {
511 java.util.List<TraitData> singleTrait = new ArrayList<TraitData>();
512 singleTrait.add(trait);
513 if (dataPanel.createFromTraits(singleTrait)) {
514 success = true;
515 }
516 }
517 } else {
518 // with
519 success = dataPanel.createFromTraits(traits);
520 }
521 if (success) {
522 frame.switchToPanel(BeautiFrame.DATA_PARTITIONS);
523 }
524 }
525
526 public void addTrait(TraitData newTrait) {
527 int selRow = options.addTrait(newTrait);
528 traitsTable.getSelectionModel().setSelectionInterval(selRow, selRow);
529 }
530
531 private void removeTrait() {
532 int selRow = traitsTable.getSelectedRow();
533 removeTrait(traitsTable.getValueAt(selRow, 0).toString());
534 }
535
536 public void removeTrait(String traitName) {
537 TraitData traitData = options.getTrait(traitName);
538 if (options.getTraitPartitions(traitData).size() > 0) {
539 JOptionPane.showMessageDialog(this, "The trait named '" + traitName + "' is being used in a partition.\nRemove the partition before deleting this trait.", "Trait in use", JOptionPane.ERROR_MESSAGE);
540 return;
541 }
542 options.removeTrait(traitName);
543
544 updateButtons();
545 fireTraitsChanged();
546 traitSelectionChanged();
547 }
548
549 public class ClearTraitAction extends AbstractAction {
550 private static final long serialVersionUID = -7281309694753868635L;
551
552 public ClearTraitAction() {
553 super("Clear trait values");
554 }
555
556 public void actionPerformed(ActionEvent ae) {
557 if (currentTrait != null) clearTraitValues(currentTrait.getName()); // Clear trait values
558 }
559 }
560
561 public class GuessTraitsAction extends AbstractAction {
562 private static final long serialVersionUID = 8514706149822252033L;
563
564 public GuessTraitsAction() {
565 super("Guess trait values");
566 }
567
568 public void actionPerformed(ActionEvent ae) {
569 guessTrait();
570 }
571 }
572
573 public class AddTraitAction extends AbstractAction {
574
575 public AddTraitAction() {
576 super("Add trait");
577 }
578
579 public void actionPerformed(ActionEvent ae) {
580 addTrait();
581 }
582 }
583
584 AbstractAction removeTraitAction = new AbstractAction() {
585 public void actionPerformed(ActionEvent ae) {
586 removeTrait();
587 }
588 };
589
590 public class SetValueAction extends AbstractAction {
591
592 public SetValueAction() {
593 super("Set trait values");
594 setToolTipText("Use this button to set the trait values of selected taxa");
595 }
596
597 public void actionPerformed(ActionEvent ae) {
598 setTraitValue();
599 }
600 }
601
602 public class CreateTraitPartitionAction extends AbstractAction {
603 public CreateTraitPartitionAction() {
604 super("Create partition from trait ...");
605 }
606
607 public void actionPerformed(ActionEvent ae) {
608 createTraitPartition();
609 }
610 }
611
612 class TraitsTableModel extends AbstractTableModel {
613
614 private static final long serialVersionUID = -6707994233020715574L;
615 String[] columnNames = {"Trait", "Type"};
616
617 public TraitsTableModel() {
618 }
619
620 public int getColumnCount() {
621 return columnNames.length;
622 }
623
624 public int getRowCount() {
625 if (options == null) return 0;
626 return options.traits.size();
627 }
628
629 public Object getValueAt(int row, int col) {
630 switch (col) {
631 case 0:
632 return options.traits.get(row).getName();
633 case 1:
634 return options.traits.get(row).getTraitType();
635 }
636 return null;
637 }
638
639 public void setValueAt(Object aValue, int row, int col) {
640 switch (col) {
641 case 0:
642 String oldName = options.traits.get(row).getName();
643 options.traits.get(row).setName(aValue.toString());
644 Object value;
645 for (Taxon t : options.taxonList) {
646 value = t.getAttribute(oldName);
647 t.setAttribute(aValue.toString(), value);
648 // cannot remvoe attribute in Attributable inteface
649 }
650 fireTraitsChanged();
651 break;
652 case 1:
653 options.traits.get(row).setTraitType((TraitData.TraitType) aValue);
654 break;
655 }
656 }
657
658 public boolean isCellEditable(int row, int col) {
659 return true;
660 }
661
662 public String getColumnName(int column) {
663 return columnNames[column];
664 }
665
666 public Class getColumnClass(int c) {
667 return getValueAt(0, c).getClass();
668 }
669
670 public String toString() {
671 StringBuffer buffer = new StringBuffer();
672
673 buffer.append(getColumnName(0));
674 for (int j = 1; j < getColumnCount(); j++) {
675 buffer.append("\t");
676 buffer.append(getColumnName(j));
677 }
678 buffer.append("\n");
679
680 for (int i = 0; i < getRowCount(); i++) {
681 buffer.append(getValueAt(i, 0));
682 for (int j = 1; j < getColumnCount(); j++) {
683 buffer.append("\t");
684 buffer.append(getValueAt(i, j));
685 }
686 buffer.append("\n");
687 }
688
689 return buffer.toString();
690 }
691 }
692
693 class DataTableModel extends AbstractTableModel {
694
695 private static final long serialVersionUID = -6707994233020715574L;
696 String[] columnNames = {"Taxon", "Value"};
697
698 public DataTableModel() {
699 }
700
701 public int getColumnCount() {
702 return columnNames.length;
703 }
704
705 public int getRowCount() {
706 if (options == null) return 0;
707 if (options.taxonList == null) return 0;
708 if (currentTrait == null) return 0;
709
710 return options.taxonList.getTaxonCount();
711 }
712
713 public Object getValueAt(int row, int col) {
714 switch (col) {
715 case 0:
716 return options.taxonList.getTaxon(row);
717 case 1:
718 Object value = null;
719 if (currentTrait != null) {
720 value = options.taxonList.getTaxon(row).getAttribute(currentTrait.getName());
721 }
722 if (value != null) {
723 return value;
724 } else {
725 return "";
726 }
727 }
728 return null;
729 }
730
731 public void setValueAt(Object aValue, int row, int col) {
732 if (col == 1) {
733 // Location location = options.taxonList.getTaxon(row).getLocation();
734 // if (location != null) {
735 // options.taxonList.getTaxon(row).setLocation(location);
736 // }
737 if (currentTrait != null) {
738 options.taxonList.getTaxon(row).setAttribute(currentTrait.getName(), aValue);
739 }
740
741 }
742 }
743
744 public boolean isCellEditable(int row, int col) {
745 if (col == 1) {
746 // Object t = options.taxonList.getTaxon(row).getAttribute(currentTrait.getName());
747 // return (t != null);
748 return true;
749 } else {
750 return false;
751 }
752 }
753
754 public String getColumnName(int column) {
755 return columnNames[column];
756 }
757
758 public Class getColumnClass(int c) {
759 return getValueAt(0, c).getClass();
760 }
761
762 public String toString() {
763 StringBuffer buffer = new StringBuffer();
764
765 buffer.append(getColumnName(0));
766 for (int j = 1; j < getColumnCount(); j++) {
767 buffer.append("\t");
768 buffer.append(getColumnName(j));
769 }
770 buffer.append("\n");
771
772 for (int i = 0; i < getRowCount(); i++) {
773 buffer.append(getValueAt(i, 0));
774 for (int j = 1; j < getColumnCount(); j++) {
775 buffer.append("\t");
776 buffer.append(getValueAt(i, j));
777 }
778 buffer.append("\n");
779 }
780
781 return buffer.toString();
782 }
783 }
784
785
786 }
787