Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/pages/PapyrusSearchPage.java2444
-rw-r--r--plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/providers/ResultContentProvider.java9
-rw-r--r--plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/query/AbstractPapyrusQuery.java45
-rw-r--r--plugins/views/org.eclipse.papyrus.views.search/src/org/eclipse/papyrus/views/search/scope/ScopeEntry.java472
-rw-r--r--plugins/views/org.eclipse.papyrus.views.search/src/org/eclipse/papyrus/views/search/utils/DefaultServiceRegistryTracker.java182
-rw-r--r--plugins/views/org.eclipse.papyrus.views.search/src/org/eclipse/papyrus/views/search/utils/IServiceRegistryTracker.java73
6 files changed, 1771 insertions, 1454 deletions
diff --git a/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/pages/PapyrusSearchPage.java b/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/pages/PapyrusSearchPage.java
index b63f785f543..0dee544a5ef 100644
--- a/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/pages/PapyrusSearchPage.java
+++ b/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/pages/PapyrusSearchPage.java
@@ -1,1218 +1,1226 @@
-/*****************************************************************************
- * 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.pages;
-
-import java.lang.reflect.InvocationTargetException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-import java.util.regex.PatternSyntaxException;
-
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.runtime.IAdaptable;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.emf.common.util.URI;
-import org.eclipse.emf.ecore.EAttribute;
-import org.eclipse.emf.ecore.EClass;
-import org.eclipse.emf.ecore.EClassifier;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.impl.EClassImpl;
-import org.eclipse.emf.ecore.resource.ResourceSet;
-import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.jdt.annotation.Nullable;
-import org.eclipse.jface.dialogs.DialogPage;
-import org.eclipse.jface.dialogs.MessageDialog;
-import org.eclipse.jface.dialogs.ProgressMonitorDialog;
-import org.eclipse.jface.operation.IRunnableWithProgress;
-import org.eclipse.jface.viewers.CheckStateChangedEvent;
-import org.eclipse.jface.viewers.CheckboxTreeViewer;
-import org.eclipse.jface.viewers.ICheckStateListener;
-import org.eclipse.jface.viewers.ICheckStateProvider;
-import org.eclipse.jface.viewers.ICheckable;
-import org.eclipse.jface.viewers.ILabelProvider;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.jface.viewers.ViewerSorter;
-import org.eclipse.ocl.examples.domain.evaluation.DomainModelManager;
-import org.eclipse.ocl.examples.domain.values.Value;
-import org.eclipse.ocl.examples.domain.values.impl.InvalidValueException;
-import org.eclipse.ocl.examples.pivot.ExpressionInOCL;
-import org.eclipse.ocl.examples.pivot.ParserException;
-import org.eclipse.ocl.examples.pivot.context.EObjectContext;
-import org.eclipse.ocl.examples.pivot.context.ParserContext;
-import org.eclipse.ocl.examples.pivot.manager.MetaModelManager;
-import org.eclipse.ocl.examples.pivot.manager.MetaModelManagerListener;
-import org.eclipse.ocl.examples.pivot.manager.MetaModelManagerResourceSetAdapter;
-import org.eclipse.ocl.examples.pivot.utilities.BaseResource;
-import org.eclipse.ocl.examples.pivot.utilities.PivotUtil;
-import org.eclipse.ocl.examples.xtext.base.utilities.CS2PivotResourceAdapter;
-import org.eclipse.ocl.examples.xtext.console.xtfo.EmbeddedXtextEditor;
-import org.eclipse.ocl.examples.xtext.essentialocl.ui.model.BaseDocument;
-import org.eclipse.ocl.examples.xtext.essentialocl.utilities.EssentialOCLCSResource;
-import org.eclipse.ocl.examples.xtext.essentialocl.utilities.EssentialOCLPlugin;
-import org.eclipse.papyrus.infra.core.resource.NotFoundException;
-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.CheckBoxFilteredTree;
-import org.eclipse.papyrus.uml.search.ui.Messages;
-import org.eclipse.papyrus.uml.search.ui.actions.ReplaceAction;
-import org.eclipse.papyrus.uml.search.ui.providers.OCLContextContentProvider;
-import org.eclipse.papyrus.uml.search.ui.providers.ParticipantTypeAttribute;
-import org.eclipse.papyrus.uml.search.ui.providers.ParticipantTypeContentProvider;
-import org.eclipse.papyrus.uml.search.ui.providers.ParticipantTypeElement;
-import org.eclipse.papyrus.uml.search.ui.providers.ParticipantTypeLabelProvider;
-import org.eclipse.papyrus.uml.search.ui.query.AbstractPapyrusQuery;
-import org.eclipse.papyrus.uml.search.ui.query.PapyrusAdvancedQuery;
-import org.eclipse.papyrus.uml.search.ui.query.PapyrusOCLQuery;
-import org.eclipse.papyrus.uml.search.ui.query.PapyrusQuery;
-import org.eclipse.papyrus.uml.stereotypecollector.StereotypeCollector;
-import org.eclipse.papyrus.uml.tools.model.UmlModel;
-import org.eclipse.papyrus.views.search.regex.PatternHelper;
-import org.eclipse.papyrus.views.search.scope.ScopeCollector;
-import org.eclipse.papyrus.views.search.scope.ScopeEntry;
-import org.eclipse.search.ui.IReplacePage;
-import org.eclipse.search.ui.ISearchPage;
-import org.eclipse.search.ui.ISearchPageContainer;
-import org.eclipse.search.ui.ISearchQuery;
-import org.eclipse.search.ui.ISearchResultPage;
-import org.eclipse.search.ui.ISearchResultViewPart;
-import org.eclipse.search.ui.NewSearchUI;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.custom.VerifyKeyListener;
-import org.eclipse.swt.events.ModifyEvent;
-import org.eclipse.swt.events.ModifyListener;
-import org.eclipse.swt.events.MouseAdapter;
-import org.eclipse.swt.events.MouseEvent;
-import org.eclipse.swt.events.SelectionAdapter;
-import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.events.SelectionListener;
-import org.eclipse.swt.events.VerifyEvent;
-import org.eclipse.swt.layout.GridData;
-import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Button;
-import org.eclipse.swt.widgets.Combo;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Control;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Group;
-import org.eclipse.swt.widgets.Label;
-import org.eclipse.swt.widgets.Text;
-import org.eclipse.ui.IWorkbench;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
-import org.eclipse.ui.dialogs.PatternFilter;
-import org.eclipse.uml2.uml.Class;
-import org.eclipse.uml2.uml.Element;
-import org.eclipse.uml2.uml.Enumeration;
-import org.eclipse.uml2.uml.NamedElement;
-import org.eclipse.uml2.uml.Property;
-import org.eclipse.uml2.uml.Stereotype;
-import org.eclipse.uml2.uml.UMLPackage;
-import org.eclipse.uml2.uml.util.UMLUtil;
-import org.eclipse.xtext.resource.XtextResource;
-import org.eclipse.xtext.ui.editor.model.IXtextDocument;
-import org.eclipse.xtext.ui.editor.outline.IOutlineNode;
-import org.eclipse.xtext.ui.editor.outline.impl.EObjectNode;
-import org.eclipse.xtext.ui.editor.outline.impl.EStructuralFeatureNode;
-import org.eclipse.xtext.util.concurrent.IUnitOfWork;
-
-import com.google.inject.Injector;
-
-/**
- *
- * Papyrus specific search page
- *
- */
-public class PapyrusSearchPage extends DialogPage implements ISearchPage, IReplacePage, MetaModelManagerListener {
-
- public PapyrusSearchPage() {
- }
-
-
- private HashMap<ParticipantTypeElement, List<ParticipantTypeAttribute>> participantsList = new HashMap<ParticipantTypeElement, List<ParticipantTypeAttribute>>();
-
- private ArrayList<ParticipantTypeElement> result = new ArrayList<ParticipantTypeElement>();
-
- private Collection<Stereotype> availableStereotypes;
-
- private static final String REGULAR_EXPRESSION_ILLFORMED = Messages.PapyrusSearchPage_0;
-
- private static final String OCL_QUERY_ILLFORMED = Messages.PapyrusSearchPage_0;
-
- private static final String SEARCH_ISSUE = Messages.PapyrusSearchPage_1;
-
- private static final String SEARCH_IN_ALL_STRINGS = Messages.PapyrusSearchPage_2;
-
- private static final String SEARCH_IN_STEREOTYPE_ATTRIBUTES = Messages.PapyrusSearchPage_3;
-
- private Text searchQueryText;
-
- private Text oclContext;
-
- private ISearchPageContainer container;
-
- private CheckBoxFilteredTree participantTypesTree;
-
- private CheckboxTreeViewer participantTypesTreeViewer;
-
- private Label searchQueryExplanatoryLabel;
-
- private Button btnRegularExpression;
-
- private Button btnCaseSensitive;
-
-
- private Button btnSearchAllStringAttributes;
-
- private Button btnSearchInName;
-
- private Combo queryKind;
-
- private Combo searchKind;
-
- private EmbeddedXtextEditor oclEditor;
-
- private Composite queryComposite;
-
- private EObject contextObject;
-
- private static final int TEXT_QUERY_KIND = 0;
-
- private static final int OCL_QUERY_KIND = 1;
-
- private static final int SIMPLE_SEARCH = 0;
-
- private static final int ADVANCED_SEARCH = 1;
-
- private ParserContext parserContext;
-
- private MetaModelManager nullMetaModelManager = null;
-
- private DomainModelManager modelManager = null;
-
- protected Composite textQueryComposite;
-
- protected Composite textQueryComposite2;
-
-
-
- private Composite advancedSearchComposite;
-
- protected Group grpSearchFor;
-
- private Composite textQueryFieldsComposite;
-
- protected Set<EObject> umlMetaClasses = new HashSet<EObject>();
-
- protected ParticipantTypeContentProvider participantTypeContentProvider = new ParticipantTypeContentProvider();
-
-
-
-
- protected void createSimpleSearchQueryField() {
-
- textQueryComposite = new Composite(queryComposite, SWT.NONE);
- textQueryComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
- textQueryComposite.setLayout(new GridLayout(2, false));
-
- textQueryFieldsComposite = new Composite(textQueryComposite, SWT.NONE);
- textQueryFieldsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
- textQueryFieldsComposite.setLayout(new GridLayout(1, false));
-
- searchQueryText = new Text(textQueryFieldsComposite, SWT.BORDER);
- searchQueryText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
- searchQueryText.addModifyListener(new ModifyListener() {
-
- public void modifyText(ModifyEvent evt) {
- validateRegex();
- }
- });
- searchQueryText.setFocus();
-
- searchQueryExplanatoryLabel = new Label(textQueryFieldsComposite, SWT.NONE);
- searchQueryExplanatoryLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
-
-
- Composite compositeParameters = new Composite(textQueryComposite, SWT.NONE);
- compositeParameters.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
- compositeParameters.setLayout(new GridLayout(1, false));
-
-
-
- btnCaseSensitive = new Button(compositeParameters, SWT.CHECK);
- btnCaseSensitive.setText(Messages.PapyrusSearchPage_5);
-
- btnRegularExpression = new Button(compositeParameters, SWT.CHECK);
- btnRegularExpression.setText(Messages.PapyrusSearchPage_6);
- btnRegularExpression.addSelectionListener(new SelectionListener() {
-
- public void widgetSelected(SelectionEvent e) {
- btnCaseSensitive.setEnabled(!btnRegularExpression.getSelection());
- validateRegex();
- searchQueryText.forceFocus();
- }
-
- public void widgetDefaultSelected(SelectionEvent e) {
- widgetSelected(e);
- }
- });
-
-
-
- grpSearchFor = new Group(textQueryComposite, SWT.NONE);
- grpSearchFor.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
- grpSearchFor.setLayout(new GridLayout(1, false));
- grpSearchFor.setText(Messages.PapyrusSearchPage_8);
-
- Composite groupComposite = new Composite(grpSearchFor, SWT.NONE);
- groupComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
- groupComposite.setLayout(new GridLayout(2, false));
-
- Label lblSearchKind = new Label(groupComposite, SWT.NONE);
- lblSearchKind.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
- lblSearchKind.setText(Messages.PapyrusSearchPage_7);
-
- searchKind = new Combo(groupComposite, SWT.VERTICAL | SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY);
- searchKind.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
- searchKind.add(Messages.PapyrusSearchPage_11);
- searchKind.add(Messages.PapyrusSearchPage_12);
- searchKind.select(SIMPLE_SEARCH);
- searchKind.addSelectionListener(new SelectionAdapter() {
-
- public void widgetSelected(SelectionEvent e) {
- for(Control childControl : advancedSearchComposite.getChildren()) {
- childControl.dispose();
- }
-
- if(searchKind.getSelectionIndex() == ADVANCED_SEARCH) {
- if(participantsList.size() == 0) {
- createResultList();
- }
- createAdvancedSearch();
- } else {
- simpleSearch();
- }
- advancedSearchComposite.layout();
- }
- });
-
- advancedSearchComposite = new Composite(groupComposite, SWT.NONE);
- // gd_advancedSearchComposite.widthHint = 479;
- advancedSearchComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
- advancedSearchComposite.setLayout(new GridLayout(2, false));
-
- simpleSearch();
-
-
-
- }
-
- protected void createResultList() {
- ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell());
- IRunnableWithProgress computeAvailableTypes = new IRunnableWithProgress() {
-
- public void run(IProgressMonitor thePM) throws InterruptedException {
-
- //UML metaclasses
- for(EClassifier eClassifier : UMLPackage.eINSTANCE.getEClassifiers()) {
- if(eClassifier instanceof EClass) {
- ParticipantTypeElement parentElement = new ParticipantTypeElement(eClassifier);
- if(parentElement.getElement() instanceof EClass) {
- List<ParticipantTypeAttribute> attributeList = new ArrayList<ParticipantTypeAttribute>();
- for(EObject eAttribute : ((EClass)(parentElement).getElement()).getEAllAttributes()) {
- ParticipantTypeAttribute attribute = new ParticipantTypeAttribute(eAttribute, ((ParticipantTypeElement)parentElement));
- attributeList.add(attribute);
-
-
- }
- participantsList.put(parentElement, attributeList);
- }
- }
- }
-
-
- //Find available stereotypes
- availableStereotypes = StereotypeCollector.getInstance().computeAvailableStereotypes(container);
- for(Stereotype stereotype : availableStereotypes) {
- ParticipantTypeElement parentElement = new ParticipantTypeElement(stereotype);
- List<ParticipantTypeAttribute> attributeList = new ArrayList<ParticipantTypeAttribute>();
- for(Property property : ((Stereotype)((ParticipantTypeElement)parentElement).getElement()).getAllAttributes()) {
- if(!property.getName().startsWith("base_")) { //$NON-NLS-1$
- if(property.getType() instanceof Element) {
- if(UMLUtil.isBoolean(property.getType()) || UMLUtil.isString(property.getType()) || UMLUtil.isInteger(property.getType()) || UMLUtil.isReal(property.getType()) || UMLUtil.isUnlimitedNatural(property.getType()) || property.getType() instanceof Enumeration) {
- ParticipantTypeAttribute attribute = new ParticipantTypeAttribute(property, (ParticipantTypeElement)parentElement);
- attributeList.add(attribute);
- }
-
- }
- }
-
- }
-
- participantsList.put(parentElement, attributeList);
-
-
- }
-
-
- }
-
- };
-
- try {
- dialog.run(true, true, computeAvailableTypes);
- } catch (InvocationTargetException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (InterruptedException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
-
- }
-
- protected void createAdvancedSearch() {
-
- participantTypesTree = new CheckBoxFilteredTree(advancedSearchComposite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE, new PatternFilter(), true);
- participantTypesTree.setLayout(new GridLayout());
- GridData chechboxTreeViewerGridData = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
- chechboxTreeViewerGridData.heightHint = 150;
- participantTypesTree.setLayoutData(chechboxTreeViewerGridData);
-
- participantTypesTreeViewer = (CheckboxTreeViewer)participantTypesTree.getViewer();
- participantTypesTreeViewer.setContentProvider(participantTypeContentProvider);
- participantTypesTreeViewer.setLabelProvider(new ParticipantTypeLabelProvider());
- participantTypesTreeViewer.setSorter(new ViewerSorter());
- participantTypesTreeViewer.setCheckStateProvider(new ICheckStateProvider() {
-
- public boolean isGrayed(Object element) {
- return false;
- }
-
- public boolean isChecked(Object element) {
- if(element instanceof ParticipantTypeElement) {
-
- return ((ParticipantTypeElement)element).isChecked();
-
- }
- return false;
- }
- });
- Composite participantManipualtionComposite = new Composite(advancedSearchComposite, SWT.NONE);
- participantManipualtionComposite.setLayout(new GridLayout(1, false));
- participantManipualtionComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, true, 1, 1));
-
- Button btnSelectSub = new Button(participantManipualtionComposite, SWT.PUSH);
- btnSelectSub.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
- btnSelectSub.setText(Messages.PapyrusSearchPage_14);
- btnSelectSub.addMouseListener(new MouseAdapter() {
-
- @Override
- public void mouseUp(MouseEvent e) {
- ISelection selection = participantTypesTreeViewer.getSelection();
- if(selection instanceof IStructuredSelection) {
- Object selectedElement = ((IStructuredSelection)selection).getFirstElement();
-
- if(selectedElement instanceof ParticipantTypeElement) {
- List<ParticipantTypeAttribute> attributeParentList = new ArrayList<ParticipantTypeAttribute>();
-
- for(Object attribute : participantsList.get(selectedElement)) {
- if(attribute instanceof ParticipantTypeAttribute) {
- if(((ParticipantTypeAttribute)attribute).isChecked()) {
- attributeParentList.add(((ParticipantTypeAttribute)attribute));
- }
-
- }
- }
- if(((ParticipantTypeElement)selectedElement).getElement() instanceof EClass) {
-
- selectAllSubUML((ParticipantTypeElement)selectedElement, attributeParentList);
-
- } else if(((ParticipantTypeElement)selectedElement).getElement() instanceof Stereotype) {
-
- selectAllSubSter((ParticipantTypeElement)selectedElement, attributeParentList);
-
- }
- }
- }
-
- participantTypesTreeViewer.refresh();
- }
- });
-
- Button btnSelectAll = new Button(participantManipualtionComposite, SWT.PUSH);
- btnSelectAll.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
- btnSelectAll.setText(Messages.PapyrusSearchPage_9);
-
- btnSelectAll.addMouseListener(new MouseAdapter() {
-
- @Override
- public void mouseUp(MouseEvent e) {
- for(ParticipantTypeElement element : participantsList.keySet()) {
- if(!element.isChecked()) {
- element.setChecked(true);
-
- for(ParticipantTypeAttribute attribute : participantsList.get(element)) {
- attribute.setChecked(true);
-
- }
- }
- }
- participantTypesTreeViewer.refresh();
-
- }
- });
-
- Button btnDeselectAll = new Button(participantManipualtionComposite, SWT.PUSH);
- btnDeselectAll.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
- btnDeselectAll.setText(Messages.PapyrusSearchPage_10);
- btnDeselectAll.addMouseListener(new MouseAdapter() {
-
- @Override
- public void mouseUp(MouseEvent e) {
- for(ParticipantTypeElement element : participantsList.keySet()) {
- if(element.isChecked()) {
- element.setChecked(false);
-
- for(ParticipantTypeAttribute attribute : participantsList.get(element)) {
- attribute.setChecked(false);
-
-
- }
- }
- }
- participantTypesTreeViewer.refresh();
- }
- });
-
-
- Button btnRefreshTypes = new Button(participantManipualtionComposite, SWT.PUSH);
- btnRefreshTypes.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
- btnRefreshTypes.setText(Messages.PapyrusSearchPage_15);
- btnRefreshTypes.addMouseListener(new MouseAdapter() {
-
- @Override
- public void mouseUp(MouseEvent e) {
-
- createAdvancedSearch();
-
- }
- });
-
- participantTypesTreeViewer.setInput(participantsList);
-
- // participantTypesTreeViewer.setAllChecked(true);
- ((ICheckable)participantTypesTreeViewer).addCheckStateListener(new ICheckStateListener() {
-
-
- public void checkStateChanged(CheckStateChangedEvent event) {
- if(event.getElement() instanceof ParticipantTypeElement) {
-
-
- // If the item is checked . . .
- if(event.getChecked()) {
- Object selectedElement = event.getElement();
-
- ((ParticipantTypeElement)selectedElement).setChecked(true);
- participantTypesTreeViewer.refresh(selectedElement);
-
- if(selectedElement instanceof ParticipantTypeAttribute) {
- ParticipantTypeElement parent = ((ParticipantTypeAttribute)selectedElement).getParent();
- if(parent != null) {
- // participantTypesTreeViewer.setChecked(parent, true);
- ((ParticipantTypeElement)parent).setChecked(true);
- participantTypesTreeViewer.refresh(parent);
- }
- }
- } else {
- Object selectedElement = event.getElement();
- ((ParticipantTypeElement)selectedElement).setChecked(false);
- participantTypesTreeViewer.refresh(selectedElement);
-
- if(((ParticipantTypeElement)selectedElement).getElement() instanceof Stereotype || ((ParticipantTypeElement)selectedElement).getElement() instanceof EClassImpl) {
- for(Object attribute : participantTypesTreeViewer.getCheckedElements()) {
- if(attribute instanceof ParticipantTypeAttribute) {
- if(((ParticipantTypeAttribute)attribute).getParent().equals(selectedElement)) {
- // participantTypesTreeViewer.setChecked(attribute, false);
- ((ParticipantTypeElement)attribute).setChecked(false);
- participantTypesTreeViewer.refresh(attribute);
- }
- }
- }
- }
- }
- }
-
-
- }
- });
- }
-
- protected void selectAllSubSter(final ParticipantTypeElement elementParent, final List<ParticipantTypeAttribute> attributeParentList) {
- // for(Object element : result) {
- // if(element instanceof Stereotype) {
- // List<Class> superTypes = ((Stereotype)element).getSuperClasses();
- // for(Class type : superTypes) {
- // ((Stereotype)elementParent).getName();
- //
- // if(((Class)type).getName().equals(((Stereotype)elementParent).getName())) {
- // participantTypesTreeViewer.setChecked(element, true);
- //
- // }
- // }
- // }
- // }
- ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell());
- IRunnableWithProgress computeAvailableTypes = new IRunnableWithProgress() {
-
- public void run(IProgressMonitor thePM) throws InterruptedException {
- for(Object element : participantsList.keySet()) {
- if(element instanceof ParticipantTypeElement) {
- if(((ParticipantTypeElement)element).getElement() instanceof Stereotype) {
- List<Class> superTypes = ((Class)((ParticipantTypeElement)element).getElement()).getSuperClasses();
-
- if(superTypes.contains(elementParent.getElement())) {
- // participantTypesTreeViewer.setChecked(element, true);
- ((ParticipantTypeElement)element).setChecked(true);
-
-
- //Proceed with attributes
- for(ParticipantTypeAttribute attributeParent : attributeParentList) {
- for(ParticipantTypeAttribute attributeToEvaluate : participantsList.get(element)) {
- if(attributeParent.getElement() == attributeToEvaluate.getElement()) {
-
- attributeToEvaluate.setChecked(true);
-
-
- }
-
- }
- }
- }
- }
- }
- }
-
-
- }
-
- };
-
- try {
- dialog.run(true, true, computeAvailableTypes);
- } catch (InvocationTargetException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (InterruptedException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
-
- }
-
- protected void selectAllSubUML(final ParticipantTypeElement elementParent, final List<ParticipantTypeAttribute> attributeParentList) {
-
- ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell());
- IRunnableWithProgress computeAvailableTypes = new IRunnableWithProgress() {
-
- public void run(IProgressMonitor thePM) throws InterruptedException {
- for(Object element : participantsList.keySet()) {
- if(element instanceof ParticipantTypeElement) {
- if(((ParticipantTypeElement)element).getElement() instanceof EClass) {
- List<EClass> superTypes = ((EClass)((ParticipantTypeElement)element).getElement()).getEAllSuperTypes();
-
- if(superTypes.contains(elementParent.getElement())) {
- // participantTypesTreeViewer.setChecked(element, true);
- ((ParticipantTypeElement)element).setChecked(true);
-
-
- //Proceed with attributes
- for(ParticipantTypeAttribute attributeParent : attributeParentList) {
- for(ParticipantTypeAttribute attributeToEvaluate : participantsList.get(element)) {
- if(attributeParent.getElement() == attributeToEvaluate.getElement()) {
-
- attributeToEvaluate.setChecked(true);
-
-
- }
-
- }
- }
- }
- }
- }
- }
-
-
- }
-
- };
-
- try {
- dialog.run(true, true, computeAvailableTypes);
- } catch (InvocationTargetException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (InterruptedException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
-
-
- protected void simpleSearch() {
-
-
- Composite participantManipualtionComposite = new Composite(advancedSearchComposite, SWT.NONE);
- participantManipualtionComposite.setLayout(new GridLayout(1, false));
- participantManipualtionComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, true, 1, 1));
-
-
- btnSearchInName = new Button(participantManipualtionComposite, SWT.RADIO);
- btnSearchInName.setText(Messages.PapyrusSearchPage_16);
- btnSearchInName.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 0, 1));
- btnSearchInName.setSelection(true);
-
-
- btnSearchAllStringAttributes = new Button(participantManipualtionComposite, SWT.RADIO);
- btnSearchAllStringAttributes.setText(Messages.PapyrusSearchPage_17);
- btnSearchAllStringAttributes.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
- }
-
- public void initMetaClasses() {
-
- for(EClassifier eClassifier : UMLPackage.eINSTANCE.getEClassifiers()) {
- if(eClassifier instanceof EClass) {
- umlMetaClasses.add(eClassifier);
- }
- }
-
- }
-
-
- protected void createOCLSearchQueryField(EObject root) {
-
- Composite client = queryComposite;
-
- Composite oclContextComposite = new Composite(client, SWT.NONE);
- oclContextComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
- oclContextComposite.setLayout(new GridLayout(2, false));
-
- oclContext = new Text(oclContextComposite, SWT.BORDER);
- oclContext.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
- oclContext.setEnabled(false);
- oclContext.setFocus();
-
- Button btnSelectContext = new Button(oclContextComposite, SWT.PUSH);
- btnSelectContext.setText(Messages.PapyrusSearchPage_18);
- btnSelectContext.addMouseListener(new MouseAdapter() {
-
- @Override
- public void mouseUp(MouseEvent e) {
-
- LabelProviderService labelProviderService = new LabelProviderServiceImpl();
- ILabelProvider labelProvider = labelProviderService.getLabelProvider();
-
- ScopeEntry currentScope = getCurrentScopeEntry();
-
- if(currentScope != null) {
-
- try {
- EObject root = ((UmlModel)currentScope.getModelSet().getModel(UmlModel.MODEL_ID)).lookupRoot();
-
- ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(Display.getCurrent().getActiveShell(), labelProvider, new OCLContextContentProvider());
- dialog.setTitle(Messages.PapyrusSearchPage_19);
- dialog.setMessage(Messages.PapyrusSearchPage_20);
- dialog.setInput(currentScope.getModelSet());
- dialog.setAllowMultiple(false);
- dialog.open();
- Object selection = dialog.getFirstResult();
-
- if(selection instanceof EObject) {
-
- refreshSelection(selection);
-
- if(contextObject instanceof NamedElement) {
- oclContext.setText(((NamedElement)contextObject).getQualifiedName());
- } else {
- oclContext.setText(labelProvider.getText(contextObject));
- }
- }
-
- } catch (NotFoundException notFoundException) {
-
- Activator.log.error(Messages.PapyrusQuery_0 + currentScope.getModelSet(), notFoundException);
- }
- }
-
- }
- });
- btnSelectContext.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
-
- Injector injector = Activator.getInstance().getInjector(EssentialOCLPlugin.LANGUAGE_ID);
- Composite editorComposite = client;
- oclEditor = new EmbeddedXtextEditor(editorComposite, injector, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
-
- oclEditor.getViewer().getTextWidget().addVerifyKeyListener(new VerifyKeyListener() {
-
- public void verifyKey(VerifyEvent e) {
- if(e.keyCode == SWT.KEYPAD_CR || e.keyCode == SWT.CR) {
- if((e.stateMask & (SWT.CTRL | SWT.SHIFT)) == 0) {
- e.doit = false;
- }
- }
- }
- });
-
- GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
- data.heightHint = convertHeightInCharsToPixels(3);
- editorComposite.setLayoutData(data);
-
- refreshSelection(root);
- }
-
- protected ScopeEntry getCurrentScopeEntry() {
- if(container.getSelectedScope() == ISearchPageContainer.SELECTION_SCOPE) {
- Collection<IResource> scope = ScopeCollector.getInstance().computeSearchScope(container);
-
- Collection<ScopeEntry> scopeEntries = createScopeEntries(scope);
-
- if(scopeEntries.size() == 1) {
- Object[] entries = scopeEntries.toArray();
- ScopeEntry selectedResource = (ScopeEntry)entries[0];
-
- return selectedResource;
- }
- }
- return null;
- }
-
- public void createControl(Composite parent) {
-
- initializeDialogUnits(parent);
- Composite searchComposite = new Composite(parent, SWT.NONE);
- searchComposite.setFont(parent.getFont());
- searchComposite.setLayout(new GridLayout(2, false));
- searchComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
-
- Label lblSearchString = new Label(searchComposite, SWT.NONE);
- lblSearchString.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
- lblSearchString.setText(Messages.PapyrusSearchPage_4);
-
- queryKind = new Combo(searchComposite, SWT.VERTICAL | SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY);
- queryKind.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, false, 1, 1));
- queryKind.add(Messages.PapyrusSearchPage_21);
- queryKind.add(Messages.PapyrusSearchPage_22);
-
-
-
- queryKind.select(TEXT_QUERY_KIND);
-
- queryKind.addSelectionListener(new SelectionAdapter() {
-
- public void widgetSelected(SelectionEvent e) {
- for(Control childControl : queryComposite.getChildren()) {
- childControl.dispose();
- }
-
- if(queryKind.getSelectionIndex() == TEXT_QUERY_KIND) {
- createSimpleSearchQueryField();
-
-
-
-
- } else {
- if(container.getSelectedScope() == ISearchPageContainer.SELECTION_SCOPE) {
-
- ScopeEntry currentScope = getCurrentScopeEntry();
- if(currentScope != null) {
- if(currentScope.getModelSet() != null) {
-
- try {
- EObject root = ((UmlModel)currentScope.getModelSet().getModel(UmlModel.MODEL_ID)).lookupRoot();
- createOCLSearchQueryField(root);
-
- if(contextObject instanceof NamedElement) {
- oclContext.setText(((NamedElement)contextObject).getQualifiedName());
- } else {
- LabelProviderService labelProviderService = new LabelProviderServiceImpl();
- ILabelProvider labelProvider = labelProviderService.getLabelProvider();
- oclContext.setText(labelProvider.getText(contextObject));
- }
- } catch (NotFoundException notFoundException) {
-
- Activator.log.error(Messages.PapyrusQuery_0 + currentScope.getModelSet(), notFoundException);
- }
- } else {
- MessageDialog.openWarning(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_23, Messages.PapyrusSearchPage_24);
- createSimpleSearchQueryField();
- queryKind.select(TEXT_QUERY_KIND);
- }
- } else {
- MessageDialog.openWarning(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_25, Messages.PapyrusSearchPage_26);
- createSimpleSearchQueryField();
- queryKind.select(TEXT_QUERY_KIND);
- }
- } else {
- MessageDialog.openWarning(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_27, Messages.PapyrusSearchPage_28);
- createSimpleSearchQueryField();
- queryKind.select(TEXT_QUERY_KIND);
- }
- }
- queryComposite.layout();
- }
- });
-
-
- queryComposite = new Composite(searchComposite, SWT.NONE);
- queryComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
- queryComposite.setLayout(new GridLayout(1, false));
-
- createSimpleSearchQueryField();
-
- setControl(parent);
- }
-
- public boolean getSearchAllStringAttributes() {
- if(btnSearchAllStringAttributes != null) {
- return btnSearchAllStringAttributes.getSelection();
- } else {
- return false;
- }
- }
-
- /**
- * Validate syntax of the regular expression of the search query text.
- *
- * @return true, if successful
- */
- private boolean validateRegex() {
-
- try {
- PatternHelper.getInstance().createPattern(searchQueryText.getText(), btnCaseSensitive.getSelection(), btnRegularExpression.getSelection());
- searchQueryExplanatoryLabel.setForeground(getControl().getForeground());
- searchQueryExplanatoryLabel.setText(""); //$NON-NLS-1$
- return true;
-
- } catch (PatternSyntaxException e) {
- searchQueryExplanatoryLabel.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_RED));
- searchQueryExplanatoryLabel.setText(e.getDescription());
-
- return false;
- }
- }
-
- protected void refreshSelection(final Object selected) {
- final BaseDocument editorDocument = (BaseDocument)oclEditor.getDocument();
- editorDocument.modify(new IUnitOfWork<Object, XtextResource>() {
-
- public Value exec(XtextResource resource) throws Exception {
- Object selectedObject = selected;
- if(selectedObject instanceof IOutlineNode) {
- if(selectedObject instanceof EObjectNode) {
- EObjectNode selectedObjectNode = (EObjectNode)selectedObject;
- @SuppressWarnings("unused")
- URI eObjectURI = selectedObjectNode.getEObjectURI();
- contextObject = null; // FIXME
- // metaModelManager.loadResource(eObjectURI,
- // null, null);
- } else if(selectedObject instanceof EStructuralFeatureNode) {
- contextObject = null;
- } else {
- contextObject = null;
- }
- } else {
- if(selectedObject instanceof IAdaptable) {
- selectedObject = ((IAdaptable)selectedObject).getAdapter(EObject.class);
- }
- if(selectedObject instanceof EObject) {
- contextObject = (EObject)selectedObject;
- } else {
- contextObject = null;
- }
- }
- MetaModelManager metaModelManager = getMetaModelManager(contextObject);
- parserContext = new EObjectContext(metaModelManager, null, contextObject);
- EssentialOCLCSResource csResource = (EssentialOCLCSResource)resource;
- if(csResource != null) {
- if(contextObject != null) {
- CS2PivotResourceAdapter.getAdapter(csResource, metaModelManager);
- }
- ResourceSet resourceSet = oclEditor.getResourceSet();
- if(resourceSet != null) {
- MetaModelManagerResourceSetAdapter.getAdapter(resourceSet, metaModelManager);
- }
- csResource.setParserContext(parserContext);
- }
-
- return null;
- }
- });
- }
-
- /**
- * Create scopeEntries based on an IResources
- *
- * @return the created scopeEntries
- */
- private Collection<ScopeEntry> createScopeEntries(Collection<IResource> scope) {
- Collection<ScopeEntry> results = new HashSet<ScopeEntry>();
-
- for(IResource resource : scope) {
-
- ScopeEntry scopeEntry = new ScopeEntry(resource);
-
- results.add(scopeEntry);
-
- }
-
- return results;
- }
-
- public boolean performAction() {
-
- if(queryKind.getSelectionIndex() == TEXT_QUERY_KIND) {
- if(validateRegex()) {
- Collection<IResource> scope = ScopeCollector.getInstance().computeSearchScope(container);
- Collection<ScopeEntry> scopeEntries = createScopeEntries(scope);
- ISearchQuery query;
- if(searchKind.getSelectionIndex() == SIMPLE_SEARCH) {
- if(searchQueryText.getText().length() == 0) {
- MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_29, Messages.PapyrusSearchPage_30);
- return false;
- } else {
- initMetaClasses();
-
- query = new PapyrusQuery(searchQueryText.getText(), btnCaseSensitive.getSelection(), btnRegularExpression.getSelection(), scopeEntries, umlMetaClasses.toArray(), btnSearchAllStringAttributes.getSelection());
- }
- } else {
-
-
- List<ParticipantTypeElement> participantsToEvaluate = new ArrayList<ParticipantTypeElement>();
- for(ParticipantTypeElement element : this.participantsList.keySet()) {
- if(element.isChecked()) {
- participantsToEvaluate.add(element);
- for(ParticipantTypeAttribute attributesToEvaluate : participantsList.get(element)) {
- if(attributesToEvaluate.isChecked()) {
- participantsToEvaluate.add(attributesToEvaluate);
- }
- }
- }
- }
- if(participantsToEvaluate.size() == 0) {
-
- MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_31, Messages.PapyrusSearchPage_32);
- return false;
- } else {
- if(searchQueryText.getText().length() == 0) {
- for(Object participantChecked : participantTypesTreeViewer.getCheckedElements()) {
- if(participantChecked instanceof ParticipantTypeAttribute) {
- MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_33, Messages.PapyrusSearchPage_34);
- return false;
- }
- }
- }
-
- query = new PapyrusAdvancedQuery(searchQueryText.getText(), btnCaseSensitive.getSelection(), btnRegularExpression.getSelection(), scopeEntries, participantsToEvaluate.toArray());
-
- }
-
- }
- if(query.canRunInBackground()) {
- NewSearchUI.runQueryInBackground(query);
- }
-
- return true;
- } else {
- MessageDialog.openError(Display.getCurrent().getActiveShell(), SEARCH_ISSUE, REGULAR_EXPRESSION_ILLFORMED);
- return false;
- }
- } else {
-
- ScopeEntry scopeEntry = getCurrentScopeEntry();
-
- if(scopeEntry != null) {
-
- try {
-
- PivotUtil.checkResourceErrors("", oclEditor.getResource()); //$NON-NLS-1$
- ExpressionInOCL expressionInOCL = parserContext.getExpression((BaseResource)oclEditor.getResource());
- ISearchQuery query = new PapyrusOCLQuery((BaseDocument)oclEditor.getDocument(), parserContext, getMetaModelManager(contextObject), modelManager, contextObject, scopeEntry);
-
-
- if(query.canRunInBackground()) {
- NewSearchUI.runQueryInBackground(query);
- }
- } catch (ParserException e) {
- Object value = new InvalidValueException(e, Messages.PapyrusSearchPage_35);
- MessageDialog.openError(Display.getCurrent().getActiveShell(), SEARCH_ISSUE, OCL_QUERY_ILLFORMED);
- return false;
- }
-
- return true;
- } else {
- return false;
- }
- }
- }
-
- public void setContainer(ISearchPageContainer container) {
- this.container = container;
- this.container.setPerformActionEnabled(true);
- }
-
- public boolean performReplace() {
- if(queryKind.getSelectionIndex() == TEXT_QUERY_KIND) {
- if(validateRegex()) {
- if(searchQueryText.getText().length() == 0) {
- MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_36, Messages.PapyrusSearchPage_37);
- return false;
- }
-
- Collection<IResource> scope = ScopeCollector.getInstance().computeSearchScope(container);
-
- Collection<ScopeEntry> scopeEntries = createScopeEntries(scope);
- AbstractPapyrusQuery query;
- if(searchKind.getSelectionIndex() == SIMPLE_SEARCH) {
- initMetaClasses();
-
- query = new PapyrusQuery(searchQueryText.getText(), btnCaseSensitive.getSelection(), btnRegularExpression.getSelection(), scopeEntries, umlMetaClasses.toArray(), btnSearchAllStringAttributes.getSelection());
- } else {
- List<ParticipantTypeElement> participantsToEvaluate = new ArrayList<ParticipantTypeElement>();
- for(ParticipantTypeElement element : this.participantsList.keySet()) {
- if(element.isChecked()) {
- participantsToEvaluate.add(element);
- if(participantsList.get(element).size() == 0) {
- MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_38, Messages.PapyrusSearchPage_39);
- return false;
-
- } else {
- for(ParticipantTypeAttribute attributesToEvaluate : participantsList.get(element)) {
- if(attributesToEvaluate.isChecked()) {
- participantsToEvaluate.add(attributesToEvaluate);
- Object value = element.getElement().eGet((EAttribute)attributesToEvaluate.getElement());
- if(!(value instanceof String)) {
- MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_40, Messages.PapyrusSearchPage_41);
- return false;
- }
- }
- }
- }
- }
- }
- query = new PapyrusAdvancedQuery(searchQueryText.getText(), btnCaseSensitive.getSelection(), btnRegularExpression.getSelection(), scopeEntries, participantsToEvaluate.toArray());
-
- }
-
- NewSearchUI.runQueryInForeground(container.getRunnableContext(), query);
-
-
- Display.getCurrent().syncExec(new Runnable() {
-
- public void run() {
- ISearchResultViewPart view = NewSearchUI.activateSearchResultView();
- if(view != null) {
- ISearchResultPage page = view.getActivePage();
-
- if(page instanceof PapyrusSearchResultPage) {
- PapyrusSearchResultPage resultPage = (PapyrusSearchResultPage)page;
- ReplaceAction replaceAction = new ReplaceAction(resultPage.getSite().getShell(), resultPage, null);
- replaceAction.run();
- }
- }
- }
- });
-
- NewSearchUI.runQueryInForeground(container.getRunnableContext(), query);
-
- return true;
- } else {
- MessageDialog.openError(Display.getCurrent().getActiveShell(), SEARCH_ISSUE, REGULAR_EXPRESSION_ILLFORMED);
- return false;
- }
- } else {
- MessageDialog.openWarning(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_42, Messages.PapyrusSearchPage_43);
- return false;
- }
-
- }
-
-
-
-
- public @NonNull
- MetaModelManager getMetaModelManager(@Nullable EObject contextObject) {
- MetaModelManager metaModelManager = contextObject != null ? PivotUtil.findMetaModelManager(contextObject) : null;
- if(metaModelManager != null) {
- return metaModelManager;
- }
- MetaModelManager nullMetaModelManager2 = nullMetaModelManager;
- if(nullMetaModelManager2 == null) {
- nullMetaModelManager2 = nullMetaModelManager = new MetaModelManager();
- nullMetaModelManager2.addListener(this);
- }
- return nullMetaModelManager2;
- }
-
- public void metaModelManagerDisposed(@NonNull MetaModelManager metaModelManager) {
- metaModelManager.removeListener(this);
- reset();
-
- }
-
- protected void flushEvents() {
- IWorkbench workbench = PlatformUI.getWorkbench();
- while(workbench.getDisplay().readAndDispatch());
- }
-
- public void reset() {
- if(oclEditor != null) {
- IXtextDocument document = oclEditor.getDocument();
- MetaModelManager metaModelManager = document.modify(new IUnitOfWork<MetaModelManager, XtextResource>() { // Cancel
-
- // validation
-
- public MetaModelManager exec(XtextResource state) throws Exception {
- if(state == null) {
- return null;
- }
- if(state instanceof BaseResource) {
- ((BaseResource)state).setParserContext(null);
- }
- return PivotUtil.findMetaModelManager(state);
- }
- });
- flushEvents();
- // editor.close(false);
- flushEvents();
- if(metaModelManager != null) {
- metaModelManager.dispose();
- }
- }
- if(modelManager != null) {
- // modelManager.dispose();
- modelManager = null;
- }
- if(nullMetaModelManager != null) {
- nullMetaModelManager.dispose();
- nullMetaModelManager = null;
- }
- parserContext = null;
- contextObject = null;
- }
-}
+/*****************************************************************************
+ * 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) - Fix leaking of all UML models in search results
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.uml.search.ui.pages;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.regex.PatternSyntaxException;
+
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EAttribute;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EClassifier;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.impl.EClassImpl;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.jface.dialogs.DialogPage;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.jface.viewers.CheckStateChangedEvent;
+import org.eclipse.jface.viewers.CheckboxTreeViewer;
+import org.eclipse.jface.viewers.ICheckStateListener;
+import org.eclipse.jface.viewers.ICheckStateProvider;
+import org.eclipse.jface.viewers.ICheckable;
+import org.eclipse.jface.viewers.ILabelProvider;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.ViewerSorter;
+import org.eclipse.ocl.examples.domain.evaluation.DomainModelManager;
+import org.eclipse.ocl.examples.domain.values.Value;
+import org.eclipse.ocl.examples.domain.values.impl.InvalidValueException;
+import org.eclipse.ocl.examples.pivot.ExpressionInOCL;
+import org.eclipse.ocl.examples.pivot.ParserException;
+import org.eclipse.ocl.examples.pivot.context.EObjectContext;
+import org.eclipse.ocl.examples.pivot.context.ParserContext;
+import org.eclipse.ocl.examples.pivot.manager.MetaModelManager;
+import org.eclipse.ocl.examples.pivot.manager.MetaModelManagerListener;
+import org.eclipse.ocl.examples.pivot.manager.MetaModelManagerResourceSetAdapter;
+import org.eclipse.ocl.examples.pivot.utilities.BaseResource;
+import org.eclipse.ocl.examples.pivot.utilities.PivotUtil;
+import org.eclipse.ocl.examples.xtext.base.utilities.CS2PivotResourceAdapter;
+import org.eclipse.ocl.examples.xtext.console.xtfo.EmbeddedXtextEditor;
+import org.eclipse.ocl.examples.xtext.essentialocl.ui.model.BaseDocument;
+import org.eclipse.ocl.examples.xtext.essentialocl.utilities.EssentialOCLCSResource;
+import org.eclipse.ocl.examples.xtext.essentialocl.utilities.EssentialOCLPlugin;
+import org.eclipse.papyrus.infra.core.resource.NotFoundException;
+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.CheckBoxFilteredTree;
+import org.eclipse.papyrus.uml.search.ui.Messages;
+import org.eclipse.papyrus.uml.search.ui.actions.ReplaceAction;
+import org.eclipse.papyrus.uml.search.ui.providers.OCLContextContentProvider;
+import org.eclipse.papyrus.uml.search.ui.providers.ParticipantTypeAttribute;
+import org.eclipse.papyrus.uml.search.ui.providers.ParticipantTypeContentProvider;
+import org.eclipse.papyrus.uml.search.ui.providers.ParticipantTypeElement;
+import org.eclipse.papyrus.uml.search.ui.providers.ParticipantTypeLabelProvider;
+import org.eclipse.papyrus.uml.search.ui.query.AbstractPapyrusQuery;
+import org.eclipse.papyrus.uml.search.ui.query.PapyrusAdvancedQuery;
+import org.eclipse.papyrus.uml.search.ui.query.PapyrusOCLQuery;
+import org.eclipse.papyrus.uml.search.ui.query.PapyrusQuery;
+import org.eclipse.papyrus.uml.stereotypecollector.StereotypeCollector;
+import org.eclipse.papyrus.uml.tools.model.UmlModel;
+import org.eclipse.papyrus.views.search.regex.PatternHelper;
+import org.eclipse.papyrus.views.search.scope.ScopeCollector;
+import org.eclipse.papyrus.views.search.scope.ScopeEntry;
+import org.eclipse.papyrus.views.search.utils.DefaultServiceRegistryTracker;
+import org.eclipse.papyrus.views.search.utils.IServiceRegistryTracker;
+import org.eclipse.search.ui.IReplacePage;
+import org.eclipse.search.ui.ISearchPage;
+import org.eclipse.search.ui.ISearchPageContainer;
+import org.eclipse.search.ui.ISearchQuery;
+import org.eclipse.search.ui.ISearchResultPage;
+import org.eclipse.search.ui.ISearchResultViewPart;
+import org.eclipse.search.ui.NewSearchUI;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.VerifyKeyListener;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.events.MouseAdapter;
+import org.eclipse.swt.events.MouseEvent;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.events.VerifyEvent;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Group;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
+import org.eclipse.ui.dialogs.PatternFilter;
+import org.eclipse.uml2.uml.Class;
+import org.eclipse.uml2.uml.Element;
+import org.eclipse.uml2.uml.Enumeration;
+import org.eclipse.uml2.uml.NamedElement;
+import org.eclipse.uml2.uml.Property;
+import org.eclipse.uml2.uml.Stereotype;
+import org.eclipse.uml2.uml.UMLPackage;
+import org.eclipse.uml2.uml.util.UMLUtil;
+import org.eclipse.xtext.resource.XtextResource;
+import org.eclipse.xtext.ui.editor.model.IXtextDocument;
+import org.eclipse.xtext.ui.editor.outline.IOutlineNode;
+import org.eclipse.xtext.ui.editor.outline.impl.EObjectNode;
+import org.eclipse.xtext.ui.editor.outline.impl.EStructuralFeatureNode;
+import org.eclipse.xtext.util.concurrent.IUnitOfWork;
+
+import com.google.inject.Injector;
+
+/**
+ *
+ * Papyrus specific search page
+ *
+ */
+public class PapyrusSearchPage extends DialogPage implements ISearchPage, IReplacePage, MetaModelManagerListener {
+
+ public PapyrusSearchPage() {
+ }
+
+
+ private HashMap<ParticipantTypeElement, List<ParticipantTypeAttribute>> participantsList = new HashMap<ParticipantTypeElement, List<ParticipantTypeAttribute>>();
+
+ private ArrayList<ParticipantTypeElement> result = new ArrayList<ParticipantTypeElement>();
+
+ private Collection<Stereotype> availableStereotypes;
+
+ private static final String REGULAR_EXPRESSION_ILLFORMED = Messages.PapyrusSearchPage_0;
+
+ private static final String OCL_QUERY_ILLFORMED = Messages.PapyrusSearchPage_0;
+
+ private static final String SEARCH_ISSUE = Messages.PapyrusSearchPage_1;
+
+ private static final String SEARCH_IN_ALL_STRINGS = Messages.PapyrusSearchPage_2;
+
+ private static final String SEARCH_IN_STEREOTYPE_ATTRIBUTES = Messages.PapyrusSearchPage_3;
+
+ private Text searchQueryText;
+
+ private Text oclContext;
+
+ private ISearchPageContainer container;
+
+ private CheckBoxFilteredTree participantTypesTree;
+
+ private CheckboxTreeViewer participantTypesTreeViewer;
+
+ private Label searchQueryExplanatoryLabel;
+
+ private Button btnRegularExpression;
+
+ private Button btnCaseSensitive;
+
+
+ private Button btnSearchAllStringAttributes;
+
+ private Button btnSearchInName;
+
+ private Combo queryKind;
+
+ private Combo searchKind;
+
+ private EmbeddedXtextEditor oclEditor;
+
+ private Composite queryComposite;
+
+ private EObject contextObject;
+
+ private static final int TEXT_QUERY_KIND = 0;
+
+ private static final int OCL_QUERY_KIND = 1;
+
+ private static final int SIMPLE_SEARCH = 0;
+
+ private static final int ADVANCED_SEARCH = 1;
+
+ private ParserContext parserContext;
+
+ private MetaModelManager nullMetaModelManager = null;
+
+ private DomainModelManager modelManager = null;
+
+ protected Composite textQueryComposite;
+
+ protected Composite textQueryComposite2;
+
+
+
+ private Composite advancedSearchComposite;
+
+ protected Group grpSearchFor;
+
+ private Composite textQueryFieldsComposite;
+
+ protected Set<EObject> umlMetaClasses = new HashSet<EObject>();
+
+ protected ParticipantTypeContentProvider participantTypeContentProvider = new ParticipantTypeContentProvider();
+
+
+
+
+ protected void createSimpleSearchQueryField() {
+
+ textQueryComposite = new Composite(queryComposite, SWT.NONE);
+ textQueryComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
+ textQueryComposite.setLayout(new GridLayout(2, false));
+
+ textQueryFieldsComposite = new Composite(textQueryComposite, SWT.NONE);
+ textQueryFieldsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
+ textQueryFieldsComposite.setLayout(new GridLayout(1, false));
+
+ searchQueryText = new Text(textQueryFieldsComposite, SWT.BORDER);
+ searchQueryText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
+ searchQueryText.addModifyListener(new ModifyListener() {
+
+ public void modifyText(ModifyEvent evt) {
+ validateRegex();
+ }
+ });
+ searchQueryText.setFocus();
+
+ searchQueryExplanatoryLabel = new Label(textQueryFieldsComposite, SWT.NONE);
+ searchQueryExplanatoryLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
+
+
+ Composite compositeParameters = new Composite(textQueryComposite, SWT.NONE);
+ compositeParameters.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
+ compositeParameters.setLayout(new GridLayout(1, false));
+
+
+
+ btnCaseSensitive = new Button(compositeParameters, SWT.CHECK);
+ btnCaseSensitive.setText(Messages.PapyrusSearchPage_5);
+
+ btnRegularExpression = new Button(compositeParameters, SWT.CHECK);
+ btnRegularExpression.setText(Messages.PapyrusSearchPage_6);
+ btnRegularExpression.addSelectionListener(new SelectionListener() {
+
+ public void widgetSelected(SelectionEvent e) {
+ btnCaseSensitive.setEnabled(!btnRegularExpression.getSelection());
+ validateRegex();
+ searchQueryText.forceFocus();
+ }
+
+ public void widgetDefaultSelected(SelectionEvent e) {
+ widgetSelected(e);
+ }
+ });
+
+
+
+ grpSearchFor = new Group(textQueryComposite, SWT.NONE);
+ grpSearchFor.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
+ grpSearchFor.setLayout(new GridLayout(1, false));
+ grpSearchFor.setText(Messages.PapyrusSearchPage_8);
+
+ Composite groupComposite = new Composite(grpSearchFor, SWT.NONE);
+ groupComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
+ groupComposite.setLayout(new GridLayout(2, false));
+
+ Label lblSearchKind = new Label(groupComposite, SWT.NONE);
+ lblSearchKind.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
+ lblSearchKind.setText(Messages.PapyrusSearchPage_7);
+
+ searchKind = new Combo(groupComposite, SWT.VERTICAL | SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY);
+ searchKind.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
+ searchKind.add(Messages.PapyrusSearchPage_11);
+ searchKind.add(Messages.PapyrusSearchPage_12);
+ searchKind.select(SIMPLE_SEARCH);
+ searchKind.addSelectionListener(new SelectionAdapter() {
+
+ public void widgetSelected(SelectionEvent e) {
+ for(Control childControl : advancedSearchComposite.getChildren()) {
+ childControl.dispose();
+ }
+
+ if(searchKind.getSelectionIndex() == ADVANCED_SEARCH) {
+ if(participantsList.size() == 0) {
+ createResultList();
+ }
+ createAdvancedSearch();
+ } else {
+ simpleSearch();
+ }
+ advancedSearchComposite.layout();
+ }
+ });
+
+ advancedSearchComposite = new Composite(groupComposite, SWT.NONE);
+ // gd_advancedSearchComposite.widthHint = 479;
+ advancedSearchComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
+ advancedSearchComposite.setLayout(new GridLayout(2, false));
+
+ simpleSearch();
+
+
+
+ }
+
+ protected void createResultList() {
+ ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell());
+ IRunnableWithProgress computeAvailableTypes = new IRunnableWithProgress() {
+
+ public void run(IProgressMonitor thePM) throws InterruptedException {
+
+ //UML metaclasses
+ for(EClassifier eClassifier : UMLPackage.eINSTANCE.getEClassifiers()) {
+ if(eClassifier instanceof EClass) {
+ ParticipantTypeElement parentElement = new ParticipantTypeElement(eClassifier);
+ if(parentElement.getElement() instanceof EClass) {
+ List<ParticipantTypeAttribute> attributeList = new ArrayList<ParticipantTypeAttribute>();
+ for(EObject eAttribute : ((EClass)(parentElement).getElement()).getEAllAttributes()) {
+ ParticipantTypeAttribute attribute = new ParticipantTypeAttribute(eAttribute, ((ParticipantTypeElement)parentElement));
+ attributeList.add(attribute);
+
+
+ }
+ participantsList.put(parentElement, attributeList);
+ }
+ }
+ }
+
+
+ //Find available stereotypes
+ availableStereotypes = StereotypeCollector.getInstance().computeAvailableStereotypes(container);
+ for(Stereotype stereotype : availableStereotypes) {
+ ParticipantTypeElement parentElement = new ParticipantTypeElement(stereotype);
+ List<ParticipantTypeAttribute> attributeList = new ArrayList<ParticipantTypeAttribute>();
+ for(Property property : ((Stereotype)((ParticipantTypeElement)parentElement).getElement()).getAllAttributes()) {
+ if(!property.getName().startsWith("base_")) { //$NON-NLS-1$
+ if(property.getType() instanceof Element) {
+ if(UMLUtil.isBoolean(property.getType()) || UMLUtil.isString(property.getType()) || UMLUtil.isInteger(property.getType()) || UMLUtil.isReal(property.getType()) || UMLUtil.isUnlimitedNatural(property.getType()) || property.getType() instanceof Enumeration) {
+ ParticipantTypeAttribute attribute = new ParticipantTypeAttribute(property, (ParticipantTypeElement)parentElement);
+ attributeList.add(attribute);
+ }
+
+ }
+ }
+
+ }
+
+ participantsList.put(parentElement, attributeList);
+
+
+ }
+
+
+ }
+
+ };
+
+ try {
+ dialog.run(true, true, computeAvailableTypes);
+ } catch (InvocationTargetException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ } catch (InterruptedException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+
+ }
+
+ protected void createAdvancedSearch() {
+
+ participantTypesTree = new CheckBoxFilteredTree(advancedSearchComposite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE, new PatternFilter(), true);
+ participantTypesTree.setLayout(new GridLayout());
+ GridData chechboxTreeViewerGridData = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
+ chechboxTreeViewerGridData.heightHint = 150;
+ participantTypesTree.setLayoutData(chechboxTreeViewerGridData);
+
+ participantTypesTreeViewer = (CheckboxTreeViewer)participantTypesTree.getViewer();
+ participantTypesTreeViewer.setContentProvider(participantTypeContentProvider);
+ participantTypesTreeViewer.setLabelProvider(new ParticipantTypeLabelProvider());
+ participantTypesTreeViewer.setSorter(new ViewerSorter());
+ participantTypesTreeViewer.setCheckStateProvider(new ICheckStateProvider() {
+
+ public boolean isGrayed(Object element) {
+ return false;
+ }
+
+ public boolean isChecked(Object element) {
+ if(element instanceof ParticipantTypeElement) {
+
+ return ((ParticipantTypeElement)element).isChecked();
+
+ }
+ return false;
+ }
+ });
+ Composite participantManipualtionComposite = new Composite(advancedSearchComposite, SWT.NONE);
+ participantManipualtionComposite.setLayout(new GridLayout(1, false));
+ participantManipualtionComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, true, 1, 1));
+
+ Button btnSelectSub = new Button(participantManipualtionComposite, SWT.PUSH);
+ btnSelectSub.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
+ btnSelectSub.setText(Messages.PapyrusSearchPage_14);
+ btnSelectSub.addMouseListener(new MouseAdapter() {
+
+ @Override
+ public void mouseUp(MouseEvent e) {
+ ISelection selection = participantTypesTreeViewer.getSelection();
+ if(selection instanceof IStructuredSelection) {
+ Object selectedElement = ((IStructuredSelection)selection).getFirstElement();
+
+ if(selectedElement instanceof ParticipantTypeElement) {
+ List<ParticipantTypeAttribute> attributeParentList = new ArrayList<ParticipantTypeAttribute>();
+
+ for(Object attribute : participantsList.get(selectedElement)) {
+ if(attribute instanceof ParticipantTypeAttribute) {
+ if(((ParticipantTypeAttribute)attribute).isChecked()) {
+ attributeParentList.add(((ParticipantTypeAttribute)attribute));
+ }
+
+ }
+ }
+ if(((ParticipantTypeElement)selectedElement).getElement() instanceof EClass) {
+
+ selectAllSubUML((ParticipantTypeElement)selectedElement, attributeParentList);
+
+ } else if(((ParticipantTypeElement)selectedElement).getElement() instanceof Stereotype) {
+
+ selectAllSubSter((ParticipantTypeElement)selectedElement, attributeParentList);
+
+ }
+ }
+ }
+
+ participantTypesTreeViewer.refresh();
+ }
+ });
+
+ Button btnSelectAll = new Button(participantManipualtionComposite, SWT.PUSH);
+ btnSelectAll.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
+ btnSelectAll.setText(Messages.PapyrusSearchPage_9);
+
+ btnSelectAll.addMouseListener(new MouseAdapter() {
+
+ @Override
+ public void mouseUp(MouseEvent e) {
+ for(ParticipantTypeElement element : participantsList.keySet()) {
+ if(!element.isChecked()) {
+ element.setChecked(true);
+
+ for(ParticipantTypeAttribute attribute : participantsList.get(element)) {
+ attribute.setChecked(true);
+
+ }
+ }
+ }
+ participantTypesTreeViewer.refresh();
+
+ }
+ });
+
+ Button btnDeselectAll = new Button(participantManipualtionComposite, SWT.PUSH);
+ btnDeselectAll.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
+ btnDeselectAll.setText(Messages.PapyrusSearchPage_10);
+ btnDeselectAll.addMouseListener(new MouseAdapter() {
+
+ @Override
+ public void mouseUp(MouseEvent e) {
+ for(ParticipantTypeElement element : participantsList.keySet()) {
+ if(element.isChecked()) {
+ element.setChecked(false);
+
+ for(ParticipantTypeAttribute attribute : participantsList.get(element)) {
+ attribute.setChecked(false);
+
+
+ }
+ }
+ }
+ participantTypesTreeViewer.refresh();
+ }
+ });
+
+
+ Button btnRefreshTypes = new Button(participantManipualtionComposite, SWT.PUSH);
+ btnRefreshTypes.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
+ btnRefreshTypes.setText(Messages.PapyrusSearchPage_15);
+ btnRefreshTypes.addMouseListener(new MouseAdapter() {
+
+ @Override
+ public void mouseUp(MouseEvent e) {
+
+ createAdvancedSearch();
+
+ }
+ });
+
+ participantTypesTreeViewer.setInput(participantsList);
+
+ // participantTypesTreeViewer.setAllChecked(true);
+ ((ICheckable)participantTypesTreeViewer).addCheckStateListener(new ICheckStateListener() {
+
+
+ public void checkStateChanged(CheckStateChangedEvent event) {
+ if(event.getElement() instanceof ParticipantTypeElement) {
+
+
+ // If the item is checked . . .
+ if(event.getChecked()) {
+ Object selectedElement = event.getElement();
+
+ ((ParticipantTypeElement)selectedElement).setChecked(true);
+ participantTypesTreeViewer.refresh(selectedElement);
+
+ if(selectedElement instanceof ParticipantTypeAttribute) {
+ ParticipantTypeElement parent = ((ParticipantTypeAttribute)selectedElement).getParent();
+ if(parent != null) {
+ // participantTypesTreeViewer.setChecked(parent, true);
+ ((ParticipantTypeElement)parent).setChecked(true);
+ participantTypesTreeViewer.refresh(parent);
+ }
+ }
+ } else {
+ Object selectedElement = event.getElement();
+ ((ParticipantTypeElement)selectedElement).setChecked(false);
+ participantTypesTreeViewer.refresh(selectedElement);
+
+ if(((ParticipantTypeElement)selectedElement).getElement() instanceof Stereotype || ((ParticipantTypeElement)selectedElement).getElement() instanceof EClassImpl) {
+ for(Object attribute : participantTypesTreeViewer.getCheckedElements()) {
+ if(attribute instanceof ParticipantTypeAttribute) {
+ if(((ParticipantTypeAttribute)attribute).getParent().equals(selectedElement)) {
+ // participantTypesTreeViewer.setChecked(attribute, false);
+ ((ParticipantTypeElement)attribute).setChecked(false);
+ participantTypesTreeViewer.refresh(attribute);
+ }
+ }
+ }
+ }
+ }
+ }
+
+
+ }
+ });
+ }
+
+ protected void selectAllSubSter(final ParticipantTypeElement elementParent, final List<ParticipantTypeAttribute> attributeParentList) {
+ // for(Object element : result) {
+ // if(element instanceof Stereotype) {
+ // List<Class> superTypes = ((Stereotype)element).getSuperClasses();
+ // for(Class type : superTypes) {
+ // ((Stereotype)elementParent).getName();
+ //
+ // if(((Class)type).getName().equals(((Stereotype)elementParent).getName())) {
+ // participantTypesTreeViewer.setChecked(element, true);
+ //
+ // }
+ // }
+ // }
+ // }
+ ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell());
+ IRunnableWithProgress computeAvailableTypes = new IRunnableWithProgress() {
+
+ public void run(IProgressMonitor thePM) throws InterruptedException {
+ for(Object element : participantsList.keySet()) {
+ if(element instanceof ParticipantTypeElement) {
+ if(((ParticipantTypeElement)element).getElement() instanceof Stereotype) {
+ List<Class> superTypes = ((Class)((ParticipantTypeElement)element).getElement()).getSuperClasses();
+
+ if(superTypes.contains(elementParent.getElement())) {
+ // participantTypesTreeViewer.setChecked(element, true);
+ ((ParticipantTypeElement)element).setChecked(true);
+
+
+ //Proceed with attributes
+ for(ParticipantTypeAttribute attributeParent : attributeParentList) {
+ for(ParticipantTypeAttribute attributeToEvaluate : participantsList.get(element)) {
+ if(attributeParent.getElement() == attributeToEvaluate.getElement()) {
+
+ attributeToEvaluate.setChecked(true);
+
+
+ }
+
+ }
+ }
+ }
+ }
+ }
+ }
+
+
+ }
+
+ };
+
+ try {
+ dialog.run(true, true, computeAvailableTypes);
+ } catch (InvocationTargetException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ } catch (InterruptedException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+
+ }
+
+ protected void selectAllSubUML(final ParticipantTypeElement elementParent, final List<ParticipantTypeAttribute> attributeParentList) {
+
+ ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell());
+ IRunnableWithProgress computeAvailableTypes = new IRunnableWithProgress() {
+
+ public void run(IProgressMonitor thePM) throws InterruptedException {
+ for(Object element : participantsList.keySet()) {
+ if(element instanceof ParticipantTypeElement) {
+ if(((ParticipantTypeElement)element).getElement() instanceof EClass) {
+ List<EClass> superTypes = ((EClass)((ParticipantTypeElement)element).getElement()).getEAllSuperTypes();
+
+ if(superTypes.contains(elementParent.getElement())) {
+ // participantTypesTreeViewer.setChecked(element, true);
+ ((ParticipantTypeElement)element).setChecked(true);
+
+
+ //Proceed with attributes
+ for(ParticipantTypeAttribute attributeParent : attributeParentList) {
+ for(ParticipantTypeAttribute attributeToEvaluate : participantsList.get(element)) {
+ if(attributeParent.getElement() == attributeToEvaluate.getElement()) {
+
+ attributeToEvaluate.setChecked(true);
+
+
+ }
+
+ }
+ }
+ }
+ }
+ }
+ }
+
+
+ }
+
+ };
+
+ try {
+ dialog.run(true, true, computeAvailableTypes);
+ } catch (InvocationTargetException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ } catch (InterruptedException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+ }
+
+
+ protected void simpleSearch() {
+
+
+ Composite participantManipualtionComposite = new Composite(advancedSearchComposite, SWT.NONE);
+ participantManipualtionComposite.setLayout(new GridLayout(1, false));
+ participantManipualtionComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, true, 1, 1));
+
+
+ btnSearchInName = new Button(participantManipualtionComposite, SWT.RADIO);
+ btnSearchInName.setText(Messages.PapyrusSearchPage_16);
+ btnSearchInName.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 0, 1));
+ btnSearchInName.setSelection(true);
+
+
+ btnSearchAllStringAttributes = new Button(participantManipualtionComposite, SWT.RADIO);
+ btnSearchAllStringAttributes.setText(Messages.PapyrusSearchPage_17);
+ btnSearchAllStringAttributes.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
+ }
+
+ public void initMetaClasses() {
+
+ for(EClassifier eClassifier : UMLPackage.eINSTANCE.getEClassifiers()) {
+ if(eClassifier instanceof EClass) {
+ umlMetaClasses.add(eClassifier);
+ }
+ }
+
+ }
+
+
+ protected void createOCLSearchQueryField(EObject root) {
+
+ Composite client = queryComposite;
+
+ Composite oclContextComposite = new Composite(client, SWT.NONE);
+ oclContextComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
+ oclContextComposite.setLayout(new GridLayout(2, false));
+
+ oclContext = new Text(oclContextComposite, SWT.BORDER);
+ oclContext.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
+ oclContext.setEnabled(false);
+ oclContext.setFocus();
+
+ Button btnSelectContext = new Button(oclContextComposite, SWT.PUSH);
+ btnSelectContext.setText(Messages.PapyrusSearchPage_18);
+ btnSelectContext.addMouseListener(new MouseAdapter() {
+
+ @Override
+ public void mouseUp(MouseEvent e) {
+
+ LabelProviderService labelProviderService = new LabelProviderServiceImpl();
+ ILabelProvider labelProvider = labelProviderService.getLabelProvider();
+
+ ScopeEntry currentScope = getCurrentScopeEntry();
+
+ if(currentScope != null) {
+
+ try {
+ EObject root = ((UmlModel)currentScope.getModelSet().getModel(UmlModel.MODEL_ID)).lookupRoot();
+
+ ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(Display.getCurrent().getActiveShell(), labelProvider, new OCLContextContentProvider());
+ dialog.setTitle(Messages.PapyrusSearchPage_19);
+ dialog.setMessage(Messages.PapyrusSearchPage_20);
+ dialog.setInput(currentScope.getModelSet());
+ dialog.setAllowMultiple(false);
+ dialog.open();
+ Object selection = dialog.getFirstResult();
+
+ if(selection instanceof EObject) {
+
+ refreshSelection(selection);
+
+ if(contextObject instanceof NamedElement) {
+ oclContext.setText(((NamedElement)contextObject).getQualifiedName());
+ } else {
+ oclContext.setText(labelProvider.getText(contextObject));
+ }
+ }
+
+ } catch (NotFoundException notFoundException) {
+
+ Activator.log.error(Messages.PapyrusQuery_0 + currentScope.getModelSet(), notFoundException);
+ }
+ }
+
+ }
+ });
+ btnSelectContext.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
+
+ Injector injector = Activator.getInstance().getInjector(EssentialOCLPlugin.LANGUAGE_ID);
+ Composite editorComposite = client;
+ oclEditor = new EmbeddedXtextEditor(editorComposite, injector, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
+
+ oclEditor.getViewer().getTextWidget().addVerifyKeyListener(new VerifyKeyListener() {
+
+ public void verifyKey(VerifyEvent e) {
+ if(e.keyCode == SWT.KEYPAD_CR || e.keyCode == SWT.CR) {
+ if((e.stateMask & (SWT.CTRL | SWT.SHIFT)) == 0) {
+ e.doit = false;
+ }
+ }
+ }
+ });
+
+ GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
+ data.heightHint = convertHeightInCharsToPixels(3);
+ editorComposite.setLayoutData(data);
+
+ refreshSelection(root);
+ }
+
+ protected ScopeEntry getCurrentScopeEntry() {
+ if(container.getSelectedScope() == ISearchPageContainer.SELECTION_SCOPE) {
+ Collection<IResource> scope = ScopeCollector.getInstance().computeSearchScope(container);
+
+ Collection<ScopeEntry> scopeEntries = createScopeEntries(scope);
+
+ if(scopeEntries.size() == 1) {
+ Object[] entries = scopeEntries.toArray();
+ ScopeEntry selectedResource = (ScopeEntry)entries[0];
+
+ return selectedResource;
+ }
+ }
+ return null;
+ }
+
+ public void createControl(Composite parent) {
+
+ initializeDialogUnits(parent);
+ Composite searchComposite = new Composite(parent, SWT.NONE);
+ searchComposite.setFont(parent.getFont());
+ searchComposite.setLayout(new GridLayout(2, false));
+ searchComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
+
+ Label lblSearchString = new Label(searchComposite, SWT.NONE);
+ lblSearchString.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
+ lblSearchString.setText(Messages.PapyrusSearchPage_4);
+
+ queryKind = new Combo(searchComposite, SWT.VERTICAL | SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY);
+ queryKind.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, false, 1, 1));
+ queryKind.add(Messages.PapyrusSearchPage_21);
+ queryKind.add(Messages.PapyrusSearchPage_22);
+
+
+
+ queryKind.select(TEXT_QUERY_KIND);
+
+ queryKind.addSelectionListener(new SelectionAdapter() {
+
+ public void widgetSelected(SelectionEvent e) {
+ for(Control childControl : queryComposite.getChildren()) {
+ childControl.dispose();
+ }
+
+ if(queryKind.getSelectionIndex() == TEXT_QUERY_KIND) {
+ createSimpleSearchQueryField();
+
+
+
+
+ } else {
+ if(container.getSelectedScope() == ISearchPageContainer.SELECTION_SCOPE) {
+
+ ScopeEntry currentScope = getCurrentScopeEntry();
+ if(currentScope != null) {
+ if(currentScope.getModelSet() != null) {
+
+ try {
+ EObject root = ((UmlModel)currentScope.getModelSet().getModel(UmlModel.MODEL_ID)).lookupRoot();
+ createOCLSearchQueryField(root);
+
+ if(contextObject instanceof NamedElement) {
+ oclContext.setText(((NamedElement)contextObject).getQualifiedName());
+ } else {
+ LabelProviderService labelProviderService = new LabelProviderServiceImpl();
+ ILabelProvider labelProvider = labelProviderService.getLabelProvider();
+ oclContext.setText(labelProvider.getText(contextObject));
+ }
+ } catch (NotFoundException notFoundException) {
+
+ Activator.log.error(Messages.PapyrusQuery_0 + currentScope.getModelSet(), notFoundException);
+ }
+ } else {
+ MessageDialog.openWarning(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_23, Messages.PapyrusSearchPage_24);
+ createSimpleSearchQueryField();
+ queryKind.select(TEXT_QUERY_KIND);
+ }
+ } else {
+ MessageDialog.openWarning(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_25, Messages.PapyrusSearchPage_26);
+ createSimpleSearchQueryField();
+ queryKind.select(TEXT_QUERY_KIND);
+ }
+ } else {
+ MessageDialog.openWarning(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_27, Messages.PapyrusSearchPage_28);
+ createSimpleSearchQueryField();
+ queryKind.select(TEXT_QUERY_KIND);
+ }
+ }
+ queryComposite.layout();
+ }
+ });
+
+
+ queryComposite = new Composite(searchComposite, SWT.NONE);
+ queryComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
+ queryComposite.setLayout(new GridLayout(1, false));
+
+ createSimpleSearchQueryField();
+
+ setControl(parent);
+ }
+
+ public boolean getSearchAllStringAttributes() {
+ if(btnSearchAllStringAttributes != null) {
+ return btnSearchAllStringAttributes.getSelection();
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Validate syntax of the regular expression of the search query text.
+ *
+ * @return true, if successful
+ */
+ private boolean validateRegex() {
+
+ try {
+ PatternHelper.getInstance().createPattern(searchQueryText.getText(), btnCaseSensitive.getSelection(), btnRegularExpression.getSelection());
+ searchQueryExplanatoryLabel.setForeground(getControl().getForeground());
+ searchQueryExplanatoryLabel.setText(""); //$NON-NLS-1$
+ return true;
+
+ } catch (PatternSyntaxException e) {
+ searchQueryExplanatoryLabel.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_RED));
+ searchQueryExplanatoryLabel.setText(e.getDescription());
+
+ return false;
+ }
+ }
+
+ protected void refreshSelection(final Object selected) {
+ final BaseDocument editorDocument = (BaseDocument)oclEditor.getDocument();
+ editorDocument.modify(new IUnitOfWork<Object, XtextResource>() {
+
+ public Value exec(XtextResource resource) throws Exception {
+ Object selectedObject = selected;
+ if(selectedObject instanceof IOutlineNode) {
+ if(selectedObject instanceof EObjectNode) {
+ EObjectNode selectedObjectNode = (EObjectNode)selectedObject;
+ @SuppressWarnings("unused")
+ URI eObjectURI = selectedObjectNode.getEObjectURI();
+ contextObject = null; // FIXME
+ // metaModelManager.loadResource(eObjectURI,
+ // null, null);
+ } else if(selectedObject instanceof EStructuralFeatureNode) {
+ contextObject = null;
+ } else {
+ contextObject = null;
+ }
+ } else {
+ if(selectedObject instanceof IAdaptable) {
+ selectedObject = ((IAdaptable)selectedObject).getAdapter(EObject.class);
+ }
+ if(selectedObject instanceof EObject) {
+ contextObject = (EObject)selectedObject;
+ } else {
+ contextObject = null;
+ }
+ }
+ MetaModelManager metaModelManager = getMetaModelManager(contextObject);
+ parserContext = new EObjectContext(metaModelManager, null, contextObject);
+ EssentialOCLCSResource csResource = (EssentialOCLCSResource)resource;
+ if(csResource != null) {
+ if(contextObject != null) {
+ CS2PivotResourceAdapter.getAdapter(csResource, metaModelManager);
+ }
+ ResourceSet resourceSet = oclEditor.getResourceSet();
+ if(resourceSet != null) {
+ MetaModelManagerResourceSetAdapter.getAdapter(resourceSet, metaModelManager);
+ }
+ csResource.setParserContext(parserContext);
+ }
+
+ return null;
+ }
+ });
+ }
+
+ /**
+ * Create scopeEntries based on IResources.
+ *
+ * @return the created scopeEntries
+ */
+ private Collection<ScopeEntry> createScopeEntries(Collection<IResource> scope) {
+ IServiceRegistryTracker tracker = createServiceRegistryTracker();
+ Collection<ScopeEntry> results = new HashSet<ScopeEntry>();
+
+ for(IResource resource : scope) {
+
+ ScopeEntry scopeEntry = new ScopeEntry(resource, tracker);
+
+ results.add(scopeEntry);
+
+ }
+
+ return results;
+ }
+
+ private IServiceRegistryTracker createServiceRegistryTracker() {
+ return new DefaultServiceRegistryTracker();
+ }
+
+ public boolean performAction() {
+
+ if(queryKind.getSelectionIndex() == TEXT_QUERY_KIND) {
+ if(validateRegex()) {
+ Collection<IResource> scope = ScopeCollector.getInstance().computeSearchScope(container);
+ Collection<ScopeEntry> scopeEntries = createScopeEntries(scope);
+ ISearchQuery query;
+ if(searchKind.getSelectionIndex() == SIMPLE_SEARCH) {
+ if(searchQueryText.getText().length() == 0) {
+ MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_29, Messages.PapyrusSearchPage_30);
+ return false;
+ } else {
+ initMetaClasses();
+
+ query = new PapyrusQuery(searchQueryText.getText(), btnCaseSensitive.getSelection(), btnRegularExpression.getSelection(), scopeEntries, umlMetaClasses.toArray(), btnSearchAllStringAttributes.getSelection());
+ }
+ } else {
+
+
+ List<ParticipantTypeElement> participantsToEvaluate = new ArrayList<ParticipantTypeElement>();
+ for(ParticipantTypeElement element : this.participantsList.keySet()) {
+ if(element.isChecked()) {
+ participantsToEvaluate.add(element);
+ for(ParticipantTypeAttribute attributesToEvaluate : participantsList.get(element)) {
+ if(attributesToEvaluate.isChecked()) {
+ participantsToEvaluate.add(attributesToEvaluate);
+ }
+ }
+ }
+ }
+ if(participantsToEvaluate.size() == 0) {
+
+ MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_31, Messages.PapyrusSearchPage_32);
+ return false;
+ } else {
+ if(searchQueryText.getText().length() == 0) {
+ for(Object participantChecked : participantTypesTreeViewer.getCheckedElements()) {
+ if(participantChecked instanceof ParticipantTypeAttribute) {
+ MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_33, Messages.PapyrusSearchPage_34);
+ return false;
+ }
+ }
+ }
+
+ query = new PapyrusAdvancedQuery(searchQueryText.getText(), btnCaseSensitive.getSelection(), btnRegularExpression.getSelection(), scopeEntries, participantsToEvaluate.toArray());
+
+ }
+
+ }
+ if(query.canRunInBackground()) {
+ NewSearchUI.runQueryInBackground(query);
+ }
+
+ return true;
+ } else {
+ MessageDialog.openError(Display.getCurrent().getActiveShell(), SEARCH_ISSUE, REGULAR_EXPRESSION_ILLFORMED);
+ return false;
+ }
+ } else {
+
+ ScopeEntry scopeEntry = getCurrentScopeEntry();
+
+ if(scopeEntry != null) {
+
+ try {
+
+ PivotUtil.checkResourceErrors("", oclEditor.getResource()); //$NON-NLS-1$
+ ExpressionInOCL expressionInOCL = parserContext.getExpression((BaseResource)oclEditor.getResource());
+ ISearchQuery query = new PapyrusOCLQuery((BaseDocument)oclEditor.getDocument(), parserContext, getMetaModelManager(contextObject), modelManager, contextObject, scopeEntry);
+
+
+ if(query.canRunInBackground()) {
+ NewSearchUI.runQueryInBackground(query);
+ }
+ } catch (ParserException e) {
+ Object value = new InvalidValueException(e, Messages.PapyrusSearchPage_35);
+ MessageDialog.openError(Display.getCurrent().getActiveShell(), SEARCH_ISSUE, OCL_QUERY_ILLFORMED);
+ return false;
+ }
+
+ return true;
+ } else {
+ return false;
+ }
+ }
+ }
+
+ public void setContainer(ISearchPageContainer container) {
+ this.container = container;
+ this.container.setPerformActionEnabled(true);
+ }
+
+ public boolean performReplace() {
+ if(queryKind.getSelectionIndex() == TEXT_QUERY_KIND) {
+ if(validateRegex()) {
+ if(searchQueryText.getText().length() == 0) {
+ MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_36, Messages.PapyrusSearchPage_37);
+ return false;
+ }
+
+ Collection<IResource> scope = ScopeCollector.getInstance().computeSearchScope(container);
+
+ Collection<ScopeEntry> scopeEntries = createScopeEntries(scope);
+ AbstractPapyrusQuery query;
+ if(searchKind.getSelectionIndex() == SIMPLE_SEARCH) {
+ initMetaClasses();
+
+ query = new PapyrusQuery(searchQueryText.getText(), btnCaseSensitive.getSelection(), btnRegularExpression.getSelection(), scopeEntries, umlMetaClasses.toArray(), btnSearchAllStringAttributes.getSelection());
+ } else {
+ List<ParticipantTypeElement> participantsToEvaluate = new ArrayList<ParticipantTypeElement>();
+ for(ParticipantTypeElement element : this.participantsList.keySet()) {
+ if(element.isChecked()) {
+ participantsToEvaluate.add(element);
+ if(participantsList.get(element).size() == 0) {
+ MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_38, Messages.PapyrusSearchPage_39);
+ return false;
+
+ } else {
+ for(ParticipantTypeAttribute attributesToEvaluate : participantsList.get(element)) {
+ if(attributesToEvaluate.isChecked()) {
+ participantsToEvaluate.add(attributesToEvaluate);
+ Object value = element.getElement().eGet((EAttribute)attributesToEvaluate.getElement());
+ if(!(value instanceof String)) {
+ MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_40, Messages.PapyrusSearchPage_41);
+ return false;
+ }
+ }
+ }
+ }
+ }
+ }
+ query = new PapyrusAdvancedQuery(searchQueryText.getText(), btnCaseSensitive.getSelection(), btnRegularExpression.getSelection(), scopeEntries, participantsToEvaluate.toArray());
+
+ }
+
+ NewSearchUI.runQueryInForeground(container.getRunnableContext(), query);
+
+
+ Display.getCurrent().syncExec(new Runnable() {
+
+ public void run() {
+ ISearchResultViewPart view = NewSearchUI.activateSearchResultView();
+ if(view != null) {
+ ISearchResultPage page = view.getActivePage();
+
+ if(page instanceof PapyrusSearchResultPage) {
+ PapyrusSearchResultPage resultPage = (PapyrusSearchResultPage)page;
+ ReplaceAction replaceAction = new ReplaceAction(resultPage.getSite().getShell(), resultPage, null);
+ replaceAction.run();
+ }
+ }
+ }
+ });
+
+ NewSearchUI.runQueryInForeground(container.getRunnableContext(), query);
+
+ return true;
+ } else {
+ MessageDialog.openError(Display.getCurrent().getActiveShell(), SEARCH_ISSUE, REGULAR_EXPRESSION_ILLFORMED);
+ return false;
+ }
+ } else {
+ MessageDialog.openWarning(Display.getCurrent().getActiveShell(), Messages.PapyrusSearchPage_42, Messages.PapyrusSearchPage_43);
+ return false;
+ }
+
+ }
+
+
+
+
+ public @NonNull
+ MetaModelManager getMetaModelManager(@Nullable EObject contextObject) {
+ MetaModelManager metaModelManager = contextObject != null ? PivotUtil.findMetaModelManager(contextObject) : null;
+ if(metaModelManager != null) {
+ return metaModelManager;
+ }
+ MetaModelManager nullMetaModelManager2 = nullMetaModelManager;
+ if(nullMetaModelManager2 == null) {
+ nullMetaModelManager2 = nullMetaModelManager = new MetaModelManager();
+ nullMetaModelManager2.addListener(this);
+ }
+ return nullMetaModelManager2;
+ }
+
+ public void metaModelManagerDisposed(@NonNull MetaModelManager metaModelManager) {
+ metaModelManager.removeListener(this);
+ reset();
+
+ }
+
+ protected void flushEvents() {
+ IWorkbench workbench = PlatformUI.getWorkbench();
+ while(workbench.getDisplay().readAndDispatch());
+ }
+
+ public void reset() {
+ if(oclEditor != null) {
+ IXtextDocument document = oclEditor.getDocument();
+ MetaModelManager metaModelManager = document.modify(new IUnitOfWork<MetaModelManager, XtextResource>() { // Cancel
+
+ // validation
+
+ public MetaModelManager exec(XtextResource state) throws Exception {
+ if(state == null) {
+ return null;
+ }
+ if(state instanceof BaseResource) {
+ ((BaseResource)state).setParserContext(null);
+ }
+ return PivotUtil.findMetaModelManager(state);
+ }
+ });
+ flushEvents();
+ // editor.close(false);
+ flushEvents();
+ if(metaModelManager != null) {
+ metaModelManager.dispose();
+ }
+ }
+ if(modelManager != null) {
+ // modelManager.dispose();
+ modelManager = null;
+ }
+ if(nullMetaModelManager != null) {
+ nullMetaModelManager.dispose();
+ nullMetaModelManager = null;
+ }
+ parserContext = null;
+ contextObject = null;
+ }
+}
diff --git a/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/providers/ResultContentProvider.java b/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/providers/ResultContentProvider.java
index 1486b31b7a8..0f9e201e0dc 100644
--- a/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/providers/ResultContentProvider.java
+++ b/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/providers/ResultContentProvider.java
@@ -1,5 +1,5 @@
/*****************************************************************************
- * Copyright (c) 2013 CEA LIST.
+ * Copyright (c) 2013 CEA LIST and others.
*
*
* All rights reserved. This program and the accompanying materials
@@ -9,6 +9,7 @@
*
* Contributors:
* CEA LIST - Initial API and implementation
+ * Christian W. Damus (CEA LIST) - Fix leaking of all UML models in search results
*
*****************************************************************************/
package org.eclipse.papyrus.uml.search.ui.providers;
@@ -22,6 +23,7 @@ import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.papyrus.uml.search.ui.pages.PapyrusSearchResultPage;
+import org.eclipse.papyrus.uml.search.ui.query.AbstractPapyrusQuery;
import org.eclipse.papyrus.uml.search.ui.results.PapyrusSearchResult;
import org.eclipse.papyrus.views.search.results.AbstractResultEntry;
import org.eclipse.papyrus.views.search.results.ResultEntry;
@@ -71,6 +73,11 @@ public class ResultContentProvider implements ITreeContentProvider {
fResult = (AbstractTextSearchResult)newInput;
initialize();
}
+ } else {
+ // forget the previous result, so that the ScopeEntry instances it contains, with all
+ // of the ModelSet and ServicesRegistry instances they hang on to (and lots of UML models!)
+ // may be garbage-collected
+ fResult = AbstractPapyrusQuery.Empty.INSTANCE.getSearchResult();
}
}
diff --git a/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/query/AbstractPapyrusQuery.java b/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/query/AbstractPapyrusQuery.java
index 33067b3ff4a..394ea7110a4 100644
--- a/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/query/AbstractPapyrusQuery.java
+++ b/plugins/uml/org.eclipse.papyrus.uml.search.ui/src/org/eclipse/papyrus/uml/search/ui/query/AbstractPapyrusQuery.java
@@ -1,5 +1,5 @@
/*****************************************************************************
- * Copyright (c) 2013 CEA LIST.
+ * Copyright (c) 2013 CEA LIST and others.
*
*
* All rights reserved. This program and the accompanying materials
@@ -9,10 +9,16 @@
*
* Contributors:
* CEA LIST - Initial API and implementation
+ * Christian W. Damus (CEA LIST) - Fix leaking of all UML models in search results
*
*****************************************************************************/
package org.eclipse.papyrus.uml.search.ui.query;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.papyrus.uml.search.ui.results.PapyrusSearchResult;
import org.eclipse.search.ui.ISearchQuery;
/**
@@ -39,4 +45,41 @@ public abstract class AbstractPapyrusQuery implements ISearchQuery {
return false;
}
+ //
+ // Nested types
+ //
+
+ public static final class Empty extends AbstractPapyrusQuery {
+ public static final Empty INSTANCE = new Empty();
+
+ private Empty() {
+ super();
+ }
+
+ public IStatus run(IProgressMonitor monitor) throws OperationCanceledException {
+ return Status.OK_STATUS;
+ }
+
+ public String getLabel() {
+ return "Empty Papyrus Search";
+ }
+
+ public boolean canRerun() {
+ return true;
+ }
+
+ public boolean canRunInBackground() {
+ return true;
+ }
+
+ public PapyrusSearchResult getSearchResult() {
+ return new PapyrusSearchResult(this);
+ }
+
+ @Override
+ public String getSearchQueryText() {
+ return "";
+ }
+
+ }
}
diff --git a/plugins/views/org.eclipse.papyrus.views.search/src/org/eclipse/papyrus/views/search/scope/ScopeEntry.java b/plugins/views/org.eclipse.papyrus.views.search/src/org/eclipse/papyrus/views/search/scope/ScopeEntry.java
index 6c81f513b19..40dd73e3b01 100644
--- a/plugins/views/org.eclipse.papyrus.views.search/src/org/eclipse/papyrus/views/search/scope/ScopeEntry.java
+++ b/plugins/views/org.eclipse.papyrus.views.search/src/org/eclipse/papyrus/views/search/scope/ScopeEntry.java
@@ -1,234 +1,238 @@
-/*****************************************************************************
- * 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.views.search.scope;
-
-import java.util.Collection;
-import java.util.HashSet;
-
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.resources.IWorkspaceRoot;
-import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.papyrus.infra.core.resource.ModelMultiException;
-import org.eclipse.papyrus.infra.core.resource.ModelSet;
-import org.eclipse.papyrus.infra.core.services.ServiceException;
-import org.eclipse.papyrus.infra.core.services.ServiceMultiException;
-import org.eclipse.papyrus.infra.core.services.ServiceNotFoundException;
-import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
-import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
-import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForResourceInitializerService;
-import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;
-import org.eclipse.papyrus.infra.services.labelprovider.service.impl.LabelProviderServiceImpl;
-import org.eclipse.papyrus.infra.services.openelement.service.OpenElementService;
-import org.eclipse.papyrus.infra.services.openelement.service.impl.OpenElementServiceImpl;
-import org.eclipse.papyrus.views.search.Activator;
-import org.eclipse.papyrus.views.search.Messages;
-import org.eclipse.papyrus.views.search.utils.ModelUtils;
-import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.IEditorReference;
-import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.ide.ResourceUtil;
-
-/**
- *
- * This class is used to aggregate information about the resource that contains the element that raised a match
- *
- */
-public class ScopeEntry {
-
- /**
- * The modelSet that contains the model element
- */
- private ModelSet modelSet;
-
- /**
- * The corresponding {@link ServicesRegistry}
- */
- private ServicesRegistry servicesRegistry;
-
- /**
- * The resource that contains the element that raised a match
- */
- // private IResource resource;
-
- private IPath pathResource;
-
-
- public ScopeEntry(IResource resource) {
- super();
- // this.resource = resource;
-
- this.pathResource = resource.getFullPath();
- this.modelSet = getModelSet();
- this.servicesRegistry = getServicesRegistry();
- }
-
- // public boolean isOpen() {
- // return isOpen;
- // }
- //
- //
- // public void setOpen(boolean isOpen) {
- // this.isOpen = isOpen;
- // }
-
- private Collection<IEditorPart> getEditors() {
- Collection<IEditorPart> results = new HashSet<IEditorPart>();
- IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
- for(IWorkbenchWindow iWorkbenchWindow : windows) {
- IWorkbenchPage[] pages = iWorkbenchWindow.getPages();
- for(IWorkbenchPage iWorkbenchPage : pages) {
- IEditorReference[] references = iWorkbenchPage.getEditorReferences();
- for(IEditorReference ref : references) {
- IEditorPart editor = ref.getEditor(true);
- results.add(editor);
- }
- }
- }
-
- return results;
- }
-
- private ServicesRegistry createServicesRegistry() {
-
- try {
- ServicesRegistry serviceRegistry = new ServicesRegistry();
- serviceRegistry.add(LabelProviderService.class, 10, new LabelProviderServiceImpl());
- serviceRegistry.add(OpenElementService.class, 10, new OpenElementServiceImpl());
- // PageIconRegistryServiceFactory factory = new PageIconRegistryServiceFactory();
- // Object instance = factory.createServiceInstance();
- // serviceRegistry.add(IPageIconsRegistry.class, 10, instance);
- // serviceRegistry.add(IPageIconsRegistry.class, 10, new PageIconRegistryServiceFactory());
- serviceRegistry.startRegistry();
-
- return serviceRegistry;
- } catch (ServiceException e) {
- Activator.log.error(Messages.ScopeEntry_0, e);
- }
- return null;
- }
-
- public ModelSet getModelSet() {
- //If null, try to find one or create one
- if(modelSet == null) {
- try {
- modelSet = ServiceUtils.getInstance().getModelSet(getServicesRegistry());
- } catch (ServiceException e) {
- //Create one
- try {
-
- modelSet = ModelUtils.openFile(this.getResource());
- getServicesRegistry().add(ModelSet.class, 10, modelSet);
- getServicesRegistry().add(ServiceUtilsForResourceInitializerService.class, 10, new ServiceUtilsForResourceInitializerService());
- getServicesRegistry().startServicesByClassKeys(ModelSet.class, ServiceUtilsForResourceInitializerService.class);
- } catch (ModelMultiException modelMultiException) {
- Activator.log.error(Messages.ScopeEntry_1 + this.getResource(), modelMultiException);
- } catch (ServiceMultiException e1) {
- Activator.log.error(e1);
- } catch (ServiceNotFoundException e1) {
- Activator.log.error(e1);
- }
- }
- }
- return modelSet;
- }
-
- public void setModelSet(ModelSet modelSet) {
- this.modelSet = modelSet;
- }
-
- private IEditorPart editorOnResource() {
- Collection<IEditorPart> editors = getEditors();
-
- for(IEditorPart editor : editors) {
-
- if(editor != null) {
- if(ResourceUtil.getResource(editor.getEditorInput()).equals(this.getResource())) {
- return editor;
- }
- }
- }
-
- return null;
- }
-
- private ServicesRegistry getUpdatedServiceRegistry() {
- IEditorPart editor = editorOnResource();
- if(editor != null) {
-
- ServicesRegistry registry = (ServicesRegistry)editor.getAdapter(ServicesRegistry.class);
-
- return registry;
-
- }
-
- return servicesRegistry;
- }
-
- public ServicesRegistry getServicesRegistry() {
-
- if(servicesRegistry == null) {
- //Try to find existing
- ServicesRegistry registry = getUpdatedServiceRegistry();
- if(registry != null) {
- //If the OpenElementService is no yet available, we must start it
- try {
- registry.getService(OpenElementService.class);
- } catch (ServiceException e) {
- registry.add(OpenElementService.class, 10, new OpenElementServiceImpl());
- }
-
- servicesRegistry = registry;
- // this.isOpen = true;
-
- } else {
- // this.isOpen = false;
- servicesRegistry = createServicesRegistry();
- }
- }
-
- return servicesRegistry;
- }
-
- public void setServicesRegistry(ServicesRegistry servicesRegistry) {
- this.servicesRegistry = servicesRegistry;
- }
-
- public IResource getResource() {
-
-
- IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
- IFile file = root.getFile(pathResource);
-
- return (IResource)file;
- // if(this.uriResource != null) {
- //
- // IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
- //
- //
- // IPath path = new Path(this.uriResource.getPath());
- // return root.getFile(path);
- // } else {
- // return null;
- // }
- }
-
- public void setResource(IResource resource) {
- this.pathResource = resource.getFullPath();
- }
-
-}
+/*****************************************************************************
+ * 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) - Fix leaking of all UML models in search results
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.views.search.scope;
+
+import java.util.Collection;
+import java.util.HashSet;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.papyrus.infra.core.resource.ModelMultiException;
+import org.eclipse.papyrus.infra.core.resource.ModelSet;
+import org.eclipse.papyrus.infra.core.services.ServiceException;
+import org.eclipse.papyrus.infra.core.services.ServiceMultiException;
+import org.eclipse.papyrus.infra.core.services.ServiceNotFoundException;
+import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
+import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
+import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForResourceInitializerService;
+import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;
+import org.eclipse.papyrus.infra.services.labelprovider.service.impl.LabelProviderServiceImpl;
+import org.eclipse.papyrus.infra.services.openelement.service.OpenElementService;
+import org.eclipse.papyrus.infra.services.openelement.service.impl.OpenElementServiceImpl;
+import org.eclipse.papyrus.views.search.Activator;
+import org.eclipse.papyrus.views.search.Messages;
+import org.eclipse.papyrus.views.search.utils.IServiceRegistryTracker;
+import org.eclipse.papyrus.views.search.utils.ModelUtils;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IEditorReference;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.ide.ResourceUtil;
+
+/**
+ *
+ * This class is used to aggregate information about the resource that contains the element that raised a match
+ *
+ */
+public class ScopeEntry {
+ /**
+ * Tracks service registries created implicitly (not borrowed from editors) by scope entries, so that they may
+ * be properly shut down when no longer needed.
+ */
+ private final IServiceRegistryTracker serviceRegistryTracker;
+
+ /**
+ * The modelSet that contains the model element
+ */
+ private ModelSet modelSet;
+
+ /**
+ * The corresponding {@link ServicesRegistry}
+ */
+ private ServicesRegistry servicesRegistry;
+
+ /**
+ * The resource that contains the element that raised a match
+ */
+ // private IResource resource;
+
+ private IPath pathResource;
+
+ public ScopeEntry(IResource resource) {
+ this(resource, null);
+ }
+
+ public ScopeEntry(IResource resource, IServiceRegistryTracker serviceRegistryTracker) {
+ super();
+
+ this.serviceRegistryTracker = serviceRegistryTracker;
+ this.pathResource = resource.getFullPath();
+ this.modelSet = getModelSet();
+ this.servicesRegistry = getServicesRegistry();
+ }
+
+ private Collection<IEditorPart> getEditors() {
+ Collection<IEditorPart> results = new HashSet<IEditorPart>();
+ IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
+ for(IWorkbenchWindow iWorkbenchWindow : windows) {
+ IWorkbenchPage[] pages = iWorkbenchWindow.getPages();
+ for(IWorkbenchPage iWorkbenchPage : pages) {
+ IEditorReference[] references = iWorkbenchPage.getEditorReferences();
+ for(IEditorReference ref : references) {
+ IEditorPart editor = ref.getEditor(true);
+ results.add(editor);
+ }
+ }
+ }
+
+ return results;
+ }
+
+ private ServicesRegistry createServicesRegistry() {
+
+ try {
+ ServicesRegistry serviceRegistry = new ServicesRegistry();
+ serviceRegistry.add(LabelProviderService.class, 10, new LabelProviderServiceImpl());
+ serviceRegistry.add(OpenElementService.class, 10, new OpenElementServiceImpl());
+ // PageIconRegistryServiceFactory factory = new PageIconRegistryServiceFactory();
+ // Object instance = factory.createServiceInstance();
+ // serviceRegistry.add(IPageIconsRegistry.class, 10, instance);
+ // serviceRegistry.add(IPageIconsRegistry.class, 10, new PageIconRegistryServiceFactory());
+ serviceRegistry.startRegistry();
+
+ if(serviceRegistryTracker != null) {
+ // register this service registry for automatic shut-down when it is no longer needed
+ serviceRegistryTracker.track(this, serviceRegistry);
+ }
+
+ return serviceRegistry;
+ } catch (ServiceException e) {
+ Activator.log.error(Messages.ScopeEntry_0, e);
+ }
+ return null;
+ }
+
+ public ModelSet getModelSet() {
+ //If null, try to find one or create one
+ if(modelSet == null) {
+ try {
+ modelSet = ServiceUtils.getInstance().getModelSet(getServicesRegistry());
+ } catch (ServiceException e) {
+ //Create one
+ try {
+
+ modelSet = ModelUtils.openFile(this.getResource());
+ getServicesRegistry().add(ModelSet.class, 10, modelSet);
+ getServicesRegistry().add(ServiceUtilsForResourceInitializerService.class, 10, new ServiceUtilsForResourceInitializerService());
+ getServicesRegistry().startServicesByClassKeys(ModelSet.class, ServiceUtilsForResourceInitializerService.class);
+ } catch (ModelMultiException modelMultiException) {
+ Activator.log.error(Messages.ScopeEntry_1 + this.getResource(), modelMultiException);
+ } catch (ServiceMultiException e1) {
+ Activator.log.error(e1);
+ } catch (ServiceNotFoundException e1) {
+ Activator.log.error(e1);
+ }
+ }
+ }
+ return modelSet;
+ }
+
+ public void setModelSet(ModelSet modelSet) {
+ this.modelSet = modelSet;
+ }
+
+ private IEditorPart editorOnResource() {
+ Collection<IEditorPart> editors = getEditors();
+
+ for(IEditorPart editor : editors) {
+
+ if(editor != null) {
+ if(ResourceUtil.getResource(editor.getEditorInput()).equals(this.getResource())) {
+ return editor;
+ }
+ }
+ }
+
+ return null;
+ }
+
+ private ServicesRegistry getUpdatedServiceRegistry() {
+ IEditorPart editor = editorOnResource();
+ if(editor != null) {
+
+ ServicesRegistry registry = (ServicesRegistry)editor.getAdapter(ServicesRegistry.class);
+
+ return registry;
+
+ }
+
+ return servicesRegistry;
+ }
+
+ public ServicesRegistry getServicesRegistry() {
+
+ if(servicesRegistry == null) {
+ //Try to find existing
+ ServicesRegistry registry = getUpdatedServiceRegistry();
+ if(registry != null) {
+ //If the OpenElementService is no yet available, we must start it
+ try {
+ registry.getService(OpenElementService.class);
+ } catch (ServiceException e) {
+ registry.add(OpenElementService.class, 10, new OpenElementServiceImpl());
+ }
+
+ servicesRegistry = registry;
+
+ } else {
+ servicesRegistry = createServicesRegistry();
+ }
+ }
+
+ return servicesRegistry;
+ }
+
+ public void setServicesRegistry(ServicesRegistry servicesRegistry) {
+ this.servicesRegistry = servicesRegistry;
+ }
+
+ public IResource getResource() {
+
+
+ IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
+ IFile file = root.getFile(pathResource);
+
+ return (IResource)file;
+ // if(this.uriResource != null) {
+ //
+ // IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
+ //
+ //
+ // IPath path = new Path(this.uriResource.getPath());
+ // return root.getFile(path);
+ // } else {
+ // return null;
+ // }
+ }
+
+ public void setResource(IResource resource) {
+ this.pathResource = resource.getFullPath();
+ }
+
+}
diff --git a/plugins/views/org.eclipse.papyrus.views.search/src/org/eclipse/papyrus/views/search/utils/DefaultServiceRegistryTracker.java b/plugins/views/org.eclipse.papyrus.views.search/src/org/eclipse/papyrus/views/search/utils/DefaultServiceRegistryTracker.java
new file mode 100644
index 00000000000..2323b40b257
--- /dev/null
+++ b/plugins/views/org.eclipse.papyrus.views.search/src/org/eclipse/papyrus/views/search/utils/DefaultServiceRegistryTracker.java
@@ -0,0 +1,182 @@
+/*****************************************************************************
+ * 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
+ *****************************************************************************/
+package org.eclipse.papyrus.views.search.utils;
+
+import java.lang.ref.ReferenceQueue;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
+import org.eclipse.papyrus.views.search.Activator;
+
+
+/**
+ * This is the DefaultServiceRegistryTracker type. Enjoy.
+ */
+public class DefaultServiceRegistryTracker implements IServiceRegistryTracker {
+
+ private static final ExecutorService EXEC = Executors.newCachedThreadPool(new ThreadFactory() {
+
+ private final AtomicInteger threadNum = new AtomicInteger();
+
+ private final ThreadGroup group = new ThreadGroup(Thread.currentThread().getThreadGroup(), "Service Registry Tracker Threads"); //$NON-NLS-1$
+
+ public Thread newThread(Runnable r) {
+ Thread result = new Thread(group, r, "SvcRegTracker-" + threadNum.incrementAndGet()); //$NON-NLS-1$
+ result.setDaemon(true);
+ return result;
+ }
+ });
+
+ private Set<TrackerReference> references = new java.util.HashSet<TrackerReference>();
+
+ private ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
+
+ private int count = 0;
+
+ private boolean active = true;
+
+ private Runnable reaper;
+
+ public DefaultServiceRegistryTracker() {
+ super();
+ }
+
+ public synchronized boolean isActive() {
+ return active;
+ }
+
+ // requires monitor
+ private void checkActive() {
+ if(!active) {
+ throw new IllegalStateException("not active"); //$NON-NLS-1$
+ }
+ }
+
+ public synchronized void track(Object owner, ServicesRegistry serviceRegistry) {
+ checkActive();
+
+ TrackerReference existing = getReference(serviceRegistry);
+ if(existing == null) {
+ // only in this case would we ever track
+ references.add(new TrackerReference(owner, serviceRegistry));
+ count++;
+ reapLater();
+ } else {
+ Object actualOwner = existing.get();
+
+ // the reference may have been cleared, in which case we assume the registry
+ // was disposed and the reference did track this owner
+ if((actualOwner != null) && (actualOwner != owner)) {
+ throw new IllegalArgumentException("registry already tracked against a different owner"); //$NON-NLS-1$
+ }
+ }
+ }
+
+ public synchronized void untrack(Object owner, ServicesRegistry serviceRegistry) {
+ checkActive();
+
+ TrackerReference existing = getReference(serviceRegistry);
+ if((existing != null) && (existing.get() == owner)) {
+ references.remove(existing);
+ // it will still be enqueued later, but we will ignore it
+ }
+ }
+
+ // requires monitor
+ private TrackerReference getReference(ServicesRegistry serviceRegistry) {
+ TrackerReference result = null;
+
+ for(TrackerReference next : references) {
+ if(next.matches(serviceRegistry)) {
+ result = next;
+ }
+ }
+
+ return result;
+ }
+
+ // requires monitor
+ private void reapLater() {
+ if(reaper == null) {
+ reaper = new Runnable() {
+
+ public void run() {
+ for(;;) {
+ try {
+ if(!dequeue()) {
+ // done!
+ break;
+ }
+ } catch (InterruptedException e) {
+ Activator.log.error("Service registry reaper thread interrupted. How?", e); //$NON-NLS-1$
+ }
+ }
+ }
+ };
+
+ EXEC.execute(reaper);
+ }
+ }
+
+ private boolean dequeue() throws InterruptedException {
+ boolean result = true;
+
+ TrackerReference ref = (TrackerReference)queue.remove();
+
+ synchronized(this) {
+ if(references.remove(ref)) {
+ // only dispose the service registry if we didn't untrack it!
+ ref.dispose();
+ }
+
+ if(--count <= 0) {
+ // nothing more to reap
+ reaper = null;
+ active = false;
+ result = false;
+ }
+ }
+
+ return result;
+ }
+
+ //
+ // Nested types
+ //
+
+ private class TrackerReference extends java.lang.ref.WeakReference<Object> {
+
+ private final ServicesRegistry serviceRegistry;
+
+ TrackerReference(Object owner, ServicesRegistry serviceRegistry) {
+ super(owner, queue);
+
+ this.serviceRegistry = serviceRegistry;
+ }
+
+ void dispose() {
+ try {
+ serviceRegistry.disposeRegistry();
+ } catch (Exception e) {
+ Activator.log.error("Exception in disposal of service registry that is no longer in use.", e); //$NON-NLS-1$
+ }
+ }
+
+ boolean matches(ServicesRegistry serviceRegistry) {
+ return this.serviceRegistry == serviceRegistry;
+ }
+ }
+}
diff --git a/plugins/views/org.eclipse.papyrus.views.search/src/org/eclipse/papyrus/views/search/utils/IServiceRegistryTracker.java b/plugins/views/org.eclipse.papyrus.views.search/src/org/eclipse/papyrus/views/search/utils/IServiceRegistryTracker.java
new file mode 100644
index 00000000000..fcc65372964
--- /dev/null
+++ b/plugins/views/org.eclipse.papyrus.views.search/src/org/eclipse/papyrus/views/search/utils/IServiceRegistryTracker.java
@@ -0,0 +1,73 @@
+/*****************************************************************************
+ * 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
+ *****************************************************************************/
+package org.eclipse.papyrus.views.search.utils;
+
+import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
+import org.eclipse.papyrus.views.search.scope.ScopeEntry;
+
+
+/**
+ * A tracker of references to service registries used by {@linkplain ScopeEntry scope entries}, which automatically shuts them down
+ * when they are no longer needed.
+ */
+public interface IServiceRegistryTracker {
+
+ /**
+ * Queries whether I am still actively tracking service registries and able to handle requests to
+ * {@link IServiceRegistryTracker#track(Object, ServicesRegistry) track} or {@link #untrack(Object, ServicesRegistry) un-track} service
+ * registries.
+ *
+ * @return whether I am currently active
+ */
+ boolean isActive();
+
+ /**
+ * <p>
+ * Adds a service registry for me to track. Multiple service registries may be tracked against one owner, but a service registry may only be
+ * tracked against at most one owner.
+ * </p>
+ * <p>
+ * Has no effect if this {@code serviceRegistry} is already tracked against this {@code owner}.
+ * </p>
+ *
+ * @param owner
+ * the owner of the {@code serviceRegistry}. I track references to this object; when it is no longer in use, the {@code serviceRegistry} is
+ * shut down.
+ *
+ * @param serviceRegistry
+ * a service registry to shut down when its {@code owner} is no longer in use
+ *
+ * @throws IllegalArgumentException
+ * if this {@code serviceRegistry} is already tracked against a different owner than the given {@code owner}
+ *
+ * @throws IllegalStateException
+ * if I am not {@link #isActive() active}
+ *
+ * @see #untrack(Object, ServicesRegistry)
+ * @see #isActive()
+ */
+ void track(Object owner, ServicesRegistry serviceRegistry);
+
+ /**
+ * Stops tracking the given service registry. Has no effect if this {@code serviceRegistry} is not currently being
+ * {@linkplain #track(Object, ServicesRegistry) tracked} against this {@code owner}.
+ *
+ * @param owner
+ * the owner of the {@code serviceRegistry}
+ * @param serviceRegistry
+ * a service registry that I may be tracking against its {@code owner}
+ *
+ * @throws IllegalStateException
+ * if I am not {@link #isActive() active}
+ */
+ void untrack(Object owner, ServicesRegistry serviceRegistry);
+}

Back to the top