| 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 |
package dr.app.treestat; |
| 27 |
|
| 28 |
import jam.framework.Exportable; |
| 29 |
import jam.table.TableRenderer; |
| 30 |
|
| 31 |
import javax.swing.*; |
| 32 |
import javax.swing.event.ListSelectionEvent; |
| 33 |
import javax.swing.event.ListSelectionListener; |
| 34 |
import javax.swing.table.AbstractTableModel; |
| 35 |
import java.awt.*; |
| 36 |
import java.awt.event.ActionEvent; |
| 37 |
import java.util.ArrayList; |
| 38 |
|
| 39 |
|
| 40 |
public class TaxonSetsPanel extends JPanel implements Exportable { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private static final long serialVersionUID = -9013475414423166476L; |
| 47 |
TreeStatFrame frame = null; |
| 48 |
TreeStatData treeStatData = null; |
| 49 |
TreeStatData.TaxonSet selectedTaxonSet = null; |
| 50 |
|
| 51 |
JScrollPane scrollPane1 = null; |
| 52 |
JTable taxonSetsTable = null; |
| 53 |
TaxonSetsTableModel taxonSetsTableModel = null; |
| 54 |
|
| 55 |
JScrollPane scrollPane2 = null; |
| 56 |
JTable excludedTaxaTable = null; |
| 57 |
TaxaTableModel excludedTaxaTableModel = null; |
| 58 |
|
| 59 |
JScrollPane scrollPane3 = null; |
| 60 |
JTable includedTaxaTable = null; |
| 61 |
TaxaTableModel includedTaxaTableModel = null; |
| 62 |
|
| 63 |
public TaxonSetsPanel(TreeStatFrame frame, TreeStatData treeStatData) { |
| 64 |
|
| 65 |
this.frame = frame; |
| 66 |
setOpaque(false); |
| 67 |
|
| 68 |
this.treeStatData = treeStatData; |
| 69 |
|
| 70 |
Icon addIcon = null, removeIcon = null, includeIcon = null, excludeIcon = null; |
| 71 |
try { |
| 72 |
addIcon = new ImageIcon(dr.app.util.Utils.getImage(this, "images/add.png")); |
| 73 |
removeIcon = new ImageIcon(dr.app.util.Utils.getImage(this, "images/minus.png")); |
| 74 |
includeIcon = new ImageIcon(dr.app.util.Utils.getImage(this, "images/include.png")); |
| 75 |
excludeIcon = new ImageIcon(dr.app.util.Utils.getImage(this, "images/exclude.png")); |
| 76 |
} catch (Exception e) { |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
JPanel buttonPanel = new JPanel(new BorderLayout()); |
| 81 |
buttonPanel.setOpaque(false); |
| 82 |
JButton importButton = new JButton(frame.getImportAction()); |
| 83 |
importButton.setFocusable(false); |
| 84 |
importButton.putClientProperty("JButton.buttonType", "textured"); |
| 85 |
importButton.setMargin(new Insets(4,4,4,4)); |
| 86 |
buttonPanel.add(importButton, BorderLayout.WEST); |
| 87 |
buttonPanel.add(new JLabel(" To define taxon sets, first import a list of taxa (i.e., from the trees to be analysed)"), BorderLayout.SOUTH); |
| 88 |
|
| 89 |
|
| 90 |
taxonSetsTableModel = new TaxonSetsTableModel(); |
| 91 |
TableSorter sorter = new TableSorter(taxonSetsTableModel); |
| 92 |
taxonSetsTable = new JTable(sorter); |
| 93 |
sorter.addTableModelListener(taxonSetsTable); |
| 94 |
|
| 95 |
taxonSetsTable.getColumnModel().getColumn(0).setCellRenderer( |
| 96 |
new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4))); |
| 97 |
|
| 98 |
taxonSetsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
| 99 |
public void valueChanged(ListSelectionEvent evt) { taxonSetsTableSelectionChanged(); } |
| 100 |
}); |
| 101 |
|
| 102 |
scrollPane1 = new JScrollPane(taxonSetsTable, |
| 103 |
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, |
| 104 |
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
| 105 |
|
| 106 |
JPanel buttonPanel1 = createAddRemoveButtonPanel(addTaxonSetAction, addIcon, "Create a new taxon set", |
| 107 |
removeTaxonSetAction, removeIcon, "Remove a taxon set", |
| 108 |
javax.swing.BoxLayout.X_AXIS); |
| 109 |
|
| 110 |
|
| 111 |
excludedTaxaTableModel = new TaxaTableModel(false); |
| 112 |
sorter = new TableSorter(excludedTaxaTableModel); |
| 113 |
excludedTaxaTable = new JTable(sorter); |
| 114 |
sorter.addTableModelListener(excludedTaxaTable); |
| 115 |
|
| 116 |
excludedTaxaTable.getColumnModel().getColumn(0).setCellRenderer( |
| 117 |
new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4))); |
| 118 |
|
| 119 |
excludedTaxaTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
| 120 |
public void valueChanged(ListSelectionEvent evt) { excludedTaxaTableSelectionChanged(); } |
| 121 |
}); |
| 122 |
|
| 123 |
scrollPane2 = new JScrollPane(excludedTaxaTable, |
| 124 |
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, |
| 125 |
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
| 126 |
|
| 127 |
JPanel buttonPanel2 = createAddRemoveButtonPanel(includeTaxonAction, includeIcon, "Include selected taxa in the taxon set", |
| 128 |
excludeTaxonAction, excludeIcon, "Exclude selected taxa from the taxon set", |
| 129 |
javax.swing.BoxLayout.Y_AXIS); |
| 130 |
|
| 131 |
|
| 132 |
includedTaxaTableModel = new TaxaTableModel(true); |
| 133 |
sorter = new TableSorter(includedTaxaTableModel); |
| 134 |
includedTaxaTable = new JTable(sorter); |
| 135 |
sorter.addTableModelListener(includedTaxaTable); |
| 136 |
|
| 137 |
includedTaxaTable.getColumnModel().getColumn(0).setCellRenderer( |
| 138 |
new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4))); |
| 139 |
|
| 140 |
includedTaxaTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
| 141 |
public void valueChanged(ListSelectionEvent evt) { includedTaxaTableSelectionChanged(); } |
| 142 |
}); |
| 143 |
|
| 144 |
scrollPane3 = new JScrollPane(includedTaxaTable, |
| 145 |
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, |
| 146 |
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
| 147 |
|
| 148 |
JPanel panel = new JPanel(); |
| 149 |
panel.setOpaque(false); |
| 150 |
panel.setLayout(new GridBagLayout()); |
| 151 |
GridBagConstraints c = new GridBagConstraints(); |
| 152 |
|
| 153 |
c.weightx = 0.333333; |
| 154 |
c.weighty = 1; |
| 155 |
c.fill = GridBagConstraints.BOTH; |
| 156 |
c.anchor = GridBagConstraints.CENTER; |
| 157 |
c.insets = new Insets(6,6,6,6); |
| 158 |
c.gridx = 0; |
| 159 |
c.gridy = 0; |
| 160 |
panel.add(scrollPane1, c); |
| 161 |
|
| 162 |
c.weightx = 0.333333; |
| 163 |
c.weighty = 0; |
| 164 |
c.fill = GridBagConstraints.NONE; |
| 165 |
c.anchor = GridBagConstraints.LINE_START; |
| 166 |
c.insets = new Insets(0,6,6,6); |
| 167 |
c.gridx = 0; |
| 168 |
c.gridy = 1; |
| 169 |
panel.add(buttonPanel1, c); |
| 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 = 1; |
| 177 |
c.gridy = 0; |
| 178 |
panel.add(scrollPane2, c); |
| 179 |
|
| 180 |
c.weightx = 0.0; |
| 181 |
c.fill = GridBagConstraints.NONE; |
| 182 |
c.anchor = GridBagConstraints.CENTER; |
| 183 |
c.insets = new Insets(0,0,0,0); |
| 184 |
c.gridx = 2; |
| 185 |
c.gridy = 0; |
| 186 |
panel.add(buttonPanel2, c); |
| 187 |
|
| 188 |
c.weightx = 0.333333; |
| 189 |
c.fill = GridBagConstraints.BOTH; |
| 190 |
c.anchor = GridBagConstraints.CENTER; |
| 191 |
c.insets = new Insets(6,6,6,6); |
| 192 |
c.gridx = 3; |
| 193 |
c.gridy = 0; |
| 194 |
panel.add(scrollPane3, c); |
| 195 |
|
| 196 |
setLayout(new BorderLayout()); |
| 197 |
setBorder(BorderFactory.createEmptyBorder(6,6,6,6)); |
| 198 |
add(buttonPanel, BorderLayout.NORTH); |
| 199 |
add(panel, BorderLayout.CENTER); |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
JPanel createAddRemoveButtonPanel(Action addAction, Icon addIcon, String addToolTip, |
| 204 |
Action removeAction, Icon removeIcon, String removeToolTip, int axis) { |
| 205 |
|
| 206 |
JPanel buttonPanel = new JPanel(); |
| 207 |
buttonPanel.setLayout(new BoxLayout(buttonPanel, axis)); |
| 208 |
buttonPanel.setOpaque(false); |
| 209 |
JButton addButton = new JButton(addAction); |
| 210 |
if (addIcon != null) { |
| 211 |
addButton.setIcon(addIcon); |
| 212 |
addButton.setText(null); |
| 213 |
} |
| 214 |
addButton.setToolTipText(addToolTip); |
| 215 |
addButton.putClientProperty("JButton.buttonType", "textured"); |
| 216 |
addButton.setOpaque(false); |
| 217 |
addAction.setEnabled(false); |
| 218 |
|
| 219 |
JButton removeButton = new JButton(removeAction); |
| 220 |
if (removeIcon != null) { |
| 221 |
removeButton.setIcon(removeIcon); |
| 222 |
removeButton.setText(null); |
| 223 |
} |
| 224 |
removeButton.setToolTipText(removeToolTip); |
| 225 |
removeButton.putClientProperty("JButton.buttonType", "textured"); |
| 226 |
removeButton.setOpaque(false); |
| 227 |
removeAction.setEnabled(false); |
| 228 |
|
| 229 |
buttonPanel.add(addButton); |
| 230 |
buttonPanel.add(new JToolBar.Separator(new Dimension(6,6))); |
| 231 |
buttonPanel.add(removeButton); |
| 232 |
|
| 233 |
return buttonPanel; |
| 234 |
} |
| 235 |
|
| 236 |
public void dataChanged() { |
| 237 |
|
| 238 |
addTaxonSetAction.setEnabled(treeStatData.allTaxa.size() > 0); |
| 239 |
|
| 240 |
taxonSetsTableModel.fireTableDataChanged(); |
| 241 |
includedTaxaTableModel.fireTableDataChanged(); |
| 242 |
excludedTaxaTableModel.fireTableDataChanged(); |
| 243 |
} |
| 244 |
|
| 245 |
private void taxonSetsTableSelectionChanged() { |
| 246 |
if (taxonSetsTable.getSelectedRowCount() == 0) { |
| 247 |
selectedTaxonSet = null; |
| 248 |
removeTaxonSetAction.setEnabled(false); |
| 249 |
} else { |
| 250 |
selectedTaxonSet = treeStatData.taxonSets.get(taxonSetsTable.getSelectedRow()); |
| 251 |
removeTaxonSetAction.setEnabled(true); |
| 252 |
} |
| 253 |
includedTaxaTableModel.fireTableDataChanged(); |
| 254 |
excludedTaxaTableModel.fireTableDataChanged(); |
| 255 |
} |
| 256 |
|
| 257 |
private void excludedTaxaTableSelectionChanged() { |
| 258 |
if (excludedTaxaTable.getSelectedRowCount() == 0) { |
| 259 |
includeTaxonAction.setEnabled(false); |
| 260 |
} else { |
| 261 |
includeTaxonAction.setEnabled(true); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
private void includedTaxaTableSelectionChanged() { |
| 266 |
if (includedTaxaTable.getSelectedRowCount() == 0) { |
| 267 |
excludeTaxonAction.setEnabled(false); |
| 268 |
} else { |
| 269 |
excludeTaxonAction.setEnabled(true); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
public JComponent getExportableComponent() { |
| 274 |
return this; |
| 275 |
} |
| 276 |
|
| 277 |
Action addTaxonSetAction = new AbstractAction("+") { |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
private static final long serialVersionUID = 1831933175582860833L; |
| 283 |
|
| 284 |
public void actionPerformed(ActionEvent ae) { |
| 285 |
TreeStatData.TaxonSet taxonSet = new TreeStatData.TaxonSet(); |
| 286 |
taxonSet.name = "untitled"; |
| 287 |
taxonSet.taxa = new ArrayList(); |
| 288 |
treeStatData.taxonSets.add(taxonSet); |
| 289 |
dataChanged(); |
| 290 |
|
| 291 |
int sel = treeStatData.taxonSets.size() - 1; |
| 292 |
taxonSetsTable.setRowSelectionInterval(sel, sel); |
| 293 |
} |
| 294 |
}; |
| 295 |
|
| 296 |
Action removeTaxonSetAction = new AbstractAction("-") { |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
private static final long serialVersionUID = -8662527333546044639L; |
| 302 |
|
| 303 |
public void actionPerformed(ActionEvent ae) { |
| 304 |
int saved = taxonSetsTable.getSelectedRow(); |
| 305 |
int row = taxonSetsTable.getSelectedRow(); |
| 306 |
if (row != -1) { |
| 307 |
treeStatData.taxonSets.remove(row); |
| 308 |
} |
| 309 |
dataChanged(); |
| 310 |
if (saved >= treeStatData.taxonSets.size()) saved = treeStatData.taxonSets.size() - 1; |
| 311 |
taxonSetsTable.setRowSelectionInterval(saved, saved); |
| 312 |
} |
| 313 |
}; |
| 314 |
|
| 315 |
Action includeTaxonAction = new AbstractAction("->") { |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
private static final long serialVersionUID = -1875904513948242608L; |
| 320 |
|
| 321 |
public void actionPerformed(ActionEvent ae) { |
| 322 |
int saved = taxonSetsTable.getSelectedRow(); |
| 323 |
int[] rows = excludedTaxaTable.getSelectedRows(); |
| 324 |
ArrayList<String> exclList = new ArrayList<String>(treeStatData.allTaxa); |
| 325 |
exclList.removeAll(selectedTaxonSet.taxa); |
| 326 |
for (int row : rows) { |
| 327 |
selectedTaxonSet.taxa.add(exclList.get(row)); |
| 328 |
} |
| 329 |
dataChanged(); |
| 330 |
taxonSetsTable.setRowSelectionInterval(saved, saved); |
| 331 |
} |
| 332 |
}; |
| 333 |
|
| 334 |
Action excludeTaxonAction = new AbstractAction("<-") { |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
private static final long serialVersionUID = 4523480086490780822L; |
| 340 |
|
| 341 |
public void actionPerformed(ActionEvent ae) { |
| 342 |
int saved = taxonSetsTable.getSelectedRow(); |
| 343 |
int[] rows = includedTaxaTable.getSelectedRows(); |
| 344 |
for (int i = rows.length - 1; i >= 0 ; i--) { |
| 345 |
selectedTaxonSet.taxa.remove(rows[i]); |
| 346 |
} |
| 347 |
dataChanged(); |
| 348 |
taxonSetsTable.setRowSelectionInterval(saved, saved); |
| 349 |
} |
| 350 |
}; |
| 351 |
|
| 352 |
class TaxonSetsTableModel extends AbstractTableModel { |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
private static final long serialVersionUID = 219223813257870207L; |
| 358 |
|
| 359 |
public TaxonSetsTableModel() { |
| 360 |
} |
| 361 |
|
| 362 |
public int getColumnCount() { |
| 363 |
return 1; |
| 364 |
} |
| 365 |
|
| 366 |
public int getRowCount() { |
| 367 |
return treeStatData.taxonSets.size(); |
| 368 |
} |
| 369 |
|
| 370 |
public Object getValueAt(int row, int col) { |
| 371 |
return (treeStatData.taxonSets.get(row)).name; |
| 372 |
} |
| 373 |
|
| 374 |
public void setValueAt(Object value, int row, int col) { |
| 375 |
(treeStatData.taxonSets.get(row)).name = (String)value; |
| 376 |
} |
| 377 |
|
| 378 |
public boolean isCellEditable(int row, int col) { |
| 379 |
return true; |
| 380 |
} |
| 381 |
|
| 382 |
public String getColumnName(int column) { |
| 383 |
return "Taxon Sets"; |
| 384 |
} |
| 385 |
|
| 386 |
public Class getColumnClass(int c) {return getValueAt(0, c).getClass();} |
| 387 |
} |
| 388 |
|
| 389 |
class TaxaTableModel extends AbstractTableModel { |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
private static final long serialVersionUID = 1559408662356843275L; |
| 395 |
boolean included; |
| 396 |
|
| 397 |
public TaxaTableModel(boolean included) { |
| 398 |
this.included = included; |
| 399 |
} |
| 400 |
|
| 401 |
public int getColumnCount() { |
| 402 |
return 1; |
| 403 |
} |
| 404 |
|
| 405 |
public int getRowCount() { |
| 406 |
if (selectedTaxonSet == null) return 0; |
| 407 |
|
| 408 |
if (included) { |
| 409 |
return selectedTaxonSet.taxa.size(); |
| 410 |
} else { |
| 411 |
return treeStatData.allTaxa.size() - selectedTaxonSet.taxa.size(); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
public Object getValueAt(int row, int col) { |
| 416 |
|
| 417 |
if (included) { |
| 418 |
return selectedTaxonSet.taxa.get(row); |
| 419 |
} else { |
| 420 |
ArrayList<String> exclList = new ArrayList<String>(treeStatData.allTaxa); |
| 421 |
exclList.removeAll(selectedTaxonSet.taxa); |
| 422 |
return exclList.get(row); |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
public boolean isCellEditable(int row, int col) { |
| 427 |
return false; |
| 428 |
} |
| 429 |
|
| 430 |
public String getColumnName(int column) { |
| 431 |
if (included) return "Included Taxa"; |
| 432 |
else return "Excluded Taxa"; |
| 433 |
} |
| 434 |
|
| 435 |
public Class getColumnClass(int c) {return getValueAt(0, c).getClass();} |
| 436 |
} |
| 437 |
} |