Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f2dcb79960e5cd092bc38b04ef3cf6de7acfad28 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*****************************************************************************
 * 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.HashSet;
import java.util.Iterator;
import java.util.Set;

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.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.utils.ServiceUtilsForActionHandlers;
import org.eclipse.papyrus.infra.emf.appearance.helper.VisualInformationPapyrusConstants;
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;
import org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSStyles;


/**
 * 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 {

	private static Set<String> papyrusStyleAnnotations = new HashSet<String>();
	static {
		papyrusStyleAnnotations.add(VisualInformationPapyrusConstants.DISPLAY_NAMELABELICON);
		papyrusStyleAnnotations.add(VisualInformationPapyrusConstants.SHADOWFIGURE);
		papyrusStyleAnnotations.add(VisualInformationPapyrusConstants.QUALIFIED_NAME);
	}

	public Object execute(ExecutionEvent event) throws ExecutionException {
		ISelection selection;

		try {
			//FIXME: Use ServiceUtilsForHandlers
			//FIXME: This method is sometimes called manually with a null ExecutionEvent. It won't work with ServiceUtilsForHandlers  
			IMultiDiagramEditor editor = ServiceUtilsForActionHandlers.getInstance().getServiceRegistry().getService(IMultiDiagramEditor.class);
			selection = editor.getEditorSite().getSelectionProvider().getSelection();
		} catch (ServiceException ex) {
			throw new ExecutionException(ex.getMessage(), ex);
		}

		//		selection = HandlerUtil.getCurrentSelection(event);
		if(selection == null || selection.isEmpty()) {
			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, true);
				}
			}
		}

		private void resetDiagram(Diagram diagram) {
			for(Object viewObject : diagram.getChildren()) {
				if(viewObject instanceof View) {
					resetStyle((View)viewObject, true);
				}
			}
			for(Object lineObject : diagram.getEdges()) {
				if(lineObject instanceof View) {
					resetStyle((View)lineObject, 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) {

			Iterator<?> styleIterator = view.getStyles().iterator();
			while(styleIterator.hasNext()) {
				Object styleObject = styleIterator.next();
				if(styleObject instanceof NamedStyle) {
					NamedStyle customStyle = (NamedStyle)styleObject;
					if(!CSSStyles.RESERVED_KEYWORDS.contains(customStyle.getName())) {
						styleIterator.remove();
					}
				} else if(styleObject instanceof Style) {
					resetStyle((Style)styleObject);
				}
			}

			if(view instanceof Style) {
				resetStyle((Style)view);
			}

			//Remove the "forceValue" annotations
			resetAnnotations(view);
			//Remove the Papyrus Style EAnnotations
			resetStyleAnnotations(view);

			if(view.eClass() != NotationPackage.eINSTANCE.getDecorationNode()) {
				//Reset the visibility (Except for labels which are not yet supported)
				view.eUnset(NotationPackage.eINSTANCE.getView_Visible());
			}
		}

		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);
				}
			}
		}

		//Resets the "Force Value" annotations (Tags to indicate that the user 
		//has manually selected a value, which will override the CSS Style)
		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();
				}
			}
		}

		//Resets the "Custom style" Annotations (elementIcon, shadow, qualifiedName)
		private void resetStyleAnnotations(View view) {
			Iterator<EAnnotation> iterator = view.getEAnnotations().iterator();
			while(iterator.hasNext()) {
				if(papyrusStyleAnnotations.contains(iterator.next().getSource())) {
					iterator.remove();
				}
			}
		}
	}


}

Back to the top