1 /*******************************************************************************
2  * Copyright (c) 2010, 2015 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  ******************************************************************************/
14 
15 package org.eclipse.e4.ui.workbench.addons.dndaddon;
16 
17 import org.eclipse.e4.core.contexts.IEclipseContext;
18 import org.eclipse.e4.ui.model.application.ui.MUIElement;
19 import org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder;
20 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
21 import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
22 import org.eclipse.e4.ui.workbench.modeling.EPartService;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.graphics.Cursor;
25 import org.eclipse.swt.graphics.Rectangle;
26 import org.eclipse.swt.widgets.Display;
27 
28 /**
29  *
30  */
31 abstract class DropAgent {
32 	protected DnDManager dndManager;
33 
34 	/**
35 	 * @param manager
36 	 */
DropAgent(DnDManager manager)37 	public DropAgent(DnDManager manager) {
38 		dndManager = manager;
39 	}
40 
reactivatePart(MUIElement dragElement)41 	public void reactivatePart(MUIElement dragElement) {
42 		IEclipseContext context = dndManager.getModelService().getContainingContext(dragElement);
43 		if (context == null) {
44 			return;
45 		}
46 
47 		EPartService ps = context.get(EPartService.class);
48 		if (ps == null) {
49 			return;
50 		}
51 
52 		MPart partToActivate = null;
53 		if (dragElement instanceof MPart) {
54 			partToActivate = (MPart) dragElement;
55 		} else if (dragElement instanceof MPlaceholder) {
56 			MPlaceholder ph = (MPlaceholder) dragElement;
57 			if (ph.getRef() instanceof MPart) {
58 				partToActivate = (MPart) ph.getRef();
59 			}
60 		} else if (dragElement instanceof MPartStack) {
61 			MPartStack stack = (MPartStack) dragElement;
62 			if (stack.getSelectedElement() instanceof MPart) {
63 				partToActivate = (MPart) stack.getSelectedElement();
64 			} else if (stack.getSelectedElement() instanceof MPlaceholder) {
65 				MPlaceholder ph = (MPlaceholder) stack.getSelectedElement();
66 				if (ph.getRef() instanceof MPart) {
67 					partToActivate = (MPart) ph.getRef();
68 				}
69 			}
70 		}
71 
72 		if (partToActivate != null) {
73 			ps.activate(null);
74 			ps.activate(partToActivate);
75 		}
76 	}
77 
canDrop(MUIElement dragElement, DnDInfo info)78 	public abstract boolean canDrop(MUIElement dragElement, DnDInfo info);
79 
drop(MUIElement dragElement, DnDInfo info)80 	public abstract boolean drop(MUIElement dragElement, DnDInfo info);
81 
track(MUIElement dragElement, DnDInfo info)82 	public abstract boolean track(MUIElement dragElement, DnDInfo info);
83 
getCursor(Display display, MUIElement dragElement, DnDInfo info)84 	public Cursor getCursor(Display display, MUIElement dragElement, DnDInfo info) {
85 		return display.getSystemCursor(SWT.CURSOR_HAND);
86 	}
87 
getRectangle(MUIElement dragElement, DnDInfo info)88 	public Rectangle getRectangle(MUIElement dragElement, DnDInfo info) {
89 		return null;
90 	}
91 
dragEnter(MUIElement dragElement, DnDInfo info)92 	public void dragEnter(MUIElement dragElement, DnDInfo info) {
93 	}
94 
dragLeave(MUIElement dragElement, DnDInfo info)95 	public void dragLeave(MUIElement dragElement, DnDInfo info) {
96 	}
97 
98 	/**
99 	 * This agent is being disposed
100 	 */
dispose()101 	public void dispose() {
102 	}
103 }
104