use of java.awt.datatransfer.Transferable in project adempiere by adempiere.
the class VTreeTransferHandler method importData.
public boolean importData(TransferHandler.TransferSupport info) {
if (!canImport(info))
return false;
JTree tree = (JTree) info.getComponent();
AdempiereTreeModel model = (AdempiereTreeModel) tree.getModel();
Transferable t = info.getTransferable();
MTreeNode to = null;
MTreeNode from = null;
int index;
try {
from = (MTreeNode) t.getTransferData(TransferableTreeNode.TREE_NODE_FLAVOR);
} catch (Exception e) {
return false;
}
if (info.isDrop()) {
JTree.DropLocation dl = (JTree.DropLocation) info.getDropLocation();
to = (MTreeNode) dl.getPath().getLastPathComponent();
if (from == to)
return false;
index = dl.getChildIndex();
if (index == -1)
// insert as first child
index = 0;
} else {
// it's a paste
MTreeNode selected = (MTreeNode) tree.getSelectionPath().getLastPathComponent();
if (selected.isLeaf()) {
to = (MTreeNode) selected.getParent();
// insert after selected
index = to.getIndex(selected) + 1;
} else {
to = selected;
index = 0;
}
}
model.insertNodeInto(from, to, index);
// display from's new location
tree.scrollPathToVisible(new TreePath(from.getPath()));
model.saveChangedNodes(from, to);
return true;
}
use of java.awt.datatransfer.Transferable in project jmeter by apache.
the class GuiUtils method getPastedText.
/**
* Get pasted text from clipboard
*
* @return String Pasted text
* @throws UnsupportedFlavorException
* if the clipboard data can not be get as a {@link String}
* @throws IOException
* if the clipboard data is no longer available
*/
public static String getPastedText() throws UnsupportedFlavorException, IOException {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable trans = clipboard.getContents(null);
DataFlavor[] flavourList = trans.getTransferDataFlavors();
Collection<DataFlavor> flavours = new ArrayList<>(flavourList.length);
if (Collections.addAll(flavours, flavourList) && flavours.contains(DataFlavor.stringFlavor)) {
return (String) trans.getTransferData(DataFlavor.stringFlavor);
} else {
return null;
}
}
use of java.awt.datatransfer.Transferable in project jmeter by apache.
the class JMeterTreeTransferHandler method importData.
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
}
// deal with the jmx files
GuiPackage guiInstance = GuiPackage.getInstance();
DataFlavor[] flavors = support.getDataFlavors();
Transferable t = support.getTransferable();
for (DataFlavor flavor : flavors) {
// Check for file lists specifically
if (flavor.isFlavorJavaFileListType()) {
try {
return guiInstance.getMainFrame().openJmxFilesFromDragAndDrop(t);
} catch (Exception e) {
log.error("Drop file failed", e);
}
return false;
}
}
// Extract transfer data.
JMeterTreeNode[] nodes = getDraggedNodes(t);
if (nodes == null || nodes.length == 0) {
return false;
}
// Get drop location and mode
JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
TreePath dest = dl.getPath();
JMeterTreeNode target = (JMeterTreeNode) dest.getLastPathComponent();
nodesForRemoval = new ArrayList<>();
int index = dl.getChildIndex();
TreePath[] pathsToSelect = new TreePath[nodes.length];
int pathPosition = 0;
JMeterTreeModel treeModel = guiInstance.getTreeModel();
for (JMeterTreeNode node : nodes) {
if (index == -1) {
// drop mode == DropMode.ON
index = target.getChildCount();
}
// Insert a clone of the node, the original one will be removed by the exportDone method
// the children are not cloned but moved to the cloned node
// working on the original node would be harder as
// you'll have to deal with the insertion index offset if you re-order a node inside a parent
JMeterTreeNode copy = (JMeterTreeNode) node.clone();
// first copy the children as the call to copy.add will modify the collection we're iterating on
Enumeration<?> enumFrom = node.children();
List<JMeterTreeNode> tmp = new ArrayList<>();
while (enumFrom.hasMoreElements()) {
JMeterTreeNode child = (JMeterTreeNode) enumFrom.nextElement();
tmp.add(child);
}
for (JMeterTreeNode jMeterTreeNode : tmp) {
copy.add(jMeterTreeNode);
}
treeModel.insertNodeInto(copy, target, index++);
nodesForRemoval.add(node);
pathsToSelect[pathPosition++] = new TreePath(treeModel.getPathToRoot(copy));
}
TreePath treePath = new TreePath(target.getPath());
// expand the destination node
JTree tree = (JTree) support.getComponent();
tree.expandPath(treePath);
tree.setSelectionPaths(pathsToSelect);
return true;
}
use of java.awt.datatransfer.Transferable in project jmeter by apache.
the class MainFrame method drop.
/**
* Handler of Top level Dnd
*/
@Override
public void drop(DropTargetDropEvent dtde) {
try {
Transferable tr = dtde.getTransferable();
DataFlavor[] flavors = tr.getTransferDataFlavors();
for (DataFlavor flavor : flavors) {
// Check for file lists specifically
if (flavor.isFlavorJavaFileListType()) {
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
try {
openJmxFilesFromDragAndDrop(tr);
} finally {
dtde.dropComplete(true);
}
return;
}
}
} catch (UnsupportedFlavorException | IOException e) {
log.warn("Dnd failed", e);
}
}
use of java.awt.datatransfer.Transferable in project intellij-community by JetBrains.
the class PsiElementListNavigator method navigateOrCreatePopup.
/**
* listUpdaterTask should be started after alarm is initialized so one-item popup won't blink
*/
@Nullable
public static JBPopup navigateOrCreatePopup(@NotNull final NavigatablePsiElement[] targets, final String title, final String findUsagesTitle, final ListCellRenderer listRenderer, @Nullable final ListBackgroundUpdaterTask listUpdaterTask, @NotNull final Consumer<Object[]> consumer) {
if (targets.length == 0)
return null;
if (targets.length == 1 && (listUpdaterTask == null || listUpdaterTask.isFinished())) {
consumer.consume(targets);
return null;
}
final CollectionListModel<NavigatablePsiElement> model = new CollectionListModel<>(targets);
final JBList list = new JBList(model);
HintUpdateSupply.installSimpleHintUpdateSupply(list);
list.setTransferHandler(new TransferHandler() {
@Nullable
@Override
protected Transferable createTransferable(JComponent c) {
final Object[] selectedValues = list.getSelectedValues();
final PsiElement[] copy = new PsiElement[selectedValues.length];
for (int i = 0; i < selectedValues.length; i++) {
copy[i] = (PsiElement) selectedValues[i];
}
return new PsiCopyPasteManager.MyTransferable(copy);
}
@Override
public int getSourceActions(JComponent c) {
return COPY;
}
});
list.setCellRenderer(listRenderer);
list.setFont(EditorUtil.getEditorFont());
final PopupChooserBuilder builder = new PopupChooserBuilder(list);
if (listRenderer instanceof PsiElementListCellRenderer) {
((PsiElementListCellRenderer) listRenderer).installSpeedSearch(builder);
}
PopupChooserBuilder popupChooserBuilder = builder.setTitle(title).setMovable(true).setResizable(true).setItemChoosenCallback(() -> {
int[] ids = list.getSelectedIndices();
if (ids == null || ids.length == 0)
return;
Object[] selectedElements = list.getSelectedValues();
consumer.consume(selectedElements);
}).setCancelCallback(() -> {
HintUpdateSupply.hideHint(list);
if (listUpdaterTask != null) {
listUpdaterTask.cancelTask();
}
return true;
});
final Ref<UsageView> usageView = new Ref<>();
if (findUsagesTitle != null) {
popupChooserBuilder = popupChooserBuilder.setCouldPin(popup -> {
final List<NavigatablePsiElement> items = model.getItems();
usageView.set(FindUtil.showInUsageView(null, items.toArray(new PsiElement[items.size()]), findUsagesTitle, targets[0].getProject()));
popup.cancel();
return false;
});
}
final JBPopup popup = popupChooserBuilder.createPopup();
builder.getScrollPane().setBorder(null);
builder.getScrollPane().setViewportBorder(null);
if (listUpdaterTask != null) {
listUpdaterTask.init((AbstractPopup) popup, list, usageView);
}
return popup;
}
Aggregations