Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a38465ccbb84898b679902585d24afe69721ce9f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*****************************************************************************
 * Copyright (c) 2012 CEA LIST.
 *
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.nattable.listener;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.eclipse.emf.edit.ui.dnd.LocalTransfer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager;
import org.eclipse.papyrus.infra.nattable.utils.LocationValue;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.graphics.Point;


public class NatTableDropListener implements DropTargetListener {


	private final INattableModelManager manager;

	private LocationValue dropKindValue;

	public NatTableDropListener(final INattableModelManager manager) {
		this.manager = manager;
	}

	public void dragEnter(final DropTargetEvent event) {

	}

	public void dragLeave(final DropTargetEvent event) {

	}

	public void dragOperationChanged(final DropTargetEvent event) {

	}

	public void dragOver(final DropTargetEvent event) {
		this.dropKindValue = null;
		final LocalTransfer localTransfer = LocalTransfer.getInstance();
		final Object data = localTransfer.nativeToJava(event.currentDataType);
		IStructuredSelection structuredSelection = null;
		if(data instanceof IStructuredSelection) {
			structuredSelection = (IStructuredSelection)data;
		}
		final Collection<Object> objectsToAdd = Collections.checkedCollection(structuredSelection.toList(), Object.class);
		this.dropKindValue = this.manager.getLocationInTheTable(new Point(event.x, event.y));
		int drop = DND.DROP_NONE;
		switch(this.dropKindValue.getKind()) {
		case AFTER_COLUMN_HEADER:
			if(this.manager.canDropColumnsElement(objectsToAdd)) {
				drop = DND.DROP_DEFAULT;
			}
			break;
		case AFTER_ROW_HEADER:
			if(this.manager.canDropRowElement(objectsToAdd)) {
				drop = DND.DROP_DEFAULT;
			}
			break;
		case COLUMN_HEADER:
			if(this.manager.canInsertColumns(objectsToAdd, this.dropKindValue.getColumnIndex())) {
				drop = DND.DROP_DEFAULT;
			}
			break;
		case ROW_HEADER:
			if(this.manager.canInsertRow(objectsToAdd, this.dropKindValue.getRowIndex())) {
				drop = DND.DROP_DEFAULT;
			}
			break;
		case CELL:
			//TODO
			drop = DND.DROP_NONE;
			break;
		case UNKNOWN:
			drop = DND.DROP_NONE;
			break;
		default:
			drop = DND.DROP_NONE;
			break;
		}
		event.detail = drop;
	}

	public void drop(final DropTargetEvent event) {
		//we drop the elements into the table
		LocalTransfer localTransfer = LocalTransfer.getInstance();
		Object data = localTransfer.nativeToJava(event.currentDataType);
		if(data instanceof StructuredSelection) {
			final IStructuredSelection selection = (IStructuredSelection)data;
			final List<Object> droppedElements = selection.toList();
			if(this.dropKindValue != null) {
				switch(this.dropKindValue.getKind()) {
				case AFTER_COLUMN_HEADER:
					this.manager.addColumns(droppedElements);
					break;
				case AFTER_ROW_HEADER:
					this.manager.addRows(droppedElements);
					break;
				case COLUMN_HEADER:
					this.manager.insertColumns(droppedElements, this.dropKindValue.getColumnIndex());
					break;
				case ROW_HEADER:
					this.manager.insertRows(droppedElements, this.dropKindValue.getRowIndex());
					break;
				case CELL:
					//TODO
					break;
				case UNKNOWN:
					break;
				default:
					break;
				}
			}
		}
		this.dropKindValue = null;
	}


	public void dropAccept(final DropTargetEvent event) {

	}

}

Back to the top