| 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.bss; |
| 27 |
|
| 28 |
import jam.panels.ActionPanel; |
| 29 |
|
| 30 |
import java.awt.BorderLayout; |
| 31 |
import java.awt.Dimension; |
| 32 |
import java.awt.Frame; |
| 33 |
import java.awt.event.ActionEvent; |
| 34 |
import java.awt.event.ActionListener; |
| 35 |
import java.io.File; |
| 36 |
import java.io.PrintWriter; |
| 37 |
|
| 38 |
import javax.swing.AbstractAction; |
| 39 |
import javax.swing.Action; |
| 40 |
import javax.swing.JButton; |
| 41 |
import javax.swing.JDialog; |
| 42 |
import javax.swing.JFileChooser; |
| 43 |
import javax.swing.JFrame; |
| 44 |
import javax.swing.JMenuBar; |
| 45 |
import javax.swing.JPanel; |
| 46 |
import javax.swing.JScrollPane; |
| 47 |
import javax.swing.JTable; |
| 48 |
import javax.swing.ScrollPaneConstants; |
| 49 |
import javax.swing.SwingUtilities; |
| 50 |
import javax.swing.SwingWorker; |
| 51 |
|
| 52 |
import dr.evolution.util.Taxa; |
| 53 |
import dr.evolution.util.Taxon; |
| 54 |
|
| 55 |
public class TaxaEditor { |
| 56 |
|
| 57 |
private PartitionDataList dataList; |
| 58 |
private MainFrame frame; |
| 59 |
private int row; |
| 60 |
|
| 61 |
|
| 62 |
private int taxonCount; |
| 63 |
private static final int minCount = 0; |
| 64 |
|
| 65 |
|
| 66 |
private JDialog window; |
| 67 |
private Frame owner; |
| 68 |
|
| 69 |
|
| 70 |
private JMenuBar menu; |
| 71 |
|
| 72 |
|
| 73 |
private JButton load; |
| 74 |
private JButton save; |
| 75 |
private JButton done; |
| 76 |
private JButton cancel; |
| 77 |
|
| 78 |
|
| 79 |
private JTable table; |
| 80 |
private TaxaEditorTableModel taxaEditorTableModel; |
| 81 |
|
| 82 |
@SuppressWarnings("serial") |
| 83 |
private Action addTaxonAction = new AbstractAction("+") { |
| 84 |
public void actionPerformed(ActionEvent ae) { |
| 85 |
|
| 86 |
taxaEditorTableModel.addEmptyRow(); |
| 87 |
setTaxa(); |
| 88 |
|
| 89 |
} |
| 90 |
}; |
| 91 |
|
| 92 |
@SuppressWarnings("serial") |
| 93 |
private Action removeTaxonAction = new AbstractAction("-") { |
| 94 |
public void actionPerformed(ActionEvent ae) { |
| 95 |
if (taxonCount > minCount) { |
| 96 |
|
| 97 |
taxaEditorTableModel.removeLastRow(); |
| 98 |
setTaxa(); |
| 99 |
|
| 100 |
} |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
public TaxaEditor(MainFrame frame, PartitionDataList dataList, int row) { |
| 105 |
|
| 106 |
this.frame = frame; |
| 107 |
this.dataList = dataList; |
| 108 |
this.row = row; |
| 109 |
|
| 110 |
|
| 111 |
taxaEditorTableModel = new TaxaEditorTableModel(); |
| 112 |
|
| 113 |
|
| 114 |
load = new JButton("Load", Utils.createImageIcon(Utils.TEXT_FILE_ICON)); |
| 115 |
save = new JButton("Save", Utils.createImageIcon(Utils.SAVE_ICON)); |
| 116 |
done = new JButton("Done", Utils.createImageIcon(Utils.CHECK_ICON)); |
| 117 |
cancel = new JButton("Cancel", Utils.createImageIcon(Utils.CLOSE_ICON)); |
| 118 |
|
| 119 |
|
| 120 |
load.addActionListener(new ListenLoadTaxaFile()); |
| 121 |
save.addActionListener(new ListenSaveTaxaFile()); |
| 122 |
done.addActionListener(new ListenOk()); |
| 123 |
cancel.addActionListener(new ListenCancel()); |
| 124 |
|
| 125 |
|
| 126 |
menu = new JMenuBar(); |
| 127 |
menu.setLayout(new BorderLayout()); |
| 128 |
JPanel buttonsHolder = new JPanel(); |
| 129 |
buttonsHolder.setOpaque(false); |
| 130 |
buttonsHolder.add(load); |
| 131 |
buttonsHolder.add(save); |
| 132 |
buttonsHolder.add(done); |
| 133 |
buttonsHolder.add(cancel); |
| 134 |
menu.add(buttonsHolder, BorderLayout.WEST); |
| 135 |
|
| 136 |
|
| 137 |
table = new JTable(); |
| 138 |
table.setModel(taxaEditorTableModel); |
| 139 |
table.setSurrendersFocusOnKeystroke(true); |
| 140 |
|
| 141 |
JScrollPane scrollPane = new JScrollPane(table, |
| 142 |
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, |
| 143 |
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); |
| 144 |
RowNumberTable rowNumberTable = new RowNumberTable(table); |
| 145 |
scrollPane.setRowHeaderView(rowNumberTable); |
| 146 |
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, |
| 147 |
rowNumberTable.getTableHeader()); |
| 148 |
|
| 149 |
ActionPanel actionPanel = new ActionPanel(false); |
| 150 |
actionPanel.setAddAction(addTaxonAction); |
| 151 |
actionPanel.setRemoveAction(removeTaxonAction); |
| 152 |
|
| 153 |
|
| 154 |
owner = Utils.getActiveFrame(); |
| 155 |
window = new JDialog(owner, "Edit taxa set..."); |
| 156 |
window.getContentPane().add(menu, BorderLayout.NORTH); |
| 157 |
window.getContentPane().add(scrollPane); |
| 158 |
window.getContentPane().add(actionPanel, BorderLayout.SOUTH); |
| 159 |
|
| 160 |
window.pack(); |
| 161 |
window.setLocationRelativeTo(owner); |
| 162 |
|
| 163 |
setTaxa(); |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
public void launch() { |
| 168 |
|
| 169 |
if (SwingUtilities.isEventDispatchThread()) { |
| 170 |
showWindow(); |
| 171 |
} else { |
| 172 |
SwingUtilities.invokeLater(new Runnable() { |
| 173 |
public void run() { |
| 174 |
showWindow(); |
| 175 |
} |
| 176 |
}); |
| 177 |
} |
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
public void showWindow() { |
| 182 |
|
| 183 |
|
| 184 |
window.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); |
| 185 |
window.setSize(new Dimension(400, 400)); |
| 186 |
window.setMinimumSize(new Dimension(100, 100)); |
| 187 |
window.setResizable(true); |
| 188 |
window.setVisible(true); |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
private void setTaxa() { |
| 193 |
|
| 194 |
taxonCount = taxaEditorTableModel.getRowCount(); |
| 195 |
|
| 196 |
addTaxonAction.setEnabled(true); |
| 197 |
if (taxonCount == minCount) { |
| 198 |
removeTaxonAction.setEnabled(false); |
| 199 |
} else { |
| 200 |
removeTaxonAction.setEnabled(true); |
| 201 |
} |
| 202 |
|
| 203 |
ColumnResizer.adjustColumnPreferredWidths(table); |
| 204 |
} |
| 205 |
|
| 206 |
private class ListenLoadTaxaFile implements ActionListener { |
| 207 |
public void actionPerformed(ActionEvent ev) { |
| 208 |
|
| 209 |
JFileChooser chooser = new JFileChooser(); |
| 210 |
chooser.setDialogTitle("Select taxa file..."); |
| 211 |
chooser.setMultiSelectionEnabled(false); |
| 212 |
chooser.setCurrentDirectory(frame.getWorkingDirectory()); |
| 213 |
|
| 214 |
int returnValue = chooser.showOpenDialog(Utils.getActiveFrame()); |
| 215 |
|
| 216 |
if (returnValue == JFileChooser.APPROVE_OPTION) { |
| 217 |
|
| 218 |
File file = chooser.getSelectedFile(); |
| 219 |
|
| 220 |
if (file != null) { |
| 221 |
|
| 222 |
loadTaxaFile(file); |
| 223 |
|
| 224 |
File tmpDir = chooser.getCurrentDirectory(); |
| 225 |
if (tmpDir != null) { |
| 226 |
frame.setWorkingDirectory(tmpDir); |
| 227 |
} |
| 228 |
|
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
private void loadTaxaFile(File file) { |
| 236 |
|
| 237 |
try { |
| 238 |
|
| 239 |
Taxa taxa = new Taxa(); |
| 240 |
String[] lines = Utils.loadStrings(file.getAbsolutePath()); |
| 241 |
|
| 242 |
Taxon taxon; |
| 243 |
for (int i = 0; i < lines.length; i++) { |
| 244 |
|
| 245 |
String[] line = lines[i].split("\\s+"); |
| 246 |
|
| 247 |
taxon = new Taxon(line[TaxaEditorTableModel.NAME_INDEX]); |
| 248 |
taxon.setAttribute(Utils.ABSOLUTE_HEIGHT, |
| 249 |
Double.valueOf(line[TaxaEditorTableModel.HEIGHT_INDEX])); |
| 250 |
|
| 251 |
taxa.addTaxon(taxon); |
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
updateTable(taxa); |
| 256 |
|
| 257 |
} catch (Exception e) { |
| 258 |
Utils.handleException(e); |
| 259 |
} |
| 260 |
|
| 261 |
} |
| 262 |
|
| 263 |
private class ListenSaveTaxaFile implements ActionListener { |
| 264 |
public void actionPerformed(ActionEvent ev) { |
| 265 |
|
| 266 |
JFileChooser chooser = new JFileChooser(); |
| 267 |
chooser.setDialogTitle("Save taxa file..."); |
| 268 |
chooser.setMultiSelectionEnabled(false); |
| 269 |
chooser.setCurrentDirectory(frame.getWorkingDirectory()); |
| 270 |
|
| 271 |
int returnVal = chooser.showSaveDialog(Utils.getActiveFrame()); |
| 272 |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
| 273 |
|
| 274 |
File file = chooser.getSelectedFile(); |
| 275 |
|
| 276 |
saveTaxaFile(file.getAbsolutePath()); |
| 277 |
|
| 278 |
File tmpDir = chooser.getCurrentDirectory(); |
| 279 |
if (tmpDir != null) { |
| 280 |
frame.setWorkingDirectory(tmpDir); |
| 281 |
} |
| 282 |
|
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
private void saveTaxaFile(String filename) { |
| 288 |
|
| 289 |
try { |
| 290 |
|
| 291 |
boolean header = false; |
| 292 |
PrintWriter writer = new PrintWriter(filename); |
| 293 |
writer.print(taxaEditorTableModel.toString(header)); |
| 294 |
writer.close(); |
| 295 |
|
| 296 |
} catch (Exception e) { |
| 297 |
Utils.handleException(e); |
| 298 |
} |
| 299 |
|
| 300 |
} |
| 301 |
|
| 302 |
private class ListenOk implements ActionListener { |
| 303 |
public void actionPerformed(ActionEvent ev) { |
| 304 |
|
| 305 |
doOk(); |
| 306 |
|
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
private void doOk() { |
| 311 |
|
| 312 |
frame.setBusy(); |
| 313 |
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() { |
| 314 |
|
| 315 |
|
| 316 |
public Void doInBackground() { |
| 317 |
|
| 318 |
try { |
| 319 |
|
| 320 |
|
| 321 |
String value = dataList.recordsList.get(row).getName(); |
| 322 |
Utils.removeTaxaWithAttributeValue(dataList, |
| 323 |
Utils.TREE_FILENAME, value); |
| 324 |
|
| 325 |
String name = String.valueOf("TaxaSet").concat( |
| 326 |
String.valueOf(row + 1)); |
| 327 |
Taxa taxa = taxaEditorTableModel.getTaxaSet(); |
| 328 |
TreesTableRecord record = new TreesTableRecord(name, taxa); |
| 329 |
|
| 330 |
dataList.recordsList.set(row, record); |
| 331 |
dataList.allTaxa.addTaxa(taxa); |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
} catch (Exception e) { |
| 336 |
Utils.handleException(e); |
| 337 |
} |
| 338 |
|
| 339 |
return null; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
public void done() { |
| 344 |
frame.setIdle(); |
| 345 |
frame.fireTaxaChanged(); |
| 346 |
window.setVisible(false); |
| 347 |
} |
| 348 |
}; |
| 349 |
|
| 350 |
worker.execute(); |
| 351 |
|
| 352 |
} |
| 353 |
|
| 354 |
private class ListenCancel implements ActionListener { |
| 355 |
public void actionPerformed(ActionEvent ev) { |
| 356 |
|
| 357 |
window.setVisible(false); |
| 358 |
|
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
public void updateTable(Taxa taxa) { |
| 363 |
taxaEditorTableModel.setTaxaSet(taxa); |
| 364 |
taxaEditorTableModel.fireTaxonListChanged(); |
| 365 |
setTaxa(); |
| 366 |
} |
| 367 |
|
| 368 |
} |