| 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.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 |
|
| 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 |
|
| 82 |
|
| 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 |
|
| 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 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public void setTrait(final TraitData trait) { |
| 168 |
this.trait = trait; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
public void firePredictorsChanged() { |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 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 |
|
| 262 |
|
| 263 |
return true; |
| 264 |
} |
| 265 |
|
| 266 |
private void removePredictor() { |
| 267 |
int[] rows = predictorsTable.getSelectedRows(); |
| 268 |
|
| 269 |
java.util.List<Predictor> predictorList = new ArrayList<Predictor>(); |
| 270 |
for (int row: rows) { |
| 271 |
predictorList.add(trait.getPredictors().get(row)); |
| 272 |
} |
| 273 |
|
| 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 |
} |