Debian Codegraph @

src/dr/app/bss/TaxaEditor.java

raw symbols SCIP
1 /*
2 * TaxaEditor.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.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 // private MutableTaxonList taxonList;
62 private int taxonCount;
63 private static final int minCount = 0;
64
65 // Window
66 private JDialog window;
67 private Frame owner;
68
69 // Menubar
70 private JMenuBar menu;
71
72 // Buttons with options
73 private JButton load;
74 private JButton save;
75 private JButton done;
76 private JButton cancel;
77
78 // Data, model & stuff for JTable
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 }// END: actionPerformed
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 }// END: actionPerformed
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 // taxonList = new Taxa();
111 taxaEditorTableModel = new TaxaEditorTableModel();
112
113 // Setup Main Menu buttons
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 // Add Main Menu buttons listeners
120 load.addActionListener(new ListenLoadTaxaFile());
121 save.addActionListener(new ListenSaveTaxaFile());
122 done.addActionListener(new ListenOk());
123 cancel.addActionListener(new ListenCancel());
124
125 // Setup menu
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 // Setup table
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 // Setup window
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 }// END: LocationCoordinatesEditor()
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 }// END: edt check
178
179 }// END: launch
180
181 public void showWindow() {
182
183 // Display Frame
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 }// END: showWindow
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 }// END: setPartitions
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 }// END: file opened check
230 }// END: dialog cancelled check
231
232 }// END: actionPerformed
233 }// END: ListenLoadTaxaFile
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 }// END: i loop
254
255 updateTable(taxa);
256
257 } catch (Exception e) {
258 Utils.handleException(e);
259 }// END: try-catch block
260
261 }// END: loadTaxaFile
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 }// END: approve check
284 }// END: actionPerformed
285 }// END: ListenSaveLocationCoordinates
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 }// END: save2DArray
301
302 private class ListenOk implements ActionListener {
303 public void actionPerformed(ActionEvent ev) {
304
305 doOk();
306
307 }// END: actionPerformed
308 }// END: ListenOk
309
310 private void doOk() {
311
312 frame.setBusy();
313 SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
314
315 // Executed in background thread
316 public Void doInBackground() {
317
318 try {
319
320 // delete taxa connected to this row
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 // treesTableModel.setRow(row, record);
334
335 } catch (Exception e) {
336 Utils.handleException(e);
337 }// END: try-catch block
338
339 return null;
340 }// END: doInBackground()
341
342 // Executed in event dispatch thread
343 public void done() {
344 frame.setIdle();
345 frame.fireTaxaChanged();
346 window.setVisible(false);
347 }// END: done
348 };
349
350 worker.execute();
351
352 }// END: doOk
353
354 private class ListenCancel implements ActionListener {
355 public void actionPerformed(ActionEvent ev) {
356
357 window.setVisible(false);
358
359 }// END: actionPerformed
360 }// END: ListenCancel
361
362 public void updateTable(Taxa taxa) {
363 taxaEditorTableModel.setTaxaSet(taxa);
364 taxaEditorTableModel.fireTaxonListChanged();
365 setTaxa();
366 }// END: updateTable
367
368 }// END: class