Lines 9-22
Link Here
|
9 |
* |
9 |
* |
10 |
* Contributors: |
10 |
* Contributors: |
11 |
* Ansgar Radermacher (CEA LIST) ansgar.radermacher@cea.fr - Initial API and implementation |
11 |
* Ansgar Radermacher (CEA LIST) ansgar.radermacher@cea.fr - Initial API and implementation |
12 |
* |
12 |
* Amine EL KOUHEN (CEA LIST / LIFL) Updates |
13 |
*****************************************************************************/ |
13 |
*****************************************************************************/ |
14 |
|
14 |
|
15 |
package org.eclipse.papyrus.validation; |
15 |
package org.eclipse.papyrus.validation; |
16 |
|
16 |
|
17 |
import java.util.List; |
|
|
18 |
|
19 |
import org.apache.commons.lang.WordUtils; |
20 |
import org.eclipse.core.resources.IFile; |
17 |
import org.eclipse.core.resources.IFile; |
21 |
import org.eclipse.core.resources.IMarker; |
18 |
import org.eclipse.core.resources.IMarker; |
22 |
import org.eclipse.core.resources.ResourcesPlugin; |
19 |
import org.eclipse.core.resources.ResourcesPlugin; |
Lines 28-43
Link Here
|
28 |
import org.eclipse.emf.ecore.EValidator; |
25 |
import org.eclipse.emf.ecore.EValidator; |
29 |
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; |
26 |
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; |
30 |
import org.eclipse.emf.edit.domain.EditingDomain; |
27 |
import org.eclipse.emf.edit.domain.EditingDomain; |
31 |
import org.eclipse.emf.facet.infra.browser.uicore.internal.model.LinkItem; |
|
|
32 |
import org.eclipse.jface.preference.IPreferenceStore; |
33 |
import org.eclipse.papyrus.validation.preferences.PreferencePage; |
34 |
import org.eclipse.papyrus.validation.preferences.PreferencePage.MarkChildren; |
35 |
|
28 |
|
36 |
public class ValidationTool { |
29 |
public class ValidationTool { |
37 |
|
30 |
|
38 |
/** Current element */ |
|
|
39 |
private Object element; |
40 |
|
41 |
/** current eobject */ |
31 |
/** current eobject */ |
42 |
private EObject eObject; |
32 |
private EObject eObject; |
43 |
|
33 |
|
Lines 48-60
Link Here
|
48 |
/** |
38 |
/** |
49 |
* Constructor: |
39 |
* Constructor: |
50 |
* create a new instance of the validation tool for a specific model element |
40 |
* create a new instance of the validation tool for a specific model element |
51 |
* |
41 |
* |
52 |
* @param element a model element |
42 |
* @param element |
|
|
43 |
* a model element |
53 |
*/ |
44 |
*/ |
54 |
public ValidationTool (Object element) { |
45 |
public ValidationTool(Object element) { |
55 |
this.element = element; |
|
|
56 |
setEObject((EObject)Platform.getAdapterManager().getAdapter(element, EObject.class)); |
46 |
setEObject((EObject)Platform.getAdapterManager().getAdapter(element, EObject.class)); |
57 |
} |
47 |
} |
58 |
|
48 |
|
59 |
/** |
49 |
/** |
60 |
* Constructor: |
50 |
* Constructor: |
Lines 63-85
Link Here
|
63 |
* @param eObject |
53 |
* @param eObject |
64 |
* a model element |
54 |
* a model element |
65 |
*/ |
55 |
*/ |
66 |
public ValidationTool (EObject eObject) { |
56 |
public ValidationTool(EObject eObject) { |
67 |
setEObject(eObject); |
57 |
setEObject(eObject); |
68 |
} |
|
|
69 |
|
70 |
|
71 |
public void tryChildIfEmpty() { |
72 |
// element has no eObject. try parent |
73 |
if(getEObject() == null) { |
74 |
// TODO: is it possible to access the children in another way (without internal access?) |
75 |
if (element instanceof LinkItem) { |
76 |
List<?> items = ((LinkItem)element).getChildrenElements(); |
77 |
if(items.size() > 0 && items.get(0) instanceof EObject) { |
78 |
// element = items[0]; |
79 |
setEObject((EObject)items.get(0)); |
80 |
} |
81 |
} |
82 |
} |
83 |
} |
58 |
} |
84 |
|
59 |
|
85 |
/** |
60 |
/** |
Lines 90-97
Link Here
|
90 |
public EObject getEObject() { |
65 |
public EObject getEObject() { |
91 |
return eObject; |
66 |
return eObject; |
92 |
} |
67 |
} |
93 |
|
|
|
94 |
|
68 |
|
|
|
69 |
|
95 |
/** |
70 |
/** |
96 |
* sets the current EObject |
71 |
* sets the current EObject |
97 |
* |
72 |
* |
Lines 101-256
Link Here
|
101 |
this.eObject = eObject; |
76 |
this.eObject = eObject; |
102 |
} |
77 |
} |
103 |
|
78 |
|
104 |
public IMarker [] getMarkers () { |
79 |
public IMarker[] getMarkers() { |
105 |
if(getEObject() != null) { |
80 |
if(getEObject() != null) { |
106 |
if(getEObject().eResource() != null) { |
81 |
if(getEObject().eResource() != null) { |
107 |
URI uri = getEObject().eResource().getURI(); |
82 |
URI uri = getEObject().eResource().getURI(); |
108 |
String platformResourceString = uri.toPlatformString(true); |
83 |
String platformResourceString = uri.toPlatformString(true); |
109 |
IFile file = (platformResourceString != null ? |
84 |
IFile file = (platformResourceString != null ? ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformResourceString)) : null); |
110 |
ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformResourceString)) : null); |
85 |
if(file != null) { |
111 |
if (file != null) { |
|
|
112 |
try { |
86 |
try { |
113 |
// TODO: quite inefficient, since requested for each element (could cache markers, already done |
87 |
// TODO: quite inefficient, since requested for each element (could cache markers, already done |
114 |
// by findMarkers operation?) |
88 |
// by findMarkers operation?) |
115 |
return file.findMarkers(IMarker.PROBLEM, true, 0); |
89 |
return file.findMarkers(IMarker.PROBLEM, true, 0); |
|
|
90 |
} catch (CoreException e) { |
116 |
} |
91 |
} |
117 |
catch (CoreException e) { |
|
|
118 |
} |
119 |
} |
92 |
} |
120 |
} |
93 |
} |
121 |
} |
94 |
} |
122 |
return null; |
95 |
return null; |
123 |
} |
96 |
} |
124 |
|
97 |
|
125 |
/** |
98 |
/** |
126 |
* |
99 |
* |
127 |
* @param marker |
100 |
* @param marker |
128 |
* @return |
101 |
* @return |
129 |
*/ |
102 |
*/ |
130 |
public EObject eObjectOfMarker (IMarker marker) { |
103 |
public EObject eObjectOfMarker(IMarker marker) { |
131 |
if(getEObject() != null) { |
104 |
if(getEObject() != null) { |
132 |
domain = AdapterFactoryEditingDomain.getEditingDomainFor(getEObject()); |
105 |
domain = AdapterFactoryEditingDomain.getEditingDomainFor(getEObject()); |
133 |
try { |
106 |
try { |
134 |
if (marker.isSubtypeOf((EValidator.MARKER))) { |
107 |
if(marker.isSubtypeOf((EValidator.MARKER))) { |
135 |
return ValidationUtils.eObjectFromMarkerOrMap (marker, null, domain); |
108 |
return ValidationUtils.eObjectFromMarkerOrMap(marker, null, domain); |
136 |
} |
109 |
} |
137 |
} |
110 |
} catch (CoreException e) { |
138 |
catch (CoreException e) { |
|
|
139 |
// only reason: marker does not exist |
111 |
// only reason: marker does not exist |
140 |
} |
112 |
} |
141 |
} |
113 |
} |
142 |
return null; |
114 |
return null; |
143 |
} |
115 |
} |
144 |
|
116 |
|
145 |
/** |
117 |
/** |
146 |
* Delete all markers that refer to eObjects owned by the passed parentEObj |
118 |
* Delete all markers that refer to eObjects owned by the passed parentEObj |
147 |
*/ |
119 |
*/ |
148 |
public void deleteSubMarkers() { |
120 |
public void deleteSubMarkers() { |
149 |
for (IMarker marker : getMarkers ()) { |
121 |
for(IMarker marker : getMarkers()) { |
150 |
EObject eObjOfMarker = eObjectOfMarker (marker); |
122 |
EObject eObjOfMarker = eObjectOfMarker(marker); |
151 |
if(isContainedBy(eObjOfMarker, getEObject())) { |
123 |
if(isContainedBy(eObjOfMarker, getEObject())) { |
152 |
try { |
124 |
try { |
153 |
marker.delete (); |
125 |
marker.delete(); |
|
|
126 |
} catch (CoreException e) { |
154 |
} |
127 |
} |
155 |
catch (CoreException e) { |
|
|
156 |
} |
157 |
} |
128 |
} |
158 |
} |
129 |
} |
159 |
} |
130 |
} |
160 |
|
131 |
|
161 |
/** |
132 |
private boolean isContainedBy(EObject subEObj, EObject eObj) { |
162 |
* @return the maximum severity of markers associated with the model element |
133 |
if(eObj == subEObj) |
163 |
* (constructor parameter of validation tool) |
134 |
return true; |
164 |
*/ |
135 |
else if(subEObj != null) { |
165 |
public int getSeverity() { |
136 |
return isContainedBy(subEObj.eContainer(), eObj); |
166 |
IMarker markers[] = getMarkers(); |
|
|
167 |
MarkChildren markChildren = PreferencePage.getHierarchicalMarkers(); |
168 |
int severity = 0; |
169 |
if (markers != null) { |
170 |
for (IMarker marker : markers) { |
171 |
EObject eObjectOfMarker = eObjectOfMarker(marker); |
172 |
boolean first = true; |
173 |
while (eObjectOfMarker != null) { |
174 |
if (eObjectOfMarker == getEObject()) { |
175 |
try { |
176 |
Integer severityI = (Integer) marker.getAttribute(IMarker.SEVERITY); |
177 |
if (severityI.intValue () > severity) { |
178 |
severity = severityI.intValue(); |
179 |
} |
180 |
} |
181 |
catch (CoreException e) { |
182 |
} |
183 |
} |
184 |
// navigate to parents, since parent folder is contaminated as well |
185 |
eObjectOfMarker = eObjectOfMarker.eContainer(); |
186 |
if (markChildren != MarkChildren.ALL) { |
187 |
if ((!first) || (markChildren == MarkChildren.NO)) { |
188 |
break; |
189 |
} |
190 |
} |
191 |
first = false; |
192 |
} |
193 |
} |
194 |
} |
137 |
} |
195 |
return severity; |
|
|
196 |
} |
197 |
|
198 |
/** |
199 |
* @return A set of messages associated with the markers for the model element |
200 |
* (constructor parameter of validation tool) |
201 |
*/ |
202 |
public String getMarkerMessages() { |
203 |
IMarker markers[] = getMarkers(); |
204 |
MarkChildren markChildren = PreferencePage.getHierarchicalMarkers(); |
205 |
if (markers != null) { |
206 |
boolean examineChilds = (markChildren != MarkChildren.NO); |
207 |
String message = ""; |
208 |
for (IMarker marker : markers) { |
209 |
EObject eObjectOfMarker = eObjectOfMarker(marker); |
210 |
if (eObjectOfMarker == getEObject()) { |
211 |
if (message.length() > 0) { |
212 |
message += "\n"; |
213 |
} |
214 |
// vt.getWrappedMessage (marker); |
215 |
try { |
216 |
message += "- " + WordUtils.wrap ((String) marker.getAttribute(IMarker.MESSAGE), 100, "\n ", true); |
217 |
} |
218 |
catch (CoreException e) { |
219 |
} |
220 |
} |
221 |
if (examineChilds && (eObjectOfMarker != null)) { |
222 |
eObjectOfMarker = eObjectOfMarker.eContainer(); |
223 |
boolean first = true; |
224 |
while (eObjectOfMarker != null) { |
225 |
if (eObjectOfMarker == getEObject ()) { |
226 |
if (message.length() > 0) { |
227 |
message += "\n"; |
228 |
} |
229 |
message += "- Problem marker in (at least) one of the children"; |
230 |
examineChilds = false; |
231 |
break; |
232 |
} |
233 |
// navigate to parents, since parent folder is contaminated as well |
234 |
eObjectOfMarker = eObjectOfMarker.eContainer(); |
235 |
if ((!first) && (markChildren == MarkChildren.DIRECT)) { |
236 |
break; |
237 |
} |
238 |
first = false; |
239 |
} |
240 |
} |
241 |
} |
242 |
return (message.length() > 0) ? message : null; |
243 |
} |
244 |
return null; |
245 |
} |
246 |
|
247 |
private boolean isContainedBy (EObject subEObj, EObject eObj) { |
248 |
if (eObj == subEObj) return true; |
249 |
else if (subEObj != null) { |
250 |
return isContainedBy (subEObj.eContainer(), eObj); |
251 |
} |
252 |
// reached, if subEObj == null |
138 |
// reached, if subEObj == null |
253 |
return false; |
139 |
return false; |
254 |
} |
140 |
} |
255 |
|
141 |
|
256 |
} |
142 |
} |