use of java.awt.datatransfer.UnsupportedFlavorException in project java-swing-tips by aterai.
the class FileDropListener method drop.
@Override
public void drop(DropTargetDropEvent e) {
try {
if (e.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
e.acceptDrop(DnDConstants.ACTION_COPY);
Transferable t = e.getTransferable();
List<?> list = (List<?>) t.getTransferData(DataFlavor.javaFileListFlavor);
for (Object o : list) {
if (o instanceof File) {
File f = (File) o;
System.out.println(f.getAbsolutePath());
}
}
e.dropComplete(true);
} else {
e.rejectDrop();
}
} catch (UnsupportedFlavorException | IOException ex) {
e.rejectDrop();
}
}
use of java.awt.datatransfer.UnsupportedFlavorException in project java-swing-tips by aterai.
the class TreeTransferHandler method createTransferable.
@Override
protected Transferable createTransferable(JComponent c) {
c.getRootPane().getGlassPane().setVisible(true);
JTable table = (JTable) c;
DefaultTableModel model = (DefaultTableModel) table.getModel();
for (int i : table.getSelectedRows()) {
indices.add(i);
}
return new Transferable() {
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { FLAVOR };
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return Objects.equals(FLAVOR, flavor);
}
@SuppressWarnings("JdkObsolete")
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (isDataFlavorSupported(flavor)) {
return indices.stream().map(model.getDataVector()::get).collect(Collectors.toList());
} else {
throw new UnsupportedFlavorException(flavor);
}
}
};
}
use of java.awt.datatransfer.UnsupportedFlavorException in project java-swing-tips by aterai.
the class TableRowTransferHandler method importData.
@Override
public boolean importData(TransferHandler.TransferSupport info) {
TransferHandler.DropLocation tdl = info.getDropLocation();
if (!(tdl instanceof JTable.DropLocation)) {
return false;
}
JTable.DropLocation dl = (JTable.DropLocation) tdl;
JTable target = (JTable) info.getComponent();
DefaultTableModel model = (DefaultTableModel) target.getModel();
// boolean insert = dl.isInsert();
int max = model.getRowCount();
int index = dl.getRow();
// index = index < 0 ? max : index; // If it is out of range, it is appended to the end
// index = Math.min(index, max);
index = index >= 0 && index < max ? index : max;
addIndex = index;
// target.setCursor(Cursor.getDefaultCursor());
try {
List<?> values = (List<?>) info.getTransferable().getTransferData(FLAVOR);
if (Objects.equals(source, target)) {
addCount = values.size();
}
Object[] type = new Object[0];
for (Object o : values) {
int row = index++;
// model.insertRow(row, (Vector<?>) o);
model.insertRow(row, ((List<?>) o).toArray(type));
target.getSelectionModel().addSelectionInterval(row, row);
}
return true;
} catch (UnsupportedFlavorException | IOException ex) {
return false;
}
}
use of java.awt.datatransfer.UnsupportedFlavorException in project java-swing-tips by aterai.
the class BasicTransferable method createInputStream.
private InputStream createInputStream(DataFlavor flavor, String data) throws IOException, UnsupportedFlavorException {
String s = getTextCharset(flavor);
String cs = Optional.ofNullable(s).orElseThrow(() -> new UnsupportedFlavorException(flavor));
return new ByteArrayInputStream(data.getBytes(cs));
}
use of java.awt.datatransfer.UnsupportedFlavorException in project jmeter by apache.
the class HeaderPanel method addFromClipboard.
/**
* Add values from the clipboard.
* The clipboard is first split into lines, and the lines are then split on ':' or '\t'
* to produce the header name and value.
* Lines without a ':' are tested with '\t' and ignored if not found.
*/
protected void addFromClipboard() {
GuiUtils.stopTableEditing(this.headerTable);
int rowCount = headerTable.getRowCount();
try {
String clipboardContent = GuiUtils.getPastedText();
if (clipboardContent == null) {
return;
}
// $NON-NLS-1$
String[] clipboardLines = clipboardContent.split(CLIPBOARD_LINE_DELIMITER);
for (String clipboardLine : clipboardLines) {
// $NON-NLS-1$
int index = clipboardLine.indexOf(CLIPBOARD_COLON_DELIMITER);
if (index < 0) {
// when pasting from another header panel the values are separated with '\t'
index = clipboardLine.indexOf(CLIPBOARD_TAB_DELIMITER);
}
if (index > 0) {
Header header = new Header(clipboardLine.substring(0, index).trim(), clipboardLine.substring(index + 1).trim());
headerManager.add(header);
}
}
tableModel.fireTableDataChanged();
if (headerTable.getRowCount() > rowCount) {
// Highlight (select) the appropriate rows.
int rowToSelect = tableModel.getRowCount() - 1;
headerTable.setRowSelectionInterval(rowCount, rowToSelect);
}
checkButtonsStatus();
} catch (IOException ioe) {
JOptionPane.showMessageDialog(this, "Could not add read headers from clipboard:\n" + ioe.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
} catch (UnsupportedFlavorException ufe) {
JOptionPane.showMessageDialog(this, "Could not add retrieved " + DataFlavor.stringFlavor.getHumanPresentableName() + " from clipboard" + ufe.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
Aggregations