Java Code Examples for org.eclipse.swt.dnd.DropTarget#addDropListener()

The following examples show how to use org.eclipse.swt.dnd.DropTarget#addDropListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TmxEditorViewer.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void createPartControl(Composite parent) {
	parent.setLayout(new FillLayout());
	this.container = parent;
	CellEditorGlobalActionHanlder.getInstance().setIActionBars(getViewSite().getActionBars());
	DropTarget target = new DropTarget(parent, DND.DROP_MOVE | DND.DROP_COPY);
	Transfer[] tfs = new Transfer[] { FileTransfer.getInstance() };
	target.setTransfer(tfs);
	target.addDropListener(new DropTargetAdapter() {
		@Override
		public void drop(DropTargetEvent event) {
			Object o = event.data;
			if (o instanceof String[]) {
				String[] s = (String[]) o;
				if (s.length == 0 && s[0] == null || s[0].length() == 0) {
					return;
				}
				File f = new File(s[0]);
				if (f.exists() && f.isFile()) {
					open(f);
				}
			}
		}
	});
}
 
Example 2
Source File: GenericObjectDropTarget.java    From elexis-3-core with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Register the provided target as {@link DropTarget} for a {@link PersistentObject}
 * @param target
 * @param r
 * @param colorizeControl whether the target control should change color during selection
 * @since 3.1.0
 */
public GenericObjectDropTarget(String name, Control target, IReceiver r, boolean colorizeControl){
	if (colorizeControl) {
		normalColor = target.getBackground();
		highlightColor = target.getDisplay().getSystemColor(SWT.COLOR_RED);
	} else {
		normalColor = null;
		highlightColor = null;
	}
	
	this.name = name;
	mine = target;
	rc = r;
	DropTarget dtarget = new DropTarget(target, DND.DROP_COPY);
	final TextTransfer textTransfer = TextTransfer.getInstance();
	Transfer[] types = new Transfer[] {
		textTransfer
	};
	dtarget.setTransfer(types);
	dtarget.addDropListener(this);
}
 
Example 3
Source File: PersistentObjectDropTarget.java    From elexis-3-core with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Register the provided target as {@link DropTarget} for a {@link PersistentObject}
 * @param target
 * @param r
 * @param colorizeControl whether the target control should change color during selection
 * @since 3.1.0
 */
public PersistentObjectDropTarget(String name, Control target, IReceiver r, boolean colorizeControl){
	if (colorizeControl) {
		normalColor = target.getBackground();
		highlightColor = target.getDisplay().getSystemColor(SWT.COLOR_RED);
	} else {
		normalColor = null;
		highlightColor = null;
	}
	
	this.name = name;
	mine = target;
	rc = r;
	DropTarget dtarget = new DropTarget(target, DND.DROP_COPY);
	final TextTransfer textTransfer = TextTransfer.getInstance();
	Transfer[] types = new Transfer[] {
		textTransfer
	};
	dtarget.setTransfer(types);
	dtarget.addDropListener(this);
}
 
Example 4
Source File: Trees.java    From olca-app with Mozilla Public License 2.0 6 votes vote down vote up
public static void addDropSupport(TreeViewer tree,
		Consumer<List<Descriptor>> handler) {
	Transfer transfer = ModelTransfer.getInstance();
	DropTarget dropTarget = new DropTarget(tree.getTree(), DND.DROP_COPY
			| DND.DROP_MOVE | DND.DROP_DEFAULT);
	dropTarget.setTransfer(new Transfer[] { transfer });
	dropTarget.addDropListener(new DropTargetAdapter() {
		@Override
		public void drop(DropTargetEvent event) {
			if (!transfer.isSupportedType(event.currentDataType))
				return;
			var list = ModelTransfer.getDescriptors(event.data);
			handler.accept(list);
		}
	});
}
 
Example 5
Source File: Tables.java    From olca-app with Mozilla Public License 2.0 6 votes vote down vote up
public static void onDrop(TableViewer table,
		Consumer<List<Descriptor>> fn) {
	Transfer transfer = ModelTransfer.getInstance();
	DropTarget target = new DropTarget(table.getTable(),
			DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_DEFAULT);
	target.setTransfer(new Transfer[] { transfer });
	target.addDropListener(new DropTargetAdapter() {
		@Override
		public void drop(DropTargetEvent event) {
			if (!transfer.isSupportedType(event.currentDataType))
				return;
			var list = ModelTransfer.getDescriptors(event.data);
			fn.accept(list);
		}
	});
}
 
Example 6
Source File: ChartManager.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public DropTarget createDropTarget ( final Transfer[] transfers, final DropTargetListener dropTargetListener )
{
    checkWidget ();

    final DropTarget target = new DropTarget ( this.chartArea, DND.DROP_DEFAULT | DND.DROP_COPY | DND.DROP_LINK );
    target.setTransfer ( transfers );
    target.addDropListener ( dropTargetListener );
    return target;
}
 
Example 7
Source File: LogViewer.java    From LogViewer with Eclipse Public License 2.0 5 votes vote down vote up
public void createPartControl(Composite parent) {
    //viewer
    this.parent = parent;
    documentListener = new ViewDocumentListener();
    tabfolder = new TabFolder(parent,0);
    tabfolder.addSelectionListener(new TabSelectionListener());
    viewer = new LogFileViewer(tabfolder,SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);

    // DnD
    DropTarget target = new DropTarget(parent, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK);
    target.setTransfer(new Transfer[] {FileTransfer.getInstance(), TextTransfer.getInstance()});
    target.addDropListener (new DropTargetAdapter() {
        public void dragEnter(DropTargetEvent e) {
            if (e.detail == DND.DROP_NONE)
                e.detail = DND.DROP_COPY;
        }
        public void drop(DropTargetEvent event) {
            if (event.data == null || ((String[])event.data).length < 1) {
                event.detail = DND.DROP_NONE;
                return;
            }
            //File file = new File(((String[])event.data)[0]);
            if (!checkAndOpenFile(LogFileType.LOGFILE_SYSTEM_FILE,((String[])event.data)[0], null, false))
                event.detail = DND.DROP_NONE;
        }
    });

    // fill the menues
    makeActions();
    hookContextMenu();
    contributeToActionBars();
    openAllLastOpenFiles();
}
 
Example 8
Source File: AbstractThemeableEditor.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void initializeDragAndDrop(ISourceViewer viewer)
{
	super.initializeDragAndDrop(viewer);

	// Adds snippet drag/drop support
	IDragAndDropService dndService = (IDragAndDropService) getSite().getService(IDragAndDropService.class);
	if (dndService == null)
	{
		return;
	}
	StyledText st = viewer.getTextWidget();
	DropTarget dropTarget = (DropTarget) st.getData(DND.DROP_TARGET_KEY);
	if (dropTarget != null)
	{
		Transfer[] transfers = dropTarget.getTransfer();
		List<Transfer> allTransfers = CollectionsUtil.newList(transfers);
		allTransfers.add(SnippetTransfer.getInstance());
		dropTarget.setTransfer(allTransfers.toArray(new Transfer[allTransfers.size()]));
		dropTarget.addDropListener(new SnippetDropTargetListener());
	}
	else
	{
		dndService.addMergedDropTarget(st, DND.DROP_COPY, new Transfer[] { SnippetTransfer.getInstance() },
				new SnippetDropTargetListener());
	}
}
 
Example 9
Source File: CallHierarchyViewPart.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void initDragAndDrop() {
    addDragAdapters(fCallHierarchyViewer);
    addDropAdapters(fCallHierarchyViewer);
    addDropAdapters(fLocationViewer);

    //dnd on empty hierarchy
    DropTarget dropTarget = new DropTarget(fPagebook, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK | DND.DROP_DEFAULT);
    dropTarget.setTransfer(new Transfer[] { LocalSelectionTransfer.getInstance() });
    dropTarget.addDropListener(new CallHierarchyTransferDropAdapter(this, fCallHierarchyViewer));
}
 
Example 10
Source File: TypeHierarchyViewPart.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void initDragAndDrop() {
	for (int i= 0; i < fAllViewers.length; i++) {
		addDragAdapters(fAllViewers[i]);
		addDropAdapters(fAllViewers[i]);
	}
	addDragAdapters(fMethodsViewer);
	fMethodsViewer.addDropSupport(DND.DROP_NONE, new Transfer[0], new DropTargetAdapter());

	//DND on empty hierarchy
	DropTarget dropTarget = new DropTarget(fPagebook, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK | DND.DROP_DEFAULT);
	dropTarget.setTransfer(new Transfer[] { LocalSelectionTransfer.getInstance() });
	dropTarget.addDropListener(new TypeHierarchyTransferDropAdapter(this, fAllViewers[0]));
}
 
Example 11
Source File: View.java    From codeexamples-eclipse with Eclipse Public License 1.0 5 votes vote down vote up
private void addDropListener(Composite parent) {
	LocalSelectionTransfer transfer = LocalSelectionTransfer.getTransfer();

	DropTargetAdapter dragAdapter = new DropTargetAdapter() {
		@Override
		public void drop(DropTargetEvent event) {
			Control droppedObj = (Control) ((StructuredSelection) transfer.getSelection()).getFirstElement();

			// Get the existing parent of the dragged control
			Composite oldParent = droppedObj.getParent();

			if (oldParent == parent) {
				return;
			}

			if  (droppedObj instanceof Label) {
				System.out.println("Dropped");
			}
			// handle the drop
			if (droppedObj instanceof Label) {
				Label droppedLabel = (Label) droppedObj;
				droppedLabel.setParent(parent); // Change parent
			}

			if (droppedObj instanceof Button) {
				Button droppedButton = (Button) droppedObj;
				droppedButton.setParent(parent); // Change parent
			}

			// request a layout pass
			oldParent.requestLayout();
			// If you change that to layout the layout will be correct
			parent.layout();
		}
	};

	DropTarget dropTarget = new DropTarget(parent, DND.DROP_MOVE | DND.DROP_COPY);
	dropTarget.setTransfer(new Transfer[] { transfer });
	dropTarget.addDropListener(dragAdapter);
}
 
Example 12
Source File: ScriptConsoleViewer.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
private void initDragDrop() {
    DragSource dragSource = new DragSource(this, DND.DROP_COPY | DND.DROP_MOVE);
    dragSource.addDragListener(new DragSourceAdapter());
    dragSource.setTransfer(new Transfer[] { org.eclipse.swt.dnd.TextTransfer.getInstance() });

    DropTarget dropTarget = new DropTarget(this, DND.DROP_COPY | DND.DROP_MOVE);
    dropTarget.setTransfer(new Transfer[] { LocalSelectionTransfer.getTransfer(),
            org.eclipse.swt.dnd.TextTransfer.getInstance() });
    dropTarget.addDropListener(new DragTargetAdapter());
}
 
Example 13
Source File: IdentifiableDropTarget.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
public IdentifiableDropTarget(Control target, IReceiver receiver){
	this.receiver = receiver;
	DropTarget dtarget = new DropTarget(target, DND.DROP_COPY);
	final TextTransfer textTransfer = TextTransfer.getInstance();
	Transfer[] types = new Transfer[] {
		textTransfer
	};
	dtarget.setTransfer(types);
	dtarget.addDropListener(this);
}
 
Example 14
Source File: FilterViewer.java    From tracecompass with Eclipse Public License 2.0 4 votes vote down vote up
public FilterViewer(Composite parent, int style, boolean isDialog) {
    super(parent, style);

    this.fIsDialog = isDialog;

    setLayout(new FillLayout());
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    setLayoutData(gd);

    final SashForm sash = new SashForm(this, SWT.HORIZONTAL);

    // Create the tree viewer to display the filter tree
    fViewer = new TreeViewer(sash, SWT.NONE);
    fViewer.setContentProvider(new FilterTreeContentProvider());
    fViewer.setLabelProvider(new FilterTreeLabelProvider());

    // Create the empty filter node properties panel
    fComposite = new Composite(sash, SWT.NONE);
    GridLayout gl = new GridLayout();
    gl.marginHeight = 0;
    gl.marginWidth = 0;
    fComposite.setLayout(gl);

    createContextMenu();

    fViewer.addSelectionChangedListener(event -> {
        if (!(event.getSelection().isEmpty()) && event.getSelection() instanceof IStructuredSelection) {
            // Update the filter node properties panel to the selection
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            ITmfFilterTreeNode node = (ITmfFilterTreeNode) selection.getFirstElement();
            updateFilterNodeComposite(node);
        } else {
            updateFilterNodeComposite(null);
        }
    });

    fViewer.getTree().addPaintListener(e -> {
        TmfFilterTreeNode root = (TmfFilterTreeNode) fViewer.getInput();
        if (root == null || root.getChildrenCount() == 0) {
            e.gc.setForeground(FOREGROUND_COLOR);
            e.gc.drawText(Messages.FilterViewer_EmptyTreeHintText, 5, 0);
        }
    });

    int operations = DND.DROP_MOVE | DND.DROP_COPY;
    DragSource dragSource = new org.eclipse.swt.dnd.DragSource(fViewer.getTree(), operations);
    dragSource.setTransfer(new Transfer[] { LocalSelectionTransfer.getTransfer() });
    dragSource.addDragListener(new FilterDragSourceAdapter(this));
    DropTarget dropTarget = new DropTarget(fViewer.getTree(), operations);
    dropTarget.setTransfer(new Transfer[] { LocalSelectionTransfer.getTransfer() });
    dropTarget.addDropListener(new FilterDropTargetAdapter(this));
}
 
Example 15
Source File: ExampleDropTargetInstaller.java    From statecharts with Eclipse Public License 1.0 4 votes vote down vote up
protected void registerDropListener(DropTarget target, DropTargetListener dropListener) {
	target.removeDropListener(dropListener);
	target.addDropListener(dropListener);
}
 
Example 16
Source File: PhotoShuffler.java    From codeexamples-eclipse with Eclipse Public License 1.0 4 votes vote down vote up
public static void main(String[] args) {
	// setup the SWT window
	Display display = new Display();
	final Shell shell = new Shell(display);
	shell.setLayout(new FillLayout());
	shell.setText("Photo Shuffler");

	// initialize a parent composite with a grid layout manager
	Composite parent = new Composite(shell, SWT.NONE);
	GridLayout gridLayout = new GridLayout();
	gridLayout.numColumns = 2;
	gridLayout.marginWidth=20;
	parent.setLayout(gridLayout);

	// determine the path where the pictures are stored
	String path = System.getProperty("user.dir") + "/images/";
	// initialize an array with the photograph names

	File imageDir= new File(path);
	
	// loop over the photo array and establish all listeners
	List<File> files = Arrays.stream(imageDir.listFiles())
				 	.filter(f -> f.isFile() && f.getName().endsWith(".png"))
					.collect(Collectors.toList());
	
	for (File file : files) {
		// labels serve as containers for the images
		Label label = new Label(parent, SWT.NONE);
		Image img = new Image(display,file.getAbsolutePath());
		label.setImage(img);

		// enable each label to be draggable
		DragSource source = new DragSource(label, DND.DROP_NONE);
		source.setTransfer(TextTransfer.getInstance()); // varargs are supported as of 4.7
		// add a drag listener
		source.addDragListener(new MyDragSourceListener(parent, source));

		// enable each label to be a drop target
		DropTarget target = new DropTarget(label, DND.DROP_NONE);
		target.setTransfer(new Transfer[] { TextTransfer.getInstance() }); // varargs are not yet supported see https://git.eclipse.org/r/#/c/92236			// add a drop listener
		target.addDropListener(new MyDropTargetListener(parent, target));
	}

	// show the SWT window
	shell.pack();
	shell.open();
	while (!shell.isDisposed()) {
		if (!display.readAndDispatch())
			display.sleep();
	}
	// tear down the SWT window
	display.dispose();
}