| 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.tools; |
| 27 |
|
| 28 |
import dr.app.gui.FileDrop; |
| 29 |
import dr.app.util.Utils; |
| 30 |
import dr.app.gui.table.TableEditorStopper; |
| 31 |
import dr.app.gui.components.WholeNumberField; |
| 32 |
import jam.panels.ActionPanel; |
| 33 |
import jam.panels.OptionsPanel; |
| 34 |
import jam.table.TableRenderer; |
| 35 |
|
| 36 |
import javax.swing.*; |
| 37 |
import javax.swing.border.Border; |
| 38 |
import javax.swing.border.EmptyBorder; |
| 39 |
import javax.swing.event.ListSelectionEvent; |
| 40 |
import javax.swing.event.ListSelectionListener; |
| 41 |
import javax.swing.table.AbstractTableModel; |
| 42 |
import java.awt.*; |
| 43 |
import java.awt.event.ActionEvent; |
| 44 |
import java.awt.event.ActionListener; |
| 45 |
import java.io.File; |
| 46 |
import java.util.ArrayList; |
| 47 |
import java.util.List; |
| 48 |
|
| 49 |
public class LogCombinerDialog { |
| 50 |
private final JFrame frame; |
| 51 |
|
| 52 |
private final OptionsPanel optionPanel; |
| 53 |
|
| 54 |
private JTable filesTable = null; |
| 55 |
private FilesTableModel filesTableModel = null; |
| 56 |
|
| 57 |
private final JComboBox fileTypeCombo = new JComboBox(new String[] { "Log Files", "Tree Files" }); |
| 58 |
private final JCheckBox decimalCheck = new JCheckBox("Convert numbers from scientific to decimal notation"); |
| 59 |
private final JCheckBox renumberOutput = new JCheckBox("Renumber output states"); |
| 60 |
private final JCheckBox resampleCheck = new JCheckBox("Resample states at lower frequency: "); |
| 61 |
private final WholeNumberField resampleText = new WholeNumberField(0, Integer.MAX_VALUE); |
| 62 |
|
| 63 |
private final List<FileInfo> files = new ArrayList<FileInfo>(); |
| 64 |
|
| 65 |
private final JTextField fileNameText = new JTextField("not selected", 16); |
| 66 |
private File outputFile = null; |
| 67 |
|
| 68 |
public LogCombinerDialog(final JFrame frame) { |
| 69 |
this.frame = frame; |
| 70 |
|
| 71 |
optionPanel = new OptionsPanel(12, 12); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
JPanel panel = new JPanel(new BorderLayout()); |
| 76 |
panel.setOpaque(false); |
| 77 |
|
| 78 |
|
| 79 |
filesTableModel = new FilesTableModel(); |
| 80 |
filesTable = new JTable(filesTableModel); |
| 81 |
|
| 82 |
filesTable.getColumnModel().getColumn(0).setCellRenderer( |
| 83 |
new TableRenderer(SwingConstants.LEFT, new Insets(0, 4, 0, 4))); |
| 84 |
filesTable.getColumnModel().getColumn(0).setPreferredWidth(120); |
| 85 |
filesTable.getColumnModel().getColumn(0).setPreferredWidth(80); |
| 86 |
|
| 87 |
TableEditorStopper.ensureEditingStopWhenTableLosesFocus(filesTable); |
| 88 |
|
| 89 |
filesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
| 90 |
public void valueChanged(ListSelectionEvent evt) { filesTableSelectionChanged(); } |
| 91 |
}); |
| 92 |
|
| 93 |
JScrollPane scrollPane1 = new JScrollPane(filesTable, |
| 94 |
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, |
| 95 |
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
| 96 |
|
| 97 |
ActionPanel actionPanel1 = new ActionPanel(false); |
| 98 |
actionPanel1.setAddAction(addFileAction); |
| 99 |
actionPanel1.setRemoveAction(removeFileAction); |
| 100 |
removeFileAction.setEnabled(false); |
| 101 |
|
| 102 |
JPanel controlPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT)); |
| 103 |
controlPanel1.add(actionPanel1); |
| 104 |
|
| 105 |
panel.setBorder(BorderFactory.createEmptyBorder(6,6,6,6)); |
| 106 |
panel.add(new JLabel("Select input files:"), BorderLayout.NORTH); |
| 107 |
panel.add(scrollPane1, BorderLayout.CENTER); |
| 108 |
panel.add(actionPanel1, BorderLayout.SOUTH); |
| 109 |
|
| 110 |
Color focusColor = UIManager.getColor("Focus.color"); |
| 111 |
Border focusBorder = BorderFactory.createMatteBorder( 2, 2, 2, 2, focusColor ); |
| 112 |
new FileDrop( null, scrollPane1, focusBorder, new FileDrop.Listener() |
| 113 |
{ public void filesDropped( java.io.File[] files ) |
| 114 |
{ |
| 115 |
addFiles(files); |
| 116 |
} |
| 117 |
}); |
| 118 |
|
| 119 |
resampleText.setEnabled(false); |
| 120 |
resampleText.setColumns(12); |
| 121 |
resampleCheck.addActionListener(new ActionListener() { |
| 122 |
public void actionPerformed(ActionEvent e) { |
| 123 |
resampleText.setEnabled(resampleCheck.isSelected()); |
| 124 |
} |
| 125 |
}); |
| 126 |
|
| 127 |
ActionListener buttonListener = new ActionListener() { |
| 128 |
public void actionPerformed(ActionEvent ae) { |
| 129 |
FileDialog dialog = new FileDialog(frame, |
| 130 |
"Select output file...", |
| 131 |
FileDialog.SAVE); |
| 132 |
|
| 133 |
dialog.setVisible(true); |
| 134 |
if( dialog.getFile() == null ) { |
| 135 |
|
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
outputFile = new File(dialog.getDirectory(), dialog.getFile()); |
| 140 |
fileNameText.setText(outputFile.getName()); |
| 141 |
|
| 142 |
} |
| 143 |
}; |
| 144 |
|
| 145 |
JButton button = new JButton("Choose File..."); |
| 146 |
button.addActionListener(buttonListener); |
| 147 |
|
| 148 |
JPanel panel2 = new JPanel(new BorderLayout(0,0)); |
| 149 |
panel2.add(resampleCheck, BorderLayout.CENTER); |
| 150 |
panel2.add(resampleText, BorderLayout.EAST); |
| 151 |
optionPanel.addComponentWithLabel("File type: ", fileTypeCombo); |
| 152 |
optionPanel.addComponent(decimalCheck); |
| 153 |
optionPanel.addComponent(renumberOutput); |
| 154 |
optionPanel.addComponent(panel2); |
| 155 |
|
| 156 |
optionPanel.addSpanningComponent(panel); |
| 157 |
|
| 158 |
fileNameText.setEditable(false); |
| 159 |
|
| 160 |
JPanel panel3 = new JPanel(new BorderLayout(0,0)); |
| 161 |
panel3.add(fileNameText, BorderLayout.CENTER); |
| 162 |
panel3.add(button, BorderLayout.EAST); |
| 163 |
optionPanel.addComponentWithLabel("Output File: ", panel3); |
| 164 |
} |
| 165 |
|
| 166 |
public boolean showDialog(String title) { |
| 167 |
|
| 168 |
addFileAction.setEnabled(true); |
| 169 |
removeFileAction.setEnabled(false); |
| 170 |
|
| 171 |
filesTableModel.fireTableDataChanged(); |
| 172 |
|
| 173 |
JOptionPane optionPane = new JOptionPane(optionPanel, |
| 174 |
JOptionPane.PLAIN_MESSAGE, |
| 175 |
JOptionPane.OK_CANCEL_OPTION, |
| 176 |
null, |
| 177 |
new String[] { "Run", "Quit" }, |
| 178 |
null); |
| 179 |
optionPane.setBorder(new EmptyBorder(12, 12, 12, 12)); |
| 180 |
|
| 181 |
final JDialog dialog = optionPane.createDialog(frame, title); |
| 182 |
|
| 183 |
dialog.pack(); |
| 184 |
|
| 185 |
dialog.setVisible(true); |
| 186 |
|
| 187 |
return optionPane.getValue().equals("Run"); |
| 188 |
} |
| 189 |
|
| 190 |
public String[] getFileNames() { |
| 191 |
String[] fileArray = new String[files.size()]; |
| 192 |
for (int i = 0; i < files.size(); i++) { |
| 193 |
FileInfo fileInfo = files.get(i); |
| 194 |
fileArray[i] = fileInfo.file.getPath(); |
| 195 |
} |
| 196 |
return fileArray; |
| 197 |
} |
| 198 |
|
| 199 |
public long[] getBurnins() { |
| 200 |
long[] burnins = new long[files.size()]; |
| 201 |
for (int i = 0; i < files.size(); i++) { |
| 202 |
FileInfo fileInfo = files.get(i); |
| 203 |
burnins[i] = fileInfo.burnin; |
| 204 |
} |
| 205 |
return burnins; |
| 206 |
} |
| 207 |
|
| 208 |
public boolean isTreeFiles() { |
| 209 |
return fileTypeCombo.getSelectedIndex() == 1; |
| 210 |
} |
| 211 |
|
| 212 |
public boolean convertToDecimal() { |
| 213 |
return decimalCheck.isSelected(); |
| 214 |
} |
| 215 |
|
| 216 |
public boolean renumberOutputStates() { |
| 217 |
return renumberOutput.isSelected(); |
| 218 |
} |
| 219 |
|
| 220 |
public boolean isResampling() { |
| 221 |
return resampleCheck.isSelected(); |
| 222 |
} |
| 223 |
|
| 224 |
public long getResampleFrequency() { |
| 225 |
return resampleText.getValue(); |
| 226 |
} |
| 227 |
|
| 228 |
public String getOutputFileName() { |
| 229 |
if (outputFile == null) return null; |
| 230 |
return outputFile.getPath(); |
| 231 |
} |
| 232 |
|
| 233 |
private void filesTableSelectionChanged() { |
| 234 |
if (filesTable.getSelectedRowCount() == 0) { |
| 235 |
removeFileAction.setEnabled(false); |
| 236 |
} else { |
| 237 |
removeFileAction.setEnabled(true); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
private void addFiles(File[] fileArray) { |
| 242 |
int sel1 = files.size(); |
| 243 |
for (File file : fileArray) { |
| 244 |
FileInfo fileInfo = new FileInfo(); |
| 245 |
fileInfo.file = file; |
| 246 |
fileInfo.burnin = 0; |
| 247 |
|
| 248 |
files.add(fileInfo); |
| 249 |
} |
| 250 |
|
| 251 |
filesTableModel.fireTableDataChanged(); |
| 252 |
|
| 253 |
int sel2 = files.size() - 1; |
| 254 |
filesTable.setRowSelectionInterval(sel1, sel2); |
| 255 |
} |
| 256 |
|
| 257 |
Action addFileAction = new AbstractAction("+") { |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
private static final long serialVersionUID = 7602227478402204088L; |
| 263 |
|
| 264 |
public void actionPerformed(ActionEvent ae) { |
| 265 |
|
| 266 |
File file = Utils.getLoadFile("Select log file"); |
| 267 |
if (file != null) { |
| 268 |
addFiles(new File[] { file }); |
| 269 |
} |
| 270 |
} |
| 271 |
}; |
| 272 |
|
| 273 |
Action removeFileAction = new AbstractAction("-") { |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
private static final long serialVersionUID = 5934278375005327047L; |
| 279 |
|
| 280 |
public void actionPerformed(ActionEvent ae) { |
| 281 |
int row = filesTable.getSelectedRow(); |
| 282 |
if (row != -1) { |
| 283 |
files.remove(row); |
| 284 |
} |
| 285 |
|
| 286 |
filesTableModel.fireTableDataChanged(); |
| 287 |
|
| 288 |
if (row >= files.size()) row = files.size() - 1; |
| 289 |
if (row >= 0) { |
| 290 |
filesTable.setRowSelectionInterval(row, row); |
| 291 |
} |
| 292 |
} |
| 293 |
}; |
| 294 |
|
| 295 |
|
| 296 |
class FilesTableModel extends AbstractTableModel { |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
private static final long serialVersionUID = 4153326364833213013L; |
| 301 |
private final String[] columns = { "File", "Burnin" }; |
| 302 |
|
| 303 |
public FilesTableModel() { |
| 304 |
} |
| 305 |
|
| 306 |
public int getColumnCount() { |
| 307 |
return columns.length; |
| 308 |
} |
| 309 |
|
| 310 |
public int getRowCount() { |
| 311 |
return files.size(); |
| 312 |
} |
| 313 |
|
| 314 |
public Object getValueAt(int rowIndex, int columnIndex) { |
| 315 |
FileInfo fileInfo = files.get(rowIndex); |
| 316 |
if (columnIndex == 0) { |
| 317 |
return fileInfo.file.getName(); |
| 318 |
} else { |
| 319 |
return fileInfo.burnin; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
| 324 |
return (columnIndex == 1); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
| 336 |
FileInfo fileInfo = files.get(rowIndex); |
| 337 |
if (columnIndex == 1) { |
| 338 |
fileInfo.burnin = (Integer)aValue; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
public String getColumnName(int columnIndex) { |
| 343 |
return columns[columnIndex]; |
| 344 |
} |
| 345 |
|
| 346 |
public Class getColumnClass(int columnIndex) {return getValueAt(0, columnIndex).getClass();} |
| 347 |
} |
| 348 |
|
| 349 |
class FileInfo { |
| 350 |
File file; |
| 351 |
Integer burnin; |
| 352 |
} |
| 353 |
} |