Skip to content

Commit

Permalink
Clean-up and simplify usage of E4's XPathContext in E4-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Feb 16, 2025
1 parent 975f72b commit 79d7e75
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="3.29.0",
org.eclipse.pde.core,
org.eclipse.e4.core.commands;bundle-version="0.10.0",
org.eclipse.e4.ui.dialogs;bundle-version="1.0.0",
org.eclipse.e4.emf.xpath,
org.eclipse.ui.forms;bundle-version="3.7.200"
Bundle-ActivationPolicy: lazy
Import-Package: jakarta.annotation;version="[2.1.0,3.0.0)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.runtime.ILog;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.emf.xpath.EcoreXPathContextFactory;
import org.eclipse.e4.emf.xpath.XPathContext;
import org.eclipse.e4.emf.xpath.XPathContextFactory;
import org.eclipse.e4.tools.emf.ui.common.IEditorFeature.FeatureClass;
import org.eclipse.e4.tools.emf.ui.common.Util;
import org.eclipse.e4.tools.emf.ui.common.Util.InternalPackage;
Expand All @@ -41,9 +38,9 @@
import org.eclipse.e4.tools.emf.ui.internal.common.component.dialogs.FindParentReferenceElementDialog;
import org.eclipse.e4.tools.emf.ui.internal.common.component.tabs.empty.E;
import org.eclipse.e4.tools.emf.ui.internal.common.component.virtual.VSnippetsEditor;
import org.eclipse.e4.ui.model.ModelXPathEvaluator;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.MApplicationElement;
import org.eclipse.e4.ui.model.application.impl.ApplicationElementImpl;
import org.eclipse.e4.ui.model.application.impl.ApplicationPackageImpl;
import org.eclipse.e4.ui.model.fragment.MModelFragment;
import org.eclipse.e4.ui.model.fragment.MStringModelFragment;
Expand Down Expand Up @@ -157,8 +154,7 @@ public FeaturePath[] getLabelProperties() {

@Override
public String getDetailLabel(Object element) {
if (element instanceof StringModelFragmentImpl) {
final StringModelFragmentImpl fragment = (StringModelFragmentImpl) element;
if (element instanceof StringModelFragmentImpl fragment) {
String ret = ""; //$NON-NLS-1$
if (E.notEmpty(fragment.getFeaturename())) {
ret += fragment.getFeaturename();
Expand Down Expand Up @@ -246,17 +242,17 @@ public static EClass findContainerType(MStringModelFragment modelFragment) {

// Deal with non default MApplication IDs.
if (xpath != null) {
if (o instanceof MApplication) {
EClass found = getTargetClassFromXPath((MApplication) o, xpath);
if (o instanceof MApplication application) {
EClass found = getTargetClassFromXPath(application, xpath);
if (found != null) {
return found;
}
}
} else {
// This is a standard search with ID.
if ((o instanceof MApplicationElement)
if ((o instanceof MApplicationElement appElement)
&& (o.eContainingFeature() != FragmentPackageImpl.Literals.MODEL_FRAGMENTS__IMPORTS)
&& parentElementId.equals(((MApplicationElement) o).getElementId())) {
&& parentElementId.equals(appElement.getElementId())) {
return o.eClass();
}
}
Expand Down Expand Up @@ -581,24 +577,14 @@ public List<Action> getActions(Object element) {
* @return the list of EClass(es) matching this xpath
*/
private static EClass getTargetClassFromXPath(MApplication application, String xpath) {

XPathContextFactory<EObject> f = EcoreXPathContextFactory.newInstance();
XPathContext xpathContext = f.newContext((EObject) application);
Iterator<Object> i = xpathContext.iterate(xpath);

try {
while (i.hasNext()) {
Object obj = i.next();
if (obj instanceof MApplicationElement) {
ApplicationElementImpl ae = (ApplicationElementImpl) obj;
return ae.eClass();
}
}
return ModelXPathEvaluator.findMatchingElements(application, xpath, MApplicationElement.class)
.map(EObject.class::cast).map(EObject::eClass) //
.findFirst().orElse(null);
} catch (Exception ex) {
// custom xpath functions will throw exceptions
ex.printStackTrace();
ILog.get().error("Failed to evaluate xpath: " + xpath, ex); //$NON-NLS-1$
}

return null;
}

Expand Down

0 comments on commit 79d7e75

Please sign in to comment.