Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a3b45239129c4c7ba56f9f9e32a0461358ea440d (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
/*****************************************************************************
 * Copyright (c) 2009 Atos Origin.
 *
 *    
 * 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:
 *  Tristan Faure (Atos Origin) tristan.faure@atosorigin.com - Initial API and implementation
 *
  *****************************************************************************/
package org.eclipse.papyrus.diagramprofile.parsers;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.common.ui.services.parser.IParserEditStatus;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserEditStatus;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.emf.type.core.commands.SetValueCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.gmf.runtime.emf.ui.services.parser.ISemanticParser;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.papyrus.diagramprofile.Activator;
import org.eclipse.papyrus.resource.ResourceEObject;

public class ResourceEObjectParser implements ISemanticParser {

	private static final Pattern patternForBaseElement = Pattern.compile("base_.*");

	private final EStructuralFeature eStructuralFeature;
	
	private String oclRule = "";

	public ResourceEObjectParser(String oclRule, EStructuralFeature eStructuralFeature) {
		this.oclRule = oclRule;
		this.eStructuralFeature = eStructuralFeature;
	}

	public IContentAssistProcessor getCompletionProcessor(IAdaptable element) {
		return null;
	}

	public String getEditString(IAdaptable element, int flags) {
		return getString(element);
	}

	public ICommand getParseCommand(IAdaptable element, String newString, int flags) {
		ICommand result = null;
		if (element instanceof EObjectAdapter) {
			EObjectAdapter adapter = (EObjectAdapter) element;
			if (adapter.getRealObject() instanceof EObject) {
				Object runOcl = getEobjectFromOCLRUle(element);
				if (runOcl instanceof EObject) {
					EObject eobject = (EObject) runOcl;
					result = new SetValueCommand(new SetRequest(eobject, eStructuralFeature, newString));
				}
			}
		}
		return result;
	}

	public String getPrintString(IAdaptable element, int flags) {
		return getString(element);
	}

	private Object getEobjectFromOCLRUle(IAdaptable element) {
		Object result = null;
		if (element instanceof EObjectAdapter) {
			EObjectAdapter adapter = (EObjectAdapter) element;
			if (adapter.getRealObject() instanceof ResourceEObject) {
				ResourceEObject rEobject = (ResourceEObject) adapter.getRealObject();
				result = org.eclipse.papyrus.diagramprofile.utils.OCLUtils.runOclRule(oclRule, rEobject);
			}
		}
		return result;
	}

	private String getString(IAdaptable element) {
		Object result = getEobjectFromOCLRUle(element);
		if (result != null && result instanceof EObject) {
			Object object = ((EObject) result).eGet(eStructuralFeature);
			return object == null ? "" : object.toString();
		}
		return "";
	}

	private EObject getBaseElement(ResourceEObject rEobject) {
		for (EStructuralFeature f : rEobject.eClass().getEAllStructuralFeatures()) {
			if (patternForBaseElement.matcher(f.getName()).matches()) {
				Object result = rEobject.eGet(f);
				if (result != null && result instanceof EObject) {
					return (EObject) result;
				}
			}
		}
		return null;
	}

	public boolean isAffectingEvent(Object event, int flags) {
		return getEStructuralFeature(event).equals(eStructuralFeature);
	}

	public IParserEditStatus isValidEditString(IAdaptable element, String editString) {
		if (element instanceof EObjectAdapter) {
			EObjectAdapter eobjectAdapter = (EObjectAdapter) element;
			if (eobjectAdapter.getRealObject() instanceof ResourceEObject) {
				return new ParserEditStatus(Status.OK, Activator.PLUGIN_ID, ParserEditStatus.EDITABLE, "ok", null);
			}
		}
		return new ParserEditStatus(Status.ERROR, Activator.PLUGIN_ID, ParserEditStatus.EDITABLE, "error", null);
	}

	/**
	 * Get the EStructuralFeature of the given notification
	 * 
	 * @param notification
	 *            the notification
	 * @return the EStructuralFeature
	 */
	protected EStructuralFeature getEStructuralFeature(Object notification) {
		EStructuralFeature featureImpl = null;
		if (notification instanceof Notification) {
			Object feature = ((Notification) notification).getFeature();
			if (feature instanceof EStructuralFeature) {
				featureImpl = (EStructuralFeature) feature;
			}
		}
		return featureImpl;
	}

	public boolean areSemanticElementsAffected(EObject listener, Object notification) {
		return true;
	}

	public List getSemanticElementsBeingParsed(EObject element) {
		List result = new ArrayList();
		result.add(element);
		result.add(org.eclipse.papyrus.diagramprofile.utils.OCLUtils.runOclRule(oclRule, element));
		return result;
	}

}

Back to the top