Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 107487424d428b102b526dca94991369c00e4f6b (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
/*****************************************************************************
 * Copyright (c) 2013 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:
 *   CEA LIST - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.cdo.internal.ui.hyperlink;

import java.util.List;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.diagram.ui.requests.DropObjectsRequest;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.cdo.internal.ui.dnd.CDOResourceURITransferData;
import org.eclipse.papyrus.commands.Activator;
import org.eclipse.papyrus.infra.gmfdiag.common.helper.SemanticElementHelper;
import org.eclipse.papyrus.infra.gmfdiag.dnd.strategy.TransactionalDropStrategy;
import org.eclipse.swt.graphics.Image;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;


/**
 * A drop strategy that extracts URIs of CDO resources from an Ecore representation of a {@link CDOResourceURITransferData} to create hyperlinks.
 */
public class CDOResourceURIDropStrategy extends TransactionalDropStrategy {

	private static final String ID = Activator.PLUGIN_ID + ".cdoResourceURI"; //$NON-NLS-1$

	public CDOResourceURIDropStrategy() {
		super();
	}

	@Override
	public String getLabel() {
		return Messages.DropStrategy_label;
	}

	@Override
	public String getDescription() {
		return Messages.DropStrategy_desc;
	}

	@Override
	public Image getImage() {
		return null;
	}

	@Override
	public String getID() {
		return ID;
	}

	@Override
	public int getPriority() {
		return 0;
	}

	@Override
	protected Command doGetCommand(Request request, EditPart targetEditPart) {
		if(request instanceof DropObjectsRequest) {
			View view = getTargetView(targetEditPart);
			if(view == null) {
				return null;
			}

			final View mainView = SemanticElementHelper.findPrimaryView(view);
			if(mainView instanceof Diagram) {
				// don't create hyperlinks on the diagram surface
				return null;
			}

			final DropObjectsRequest dropRequest = (DropObjectsRequest)request;
			List<URI> hyperlinkURIs = null;

			for(EObject next : Iterables.filter(dropRequest.getObjects(), EObject.class)) {
				if(CDOResourceURITransferData.isCDOResourceURITransferData(next)) {
					if(hyperlinkURIs == null) {
						hyperlinkURIs = Lists.newArrayListWithCapacity(dropRequest.getObjects().size());
					}
					hyperlinkURIs.addAll(CDOResourceURITransferData.fromEObject(next).getURIs());
				}
			}

			if(hyperlinkURIs != null) {
				final TransactionalEditingDomain domain = getTransactionalEditingDomain(targetEditPart);

				final List<URI> _hyperlinkURIs = hyperlinkURIs;
				return new Command() {

					@Override
					public void execute() {
						for(URI next : _hyperlinkURIs) {
							new CreateCDOResourceHyperlinkCommand(domain, mainView, next.lastSegment(), next, false).execute();
						}
					}
				};
			}
		}

		return null;
	}

}

Back to the top