Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 850220e1da40587027a82be7154675659f7cf982 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*****************************************************************************
 * 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.css.handler;

import java.util.Iterator;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.NamedStyle;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.Style;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.utils.ServiceUtilsForActionHandlers;
import org.eclipse.papyrus.infra.emf.Activator;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.helper.NotationHelper;
import org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSAnnotations;


/**
 * This handler resets all the local appearance to their default value
 * for a set of GMF Views.
 * 
 * @author Camille Letavernier
 */
public class ResetStyleHandler extends AbstractHandler {

	public Object execute(ExecutionEvent event) throws ExecutionException {
		ISelection selection;
		try {
			selection = ServiceUtilsForActionHandlers.getInstance().getNestedActiveIEditorPart().getSite().getSelectionProvider().getSelection();
			if(selection.isEmpty()) {
				return null;
			}
		} catch (ServiceException ex) {
			// TODO Auto-generated catch block
			Activator.log.error(ex);
			return null;
		}

		if(!(selection instanceof IStructuredSelection)) {
			return null;
		}

		IStructuredSelection sSelection = (IStructuredSelection)selection;
		Iterator<?> iterator = sSelection.iterator();

		TransactionalEditingDomain domain = null;
		while(iterator.hasNext()) {
			Object selectedItem = iterator.next();
			View view = NotationHelper.findView(selectedItem);
			if(view == null) {
				continue;
			}

			EditingDomain editingDomain = EMFHelper.resolveEditingDomain(view);
			if(editingDomain instanceof TransactionalEditingDomain) {
				domain = (TransactionalEditingDomain)editingDomain;
			}
			break;
		}

		if(domain == null) {
			return null;
		}

		Command command = new ResetStyleCommand(domain, sSelection);
		domain.getCommandStack().execute(command);

		return null;
	}

	private class ResetStyleCommand extends RecordingCommand {

		private IStructuredSelection selection;

		public ResetStyleCommand(TransactionalEditingDomain domain, IStructuredSelection selection) {
			super(domain);
			this.selection = selection;
		}

		@Override
		public void doExecute() {
			Iterator<?> iterator = selection.iterator();
			while(iterator.hasNext()) {
				Object selectedItem = iterator.next();
				View view = NotationHelper.findView(selectedItem);
				if(view == null) {
					continue;
				}

				if(view instanceof Diagram) {
					resetDiagram((Diagram)view);
					break;
				} else {
					//Reset the style attribute to their default value
					resetStyle(view);
				}
			}
		}

		private void resetDiagram(Diagram diagram) {
			for(Object viewObject : diagram.getChildren()) {
				if(viewObject instanceof View) {
					resetStyle((View)viewObject, true);
				}
			}
		}

		private void resetStyle(View view, boolean recursive) {
			resetStyle(view);
			if(recursive) {
				for(Object childObject : view.getChildren()) {
					if(childObject instanceof View) {
						resetStyle((View)childObject, recursive);
					}
				}
			}
		}

		private void resetStyle(View view) {
			for(Object styleObject : view.getStyles()) {
				if(styleObject instanceof Style) {
					resetStyle((Style)styleObject);
				}
			}
			if(view instanceof Style) {
				resetStyle((Style)view);
			}

			//Remove the "forceValue" annotations
			resetAnnotations(view);
		}

		private void resetStyle(Style style) {
			if(style instanceof NamedStyle) {
				//Skip custom styles. 
				//TODO: We should skip CSS Styles (CSSClass, CSSId, CSSStyle, DiagramStyleSheets),
				//and reset custom GMF Styles (elementIcon, shadow, ...). 
				//What about external custom styles (ie. unkwnown styles)?
				//They should be stylable, but they might contain something we don't want to reset...
				return;
			}

			for(EStructuralFeature feature : style.eClass().getEAllStructuralFeatures()) {
				//Only edit Style features
				if(NotationPackage.eINSTANCE.getStyle().isSuperTypeOf(feature.getEContainingClass())) {
					//Reset the value to default
					style.eUnset(feature);
				}
			}
		}

		private void resetAnnotations(View view) {
			Iterator<EAnnotation> iterator = view.getEAnnotations().iterator();
			while(iterator.hasNext()) {
				if(CSSAnnotations.CSS_FORCE_VALUE.equals(iterator.next().getSource())) {
					iterator.remove();
				}
			}
		}
	}


}

Back to the top