use of org.eclipse.swt.dnd.FileTransfer in project translationstudio8 by heartsome.
the class PasteAction method updateSelection.
/**
* The <code>PasteAction</code> implementation of this
* <code>SelectionListenerAction</code> method enables this action if
* a resource compatible with what is on the clipboard is selected.
*
* -Clipboard must have IResource or java.io.File
* -Projects can always be pasted if they are open
* -Workspace folder may not be copied into itself
* -Files and folders may be pasted to a single selected folder in open
* project or multiple selected files in the same folder
*/
protected boolean updateSelection(IStructuredSelection selection) {
if (!super.updateSelection(selection)) {
return false;
}
final IResource[][] clipboardData = new IResource[1][];
shell.getDisplay().syncExec(new Runnable() {
public void run() {
// clipboard must have resources or files
ResourceTransfer resTransfer = ResourceTransfer.getInstance();
clipboardData[0] = (IResource[]) clipboard.getContents(resTransfer);
}
});
IResource[] resourceData = clipboardData[0];
boolean isProjectRes = resourceData != null && resourceData.length > 0 && resourceData[0].getType() == IResource.PROJECT;
if (isProjectRes) {
for (int i = 0; i < resourceData.length; i++) {
// can paste open projects regardless of selection
if (resourceData[i].getType() != IResource.PROJECT || ((IProject) resourceData[i]).isOpen() == false) {
return false;
}
}
return true;
}
if (getSelectedNonResources().size() > 0) {
return false;
}
IResource targetResource = getTarget();
// or selection is empty
if (targetResource == null) {
return false;
}
// can paste files and folders to a single selection (file, folder,
// open project) or multiple file selection with the same parent
List selectedResources = getSelectedResources();
if (selectedResources.size() > 1) {
for (int i = 0; i < selectedResources.size(); i++) {
IResource resource = (IResource) selectedResources.get(i);
if (resource.getType() != IResource.FILE) {
return false;
}
if (!targetResource.equals(resource.getParent())) {
return false;
}
}
}
if (resourceData != null) {
// linked resources can only be pasted into projects
if (isLinked(resourceData) && targetResource.getType() != IResource.PROJECT && targetResource.getType() != IResource.FOLDER) {
return false;
}
if (targetResource.getType() == IResource.FOLDER) {
// don't try to copy folder to self
for (int i = 0; i < resourceData.length; i++) {
if (targetResource.equals(resourceData[i])) {
return false;
}
}
}
return true;
}
TransferData[] transfers = clipboard.getAvailableTypes();
FileTransfer fileTransfer = FileTransfer.getInstance();
for (int i = 0; i < transfers.length; i++) {
if (fileTransfer.isSupportedType(transfers[i])) {
return true;
}
}
return false;
}
use of org.eclipse.swt.dnd.FileTransfer in project eclipse.platform.swt by eclipse.
the class DNDExample method createDragSource.
private void createDragSource() {
if (dragSource != null)
dragSource.dispose();
dragSource = new DragSource(dragControl, dragOperation);
dragSource.setTransfer(dragTypes);
dragSource.addDragListener(new DragSourceListener() {
@Override
public void dragFinished(org.eclipse.swt.dnd.DragSourceEvent event) {
dragConsole.append(">>dragFinished\n");
printEvent(event);
dragDataText = dragDataRTF = dragDataHTML = dragDataURL = null;
dragDataFiles = null;
if (event.detail == DND.DROP_MOVE) {
switch(dragControlType) {
case BUTTON_CHECK:
case BUTTON_TOGGLE:
case BUTTON_RADIO:
{
Button b = (Button) dragControl;
b.setText("");
break;
}
case STYLED_TEXT:
{
StyledText text = (StyledText) dragControl;
text.insert("");
break;
}
case TABLE:
{
Table table = (Table) dragControl;
TableItem[] items = table.getSelection();
for (TableItem item : items) {
item.dispose();
}
break;
}
case TEXT:
{
Text text = (Text) dragControl;
text.insert("");
break;
}
case TREE:
{
Tree tree = (Tree) dragControl;
TreeItem[] items = tree.getSelection();
for (TreeItem item : items) {
item.dispose();
}
break;
}
case CANVAS:
{
dragControl.setData("STRINGS", null);
dragControl.redraw();
break;
}
case LABEL:
{
Label label = (Label) dragControl;
label.setText("");
break;
}
case LIST:
{
List list = (List) dragControl;
int[] indices = list.getSelectionIndices();
list.remove(indices);
break;
}
case COMBO:
{
Combo combo = (Combo) dragControl;
combo.setText("");
break;
}
}
}
}
@Override
public void dragSetData(org.eclipse.swt.dnd.DragSourceEvent event) {
dragConsole.append(">>dragSetData\n");
printEvent(event);
if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
event.data = dragDataText;
}
if (RTFTransfer.getInstance().isSupportedType(event.dataType)) {
event.data = dragDataRTF;
}
if (HTMLTransfer.getInstance().isSupportedType(event.dataType)) {
event.data = dragDataHTML;
}
if (URLTransfer.getInstance().isSupportedType(event.dataType)) {
event.data = dragDataURL;
}
if (FileTransfer.getInstance().isSupportedType(event.dataType)) {
event.data = dragDataFiles;
}
}
@Override
public void dragStart(org.eclipse.swt.dnd.DragSourceEvent event) {
dragConsole.append(">>dragStart\n");
printEvent(event);
dragDataFiles = fileList.getItems();
switch(dragControlType) {
case BUTTON_CHECK:
case BUTTON_TOGGLE:
case BUTTON_RADIO:
{
Button b = (Button) dragControl;
dragDataText = b.getSelection() ? "true" : "false";
break;
}
case STYLED_TEXT:
{
StyledText text = (StyledText) dragControl;
String s = text.getSelectionText();
if (s.length() == 0) {
event.doit = false;
} else {
dragDataText = s;
}
break;
}
case TABLE:
{
Table table = (Table) dragControl;
TableItem[] items = table.getSelection();
if (items.length == 0) {
event.doit = false;
} else {
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < items.length; i++) {
buffer.append(items[i].getText());
if (items.length > 1 && i < items.length - 1) {
buffer.append("\n");
}
}
dragDataText = buffer.toString();
}
break;
}
case TEXT:
{
Text text = (Text) dragControl;
String s = text.getSelectionText();
if (s.length() == 0) {
event.doit = false;
} else {
dragDataText = s;
}
break;
}
case TREE:
{
Tree tree = (Tree) dragControl;
TreeItem[] items = tree.getSelection();
if (items.length == 0) {
event.doit = false;
} else {
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < items.length; i++) {
buffer.append(items[i].getText());
if (items.length > 1 && i < items.length - 1) {
buffer.append("\n");
}
}
dragDataText = buffer.toString();
}
break;
}
case CANVAS:
{
String[] strings = (String[]) dragControl.getData("STRINGS");
if (strings == null || strings.length == 0) {
event.doit = false;
} else {
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < strings.length; i++) {
buffer.append(strings[i]);
if (strings.length > 1 && i < strings.length - 1) {
buffer.append("\n");
}
}
dragDataText = buffer.toString();
}
break;
}
case LABEL:
{
Label label = (Label) dragControl;
String string = label.getText();
if (string.length() == 0) {
event.doit = false;
} else {
dragDataText = string;
}
break;
}
case LIST:
{
List list = (List) dragControl;
String[] selection = list.getSelection();
if (selection.length == 0) {
event.doit = false;
} else {
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < selection.length; i++) {
buffer.append(selection[i]);
if (selection.length > 1 && i < selection.length - 1) {
buffer.append("\n");
}
}
dragDataText = buffer.toString();
}
break;
}
case COMBO:
{
Combo combo = (Combo) dragControl;
String string = combo.getText();
Point selection = combo.getSelection();
if (selection.x == selection.y) {
event.doit = false;
} else {
dragDataText = string.substring(selection.x, selection.y);
}
break;
}
default:
throw new SWTError(SWT.ERROR_NOT_IMPLEMENTED);
}
if (dragDataText != null) {
dragDataRTF = "{\\rtf1{\\colortbl;\\red255\\green0\\blue0;}\\cf1\\b " + dragDataText + "}";
dragDataHTML = "<b>" + dragDataText + "</b>";
dragDataURL = "http://" + dragDataText.replace(' ', '.');
try {
new URL(dragDataURL);
} catch (MalformedURLException e) {
dragDataURL = null;
}
}
for (Transfer dragType : dragTypes) {
if (dragType instanceof TextTransfer && dragDataText == null) {
event.doit = false;
}
if (dragType instanceof RTFTransfer && dragDataRTF == null) {
event.doit = false;
}
if (dragType instanceof HTMLTransfer && dragDataHTML == null) {
event.doit = false;
}
if (dragType instanceof URLTransfer && dragDataURL == null) {
event.doit = false;
}
if (dragType instanceof FileTransfer && (dragDataFiles == null || dragDataFiles.length == 0)) {
event.doit = false;
}
}
}
});
}
use of org.eclipse.swt.dnd.FileTransfer in project translationstudio8 by heartsome.
the class PasteAction method run.
/**
* Implementation of method defined on <code>IAction</code>.
*/
public void run() {
// try a resource transfer
ResourceTransfer resTransfer = ResourceTransfer.getInstance();
IResource[] resourceData = (IResource[]) clipboard.getContents(resTransfer);
if (resourceData != null && resourceData.length > 0) {
if (resourceData[0].getType() == IResource.PROJECT) {
// enablement checks for all projects
for (int i = 0; i < resourceData.length; i++) {
CopyProjectOperation operation = new CopyProjectOperation(this.shell);
operation.copyProject((IProject) resourceData[i]);
}
} else {
// enablement should ensure that we always have access to a container
IContainer container = getContainer();
CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(this.shell);
operation.copyResources(resourceData, container);
}
return;
}
// try a file transfer
FileTransfer fileTransfer = FileTransfer.getInstance();
String[] fileData = (String[]) clipboard.getContents(fileTransfer);
if (fileData != null) {
// enablement should ensure that we always have access to a container
IContainer container = getContainer();
CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(this.shell);
operation.copyFiles(fileData, container);
}
}
use of org.eclipse.swt.dnd.FileTransfer in project eclipse.platform.swt by eclipse.
the class ClipboardExample method createFileTransfer.
void createFileTransfer(Composite copyParent, Composite pasteParent) {
// File Transfer
Label l = new Label(copyParent, SWT.NONE);
// $NON-NLS-1$
l.setText("FileTransfer:");
GridData data = new GridData();
data.verticalSpan = 3;
l.setLayoutData(data);
final Table copyFileTable = new Table(copyParent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
data = new GridData(GridData.FILL_BOTH);
data.widthHint = HSIZE;
data.heightHint = VSIZE;
data.verticalSpan = 3;
copyFileTable.setLayoutData(data);
Button b = new Button(copyParent, SWT.PUSH);
b.setText("Select file(s)");
b.addSelectionListener(widgetSelectedAdapter(e -> {
FileDialog dialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI);
String result = dialog.open();
if (result != null && result.length() > 0) {
String path = dialog.getFilterPath();
String[] names = dialog.getFileNames();
for (String name : names) {
TableItem item = new TableItem(copyFileTable, SWT.NONE);
item.setText(path + File.separator + name);
}
}
}));
b = new Button(copyParent, SWT.PUSH);
b.setText("Select directory");
b.addSelectionListener(widgetSelectedAdapter(e -> {
DirectoryDialog dialog = new DirectoryDialog(shell, SWT.OPEN);
String result = dialog.open();
if (result != null && result.length() > 0) {
// copyFileTable.removeAll();
TableItem item = new TableItem(copyFileTable, SWT.NONE);
item.setText(result);
}
}));
b = new Button(copyParent, SWT.PUSH);
b.setText("Copy");
b.addSelectionListener(widgetSelectedAdapter(e -> {
TableItem[] items = copyFileTable.getItems();
if (items.length > 0) {
status.setText("");
String[] itemsData = new String[items.length];
for (int i = 0; i < itemsData.length; i++) {
itemsData[i] = items[i].getText();
}
clipboard.setContents(new Object[] { itemsData }, new Transfer[] { FileTransfer.getInstance() });
} else {
status.setText("No file to copy");
}
}));
l = new Label(pasteParent, SWT.NONE);
// $NON-NLS-1$
l.setText("FileTransfer:");
final Table pasteFileTable = new Table(pasteParent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
data = new GridData(GridData.FILL_BOTH);
data.widthHint = HSIZE;
data.heightHint = VSIZE;
pasteFileTable.setLayoutData(data);
b = new Button(pasteParent, SWT.PUSH);
b.setText("Paste");
b.addSelectionListener(widgetSelectedAdapter(e -> {
String[] textData = (String[]) clipboard.getContents(FileTransfer.getInstance());
if (textData != null && textData.length > 0) {
status.setText("");
pasteFileTable.removeAll();
for (String element : textData) {
TableItem item = new TableItem(pasteFileTable, SWT.NONE);
item.setText(element);
}
} else {
status.setText("No file to paste");
}
}));
}
use of org.eclipse.swt.dnd.FileTransfer in project webtools.sourceediting by eclipse.
the class ExtendedEditorDropTargetAdapter method doDrop.
protected boolean doDrop(Transfer transfer, DropTargetEvent event) {
TransferBuilder tb = new TransferBuilder(useProxy);
IDropAction[] as = null;
if (editorIds != null && editorIds.length > 0)
as = tb.getDropActions(editorIds, transfer);
else
as = tb.getDropActions(getTargetEditor().getClass().getName(), transfer);
for (int i = 0; i < as.length; ++i) {
IDropAction da = as[i];
Transfer actualTransfer;
if (transfer instanceof TransferProxyForDelayLoading) {
actualTransfer = ((TransferProxyForDelayLoading) transfer).getTransferClass();
} else {
actualTransfer = transfer;
}
if (actualTransfer instanceof FileTransfer) {
if (event.data == null) {
// $NON-NLS-1$
Logger.log(Logger.ERROR, "No data in DropTargetEvent from " + event.widget);
return false;
}
String[] strs = (String[]) event.data;
boolean[] bs = new boolean[strs.length];
int c = 0;
for (int j = 0; j < strs.length; ++j) {
bs[j] = false;
if (da.isSupportedData(strs[j])) {
event.data = new String[] { strs[j] };
if (!da.run(event, targetEditor)) {
bs[j] = true;
c++;
}
} else {
bs[j] = true;
c++;
}
}
if (c == 0) {
return true;
}
int k = 0;
String[] rests = new String[c];
for (int j = 0; j < strs.length; ++j) {
if (bs[j])
rests[k++] = strs[j];
}
event.data = rests;
} else if (da.isSupportedData(event.data)) {
if (da.run(event, targetEditor)) {
return true;
}
}
}
return false;
}
Aggregations