Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d10ed59aa39cdefa840c63edbfd363900469f8a8 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*****************************************************************************
 * Copyright (c) 2010 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:
 *  Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
 *  Fanch BONNABESSE (ALL4TEC) fanch.bonnabesse@all4tec.net - Bug 497289
 *
 *****************************************************************************/
package org.eclipse.papyrus.views.modelexplorer;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.jface.viewers.ColumnViewerEditor;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
import org.eclipse.jface.viewers.FocusCellHighlighter;
import org.eclipse.jface.viewers.IElementComparer;
import org.eclipse.jface.viewers.TreeViewerEditor;
import org.eclipse.jface.viewers.TreeViewerFocusCellManager;
import org.eclipse.papyrus.emf.facet.custom.metamodel.v0_2_0.internal.treeproxy.EObjectTreeElement;
import org.eclipse.papyrus.emf.facet.custom.metamodel.v0_2_0.internal.treeproxy.EReferenceTreeElement;
import org.eclipse.papyrus.views.modelexplorer.matching.HashCodeCalculus;
import org.eclipse.papyrus.views.modelexplorer.matching.IMatchingItem;
import org.eclipse.papyrus.views.modelexplorer.matching.IReferencable;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.internal.navigator.dnd.NavigatorDnDService;
import org.eclipse.ui.navigator.CommonDragAdapter;
import org.eclipse.ui.navigator.CommonDropAdapter;
import org.eclipse.ui.navigator.CommonViewer;

/**
 * this class was created in order to access to the drop adapter
 *
 */
@SuppressWarnings("restriction")
public class CustomCommonViewer extends CommonViewer {
	protected CommonDropAdapter dropAdapter;

	public CustomCommonViewer(String aViewerId, Composite aParent, int aStyle) {
		super(aViewerId, aParent, aStyle);
		// TODO Auto-generated constructor stub
		setComparer(new IElementComparer() {

			@Override
			public int hashCode(Object element) {
				if (element instanceof EObjectTreeElement) {
					EObject eObject = ((EObjectTreeElement) element).getEObject();
					return HashCodeCalculus.getHashCode(eObject);
				}

				if (element instanceof EReferenceTreeElement) {
					EObject eParent = ((EReferenceTreeElement) element).getParent().getEObject();
					EReference eref = ((EReferenceTreeElement) element).getEReference();
					return HashCodeCalculus.getHashCode(eParent, eref);
				}
				if (element instanceof IReferencable) {
					IReferencable ref = (IReferencable) element;
					return ref.getElementBehind().hashCode();
				}
				if (element instanceof IMatchingItem) {
					IMatchingItem matchItem = (IMatchingItem) element;
					return matchItem.matchingItemHashcode();
				}
				return element.hashCode();
			}

			@Override
			public boolean equals(Object a, Object b) {
				if (a instanceof IMatchingItem) {
					return ((IMatchingItem) a).matchingItemEquals(b);
				}

				if (b != null) {
					return b.equals(a);
				}
				return false;
			}
		});
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void initDragAndDrop() {
		dropAdapter = null;
		int operations = DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK;

		CommonDragAdapter dragAdapter = createDragAdapter();
		addDragSupport(operations, dragAdapter.getSupportedDragTransfers(), dragAdapter);
		dropAdapter = createDropAdapter();
		addDropSupport(operations, dropAdapter.getSupportedDropTransfers(), dropAdapter);

		NavigatorDnDService dnd = (NavigatorDnDService) getNavigatorContentService().getDnDService();
		dnd.setDropAdaptor(dropAdapter);
	}

	/**
	 * get the listener in order to parameterize during the runtime the drop
	 *
	 * @return the dropadapter
	 */
	public CommonDropAdapter getDropAdapter() {
		return dropAdapter;
	}

	/**
	 * Overridden to disable cell editor activation by single click, it can be activated by double click instead.
	 */
	@Override
	protected ColumnViewerEditor createViewerEditor() {
		// instantiate abstract focus cell high-lighter as dummy object for focus manager. The sub class
		// FocusCellOwnerDrawHighlighter would break multi-selections in Papyrus, see bug 419591.
		// (but we need to create a high-lighter, since the focus cell manager does not accept null pointer. The
		// TreeViewerEditor could work without focusCellManager, but would ignore keyboard events in this case.)
		FocusCellHighlighter fch = new FocusCellHighlighter(this) {
		};
		TreeViewerFocusCellManager focusCellManager = new TreeViewerFocusCellManager(
				this, fch);



		TreeViewerEditor.create(this, focusCellManager, new ColumnViewerEditorActivationStrategy(this) {
			@Override
			protected boolean isEditorActivationEvent(
					ColumnViewerEditorActivationEvent event) {
				// activation will uses F2 (also used by rename-popup, but not taken into account by the latter
				// for model elements for which a direct-editor exists)
				return (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED &&
						event.keyCode == SWT.F2) || (event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC);
			}
		}, ColumnViewerEditor.KEYBOARD_ACTIVATION);
		ColumnViewerEditor editor = this.getColumnViewerEditor();

		return editor;
	}

	/**
	 * @see org.eclipse.ui.navigator.CommonViewer#dispose()
	 *
	 */
	@Override
	public void dispose() {
		// Remove the custom column viewer editor which causes NPE after dispose
		// ViewerEditor cannot be nulled or disposed, so we just recreate a default one
		setColumnViewerEditor(super.createViewerEditor());
		super.dispose();
	}

}

Back to the top