| 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.treespace.inputpanel; |
| 27 |
|
| 28 |
import dr.app.gui.DeleteActionResponder; |
| 29 |
import dr.app.gui.table.MultiLineTableCellRenderer; |
| 30 |
import dr.app.gui.table.TableEditorStopper; |
| 31 |
import jam.framework.Exportable; |
| 32 |
import jam.panels.ActionPanel; |
| 33 |
|
| 34 |
import javax.swing.*; |
| 35 |
import javax.swing.event.ListSelectionEvent; |
| 36 |
import javax.swing.event.ListSelectionListener; |
| 37 |
import javax.swing.plaf.BorderUIResource; |
| 38 |
import javax.swing.table.AbstractTableModel; |
| 39 |
import javax.swing.table.TableColumn; |
| 40 |
import java.awt.*; |
| 41 |
import java.awt.datatransfer.Transferable; |
| 42 |
import java.awt.datatransfer.DataFlavor; |
| 43 |
import java.awt.datatransfer.UnsupportedFlavorException; |
| 44 |
import java.awt.event.*; |
| 45 |
import java.util.HashSet; |
| 46 |
import java.util.Set; |
| 47 |
import java.util.Iterator; |
| 48 |
import java.io.File; |
| 49 |
import java.io.IOException; |
| 50 |
|
| 51 |
import dr.evolution.io.Importer; |
| 52 |
import dr.app.treespace.*; |
| 53 |
import jebl.evolution.io.ImportException; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public class InputPanel extends JPanel implements DeleteActionResponder, Exportable { |
| 60 |
private JScrollPane scrollPane = new JScrollPane(); |
| 61 |
private JTable dataTable = null; |
| 62 |
private DataTableModel dataTableModel = null; |
| 63 |
|
| 64 |
private TreeSpaceFrame frame = null; |
| 65 |
|
| 66 |
private InputFileSettingsDialog inputFileSettingsDialog = null; |
| 67 |
private final TreeSpaceDocument document; |
| 68 |
|
| 69 |
public InputPanel(final TreeSpaceFrame parent, final TreeSpaceDocument document, final Action addDataAction) { |
| 70 |
|
| 71 |
this.frame = parent; |
| 72 |
this.document = document; |
| 73 |
|
| 74 |
dataTableModel = new DataTableModel(); |
| 75 |
dataTable = new JTable(dataTableModel); |
| 76 |
|
| 77 |
dataTable.getTableHeader().setReorderingAllowed(false); |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
TableColumn col = dataTable.getColumnModel().getColumn(0); |
| 82 |
col.setCellRenderer(new MultiLineTableCellRenderer()); |
| 83 |
|
| 84 |
dataTable.setRowHeight(dataTable.getRowHeight() * 2); |
| 85 |
|
| 86 |
dataTable.setDragEnabled(false); |
| 87 |
dataTable.setTransferHandler(new FSTransfer()); |
| 88 |
|
| 89 |
TableEditorStopper.ensureEditingStopWhenTableLosesFocus(dataTable); |
| 90 |
|
| 91 |
dataTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
| 92 |
public void valueChanged(ListSelectionEvent evt) { |
| 93 |
selectionChanged(); |
| 94 |
} |
| 95 |
}); |
| 96 |
|
| 97 |
dataTable.addMouseListener(new MouseAdapter() { |
| 98 |
public void mouseClicked(MouseEvent e) { |
| 99 |
if (e.getClickCount() == 2) { |
| 100 |
editSelection(); |
| 101 |
} |
| 102 |
} |
| 103 |
}); |
| 104 |
|
| 105 |
scrollPane = new JScrollPane(dataTable, |
| 106 |
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, |
| 107 |
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); |
| 108 |
scrollPane.setOpaque(false); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
ActionPanel actionPanel1 = new ActionPanel(true); |
| 122 |
actionPanel1.setAddAction(addDataAction); |
| 123 |
actionPanel1.setRemoveAction(removeAction); |
| 124 |
|
| 125 |
removeAction.setEnabled(false); |
| 126 |
|
| 127 |
JPanel controlPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT)); |
| 128 |
controlPanel1.setOpaque(false); |
| 129 |
controlPanel1.add(actionPanel1); |
| 130 |
|
| 131 |
setOpaque(false); |
| 132 |
setBorder(new BorderUIResource.EmptyBorderUIResource(new Insets(12, 12, 12, 12))); |
| 133 |
setLayout(new BorderLayout(0, 0)); |
| 134 |
|
| 135 |
add(scrollPane, BorderLayout.CENTER); |
| 136 |
add(controlPanel1, BorderLayout.SOUTH); |
| 137 |
|
| 138 |
document.addListener(new TreeSpaceDocument.Listener() { |
| 139 |
public void dataChanged() { |
| 140 |
dataTableModel.fireTableDataChanged(); |
| 141 |
} |
| 142 |
|
| 143 |
public void settingsChanged() { } |
| 144 |
}); |
| 145 |
} |
| 146 |
|
| 147 |
public void selectionChanged() { |
| 148 |
int[] selRows = dataTable.getSelectedRows(); |
| 149 |
boolean hasSelection = (selRows != null && selRows.length != 0); |
| 150 |
removeAction.setEnabled(hasSelection); |
| 151 |
|
| 152 |
frame.setRemoveActionEnabled(this, hasSelection); |
| 153 |
} |
| 154 |
|
| 155 |
public JComponent getExportableComponent() { |
| 156 |
return dataTable; |
| 157 |
} |
| 158 |
|
| 159 |
public void editSelection() { |
| 160 |
int selRow = dataTable.getSelectedRow(); |
| 161 |
if (selRow >= 0) { |
| 162 |
InputFile inputFile = document.getInputFiles().get(selRow); |
| 163 |
editSettings(inputFile); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
private void editSettings(InputFile inputFile) { |
| 168 |
if (inputFileSettingsDialog == null) { |
| 169 |
inputFileSettingsDialog = new InputFileSettingsDialog(frame); |
| 170 |
} |
| 171 |
|
| 172 |
int result = inputFileSettingsDialog.showDialog(inputFile); |
| 173 |
|
| 174 |
if (result != JOptionPane.CANCEL_OPTION) { |
| 175 |
inputFileSettingsDialog.getInputFile(); |
| 176 |
document.fireDataChanged(); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
public void delete() { |
| 181 |
int[] selRows = dataTable.getSelectedRows(); |
| 182 |
Set<InputFile> dataToRemove = new HashSet<InputFile>(); |
| 183 |
for (int row : selRows) { |
| 184 |
dataToRemove.add(document.getInputFiles().get(row)); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
document.getInputFiles().removeAll(dataToRemove); |
| 189 |
document.fireDataChanged(); |
| 190 |
|
| 191 |
if (document.getInputFiles().size() == 0) { |
| 192 |
|
| 193 |
frame.setStatusMessage("No data loaded"); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
public void selectAll() { |
| 198 |
dataTable.selectAll(); |
| 199 |
} |
| 200 |
|
| 201 |
class DataTableModel extends AbstractTableModel { |
| 202 |
|
| 203 |
private static final long serialVersionUID = -6707994233020715574L; |
| 204 |
String[] columnNames = {"Input Files"}; |
| 205 |
|
| 206 |
public DataTableModel() { |
| 207 |
} |
| 208 |
|
| 209 |
public int getColumnCount() { |
| 210 |
return columnNames.length; |
| 211 |
} |
| 212 |
|
| 213 |
public int getRowCount() { |
| 214 |
return document.getInputFiles().size(); |
| 215 |
} |
| 216 |
|
| 217 |
public Object getValueAt(int row, int col) { |
| 218 |
InputFile inputFile = document.getInputFiles().get(row); |
| 219 |
switch (col) { |
| 220 |
case 0: |
| 221 |
return inputFile; |
| 222 |
default: |
| 223 |
throw new IllegalArgumentException("unknown column, " + col); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
public boolean isCellEditable(int row, int col) { |
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
public String getColumnName(int column) { |
| 232 |
return columnNames[column]; |
| 233 |
} |
| 234 |
|
| 235 |
public Class getColumnClass(int c) { |
| 236 |
if (getRowCount() == 0) { |
| 237 |
return Object.class; |
| 238 |
} |
| 239 |
return getValueAt(0, c).getClass(); |
| 240 |
} |
| 241 |
|
| 242 |
public String toString() { |
| 243 |
StringBuffer buffer = new StringBuffer(); |
| 244 |
|
| 245 |
buffer.append(getColumnName(0)); |
| 246 |
for (int j = 1; j < getColumnCount(); j++) { |
| 247 |
buffer.append("\t"); |
| 248 |
buffer.append(getColumnName(j)); |
| 249 |
} |
| 250 |
buffer.append("\n"); |
| 251 |
|
| 252 |
for (int i = 0; i < getRowCount(); i++) { |
| 253 |
buffer.append(getValueAt(i, 0)); |
| 254 |
for (int j = 1; j < getColumnCount(); j++) { |
| 255 |
buffer.append("\t"); |
| 256 |
buffer.append(getValueAt(i, j)); |
| 257 |
} |
| 258 |
buffer.append("\n"); |
| 259 |
} |
| 260 |
|
| 261 |
return buffer.toString(); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
public class FSTransfer extends TransferHandler { |
| 266 |
public boolean importData(JComponent comp, Transferable t) { |
| 267 |
|
| 268 |
if (!(comp instanceof JTable)) { |
| 269 |
return false; |
| 270 |
} |
| 271 |
if (!t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { |
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
try { |
| 276 |
java.util.List data = (java.util.List) t.getTransferData(DataFlavor.javaFileListFlavor); |
| 277 |
Iterator i = data.iterator(); |
| 278 |
while (i.hasNext()) { |
| 279 |
File f = (File) i.next(); |
| 280 |
frame.importDataFile(f); |
| 281 |
} |
| 282 |
return true; |
| 283 |
} catch (UnsupportedFlavorException ufe) { |
| 284 |
System.err.println("Ack! we should not be here.\nBad Flavor."); |
| 285 |
} catch (IOException ioe) { |
| 286 |
System.out.println("Something failed during import:\n" + ioe); |
| 287 |
} catch (ImportException e) { |
| 288 |
e.printStackTrace(); |
| 289 |
} |
| 290 |
return false; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) { |
| 295 |
if (comp instanceof JTable) { |
| 296 |
for (int i = 0; i < transferFlavors.length; i++) { |
| 297 |
if (!transferFlavors[i].equals(DataFlavor.javaFileListFlavor)) { |
| 298 |
return false; |
| 299 |
} |
| 300 |
} |
| 301 |
return true; |
| 302 |
} |
| 303 |
return false; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
public Action getDeleteAction() { |
| 308 |
return removeAction; |
| 309 |
} |
| 310 |
|
| 311 |
private Action removeAction = new RemoveDataAction(); |
| 312 |
private class RemoveDataAction extends AbstractAction { |
| 313 |
public RemoveDataAction() { |
| 314 |
super("Remove"); |
| 315 |
setToolTipText("Use this button to remove selected input data from the table"); |
| 316 |
} |
| 317 |
|
| 318 |
public void actionPerformed(ActionEvent ae) { |
| 319 |
delete(); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
} |