Project

General

Profile

Download (4.19 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.gef.dnd;
12

    
13
import org.eclipse.swt.dnd.DND;
14

    
15
import org.eclipse.gef.EditPart;
16
import org.eclipse.gef.EditPartViewer;
17
import org.eclipse.gef.Request;
18
import org.eclipse.gef.requests.CreateRequest;
19
import org.eclipse.gef.requests.CreationFactory;
20
import org.eclipse.gef.requests.SimpleFactory;
21

    
22
/**
23
 * Performs a native Drop using the {@link TemplateTransfer}. The Drop is
24
 * performed by using a {@link CreateRequest} to obtain a <code>Command</code>
25
 * from the targeted <code>EditPart</code>.
26
 * <P>
27
 * This class is <code>abstract</code>. Subclasses are responsible for providing
28
 * the appropriate <code>Factory</code> object based on the template that is
29
 * being dragged.
30
 * 
31
 * @since 2.1
32
 * @author Eric Bordeau
33
 */
34
public class TemplateTransferDropTargetListener extends
35
		AbstractTransferDropTargetListener {
36

    
37
	/**
38
	 * Constructs a listener on the specified viewer.
39
	 * 
40
	 * @param viewer
41
	 *            the EditPartViewer
42
	 */
43
	public TemplateTransferDropTargetListener(EditPartViewer viewer) {
44
		super(viewer, TemplateTransfer.getInstance());
45
	}
46

    
47
	/**
48
	 * @see org.eclipse.gef.dnd.AbstractTransferDropTargetListener#createTargetRequest()
49
	 */
50
	protected Request createTargetRequest() {
51
		// Look at the data on templatetransfer.
52
		// Create factory
53
		CreateRequest request = new CreateRequest();
54
		request.setFactory(getFactory(TemplateTransfer.getInstance()
55
				.getTemplate()));
56
		return request;
57
	}
58

    
59
	/**
60
	 * A helper method that casts the target Request to a CreateRequest.
61
	 * 
62
	 * @return CreateRequest
63
	 */
64
	protected final CreateRequest getCreateRequest() {
65
		return ((CreateRequest) getTargetRequest());
66
	}
67

    
68
	/**
69
	 * Returns the appropriate Factory object to be used for the specified
70
	 * template. This Factory is used on the CreateRequest that is sent to the
71
	 * target EditPart.
72
	 * 
73
	 * @param template
74
	 *            the template Object
75
	 * @return a Factory
76
	 */
77
	protected CreationFactory getFactory(Object template) {
78
		if (template instanceof CreationFactory) {
79
			return ((CreationFactory) template);
80
		} else if (template instanceof Class) {
81
			return new SimpleFactory((Class) template);
82
		} else
83
			return null;
84
	}
85

    
86
	/**
87
	 * The purpose of a template is to be copied. Therefore, the drop operation
88
	 * can't be anything but <code>DND.DROP_COPY</code>.
89
	 * 
90
	 * @see AbstractTransferDropTargetListener#handleDragOperationChanged()
91
	 */
92
	protected void handleDragOperationChanged() {
93
		getCurrentEvent().detail = DND.DROP_COPY;
94
		super.handleDragOperationChanged();
95
	}
96

    
97
	/**
98
	 * The purpose of a template is to be copied. Therefore, the Drop operation
99
	 * is set to <code>DND.DROP_COPY</code> by default.
100
	 * 
101
	 * @see org.eclipse.gef.dnd.AbstractTransferDropTargetListener#handleDragOver()
102
	 */
103
	protected void handleDragOver() {
104
		getCurrentEvent().detail = DND.DROP_COPY;
105
		getCurrentEvent().feedback = DND.FEEDBACK_SCROLL | DND.FEEDBACK_EXPAND;
106
		super.handleDragOver();
107
	}
108

    
109
	/**
110
	 * Overridden to select the created object.
111
	 * 
112
	 * @see org.eclipse.gef.dnd.AbstractTransferDropTargetListener#handleDrop()
113
	 */
114
	protected void handleDrop() {
115
		super.handleDrop();
116
		selectAddedObject();
117
	}
118

    
119
	private void selectAddedObject() {
120
		Object model = getCreateRequest().getNewObject();
121
		if (model == null)
122
			return;
123
		EditPartViewer viewer = getViewer();
124
		viewer.getControl().forceFocus();
125
		Object editpart = viewer.getEditPartRegistry().get(model);
126
		if (editpart instanceof EditPart) {
127
			// Force a layout first.
128
			getViewer().flush();
129
			viewer.select((EditPart) editpart);
130
		}
131
	}
132

    
133
	/**
134
	 * Assumes that the target request is a {@link CreateRequest}.
135
	 */
136
	protected void updateTargetRequest() {
137
		CreateRequest request = getCreateRequest();
138
		request.setLocation(getDropLocation());
139
	}
140

    
141
}
(8-8/10)