Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShuai Li2015-02-06 10:14:47 +0000
committerRemi Schnekenburger2015-02-06 13:56:35 +0000
commitb3c987478d78eb87a023ce621daccca33065d454 (patch)
tree4d9442df2eca3918de2aa86f36ca39da85fb30b0
parent75781a96da011cb981cb592e6712e63948d34fe3 (diff)
downloadorg.eclipse.papyrus-b3c987478d78eb87a023ce621daccca33065d454.tar.gz
org.eclipse.papyrus-b3c987478d78eb87a023ce621daccca33065d454.tar.xz
org.eclipse.papyrus-b3c987478d78eb87a023ce621daccca33065d454.zip
Bug 456874 [Search] Error when searching in the models for a specific
String https://bugs.eclipse.org/bugs/show_bug.cgi?id=456874 - The endindex for substring (called on the matched value) is now computed correctly as the length of the matched value instead of (offset + length) - The indexes of the matched value in the string is now displayed correctly. E.g. '"clas" in "myclass" [3, 5]' Change-Id: I9952b2a610d86079431ef5faa2743dda1a4c6e45 Signed-off-by: Shuai Li <shuai.li88@gmail.com>
-rw-r--r--plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/providers/AttributeMatchLabelProvider.java249
-rw-r--r--plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/results/PapyrusSearchResult.java581
2 files changed, 414 insertions, 416 deletions
diff --git a/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/providers/AttributeMatchLabelProvider.java b/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/providers/AttributeMatchLabelProvider.java
index 26ec1030361..c6373497dd8 100644
--- a/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/providers/AttributeMatchLabelProvider.java
+++ b/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/providers/AttributeMatchLabelProvider.java
@@ -1,125 +1,124 @@
-/*****************************************************************************
- * Copyright (c) 2013 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:
- * CEA LIST - Initial API and implementation
- *
- *****************************************************************************/
-package org.eclipse.papyrus.uml.search.ui.providers;
-
-import org.eclipse.emf.ecore.EAttribute;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.jface.viewers.ILabelProviderListener;
-import org.eclipse.papyrus.infra.core.services.ServiceException;
-import org.eclipse.papyrus.infra.services.labelprovider.service.IFilteredLabelProvider;
-import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;
-import org.eclipse.papyrus.infra.services.labelprovider.service.impl.LabelProviderServiceImpl;
-import org.eclipse.papyrus.uml.search.ui.Activator;
-import org.eclipse.papyrus.uml.search.ui.Messages;
-import org.eclipse.papyrus.views.search.results.AttributeMatch;
-import org.eclipse.swt.graphics.Image;
-import org.eclipse.uml2.uml.Class;
-import org.eclipse.uml2.uml.Element;
-import org.eclipse.uml2.uml.EnumerationLiteral;
-import org.eclipse.uml2.uml.Property;
-import org.eclipse.uml2.uml.Stereotype;
-
-public class AttributeMatchLabelProvider implements IFilteredLabelProvider {
-
- public Image getImage(Object element) {
- if (element instanceof AttributeMatch) {
- LabelProviderService service = new LabelProviderServiceImpl();
- try {
- service.startService();
- return service.getLabelProvider().getImage(((AttributeMatch) element).getMetaAttribute());
- } catch (ServiceException e) {
- Activator.log.warn(Messages.AttributeMatchLabelProvider_0 + ((AttributeMatch) element).getMetaAttribute());
- }
- }
- return null;
- }
-
- private String printResult(String sectionThatMatch, String value, int offset, int lenght, String attributeName) {
- return "\"" + sectionThatMatch + "\"" + Messages.AttributeMatchLabelProvider_3 + "\"" + value + "\" [" + offset + "," + (offset + lenght) + "] (" + attributeName + Messages.AttributeMatchLabelProvider_8 + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
- }
-
- public String getText(Object element) {
-
- if (element instanceof AttributeMatch) {
- AttributeMatch attributeMatch = ((AttributeMatch) element);
- if ((attributeMatch).getSource() instanceof EObject) {
- EObject target = (EObject) attributeMatch.getSource();
- if (attributeMatch.getMetaAttribute() instanceof EAttribute) {
- EAttribute source = (EAttribute) attributeMatch.getMetaAttribute();
- if (target.eGet(source) instanceof String) {
- String value = (String) target.eGet(source);
- int end = attributeMatch.getOffset() + attributeMatch.getLength();
- return printResult(value.substring(attributeMatch.getOffset(), end), value, attributeMatch.getOffset(), attributeMatch.getLength(), source.getName());
-
- } else {
- String value = String.valueOf(target.eGet(source));
- int end = attributeMatch.getOffset() + attributeMatch.getLength();
- return printResult(value.substring(attributeMatch.getOffset(), end), value, attributeMatch.getOffset(), attributeMatch.getLength(), source.getName());
- }
- } else if (attributeMatch.getMetaAttribute() instanceof Property) {
-
- Property source = (Property) attributeMatch.getMetaAttribute();
- Class containingClass = source.getClass_();
- if (containingClass instanceof Stereotype) {
- if (target instanceof Element) {
- String value = getStringValueOfProperty(((Element) target), (Stereotype) containingClass, (Property) attributeMatch.getMetaAttribute());
- return printResult(value.substring(attributeMatch.getOffset(), attributeMatch.getLength()), value, attributeMatch.getOffset(), attributeMatch.getLength(), source.getName());
-
- }
- }
- }
- }
- }
-
- return ""; //$NON-NLS-1$
-
- }
-
- public void addListener(ILabelProviderListener listener) {
-
- }
-
- public void dispose() {
-
- }
-
- public boolean isLabelProperty(Object element, String property) {
-
- return false;
- }
-
- public void removeListener(ILabelProviderListener listener) {
-
- }
-
- public boolean accept(Object element) {
- if (element instanceof AttributeMatch) {
- return true;
-
- }
- return false;
- }
-
- private String getStringValueOfProperty(Element element, Stereotype stereotype, Property property) {
- Object value = element.getValue(stereotype, property.getName());
- if (value instanceof String) {
- return (String) value;
- } else if (value instanceof EnumerationLiteral) {
- return ((EnumerationLiteral) value).getName();
- } else {
- return String.valueOf(value);
- }
- }
-
-}
+/*****************************************************************************
+ * Copyright (c) 2013 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:
+ * CEA LIST - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.uml.search.ui.providers;
+
+import org.eclipse.emf.ecore.EAttribute;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.jface.viewers.ILabelProviderListener;
+import org.eclipse.papyrus.infra.core.services.ServiceException;
+import org.eclipse.papyrus.infra.services.labelprovider.service.IFilteredLabelProvider;
+import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;
+import org.eclipse.papyrus.infra.services.labelprovider.service.impl.LabelProviderServiceImpl;
+import org.eclipse.papyrus.uml.search.ui.Activator;
+import org.eclipse.papyrus.uml.search.ui.Messages;
+import org.eclipse.papyrus.views.search.results.AttributeMatch;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.uml2.uml.Class;
+import org.eclipse.uml2.uml.Element;
+import org.eclipse.uml2.uml.EnumerationLiteral;
+import org.eclipse.uml2.uml.Property;
+import org.eclipse.uml2.uml.Stereotype;
+
+public class AttributeMatchLabelProvider implements IFilteredLabelProvider {
+
+ public Image getImage(Object element) {
+ if (element instanceof AttributeMatch) {
+ LabelProviderService service = new LabelProviderServiceImpl();
+ try {
+ service.startService();
+ return service.getLabelProvider().getImage(((AttributeMatch) element).getMetaAttribute());
+ } catch (ServiceException e) {
+ Activator.log.warn(Messages.AttributeMatchLabelProvider_0 + ((AttributeMatch) element).getMetaAttribute());
+ }
+ }
+ return null;
+ }
+
+ private String printResult(String sectionThatMatch, String value, int offset, int length, String attributeName) {
+ return "\"" + sectionThatMatch + "\"" + Messages.AttributeMatchLabelProvider_3 + "\"" + value + "\" [" + (offset + 1) + "," + length + "] (" + attributeName + Messages.AttributeMatchLabelProvider_8 + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
+ }
+
+ public String getText(Object element) {
+
+ if (element instanceof AttributeMatch) {
+ AttributeMatch attributeMatch = ((AttributeMatch) element);
+ if ((attributeMatch).getSource() instanceof EObject) {
+ EObject target = (EObject) attributeMatch.getSource();
+ if (attributeMatch.getMetaAttribute() instanceof EAttribute) {
+ EAttribute source = (EAttribute) attributeMatch.getMetaAttribute();
+ if (target.eGet(source) instanceof String) {
+ String value = (String) target.eGet(source);
+ return printResult(value.substring(attributeMatch.getOffset(), attributeMatch.getLength()), value, attributeMatch.getOffset(), attributeMatch.getLength(), source.getName());
+
+ } else {
+ String value = String.valueOf(target.eGet(source));
+ int end = attributeMatch.getOffset() + attributeMatch.getLength();
+ return printResult(value.substring(attributeMatch.getOffset(), end), value, attributeMatch.getOffset(), attributeMatch.getLength(), source.getName());
+ }
+ } else if (attributeMatch.getMetaAttribute() instanceof Property) {
+
+ Property source = (Property) attributeMatch.getMetaAttribute();
+ Class containingClass = source.getClass_();
+ if (containingClass instanceof Stereotype) {
+ if (target instanceof Element) {
+ String value = getStringValueOfProperty(((Element) target), (Stereotype) containingClass, (Property) attributeMatch.getMetaAttribute());
+ return printResult(value.substring(attributeMatch.getOffset(), attributeMatch.getLength()), value, attributeMatch.getOffset(), attributeMatch.getLength(), source.getName());
+
+ }
+ }
+ }
+ }
+ }
+
+ return ""; //$NON-NLS-1$
+
+ }
+
+ public void addListener(ILabelProviderListener listener) {
+
+ }
+
+ public void dispose() {
+
+ }
+
+ public boolean isLabelProperty(Object element, String property) {
+
+ return false;
+ }
+
+ public void removeListener(ILabelProviderListener listener) {
+
+ }
+
+ public boolean accept(Object element) {
+ if (element instanceof AttributeMatch) {
+ return true;
+
+ }
+ return false;
+ }
+
+ private String getStringValueOfProperty(Element element, Stereotype stereotype, Property property) {
+ Object value = element.getValue(stereotype, property.getName());
+ if (value instanceof String) {
+ return (String) value;
+ } else if (value instanceof EnumerationLiteral) {
+ return ((EnumerationLiteral) value).getName();
+ } else {
+ return String.valueOf(value);
+ }
+ }
+
+}
diff --git a/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/results/PapyrusSearchResult.java b/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/results/PapyrusSearchResult.java
index 51ca09f4050..d6ddc8cc910 100644
--- a/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/results/PapyrusSearchResult.java
+++ b/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/results/PapyrusSearchResult.java
@@ -1,291 +1,290 @@
-/*****************************************************************************
- * Copyright (c) 2013 CEA LIST and others.
- *
- *
- * 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:
- * CEA LIST - Initial API and implementation
- * Christian W. Damus (CEA LIST) - Replace workspace IResource dependency with URI for CDO compatibility
- *
- *****************************************************************************/
-package org.eclipse.papyrus.uml.search.ui.results;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.emf.common.util.URI;
-import org.eclipse.emf.ecore.EAttribute;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.EStructuralFeature;
-import org.eclipse.jface.resource.ImageDescriptor;
-import org.eclipse.papyrus.infra.core.utils.EditorUtils;
-import org.eclipse.papyrus.uml.search.ui.Activator;
-import org.eclipse.papyrus.uml.search.ui.Messages;
-import org.eclipse.papyrus.uml.search.ui.query.AbstractPapyrusQuery;
-import org.eclipse.papyrus.views.search.regex.PatternHelper;
-import org.eclipse.papyrus.views.search.results.AbstractResultEntry;
-import org.eclipse.papyrus.views.search.results.AttributeMatch;
-import org.eclipse.papyrus.views.search.results.ModelElementMatch;
-import org.eclipse.papyrus.views.search.results.ModelMatch;
-import org.eclipse.papyrus.views.search.results.ViewerMatch;
-import org.eclipse.papyrus.views.search.scope.ScopeEntry;
-import org.eclipse.papyrus.views.search.utils.MatchUtils;
-import org.eclipse.search.ui.text.AbstractTextSearchResult;
-import org.eclipse.search.ui.text.IEditorMatchAdapter;
-import org.eclipse.search.ui.text.IFileMatchAdapter;
-import org.eclipse.search.ui.text.Match;
-import org.eclipse.search.ui.text.MatchFilter;
-import org.eclipse.ui.IEditorPart;
-import org.eclipse.uml2.uml.Element;
-import org.eclipse.uml2.uml.EnumerationLiteral;
-import org.eclipse.uml2.uml.Property;
-import org.eclipse.uml2.uml.Stereotype;
-
-import com.google.common.base.Objects;
-import com.swtdesigner.ResourceManager;
-
-public class PapyrusSearchResult extends AbstractTextSearchResult implements IEditorMatchAdapter, IFileMatchAdapter {
-
- MatchFilter[] possibleMatchFilter;
-
- private AbstractPapyrusQuery searchQuery;
-
- public PapyrusSearchResult(AbstractPapyrusQuery query) {
- this.searchQuery = query;
- possibleMatchFilter = new MatchFilter[0];
- }
-
- public AbstractPapyrusQuery getQuery() {
- return searchQuery;
- }
-
-
- public void setPossibleMatchFilter(MatchFilter[] possibleMatchFilter) {
- this.possibleMatchFilter = possibleMatchFilter;
- }
-
- @Override
- public MatchFilter[] getAllMatchFilters() {
- return possibleMatchFilter;
-
- }
-
- public String getLabel() {
-
- return getMatchCount() + Messages.PapyrusSearchResult_0 + searchQuery.getSearchQueryText() + Messages.PapyrusSearchResult_1;
- }
-
- public String getTooltip() {
-
- return Messages.PapyrusSearchResult_2;
- }
-
- public ImageDescriptor getImageDescriptor() {
- return ResourceManager.getPluginImageDescriptor(Activator.PLUGIN_ID, "icons/PapyrusSearch.png"); //$NON-NLS-1$
- }
-
- @Override
- public IEditorMatchAdapter getEditorMatchAdapter() {
- return this;
- }
-
- @Override
- public IFileMatchAdapter getFileMatchAdapter() {
- return this;
- }
-
- public Match[] computeContainedMatches(AbstractTextSearchResult result, IFile file) {
- Set<Match> results = new HashSet<Match>();
-
- Set<AbstractResultEntry> allMatches = MatchUtils.getMatches(result, true);
- for (AbstractResultEntry modelMatch : allMatches) {
- Object element = modelMatch.getElement();
- if (element instanceof ScopeEntry) {
- if (file.equals(getWorkspaceResource((ScopeEntry) element))) {
- results.add(modelMatch);
- }
- }
- }
- Match[] arrayResult = new Match[results.size()];
-
- return results.toArray(arrayResult);
- }
-
- protected IResource getWorkspaceResource(ScopeEntry scopeEntry) {
- IResource result = null;
-
- URI uri = scopeEntry.getResourceURI();
- if ((uri != null) && uri.isPlatformResource()) {
- String path = uri.toPlatformString(true);
- result = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
- }
-
- return result;
- }
-
- public IFile getFile(Object element) {
- if (element instanceof ScopeEntry) {
- IResource resource = getWorkspaceResource((ScopeEntry) element);
- if (resource instanceof IFile) {
- return (IFile) resource;
- }
- }
- return null;
- }
-
- public boolean isShownInEditor(Match match, IEditorPart editor) {
- if (match instanceof AbstractResultEntry) {
- Object element = match.getElement();
- if (element instanceof ScopeEntry) {
- if (Objects.equal(EditorUtils.getResourceURI(editor), ((ScopeEntry) element).getResourceURI())) {
- return true;
- }
- }
- }
-
- return false;
- }
-
- @Override
- public Match[] getMatches(Object element) {
-
- Match[] matchList = super.getMatches(element);
- Set<Match> matchToKeep = new HashSet<Match>();
- List<Object> sourceList = new ArrayList<Object>();
- // Get matches which are still true
- for (Match match : matchList) {
- if (match instanceof AbstractResultEntry) {
- if (((AbstractResultEntry) match).getSource() != null) {
- if (match instanceof AttributeMatch) {
- Object attribute = ((AttributeMatch) match).getMetaAttribute();
- String value = null;
- EObject target = (EObject) ((AbstractResultEntry) match).getSource();
- if (attribute instanceof EAttribute) {
-
- value = String.valueOf(target.eGet((EStructuralFeature) attribute));
- } else if (attribute instanceof Property) {
-
- value = getStringValueOfProperty((Element) ((AbstractResultEntry) match).getSource(), ((AttributeMatch) match).getStereotype(), ((Property) attribute));
-
-
- }
- if (value != null && !this.getQuery().isRegularExpression()) {
- if (value.length() >= match.getLength() - match.getOffset()) {
- int end = match.getOffset() + match.getLength();
- value = value.substring(match.getOffset(), end);
- if (this.searchQuery.isCaseSensitive()) {
- if (value.equals(this.searchQuery.getSearchQueryText())) {
- ((AbstractResultEntry) match).recursiveHierarchy((AbstractResultEntry) ((AbstractResultEntry) match).getParent());
- matchToKeep.add(match);
- sourceList.add(((AbstractResultEntry) match).getSource());
- }
- } else {
- if (value.equalsIgnoreCase(this.searchQuery.getSearchQueryText())) {
-
- ((AbstractResultEntry) match).recursiveHierarchy((AbstractResultEntry) ((AbstractResultEntry) match).getParent());
-
- matchToKeep.add(match);
- sourceList.add(((AbstractResultEntry) match).getSource());
- }
- }
- }
- } else if (this.getQuery().isRegularExpression()) {
- if (this.getQuery().getSearchQueryText() != null) {
-
-
- Pattern pattern = PatternHelper.getInstance().createPattern(this.getQuery().getSearchQueryText(), false, true);
- Matcher m = pattern.matcher(value);
- if (m.matches()) {
- int start = m.start();
- int end = m.end();
- if (start == match.getOffset() && end == match.getOffset() + match.getLength()) {
- matchToKeep.add(match);
- sourceList.add(((AbstractResultEntry) match).getSource());
- }
- }
- }
- }
- } else if (match instanceof ModelElementMatch) {
- ((AbstractResultEntry) match).recursiveHierarchy((AbstractResultEntry) match);
-
- matchToKeep.add(match);
- sourceList.add(((AbstractResultEntry) match).getSource());
- }
- }
- }
- }
- // Now get Viewer
- for (Match match : matchList) {
- if (match instanceof ViewerMatch) {
- Object source = ((ViewerMatch) match).getSemanticElement();
- if (sourceList.contains(source)) {
- matchToKeep.add(match);
- }
- }
-
- }
-
- return matchToKeep.toArray(new Match[matchToKeep.size()]);
- // return ((PapyrusQuery)searchQuery).getfResults().toArray(new Match[matchToKeep.size()]);
- //
- }
-
- public Match[] computeContainedMatches(AbstractTextSearchResult result, IEditorPart editor) {
- Set<Object> results = new HashSet<Object>();
- Set<AbstractResultEntry> allMatches = MatchUtils.getMatches(result, true);
- for (AbstractResultEntry modelMatch : allMatches) {
- Object element = modelMatch.getElement();
- if (element instanceof ScopeEntry) {
- if (((ScopeEntry) element).getResourceURI().equals(EditorUtils.getResourceURI(editor))) {
- results.add(modelMatch);
- }
- }
- }
-
- Match[] arrayResult = new Match[results.size()];
-
- return results.toArray(arrayResult);
- }
-
- @Override
- public int getMatchCount() {
-
- List<Object> elementList = Arrays.asList(this.getElements());
- int count = 0;
- for (Object element : elementList) {
-
- for (Match match : this.getMatches(element)) {
- if (match instanceof ModelMatch || match instanceof ViewerMatch) {
- count++;
- }
- }
-
- }
- return count;
- }
-
- private String getStringValueOfProperty(Element element, Stereotype stereotype, Property property) {
- Object value = element.getValue(stereotype, property.getName());
- if (value instanceof String) {
- return (String) value;
- } else if (value instanceof EnumerationLiteral) {
- return ((EnumerationLiteral) value).getName();
- } else {
- return String.valueOf(value);
- }
- }
-
-
-}
+/*****************************************************************************
+ * Copyright (c) 2013 CEA LIST and others.
+ *
+ *
+ * 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:
+ * CEA LIST - Initial API and implementation
+ * Christian W. Damus (CEA LIST) - Replace workspace IResource dependency with URI for CDO compatibility
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.uml.search.ui.results;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EAttribute;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.papyrus.infra.core.utils.EditorUtils;
+import org.eclipse.papyrus.uml.search.ui.Activator;
+import org.eclipse.papyrus.uml.search.ui.Messages;
+import org.eclipse.papyrus.uml.search.ui.query.AbstractPapyrusQuery;
+import org.eclipse.papyrus.views.search.regex.PatternHelper;
+import org.eclipse.papyrus.views.search.results.AbstractResultEntry;
+import org.eclipse.papyrus.views.search.results.AttributeMatch;
+import org.eclipse.papyrus.views.search.results.ModelElementMatch;
+import org.eclipse.papyrus.views.search.results.ModelMatch;
+import org.eclipse.papyrus.views.search.results.ViewerMatch;
+import org.eclipse.papyrus.views.search.scope.ScopeEntry;
+import org.eclipse.papyrus.views.search.utils.MatchUtils;
+import org.eclipse.search.ui.text.AbstractTextSearchResult;
+import org.eclipse.search.ui.text.IEditorMatchAdapter;
+import org.eclipse.search.ui.text.IFileMatchAdapter;
+import org.eclipse.search.ui.text.Match;
+import org.eclipse.search.ui.text.MatchFilter;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.uml2.uml.Element;
+import org.eclipse.uml2.uml.EnumerationLiteral;
+import org.eclipse.uml2.uml.Property;
+import org.eclipse.uml2.uml.Stereotype;
+
+import com.google.common.base.Objects;
+import com.swtdesigner.ResourceManager;
+
+public class PapyrusSearchResult extends AbstractTextSearchResult implements IEditorMatchAdapter, IFileMatchAdapter {
+
+ MatchFilter[] possibleMatchFilter;
+
+ private AbstractPapyrusQuery searchQuery;
+
+ public PapyrusSearchResult(AbstractPapyrusQuery query) {
+ this.searchQuery = query;
+ possibleMatchFilter = new MatchFilter[0];
+ }
+
+ public AbstractPapyrusQuery getQuery() {
+ return searchQuery;
+ }
+
+
+ public void setPossibleMatchFilter(MatchFilter[] possibleMatchFilter) {
+ this.possibleMatchFilter = possibleMatchFilter;
+ }
+
+ @Override
+ public MatchFilter[] getAllMatchFilters() {
+ return possibleMatchFilter;
+
+ }
+
+ public String getLabel() {
+
+ return getMatchCount() + Messages.PapyrusSearchResult_0 + searchQuery.getSearchQueryText() + Messages.PapyrusSearchResult_1;
+ }
+
+ public String getTooltip() {
+
+ return Messages.PapyrusSearchResult_2;
+ }
+
+ public ImageDescriptor getImageDescriptor() {
+ return ResourceManager.getPluginImageDescriptor(Activator.PLUGIN_ID, "icons/PapyrusSearch.png"); //$NON-NLS-1$
+ }
+
+ @Override
+ public IEditorMatchAdapter getEditorMatchAdapter() {
+ return this;
+ }
+
+ @Override
+ public IFileMatchAdapter getFileMatchAdapter() {
+ return this;
+ }
+
+ public Match[] computeContainedMatches(AbstractTextSearchResult result, IFile file) {
+ Set<Match> results = new HashSet<Match>();
+
+ Set<AbstractResultEntry> allMatches = MatchUtils.getMatches(result, true);
+ for (AbstractResultEntry modelMatch : allMatches) {
+ Object element = modelMatch.getElement();
+ if (element instanceof ScopeEntry) {
+ if (file.equals(getWorkspaceResource((ScopeEntry) element))) {
+ results.add(modelMatch);
+ }
+ }
+ }
+ Match[] arrayResult = new Match[results.size()];
+
+ return results.toArray(arrayResult);
+ }
+
+ protected IResource getWorkspaceResource(ScopeEntry scopeEntry) {
+ IResource result = null;
+
+ URI uri = scopeEntry.getResourceURI();
+ if ((uri != null) && uri.isPlatformResource()) {
+ String path = uri.toPlatformString(true);
+ result = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
+ }
+
+ return result;
+ }
+
+ public IFile getFile(Object element) {
+ if (element instanceof ScopeEntry) {
+ IResource resource = getWorkspaceResource((ScopeEntry) element);
+ if (resource instanceof IFile) {
+ return (IFile) resource;
+ }
+ }
+ return null;
+ }
+
+ public boolean isShownInEditor(Match match, IEditorPart editor) {
+ if (match instanceof AbstractResultEntry) {
+ Object element = match.getElement();
+ if (element instanceof ScopeEntry) {
+ if (Objects.equal(EditorUtils.getResourceURI(editor), ((ScopeEntry) element).getResourceURI())) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ @Override
+ public Match[] getMatches(Object element) {
+
+ Match[] matchList = super.getMatches(element);
+ Set<Match> matchToKeep = new HashSet<Match>();
+ List<Object> sourceList = new ArrayList<Object>();
+ // Get matches which are still true
+ for (Match match : matchList) {
+ if (match instanceof AbstractResultEntry) {
+ if (((AbstractResultEntry) match).getSource() != null) {
+ if (match instanceof AttributeMatch) {
+ Object attribute = ((AttributeMatch) match).getMetaAttribute();
+ String value = null;
+ EObject target = (EObject) ((AbstractResultEntry) match).getSource();
+ if (attribute instanceof EAttribute) {
+
+ value = String.valueOf(target.eGet((EStructuralFeature) attribute));
+ } else if (attribute instanceof Property) {
+
+ value = getStringValueOfProperty((Element) ((AbstractResultEntry) match).getSource(), ((AttributeMatch) match).getStereotype(), ((Property) attribute));
+
+
+ }
+ if (value != null && !this.getQuery().isRegularExpression()) {
+ if (value.length() >= match.getLength() - match.getOffset()) {
+ value = value.substring(match.getOffset(), match.getLength());
+ if (this.searchQuery.isCaseSensitive()) {
+ if (value.equals(this.searchQuery.getSearchQueryText())) {
+ ((AbstractResultEntry) match).recursiveHierarchy((AbstractResultEntry) ((AbstractResultEntry) match).getParent());
+ matchToKeep.add(match);
+ sourceList.add(((AbstractResultEntry) match).getSource());
+ }
+ } else {
+ if (value.equalsIgnoreCase(this.searchQuery.getSearchQueryText())) {
+
+ ((AbstractResultEntry) match).recursiveHierarchy((AbstractResultEntry) ((AbstractResultEntry) match).getParent());
+
+ matchToKeep.add(match);
+ sourceList.add(((AbstractResultEntry) match).getSource());
+ }
+ }
+ }
+ } else if (this.getQuery().isRegularExpression()) {
+ if (this.getQuery().getSearchQueryText() != null) {
+
+
+ Pattern pattern = PatternHelper.getInstance().createPattern(this.getQuery().getSearchQueryText(), false, true);
+ Matcher m = pattern.matcher(value);
+ if (m.matches()) {
+ int start = m.start();
+ int end = m.end();
+ if (start == match.getOffset() && end == match.getOffset() + match.getLength()) {
+ matchToKeep.add(match);
+ sourceList.add(((AbstractResultEntry) match).getSource());
+ }
+ }
+ }
+ }
+ } else if (match instanceof ModelElementMatch) {
+ ((AbstractResultEntry) match).recursiveHierarchy((AbstractResultEntry) match);
+
+ matchToKeep.add(match);
+ sourceList.add(((AbstractResultEntry) match).getSource());
+ }
+ }
+ }
+ }
+ // Now get Viewer
+ for (Match match : matchList) {
+ if (match instanceof ViewerMatch) {
+ Object source = ((ViewerMatch) match).getSemanticElement();
+ if (sourceList.contains(source)) {
+ matchToKeep.add(match);
+ }
+ }
+
+ }
+
+ return matchToKeep.toArray(new Match[matchToKeep.size()]);
+ // return ((PapyrusQuery)searchQuery).getfResults().toArray(new Match[matchToKeep.size()]);
+ //
+ }
+
+ public Match[] computeContainedMatches(AbstractTextSearchResult result, IEditorPart editor) {
+ Set<Object> results = new HashSet<Object>();
+ Set<AbstractResultEntry> allMatches = MatchUtils.getMatches(result, true);
+ for (AbstractResultEntry modelMatch : allMatches) {
+ Object element = modelMatch.getElement();
+ if (element instanceof ScopeEntry) {
+ if (((ScopeEntry) element).getResourceURI().equals(EditorUtils.getResourceURI(editor))) {
+ results.add(modelMatch);
+ }
+ }
+ }
+
+ Match[] arrayResult = new Match[results.size()];
+
+ return results.toArray(arrayResult);
+ }
+
+ @Override
+ public int getMatchCount() {
+
+ List<Object> elementList = Arrays.asList(this.getElements());
+ int count = 0;
+ for (Object element : elementList) {
+
+ for (Match match : this.getMatches(element)) {
+ if (match instanceof ModelMatch || match instanceof ViewerMatch) {
+ count++;
+ }
+ }
+
+ }
+ return count;
+ }
+
+ private String getStringValueOfProperty(Element element, Stereotype stereotype, Property property) {
+ Object value = element.getValue(stereotype, property.getName());
+ if (value instanceof String) {
+ return (String) value;
+ } else if (value instanceof EnumerationLiteral) {
+ return ((EnumerationLiteral) value).getName();
+ } else {
+ return String.valueOf(value);
+ }
+ }
+
+
+}

Back to the top