use of java.awt.datatransfer.DataFlavor in project jabref by JabRef.
the class FileListEditorTransferHandler method importData.
@Override
public boolean importData(JComponent comp, Transferable t) {
try {
List<Path> files = new ArrayList<>();
// This flavor is used for dragged file links in Windows:
if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
@SuppressWarnings("unchecked") List<Path> transferedFiles = (List<Path>) t.getTransferData(DataFlavor.javaFileListFlavor);
files.addAll(transferedFiles);
}
if (t.isDataFlavorSupported(urlFlavor)) {
URL dropLink = (URL) t.getTransferData(urlFlavor);
LOGGER.debug("URL: " + dropLink);
}
// under Gnome. The data consists of the file paths, one file per line:
if (t.isDataFlavorSupported(stringFlavor)) {
String dropStr = (String) t.getTransferData(stringFlavor);
files.addAll(EntryTableTransferHandler.getFilesFromDraggedFilesString(dropStr));
}
SwingUtilities.invokeLater(() -> {
for (Path file : files) {
// Find the file's extension, if any:
String name = file.toAbsolutePath().toString();
FileHelper.getFileExtension(name).ifPresent(extension -> ExternalFileTypes.getInstance().getExternalFileTypeByExt(extension).ifPresent(fileType -> {
if (droppedFileHandler == null) {
droppedFileHandler = new DroppedFileHandler(frame, frame.getCurrentBasePanel());
}
droppedFileHandler.handleDroppedfile(name, fileType, entryContainer.getEntry());
}));
}
});
if (!files.isEmpty()) {
// Found some files, return
return true;
}
} catch (IOException ioe) {
LOGGER.warn("Failed to read dropped data. ", ioe);
} catch (UnsupportedFlavorException | ClassCastException ufe) {
LOGGER.warn("Drop type error. ", ufe);
}
// all supported flavors failed
StringBuilder logMessage = new StringBuilder("Cannot transfer input:");
DataFlavor[] inflavs = t.getTransferDataFlavors();
for (DataFlavor inflav : inflavs) {
logMessage.append(' ').append(inflav);
}
LOGGER.warn(logMessage.toString());
return false;
}
use of java.awt.datatransfer.DataFlavor in project JMRI by JMRI.
the class ControlPanelEditor method drop.
@SuppressWarnings("unchecked")
@Override
public void drop(DropTargetDropEvent evt) {
try {
//Point pt = evt.getLocation(); coords relative to entire window
Point pt = _targetPanel.getMousePosition(true);
Transferable tr = evt.getTransferable();
if (log.isDebugEnabled()) {
// avoid string building if not debug
DataFlavor[] flavors = tr.getTransferDataFlavors();
StringBuilder flavor = new StringBuilder();
for (DataFlavor flavor1 : flavors) {
flavor.append(flavor1.getRepresentationClass().getName()).append(", ");
}
log.debug("Editor Drop: flavor classes={}", flavor);
}
if (tr.isDataFlavorSupported(_positionableDataFlavor)) {
Positionable item = (Positionable) tr.getTransferData(_positionableDataFlavor);
if (item == null) {
return;
}
item.setLocation(pt.x, pt.y);
// now set display level in the pane.
item.setDisplayLevel(item.getDisplayLevel());
item.setEditor(this);
putItem(item);
item.updateSize();
//if (_debug) log.debug("Drop positionable "+item.getNameString()+
// " as "+item.getClass().getName()+
// ", w= "+item.maxWidth()+", h= "+item.maxHeight());
evt.dropComplete(true);
return;
} else if (tr.isDataFlavorSupported(_namedIconDataFlavor)) {
NamedIcon newIcon = new NamedIcon((NamedIcon) tr.getTransferData(_namedIconDataFlavor));
String url = newIcon.getURL();
NamedIcon icon = NamedIcon.getIconByName(url);
PositionableLabel ni = new PositionableLabel(icon, this);
// infer a background icon from the size
if (icon.getIconHeight() > 500 || icon.getIconWidth() > 600) {
ni.setDisplayLevel(BKG);
} else {
ni.setDisplayLevel(ICONS);
}
ni.setLocation(pt.x, pt.y);
ni.setEditor(this);
putItem(ni);
ni.updateSize();
evt.dropComplete(true);
return;
} else if (tr.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String text = (String) tr.getTransferData(DataFlavor.stringFlavor);
PositionableLabel l = new PositionableLabel(text, this);
l.setSize(l.getPreferredSize().width, l.getPreferredSize().height);
l.setDisplayLevel(LABELS);
l.setLocation(pt.x, pt.y);
l.setEditor(this);
putItem(l);
evt.dropComplete(true);
} else if (tr.isDataFlavorSupported(_positionableListDataFlavor)) {
List<Positionable> dragGroup = (List<Positionable>) tr.getTransferData(_positionableListDataFlavor);
for (Positionable pos : dragGroup) {
pos.setEditor(this);
putItem(pos);
pos.updateSize();
log.debug("DnD Add {}", pos.getNameString());
}
} else {
log.warn("Editor DropTargetListener supported DataFlavors not avaialable at drop from " + tr.getClass().getName());
}
} catch (IOException ioe) {
log.warn("Editor DropTarget caught IOException", ioe);
} catch (UnsupportedFlavorException ufe) {
log.warn("Editor DropTarget caught UnsupportedFlavorException", ufe);
}
log.debug("Editor DropTargetListener drop REJECTED!");
evt.rejectDrop();
}
use of java.awt.datatransfer.DataFlavor in project JMRI by JMRI.
the class ClockItemPanel method addIconsToPanel.
@Override
protected void addIconsToPanel(HashMap<String, NamedIcon> iconMap) {
_iconPanel = new JPanel();
Iterator<Entry<String, NamedIcon>> it = iconMap.entrySet().iterator();
while (it.hasNext()) {
Entry<String, NamedIcon> entry = it.next();
// make copy for possible reduction
NamedIcon icon = new NamedIcon(entry.getValue());
JPanel panel = new JPanel();
String borderName = ItemPalette.convertText(entry.getKey());
panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), borderName));
try {
JLabel label = new ClockDragJLabel(new DataFlavor(Editor.POSITIONABLE_FLAVOR));
if (icon.getIconWidth() < 1 || icon.getIconHeight() < 1) {
label.setText(Bundle.getMessage("invisibleIcon"));
label.setForeground(Color.lightGray);
} else {
icon.reduceTo(100, 100, 0.2);
}
label.setIcon(icon);
label.setName(borderName);
panel.add(label);
} catch (java.lang.ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
_iconPanel.add(panel);
}
add(_iconPanel, 1);
}
use of java.awt.datatransfer.DataFlavor in project JMRI by JMRI.
the class MemoryItemPanel method makeDragIcon.
private JPanel makeDragIcon(JComponent mem, Type type) {
JPanel panel = new JPanel();
JPanel comp;
try {
comp = getDragger(new DataFlavor(Editor.POSITIONABLE_FLAVOR), type, mem);
comp.setToolTipText(Bundle.getMessage("ToolTipDragIcon"));
} catch (java.lang.ClassNotFoundException cnfe) {
cnfe.printStackTrace();
comp = new JPanel();
}
panel.add(comp);
return panel;
}
use of java.awt.datatransfer.DataFlavor in project JMRI by JMRI.
the class ControlPanelEditor method pasteFromClipboard.
private void pasteFromClipboard() {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
DataFlavor[] flavors = clipboard.getAvailableDataFlavors();
for (DataFlavor flavor : flavors) {
if (_positionableListDataFlavor.equals(flavor)) {
try {
@SuppressWarnings("unchecked") List<Positionable> clipGroup = (List<Positionable>) clipboard.getData(_positionableListDataFlavor);
if (clipGroup != null && clipGroup.size() > 0) {
Positionable pos = clipGroup.get(0);
int minX = pos.getLocation().x;
int minY = pos.getLocation().y;
// locate group at mouse point
for (int i = 1; i < clipGroup.size(); i++) {
pos = clipGroup.get(i);
minX = Math.min(minX, pos.getLocation().x);
minY = Math.min(minY, pos.getLocation().y);
}
if (_pastePending) {
abortPasteItems();
}
_selectionGroup = new ArrayList<Positionable>();
for (int i = 0; i < clipGroup.size(); i++) {
pos = clipGroup.get(i);
// make positionable belong to this editor
pos.setEditor(this);
pos.setLocation(pos.getLocation().x + _anchorX - minX, pos.getLocation().y + _anchorY - minY);
// now set display level in the pane.
pos.setDisplayLevel(pos.getDisplayLevel());
putItem(pos);
pos.updateSize();
pos.setVisible(true);
_selectionGroup.add(pos);
if (pos instanceof PositionableIcon) {
jmri.NamedBean bean = pos.getNamedBean();
if (bean != null) {
((PositionableIcon) pos).displayState(bean.getState());
}
} else if (pos instanceof MemoryIcon) {
((MemoryIcon) pos).displayState();
} else if (pos instanceof PositionableJComponent) {
((PositionableJComponent) pos).displayState();
}
log.debug("Paste Added at ({}, {})", pos.getLocation().x, pos.getLocation().y);
}
}
return;
} catch (IOException ioe) {
log.warn("Editor Paste caught IOException", ioe);
} catch (UnsupportedFlavorException ufe) {
log.warn("Editor Paste caught UnsupportedFlavorException", ufe);
}
}
}
}
Aggregations