Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4aefc817568f2f2176ed058780c4f5be4c90be86 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*****************************************************************************
 * 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.util.Set;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.OpenEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject;
import org.eclipse.papyrus.infra.services.navigation.service.NavigationService;
import org.eclipse.papyrus.infra.services.openelement.service.OpenElementService;
import org.eclipse.papyrus.uml.search.ui.Activator;
import org.eclipse.papyrus.uml.search.ui.Messages;
import org.eclipse.papyrus.uml.search.ui.actions.FilterTypesAction;
import org.eclipse.papyrus.uml.search.ui.providers.ResultContentProvider;
import org.eclipse.papyrus.uml.search.ui.providers.ResultLabelProvider;
import org.eclipse.papyrus.views.search.results.AbstractResultEntry;
import org.eclipse.papyrus.views.search.results.ModelMatch;
import org.eclipse.papyrus.views.search.results.ResultEntry;
import org.eclipse.papyrus.views.search.scope.ScopeEntry;
import org.eclipse.papyrus.views.search.utils.MatchUtils;
import org.eclipse.search.ui.IContextMenuConstants;
import org.eclipse.search.ui.text.AbstractTextSearchViewPage;
import org.eclipse.search.ui.text.Match;
import org.eclipse.ui.PartInitException;
import org.eclipse.uml2.uml.Element;

/**
 *
 * Papyrus specific search results page
 * @since 2.0
 *
 */
public class PapyrusSearchResultPage extends AbstractTextSearchViewPage {

	ResultContentProvider fContentProvider = null;

	private IAction fFiltertypesAction = null;

	private static final int DEFAULT_ELEMENT_LIMIT = 1000;

	public PapyrusSearchResultPage() {

		super(FLAG_LAYOUT_TREE);
		setElementLimit(new Integer(DEFAULT_ELEMENT_LIMIT));
		fFiltertypesAction = new FilterTypesAction(this);
	}



	@Override
	public Match[] getDisplayedMatches(Object element) {
		
		Set<AbstractResultEntry> results = MatchUtils.getMatches(this.getInput(), true);

		if (element instanceof AbstractResultEntry) {
			AbstractResultEntry resultEntry = (AbstractResultEntry) element;

			if (results.contains(resultEntry)) {

				return new Match[] { resultEntry };

			}
			return new Match[0];
		} else {
			return super.getDisplayedMatches(element);
		}
	}

	@Override
	public int getDisplayedMatchCount(Object element) {

		if (element instanceof AbstractResultEntry) {
			AbstractResultEntry resultEntry = (AbstractResultEntry) element;

			Set<AbstractResultEntry> results = MatchUtils.getMatches(this.getInput(), true);
			if (results.contains(resultEntry)) {
				return 1;
			}

			return 0;
		} else {
			return super.getDisplayedMatchCount(element);
		}
	}

	@Override
	protected void fillToolbar(IToolBarManager tbm) {
		super.fillToolbar(tbm);
		tbm.appendToGroup(IContextMenuConstants.GROUP_REMOVE_MATCHES, fFiltertypesAction);
	}

	@Override
	protected void handleOpen(OpenEvent event) {
		ISelection selection = event.getSelection();
		if (!selection.isEmpty()) {
			if (selection instanceof IStructuredSelection) {
				Object firstElement = ((IStructuredSelection) selection).getFirstElement();

				if (firstElement instanceof AbstractResultEntry) {
					AbstractResultEntry resultEntry = (AbstractResultEntry) firstElement;
					ScopeEntry scopeEntry = (ScopeEntry) resultEntry.getElement();
					if (scopeEntry != null && scopeEntry.getServicesRegistry() != null) {
						Object source = resultEntry.getSource();
						if (source instanceof Element) { // UML element => use NavigationService to navigate to Model Explorer
							try {
								NavigationService navigationService = ServiceUtilsForEObject.getInstance().getService(NavigationService.class, (Element) source);
								navigationService.navigate((Element) source, "org.eclipse.papyrus.views.modelexplorer.navigation.target");
							} catch (ServiceException e) {
								Activator.log.error(e);
							}
						} else { // Anything else => Let the OpenElementService handle it
							try {
								OpenElementService service = scopeEntry.getServicesRegistry().getService(OpenElementService.class);
								resultEntry.openElement(service);
							} catch (ServiceException e) {
								// Activator.log.error(Messages.PapyrusSearchResultPage_0 + resultEntry.elementToOpen(), e);
							} catch (PartInitException e) {
								Activator.log.error(Messages.PapyrusSearchResultPage_1, e);
							}
						}
					}

				}

			}

		}

		// super.handleOpen(event);
	}

	@Override
	protected void elementsChanged(Object[] objects) {
		if (fContentProvider != null) {
			fContentProvider.elementsChanged(objects);
		}
	}


	@SuppressWarnings("unchecked")
	@Override
	protected void evaluateChangedElements(Match[] matches, @SuppressWarnings("rawtypes") Set changedElements) {
		for (int i = 0; i < matches.length; i++) {
			changedElements.add(matches[i]);
		}
	}

	@Override
	protected void clear() {
		if (fContentProvider != null) {
			fContentProvider.clear();
		}
	}

	@Override
	protected void configureTreeViewer(TreeViewer viewer) {
		viewer.setContentProvider(new ResultContentProvider(this, viewer));
		viewer.setLabelProvider(new ResultLabelProvider());
		fContentProvider = (ResultContentProvider) viewer.getContentProvider();

	}

	@Override
	protected void configureTableViewer(TableViewer viewer) {

	}

	@Override
	public void dispose() {
		super.dispose();
	}
}

Back to the top