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 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 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;
}
use of java.awt.datatransfer.Transferable in project intellij-community by JetBrains.
the class FileListPasteProvider method performPaste.
@Override
public void performPaste(@NotNull DataContext dataContext) {
final Project project = CommonDataKeys.PROJECT.getData(dataContext);
final IdeView ideView = LangDataKeys.IDE_VIEW.getData(dataContext);
if (project == null || ideView == null)
return;
if (!FileCopyPasteUtil.isFileListFlavorAvailable())
return;
final Transferable contents = CopyPasteManager.getInstance().getContents();
if (contents == null)
return;
final List<File> fileList = FileCopyPasteUtil.getFileList(contents);
if (fileList == null)
return;
if (DumbService.isDumb(project)) {
DumbService.getInstance(project).showDumbModeNotification("Sorry, file copy/paste is not available during indexing");
return;
}
final List<PsiElement> elements = new ArrayList<>();
for (File file : fileList) {
final VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
if (vFile != null) {
final PsiManager instance = PsiManager.getInstance(project);
PsiFileSystemItem item = vFile.isDirectory() ? instance.findDirectory(vFile) : instance.findFile(vFile);
if (item != null) {
elements.add(item);
}
}
}
if (elements.size() > 0) {
final PsiDirectory dir = ideView.getOrChooseDirectory();
if (dir != null) {
final boolean move = LinuxDragAndDropSupport.isMoveOperation(contents);
if (move) {
new MoveFilesOrDirectoriesHandler().doMove(PsiUtilCore.toPsiElementArray(elements), dir);
} else {
new CopyFilesOrDirectoriesHandler().doCopy(PsiUtilCore.toPsiElementArray(elements), dir);
}
}
}
}
Aggregations