Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1e6ff8d1e384b013782b41916e30d85a138c63a3 (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) 2015 Christian W. Damus and others.
 * 
 * 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.infra.gmfdiag.css.properties.dnd;

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

import org.eclipse.core.resources.IFile;
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.gef.commands.CompoundCommand;
import org.eclipse.gmf.runtime.notation.EObjectListValueStyle;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.util.LocalSelectionTransfer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.commands.wrappers.EMFtoGEFCommandWrapper;
import org.eclipse.papyrus.infra.core.utils.AdapterUtils;
import org.eclipse.papyrus.infra.gmfdiag.common.helper.NotationHelper;
import org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSStyles;
import org.eclipse.papyrus.infra.gmfdiag.css.properties.Activator;
import org.eclipse.papyrus.infra.gmfdiag.css.properties.databinding.AddCSSStyleSheetCommand;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheetReference;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StylesheetsFactory;
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;

/**
 * Strategy for dropping CSS resources onto a diagram to add a stylesheet reference.
 */
public class StyleSheetDropStrategy extends TransactionalDropStrategy {
	public static final String ID = Activator.PLUGIN_ID + ".drop"; //$NON-NLS-1$

	public StyleSheetDropStrategy() {
		super();
	}

	public String getLabel() {
		return "Add Stylesheet Reference";
	}

	public String getDescription() {
		return "Adds a stylesheet to the diagram by reference to the dropped CSS resource";
	}

	public Image getImage() {
		return null;
	}

	public String getID() {
		return ID;
	}

	public int getPriority() {
		return 0;
	}

	@Override
	protected Command doGetCommand(Request request, EditPart targetEditPart) {
		Command result = null;

		// Only diagram supports stylesheet reference
		View targetView = NotationHelper.findView(targetEditPart);
		targetView = (targetView != null) ? targetView.getDiagram() : targetView;
		if (targetView != null) {
			// The DropObjectsRequest has only EObjects if it contains anything, so go directly to the source
			List<?> objects;
			ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
			if (selection instanceof IStructuredSelection) {
				objects = ((IStructuredSelection) selection).toList();
			} else {
				objects = Collections.emptyList();
			}

			EObjectListValueStyle existingReferences = (EObjectListValueStyle) targetView.getNamedStyle(NotationPackage.Literals.EOBJECT_LIST_VALUE_STYLE, CSSStyles.CSS_DIAGRAM_STYLESHEETS_KEY);
			List<String> cssPaths = Lists.newArrayListWithCapacity(objects.size());
			for (Object next : objects) {
				IFile file = AdapterUtils.adapt(next, IFile.class, null);
				if ((file != null) && "css".equals(file.getFileExtension())) {
					// Maybe we have this one already?
					final String path = file.getFullPath().toString();
					boolean found = false;
					if (existingReferences != null) {
						for (StyleSheetReference ref : Iterables.filter(existingReferences.getEObjectListValue(), StyleSheetReference.class)) {
							if (path.equals(ref.getPath())) {
								found = true;
								break;
							}
						}
					}

					if (!found) {
						cssPaths.add(path);
					}
				} else {
					// All-or-nothing drop
					cssPaths.clear();
					break;
				}
			}

			if (!cssPaths.isEmpty()) {
				CompoundCommand compound = new CompoundCommand(getLabel());
				TransactionalEditingDomain domain = getTransactionalEditingDomain(targetEditPart);
				for (String next : cssPaths) {
					StyleSheetReference ref = StylesheetsFactory.eINSTANCE.createStyleSheetReference();
					ref.setPath(next);
					AddCSSStyleSheetCommand command = new AddCSSStyleSheetCommand(domain, targetView,
							CSSStyles.CSS_DIAGRAM_STYLESHEETS_KEY,
							NotationPackage.Literals.EOBJECT_LIST_VALUE_STYLE, NotationPackage.Literals.EOBJECT_LIST_VALUE_STYLE__EOBJECT_LIST_VALUE,
							ref);
					compound.add(EMFtoGEFCommandWrapper.wrap(command));
				}

				result = compound.unwrap();
			}
		}

		return result;
	}

}

Back to the top