Skip to content

Commit 79d7e75

Browse files
committed
Clean-up and simplify usage of E4's XPathContext in E4-tools
1 parent 975f72b commit 79d7e75

File tree

2 files changed

+11
-26
lines changed

2 files changed

+11
-26
lines changed

e4tools/bundles/org.eclipse.e4.tools.emf.ui/META-INF/MANIFEST.MF

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="3.29.0",
2727
org.eclipse.pde.core,
2828
org.eclipse.e4.core.commands;bundle-version="0.10.0",
2929
org.eclipse.e4.ui.dialogs;bundle-version="1.0.0",
30-
org.eclipse.e4.emf.xpath,
3130
org.eclipse.ui.forms;bundle-version="3.7.200"
3231
Bundle-ActivationPolicy: lazy
3332
Import-Package: jakarta.annotation;version="[2.1.0,3.0.0)",

e4tools/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/StringModelFragment.java

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@
2020
import java.util.ArrayList;
2121
import java.util.Arrays;
2222
import java.util.Collections;
23-
import java.util.Iterator;
2423
import java.util.List;
2524

2625
import org.eclipse.core.databinding.observable.list.IObservableList;
26+
import org.eclipse.core.runtime.ILog;
2727
import org.eclipse.e4.core.contexts.IEclipseContext;
28-
import org.eclipse.e4.emf.xpath.EcoreXPathContextFactory;
29-
import org.eclipse.e4.emf.xpath.XPathContext;
30-
import org.eclipse.e4.emf.xpath.XPathContextFactory;
3128
import org.eclipse.e4.tools.emf.ui.common.IEditorFeature.FeatureClass;
3229
import org.eclipse.e4.tools.emf.ui.common.Util;
3330
import org.eclipse.e4.tools.emf.ui.common.Util.InternalPackage;
@@ -41,9 +38,9 @@
4138
import org.eclipse.e4.tools.emf.ui.internal.common.component.dialogs.FindParentReferenceElementDialog;
4239
import org.eclipse.e4.tools.emf.ui.internal.common.component.tabs.empty.E;
4340
import org.eclipse.e4.tools.emf.ui.internal.common.component.virtual.VSnippetsEditor;
41+
import org.eclipse.e4.ui.model.ModelXPathEvaluator;
4442
import org.eclipse.e4.ui.model.application.MApplication;
4543
import org.eclipse.e4.ui.model.application.MApplicationElement;
46-
import org.eclipse.e4.ui.model.application.impl.ApplicationElementImpl;
4744
import org.eclipse.e4.ui.model.application.impl.ApplicationPackageImpl;
4845
import org.eclipse.e4.ui.model.fragment.MModelFragment;
4946
import org.eclipse.e4.ui.model.fragment.MStringModelFragment;
@@ -157,8 +154,7 @@ public FeaturePath[] getLabelProperties() {
157154

158155
@Override
159156
public String getDetailLabel(Object element) {
160-
if (element instanceof StringModelFragmentImpl) {
161-
final StringModelFragmentImpl fragment = (StringModelFragmentImpl) element;
157+
if (element instanceof StringModelFragmentImpl fragment) {
162158
String ret = ""; //$NON-NLS-1$
163159
if (E.notEmpty(fragment.getFeaturename())) {
164160
ret += fragment.getFeaturename();
@@ -246,17 +242,17 @@ public static EClass findContainerType(MStringModelFragment modelFragment) {
246242

247243
// Deal with non default MApplication IDs.
248244
if (xpath != null) {
249-
if (o instanceof MApplication) {
250-
EClass found = getTargetClassFromXPath((MApplication) o, xpath);
245+
if (o instanceof MApplication application) {
246+
EClass found = getTargetClassFromXPath(application, xpath);
251247
if (found != null) {
252248
return found;
253249
}
254250
}
255251
} else {
256252
// This is a standard search with ID.
257-
if ((o instanceof MApplicationElement)
253+
if ((o instanceof MApplicationElement appElement)
258254
&& (o.eContainingFeature() != FragmentPackageImpl.Literals.MODEL_FRAGMENTS__IMPORTS)
259-
&& parentElementId.equals(((MApplicationElement) o).getElementId())) {
255+
&& parentElementId.equals(appElement.getElementId())) {
260256
return o.eClass();
261257
}
262258
}
@@ -581,24 +577,14 @@ public List<Action> getActions(Object element) {
581577
* @return the list of EClass(es) matching this xpath
582578
*/
583579
private static EClass getTargetClassFromXPath(MApplication application, String xpath) {
584-
585-
XPathContextFactory<EObject> f = EcoreXPathContextFactory.newInstance();
586-
XPathContext xpathContext = f.newContext((EObject) application);
587-
Iterator<Object> i = xpathContext.iterate(xpath);
588-
589580
try {
590-
while (i.hasNext()) {
591-
Object obj = i.next();
592-
if (obj instanceof MApplicationElement) {
593-
ApplicationElementImpl ae = (ApplicationElementImpl) obj;
594-
return ae.eClass();
595-
}
596-
}
581+
return ModelXPathEvaluator.findMatchingElements(application, xpath, MApplicationElement.class)
582+
.map(EObject.class::cast).map(EObject::eClass) //
583+
.findFirst().orElse(null);
597584
} catch (Exception ex) {
598585
// custom xpath functions will throw exceptions
599-
ex.printStackTrace();
586+
ILog.get().error("Failed to evaluate xpath: " + xpath, ex); //$NON-NLS-1$
600587
}
601-
602588
return null;
603589
}
604590

0 commit comments

Comments
 (0)