use of java.awt.datatransfer.UnsupportedFlavorException in project Spark by igniterealtime.
the class SparkTabbedPane method enableDragAndDrop.
/**
* Drag and Drop
*/
public void enableDragAndDrop() {
final DragSourceListener dsl = new DragSourceListener() {
@Override
public void dragDropEnd(DragSourceDropEvent event) {
dragTabIndex = -1;
}
@Override
public void dragEnter(DragSourceDragEvent event) {
event.getDragSourceContext().setCursor(DragSource.DefaultMoveDrop);
}
@Override
public void dragExit(DragSourceEvent event) {
}
@Override
public void dragOver(DragSourceDragEvent event) {
}
@Override
public void dropActionChanged(DragSourceDragEvent event) {
}
};
final Transferable t = new Transferable() {
private final DataFlavor FLAVOR = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType, NAME);
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
return pane;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
DataFlavor[] f = new DataFlavor[1];
f[0] = this.FLAVOR;
return f;
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.getHumanPresentableName().equals(NAME);
}
};
final DragGestureListener dgl = event -> {
dragTabIndex = pane.indexAtLocation(event.getDragOrigin().x, event.getDragOrigin().y);
try {
event.startDrag(DragSource.DefaultMoveDrop, t, dsl);
} catch (Exception idoe) {
Log.error(idoe);
}
};
final DropTargetListener dtl = new DropTargetListener() {
@Override
public void dragEnter(DropTargetDragEvent event) {
}
@Override
public void dragExit(DropTargetEvent event) {
}
@Override
public void dragOver(DropTargetDragEvent event) {
}
@Override
public void drop(DropTargetDropEvent event) {
int dropTabIndex = getTargetTabIndex(event.getLocation());
moveTab(dragTabIndex, dropTabIndex);
}
@Override
public void dropActionChanged(DropTargetDragEvent event) {
}
};
new DropTarget(pane, DnDConstants.ACTION_COPY_OR_MOVE, dtl, true);
new DragSource().createDefaultDragGestureRecognizer(pane, DnDConstants.ACTION_COPY_OR_MOVE, dgl);
}
use of java.awt.datatransfer.UnsupportedFlavorException in project freeplane by freeplane.
the class MClipboardController method getFlavorHandlers.
Collection<IDataFlavorHandler> getFlavorHandlers() {
final Transferable t = getClipboardContents();
final Collection<IDataFlavorHandler> handlerList = new LinkedList<IDataFlavorHandler>();
if (t == null) {
return handlerList;
}
if (t.isDataFlavorSupported(MindMapNodesSelection.mindMapNodesFlavor)) {
try {
final String textFromClipboard = t.getTransferData(MindMapNodesSelection.mindMapNodesFlavor).toString();
handlerList.add(new MindMapNodesFlavorHandler(textFromClipboard));
} catch (final UnsupportedFlavorException e) {
} catch (final IOException e) {
}
}
DataFlavor supportedHtmlFlavor = getSupportedHtmlFlavor(t);
if (supportedHtmlFlavor != null) {
try {
final String textFromClipboard = t.getTransferData(supportedHtmlFlavor).toString();
if (textFromClipboard.charAt(0) != 65533) {
if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
handlerList.add(new StructuredHtmlFlavorHandler(textFromClipboard));
handlerList.add(new DirectHtmlFlavorHandler(textFromClipboard));
}
}
} catch (final UnsupportedFlavorException e) {
} catch (final IOException e) {
}
}
if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
try {
final String plainTextFromClipboard = t.getTransferData(DataFlavor.stringFlavor).toString();
handlerList.add(new StringFlavorHandler(plainTextFromClipboard));
} catch (final UnsupportedFlavorException e) {
} catch (final IOException e) {
}
}
if (t.isDataFlavorSupported(MindMapNodesSelection.fileListFlavor)) {
try {
final List<File> fileList = castToFileList(t.getTransferData(MindMapNodesSelection.fileListFlavor));
handlerList.add(new FileListFlavorHandler(fileList));
} catch (final UnsupportedFlavorException e) {
} catch (final IOException e) {
}
}
if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
try {
Image image = (Image) t.getTransferData(DataFlavor.imageFlavor);
handlerList.add(new ImageFlavorHandler(image));
} catch (final UnsupportedFlavorException e) {
} catch (final IOException e) {
}
}
return handlerList;
}
use of java.awt.datatransfer.UnsupportedFlavorException in project blue by kunstmusik.
the class EffectTreeDropTarget method drop.
@Override
public void drop(DropTargetDropEvent dtde) {
Point pt = dtde.getLocation();
DropTargetContext dtc = dtde.getDropTargetContext();
JTree tree = (JTree) dtc.getComponent();
TreePath parentpath = tree.getClosestPathForLocation(pt.x, pt.y);
Object node = parentpath.getLastPathComponent();
EffectsLibrary eLibrary = EffectsLibrary.getInstance();
if (dtde.isDataFlavorSupported(TransferableEffect.EFFECT_CAT_FLAVOR)) {
if (!(node instanceof EffectCategory)) {
dtde.rejectDrop();
return;
}
if (dtde.getDropAction() == DnDConstants.ACTION_MOVE) {
dtde.acceptDrop(dtde.getDropAction());
Transferable tr = dtde.getTransferable();
try {
Object transferNode = tr.getTransferData(TransferableEffect.EFFECT_CAT_FLAVOR);
EffectCategory effectCategory = (EffectCategory) transferNode;
EffectCategory parentNode = (EffectCategory) node;
eLibrary.addCategory(parentNode, effectCategory);
dtde.dropComplete(true);
} catch (UnsupportedFlavorException | IOException e) {
dtde.dropComplete(false);
}
} else {
dtde.rejectDrop();
}
} else if (dtde.isDataFlavorSupported(TransferableEffect.EFFECT_FLAVOR)) {
dtde.acceptDrop(dtde.getDropAction());
try {
Transferable tr = dtde.getTransferable();
Object transferNode = tr.getTransferData(TransferableEffect.EFFECT_FLAVOR);
Effect effect = (Effect) transferNode;
effect.clearParameters();
// iLibrary.removeInstrument(instrument);
if (node instanceof EffectCategory) {
EffectCategory parentNode = (EffectCategory) node;
eLibrary.addEffect(parentNode, effect);
} else if (node instanceof Effect) {
EffectCategory parentNode = (EffectCategory) parentpath.getPathComponent(parentpath.getPathCount() - 2);
int index = ListUtil.indexOfByRef(parentNode.getEffects(), node);
int closestRow = tree.getClosestRowForLocation(pt.x, pt.y);
Rectangle bounds = tree.getRowBounds(closestRow);
if (pt.y > bounds.y + bounds.height) {
eLibrary.addEffect(parentNode, effect);
} else {
eLibrary.addEffect(parentNode, index, effect);
}
}
dtde.dropComplete(true);
} catch (UnsupportedFlavorException | IOException e) {
dtde.dropComplete(false);
}
} else {
dtde.rejectDrop();
}
}
use of java.awt.datatransfer.UnsupportedFlavorException in project blue by kunstmusik.
the class PresetsTreeDropTarget method drop.
@Override
public void drop(DropTargetDropEvent dtde) {
Point pt = dtde.getLocation();
DropTargetContext dtc = dtde.getDropTargetContext();
JTree tree = (JTree) dtc.getComponent();
TreePath parentpath = tree.getClosestPathForLocation(pt.x, pt.y);
Object node = parentpath.getLastPathComponent();
if (!(node instanceof PresetGroup)) {
dtde.rejectDrop();
return;
}
try {
Transferable tr = dtde.getTransferable();
DataFlavor[] flavors = tr.getTransferDataFlavors();
for (int i = 0; i < flavors.length; i++) {
if (tr.isDataFlavorSupported(flavors[i])) {
dtde.acceptDrop(dtde.getDropAction());
Object transferNode = tr.getTransferData(flavors[i]);
PresetsTreeModel presetsTreeModel = (PresetsTreeModel) tree.getModel();
PresetGroup parentNode = (PresetGroup) node;
if (transferNode instanceof Preset) {
Preset preset = (Preset) transferNode;
// presetsTreeModel.removePreset(preset);
presetsTreeModel.addPreset(parentNode, preset);
dtde.dropComplete(true);
} else if (transferNode instanceof PresetGroup) {
PresetGroup presetGroup = (PresetGroup) transferNode;
// presetsTreeModel.removePresetGroup(presetGroup);
presetsTreeModel.addPresetGroup(parentNode, presetGroup);
dtde.dropComplete(true);
} else {
dtde.rejectDrop();
}
return;
}
}
dtde.rejectDrop();
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
use of java.awt.datatransfer.UnsupportedFlavorException in project blue by kunstmusik.
the class AudioLayersDropTargetListener method drop.
/*
* (non-Javadoc)
*
* @see java.awt.dnd.DropTargetListener#drop(java.awt.dnd.DropTargetDropEvent)
*/
@Override
public void drop(DropTargetDropEvent dtde) {
Node node = NodeTransfer.node(dtde.getTransferable(), NodeTransfer.DND_COPY);
if (node != null) {
File f = node.getLookup().lookup(File.class);
if (f == null || !f.isFile()) {
dtde.dropComplete(false);
return;
}
dtde.acceptDrop(DnDConstants.ACTION_COPY);
Point p = dtde.getLocation();
AudioLayerGroup audioLayerGroup = audioLayersPanel.getAudioLayerGroup();
int index = audioLayerGroup.getLayerNumForY(p.y);
AudioClip af = new AudioClip();
af.setName(f.getName());
af.setAudioFile(f);
float startTime = (float) p.x / audioLayersPanel.getTimeState().getPixelSecond();
af.setStartTime(startTime);
af.setSubjectiveDuration(af.getAudioDuration());
AudioLayer layer = audioLayerGroup.get(index);
layer.add(af);
dtde.dropComplete(true);
}
try {
Transferable tr = dtde.getTransferable();
if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
dtde.acceptDrop(DnDConstants.ACTION_LINK);
List<?> list = (List<?>) tr.getTransferData(DataFlavor.javaFileListFlavor);
if (list.size() != 1) {
dtde.dropComplete(false);
return;
}
String s = list.get(0).toString().trim();
System.out.println("file flavor found: " + s);
if (!s.toLowerCase().endsWith("wav") && !s.toLowerCase().endsWith("aif") && !s.toLowerCase().endsWith("aiff")) {
dtde.dropComplete(false);
return;
}
String sObjName = s.substring(s.lastIndexOf(File.separator) + 1);
Point p = dtde.getLocation();
AudioLayerGroup audioLayerGroup = audioLayersPanel.getAudioLayerGroup();
int index = audioLayerGroup.getLayerNumForY(p.y);
AudioClip af = new AudioClip();
af.setName(sObjName);
af.setAudioFile(new File(s));
float startTime = (float) p.x / audioLayersPanel.getTimeState().getPixelSecond();
af.setStartTime(startTime);
af.setSubjectiveDuration(af.getAudioDuration());
AudioLayer layer = audioLayerGroup.get(index);
layer.add(af);
dtde.dropComplete(true);
return;
} else if (dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) {
dtde.acceptDrop(DnDConstants.ACTION_LINK);
String str = (String) tr.getTransferData(DataFlavor.stringFlavor);
System.out.println("String flavor found: " + str);
if (!str.startsWith("file://")) {
dtde.dropComplete(false);
return;
}
str = str.substring(7).trim();
if (!str.toLowerCase().endsWith("wav") && !str.toLowerCase().endsWith("aif") && !str.toLowerCase().endsWith("aiff")) {
System.err.println("Could not open file: " + str);
dtde.dropComplete(false);
return;
}
str = URLDecoder.decode(str);
str = str.replaceAll(" ", "\\ ");
File f = new File(str);
if (!f.exists()) {
dtde.dropComplete(false);
return;
}
String sObjName = str.substring(str.lastIndexOf(File.separator) + 1);
Point p = dtde.getLocation();
AudioLayerGroup audioLayerGroup = audioLayersPanel.getAudioLayerGroup();
int index = audioLayerGroup.getLayerNumForY(p.y);
AudioClip af = new AudioClip();
af.setName(sObjName);
af.setAudioFile(f);
float startTime = (float) p.x / audioLayersPanel.getTimeState().getPixelSecond();
af.setStartTime(startTime);
af.setSubjectiveDuration(af.getAudioDuration());
AudioLayer layer = audioLayerGroup.get(index);
layer.add(af);
dtde.dropComplete(true);
return;
}
dtde.rejectDrop();
} catch (UnsupportedFlavorException | IOException | InvalidDnDOperationException e) {
e.printStackTrace();
dtde.rejectDrop();
}
}
Aggregations