Debian Codegraph @

src/dr/app/beauti/sitemodelspanel/GLMSettingsPanel.java

raw symbols SCIP
1 /*
2 * PriorSettingsPanel.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.sitemodelspanel;
27
28 import dr.app.beauti.BeautiFrame;
29 import dr.app.beauti.options.BeautiOptions;
30 import dr.app.beauti.options.Predictor;
31 import dr.app.beauti.options.TraitData;
32 import dr.app.beauti.util.BEAUTiImporter;
33 import dr.app.beauti.util.PanelUtils;
34 import dr.app.gui.table.TableEditorStopper;
35 import dr.app.gui.table.TableSorter;
36 import jam.panels.ActionPanel;
37
38 import javax.swing.*;
39 import javax.swing.event.ListSelectionEvent;
40 import javax.swing.event.ListSelectionListener;
41 import javax.swing.filechooser.FileNameExtensionFilter;
42 import javax.swing.plaf.BorderUIResource;
43 import javax.swing.table.AbstractTableModel;
44 import java.awt.*;
45 import java.awt.event.ActionEvent;
46 import java.io.File;
47 import java.io.FileNotFoundException;
48 import java.io.IOException;
49 import java.util.ArrayList;
50
51 /**
52 * @author Andrew Rambaut
53 */
54 public class GLMSettingsPanel extends JPanel {
55
56 private static final int MINIMUM_TABLE_WIDTH = 140;
57
58 private static final String IMPORT_PREDICTORS_TOOLTIP = "<html>Import one or more predictor matrices for these taxa from a tab-delimited<br>" +
59 "file. Taxa should be in the first column and the trait names<br>" +
60 "in the first row</html>";
61
62 private final BeautiFrame frame;
63 private JDialog dialog;
64 private BeautiOptions options;
65
66 private TraitData trait;
67
68 public final JTable predictorsTable;
69 private final PredictorsTableModel predictorsTableModel;
70
71 public GLMSettingsPanel(BeautiFrame frame) {
72 this.frame = frame;
73
74 predictorsTableModel = new PredictorsTableModel();
75 TableSorter sorter = new TableSorter(predictorsTableModel);
76 predictorsTable = new JTable(sorter);
77 sorter.setTableHeader(predictorsTable.getTableHeader());
78
79 predictorsTable.getTableHeader().setReorderingAllowed(false);
80 predictorsTable.getTableHeader().setResizingAllowed(false);
81 // predictorsTable.getTableHeader().setDefaultRenderer(
82 // new HeaderRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4)));
83
84 predictorsTable.getColumnModel().getColumn(0).setMinWidth(40);
85 predictorsTable.getColumnModel().getColumn(0).setMaxWidth(40);
86 predictorsTable.getColumnModel().getColumn(2).setMinWidth(40);
87 predictorsTable.getColumnModel().getColumn(2).setMaxWidth(40);
88 predictorsTable.getColumnModel().getColumn(3).setMinWidth(40);
89 predictorsTable.getColumnModel().getColumn(3).setMaxWidth(40);
90 predictorsTable.getColumnModel().getColumn(4).setMinWidth(40);
91 predictorsTable.getColumnModel().getColumn(4).setMaxWidth(40);
92 predictorsTable.getColumnModel().getColumn(5).setMinWidth(40);
93 predictorsTable.getColumnModel().getColumn(5).setMaxWidth(40);
94
95
96 TableEditorStopper.ensureEditingStopWhenTableLosesFocus(predictorsTable);
97
98 predictorsTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
99 predictorsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
100 public void valueChanged(ListSelectionEvent evt) {
101 updateButtons();
102 }
103 });
104
105 JScrollPane scrollPane1 = new JScrollPane(predictorsTable,
106 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
107 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
108 scrollPane1.setOpaque(false);
109
110 JToolBar toolBar1 = new JToolBar();
111 toolBar1.setFloatable(false);
112 toolBar1.setOpaque(false);
113 toolBar1.setBorder(BorderFactory.createEmptyBorder());
114
115 toolBar1.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
116
117 JButton button;
118
119 button = new JButton(importPredictorsAction);
120 PanelUtils.setupComponent(button);
121 button.setToolTipText(IMPORT_PREDICTORS_TOOLTIP);
122 toolBar1.add(button);
123
124 toolBar1.add(new JToolBar.Separator(new Dimension(12, 12)));
125
126 ActionPanel actionPanel1 = new ActionPanel(false);
127 actionPanel1.setAddAction(importPredictorsAction);
128 actionPanel1.setRemoveAction(removePredictorAction);
129 actionPanel1.setAddToolTipText(IMPORT_PREDICTORS_TOOLTIP);
130
131 importPredictorsAction.setEnabled(true);
132 removePredictorAction.setEnabled(false);
133
134 JPanel controlPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
135 controlPanel1.setOpaque(false);
136 controlPanel1.add(actionPanel1);
137
138 JPanel panel1 = new JPanel(new BorderLayout(0, 0));
139 panel1.setOpaque(false);
140 panel1.add(scrollPane1, BorderLayout.CENTER);
141 panel1.add(controlPanel1, BorderLayout.SOUTH);
142 // panel1.setMinimumSize(new Dimension(MINIMUM_TABLE_WIDTH, 0));
143
144 setOpaque(false);
145 setBorder(new BorderUIResource.EmptyBorderUIResource(new Insets(12, 12, 12, 12)));
146 setLayout(new BorderLayout(0, 0));
147 add(panel1, BorderLayout.CENTER);
148 add(toolBar1, BorderLayout.NORTH);
149 }
150
151 public void setOptions(BeautiOptions options) {
152 this.options = options;
153 }
154
155 public void getOptions(BeautiOptions options) {
156 }
157
158 public void setDialog(final JDialog dialog) {
159 this.dialog = dialog;
160 }
161
162 /**
163 * Set the trait to be controlled
164 *
165 * @param trait
166 */
167 public void setTrait(final TraitData trait) {
168 this.trait = trait;
169 // this.parameter = parameter;
170 //
171 // priorCombo = new JComboBox();
172 // for (PriorType priorType : PriorType.getPriorTypes(parameter)) {
173 // priorCombo.addItem(priorType);
174 // }
175 //
176 // if (parameter.priorType != null) {
177 // priorCombo.setSelectedItem(parameter.priorType);
178 // }
179 //
180 // setupComponents(); // setArguments here
181 //
182 //
183 // priorCombo.addItemListener(new ItemListener() {
184 // public void itemStateChanged(ItemEvent e) {
185 // setupComponents();
186 // dialog.pack();
187 // dialog.repaint();
188 // }
189 // });
190 //
191 // for (PriorOptionsPanel optionsPanel : optionsPanels.values()) {
192 // optionsPanel.removeAllListeners();
193 // optionsPanel.addListener(new PriorOptionsPanel.Listener() {
194 // public void optionsPanelChanged() {
195 // setupChart();
196 // dialog.pack();
197 // dialog.repaint();
198 // }
199 // });
200 // }
201 }
202
203 public void firePredictorsChanged() {
204 // if (currentTrait != null) {
205 //// if (currentTrait.getName().equalsIgnoreCase(TraitData.Traits.TRAIT_SPECIES.toString())) {
206 //// frame.setupStarBEAST();
207 //// } else
208 // if (currentTrait != null && currentTrait.getTraitType() == TraitData.TraitType.DISCRETE) {
209 // frame.updateDiscreteTraitAnalysis();
210 // }
211 //
212 //// if (selRow > 0) {
213 //// predictorsTable.getSelectionModel().setSelectionInterval(selRow-1, selRow-1);
214 //// } else if (selRow == 0 && options.traitsOptions.traits.size() > 0) { // options.traitsOptions.traits.size() after remove
215 //// predictorsTable.getSelectionModel().setSelectionInterval(0, 0);
216 //// }
217 //
218 // traitsTableModel.fireTableDataChanged();
219 // options.updatePartitionAllLinks();
220 // frame.setDirty();
221 // }
222 predictorsTableModel.fireTableDataChanged();
223 }
224
225 private void updateButtons() {
226 removePredictorAction.setEnabled(predictorsTable.getSelectedRows().length > 0);
227 }
228
229 private boolean importPredictors() {
230 File[] files = frame.selectImportFiles("Import Predictors File...", false, new FileNameExtensionFilter[] {
231 new FileNameExtensionFilter("Tab-delimited text files", "txt", "tab", "dat") });
232
233 if (files != null && files.length != 0) {
234 try {
235 BEAUTiImporter beautiImporter = new BEAUTiImporter(frame, options);
236 beautiImporter.importPredictors(files[0], trait);
237 } catch (FileNotFoundException fnfe) {
238 JOptionPane.showMessageDialog(this, "Unable to open file: File not found",
239 "Unable to open file",
240 JOptionPane.ERROR_MESSAGE);
241 return false;
242 } catch (IOException ioe) {
243 JOptionPane.showMessageDialog(this, "Unable to read file: " + ioe.getMessage(),
244 "Unable to read file",
245 JOptionPane.ERROR_MESSAGE);
246 return false;
247 } catch (Exception ex) {
248 ex.printStackTrace(System.err);
249 JOptionPane.showMessageDialog(this, "Fatal exception: " + ex,
250 "Error reading file",
251 JOptionPane.ERROR_MESSAGE);
252 ex.printStackTrace();
253 return false;
254 }
255 } else {
256 return false;
257 }
258
259
260 firePredictorsChanged();
261 // setAllOptions();
262
263 return true;
264 }
265
266 private void removePredictor() {
267 int[] rows = predictorsTable.getSelectedRows();
268 // get the list of predictors before removing them
269 java.util.List<Predictor> predictorList = new ArrayList<Predictor>();
270 for (int row: rows) {
271 predictorList.add(trait.getPredictors().get(row));
272 }
273 // or the indices will be wrong
274 for (Predictor predictor : predictorList) {
275 trait.removePredictor(predictor);
276 }
277 firePredictorsChanged();
278 }
279
280 AbstractAction importPredictorsAction = new AbstractAction("Import Predictors...") {
281 public void actionPerformed(ActionEvent ae) {
282 importPredictors();
283 }
284 };
285
286 AbstractAction removePredictorAction = new AbstractAction() {
287 public void actionPerformed(ActionEvent ae) {
288 removePredictor();
289 }
290 };
291
292 class PredictorsTableModel extends AbstractTableModel {
293
294 private static final long serialVersionUID = -6707994233020715574L;
295 String[] columnNames = {"Include", "Predictor", "Log", "Std", "Origin", "Dest"};
296
297 public PredictorsTableModel() {
298 }
299
300 public int getColumnCount() {
301 return columnNames.length;
302 }
303
304 public int getRowCount() {
305 return (trait == null ? 0 : trait.getPredictors().size());
306 }
307
308 public Object getValueAt(int row, int col) {
309 Predictor predictor = trait.getPredictors().get(row);
310 switch (col) {
311 case 0:
312 return predictor.isIncluded();
313 case 1:
314 return predictor.getName();
315 case 2:
316 return predictor.isLogged();
317 case 3:
318 return predictor.isStandardized();
319 case 4:
320 return predictor.isOrigin();
321 case 5:
322 return predictor.isDestination();
323 }
324 return null;
325 }
326
327 public void setValueAt(Object aValue, int row, int col) {
328 Predictor predictor = trait.getPredictors().get(row);
329 switch (col) {
330 case 0:
331 predictor.setIncluded((Boolean)aValue); break;
332 case 1:
333 predictor.setName((String)aValue); break;
334 case 2:
335 predictor.setLogged((Boolean)aValue); break;
336 case 3:
337 predictor.setStandardized((Boolean)aValue); break;
338 case 4:
339 predictor.setOrigin((Boolean)aValue); break;
340 case 5:
341 predictor.setDestination((Boolean)aValue); break;
342 }
343 }
344
345 public boolean isCellEditable(int row, int col) {
346 Predictor predictor = trait.getPredictors().get(row);
347 if (predictor.getType() == Predictor.Type.MATRIX) {
348 return (col < 4);
349 }
350 return true;
351 }
352
353 public String getColumnName(int column) {
354 return columnNames[column];
355 }
356
357 public Class getColumnClass(int c) {
358 return getValueAt(0, c).getClass();
359 }
360
361 public String toString() {
362 StringBuffer buffer = new StringBuffer();
363
364 buffer.append(getColumnName(0));
365 for (int j = 1; j < getColumnCount(); j++) {
366 buffer.append("\t");
367 buffer.append(getColumnName(j));
368 }
369 buffer.append("\n");
370
371 for (int i = 0; i < getRowCount(); i++) {
372 buffer.append(getValueAt(i, 0));
373 for (int j = 1; j < getColumnCount(); j++) {
374 buffer.append("\t");
375 buffer.append(getValueAt(i, j));
376 }
377 buffer.append("\n");
378 }
379
380 return buffer.toString();
381 }
382 }
383
384 }