use of java.awt.datatransfer.Transferable 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.Transferable in project JMRI by JMRI.
the class DnDTableImportExportHandler method importData.
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
}
if (log.isDebugEnabled()) {
JTable.DropLocation loc = (JTable.DropLocation) support.getDropLocation();
log.debug("importData: isDrop= {} location= {} row= {} col= {}", support.isDrop(), loc.getDropPoint(), loc.getRow(), loc.getColumn());
}
if (!support.isDrop()) {
return false;
}
TransferHandler.DropLocation loc = support.getDropLocation();
if (!(loc instanceof JTable.DropLocation)) {
return false;
}
Component comp = support.getComponent();
if (!(comp instanceof JTable)) {
return false;
}
JTable.DropLocation location = (JTable.DropLocation) loc;
JTable table = (JTable) comp;
int row = location.getRow();
int col = location.getColumn();
row = table.convertRowIndexToModel(row);
col = table.convertColumnIndexToModel(col);
for (int i = 0; i < _skipColumns.length; i++) {
if (_skipColumns[i] == col) {
return false;
}
}
try {
Transferable trans = support.getTransferable();
Object obj = trans.getTransferData(DataFlavor.stringFlavor);
table.getModel().setValueAt(obj, row, col);
return true;
} catch (UnsupportedFlavorException ufe) {
log.warn("DnDStringImportHandler.importData: " + ufe.getMessage());
} catch (IOException ioe) {
log.warn("DnDStringImportHandler.importData: " + ioe.getMessage());
}
return false;
}
use of java.awt.datatransfer.Transferable in project jdk8u_jdk by JetBrains.
the class SunDragSourceContextPeer method startDrag.
/**
* initiate a DnD operation ...
*/
public void startDrag(DragSourceContext dsc, Cursor c, Image di, Point p) throws InvalidDnDOperationException {
/* Fix for 4354044: don't initiate a drag if event sequence provided by
* DragGestureRecognizer is empty */
if (getTrigger().getTriggerEvent() == null) {
throw new InvalidDnDOperationException("DragGestureEvent has a null trigger");
}
dragSourceContext = dsc;
cursor = c;
sourceActions = getDragSourceContext().getSourceActions();
dragImage = di;
dragImageOffset = p;
Transferable transferable = getDragSourceContext().getTransferable();
SortedMap<Long, DataFlavor> formatMap = DataTransferer.getInstance().getFormatsForTransferable(transferable, DataTransferer.adaptFlavorMap(getTrigger().getDragSource().getFlavorMap()));
long[] formats = DataTransferer.getInstance().keysToLongArray(formatMap);
startDrag(transferable, formats, formatMap);
/*
* Fix for 4613903.
* Filter out all mouse events that are currently on the event queue.
*/
discardingMouseEvents = true;
EventQueue.invokeLater(new Runnable() {
public void run() {
discardingMouseEvents = false;
}
});
}
use of java.awt.datatransfer.Transferable in project jdk8u_jdk by JetBrains.
the class SunClipboard method lostOwnershipNow.
protected void lostOwnershipNow(final AppContext disposedContext) {
final SunClipboard sunClipboard = SunClipboard.this;
ClipboardOwner owner = null;
Transferable contents = null;
synchronized (sunClipboard) {
final AppContext context = sunClipboard.contentsContext;
if (context == null) {
return;
}
if (disposedContext == null || context == disposedContext) {
owner = sunClipboard.owner;
contents = sunClipboard.contents;
sunClipboard.contentsContext = null;
sunClipboard.owner = null;
sunClipboard.contents = null;
sunClipboard.clearNativeContext();
context.removePropertyChangeListener(AppContext.DISPOSED_PROPERTY_NAME, sunClipboard);
} else {
return;
}
}
if (owner != null) {
owner.lostOwnership(sunClipboard, contents);
}
}
use of java.awt.datatransfer.Transferable in project android by JetBrains.
the class PTable method performPaste.
@Override
public void performPaste(@NotNull DataContext dataContext) {
PTableItem item = getSelectedNonGroupItem();
if (item == null) {
return;
}
Transferable transferable = myCopyPasteManager.getContents();
if (transferable == null || !transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
return;
}
try {
item.setValue(transferable.getTransferData(DataFlavor.stringFlavor));
} catch (IOException | UnsupportedFlavorException exception) {
Logger.getInstance(PTable.class).warn(exception);
}
}
Aggregations