Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0888b3b203a9ce25cc93eb3daf7f1d8f7364016b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*****************************************************************************
 * Copyright (c) 2013, 2016 CEA LIST, Christian W. Damus, 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
 *  Christian W. Damus - bug 485220
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.search.ui.query;

import java.util.LinkedList;
import java.util.List;

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.core.runtime.SubMonitor;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.core.sashwindows.di.service.IPageManager;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject;
import org.eclipse.papyrus.uml.search.ui.Activator;
import org.eclipse.papyrus.uml.search.ui.results.PapyrusSearchResult;
import org.eclipse.search.ui.ISearchQuery;
import org.eclipse.uml2.uml.Element;

/**
 *
 * Papyrus specific search query
 *
 */
public abstract class AbstractPapyrusQuery implements ISearchQuery {

	protected SubMonitor progressMonitor;

	/**
	 * Getter for the text query
	 *
	 * @return the the query text
	 */
	public abstract String getSearchQueryText();

	public boolean isCaseSensitive() {
		
		return false;
	}

	public boolean isRegularExpression() {
		
		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 "";
		}
	}

	/**
	 * Get views in which the UML element is shown
	 * 
	 * @param element
	 * @return
	 */

	protected List<View> getViews(Element element) {
		if (element == null) {
			return null;
		}

		IPageManager pageManager;
		try {
			pageManager = ServiceUtilsForEObject.getInstance().getService(IPageManager.class, element);
		} catch (ServiceException e) {
			Activator.log.error(e);
			return null;
		}

		List<View> viewsToSelect = new LinkedList<View>();

		try {
			for (Object page : pageManager.allPages()) {
				try {
					if (page instanceof View) {
						View view = (View) page;
						TreeIterator<EObject> allViews = view.eAllContents();
						while (allViews.hasNext()) {
							EObject next = allViews.next();
							if (!(next instanceof View)) {
								allViews.prune();
								continue;
							}

							View nextView = (View) next;
							if (element.equals(nextView.getElement())) {
								viewsToSelect.add(nextView);
								allViews.prune();
							}
						}
					}
				} catch (Exception e) {
					Activator.log.error(e);
					return null;
				}
			}
		} catch (Exception e) {
			Activator.log.error(e);
			return null;
		}

		return viewsToSelect;
	}
}

Back to the top