Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 592342b04010a2e17d45b852e4a357be5f545112 (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
/*****************************************************************************
 * 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.cdo.uml.search.internal.ui.scope;

import java.util.Collection;
import java.util.Collections;

import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.eresource.CDOResourceFolder;
import org.eclipse.emf.cdo.eresource.CDOResourceNode;
import org.eclipse.emf.cdo.util.CDOURIUtil;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.papyrus.cdo.internal.core.CDOUtils;
import org.eclipse.papyrus.cdo.internal.core.IInternalPapyrusRepository;
import org.eclipse.papyrus.cdo.internal.core.PapyrusRepositoryManager;
import org.eclipse.papyrus.views.search.scope.IScopeProvider;
import org.eclipse.uml2.uml.resource.UMLResource;

import com.google.common.collect.Lists;


/**
 * A scope provider for CDO model repositories.
 */
public class CDOScopeProvider implements IScopeProvider {

	public CDOScopeProvider() {
		super();
	}

	/**
	 * Gets the URIs of the root resource (implying the repository in its entirety) of all repositories that are currently connected.
	 */
	@Override
	public Collection<URI> getScope() {
		Collection<URI> result;

		final Collection<? extends IInternalPapyrusRepository> repos = PapyrusRepositoryManager.INSTANCE.getRepositories();
		if(repos.isEmpty()) {
			result = Collections.emptyList();
		} else {
			result = Lists.newArrayListWithCapacity(repos.size());
			for(IInternalPapyrusRepository next : repos) {
				if(next.isConnected()) {
					CDOView view = next.getMasterView();
					if(view != null) {
						result.add(view.getRootResource().getURI());
					}
				}
			}
		}

		return result;
	}

	@Override
	public Collection<URI> getScope(Object selected) {
		Collection<URI> result;

		// try to get the contextual resource node
		CDOResourceNode node = CDOUtils.adapt(selected, CDOResourceNode.class);
		if(node == null) {
			EObject obj = CDOUtils.adapt(selected, EObject.class);
			if(obj != null) {
				node = CDOUtils.adapt(obj.eResource(), CDOResource.class);
			}
		}

		if(node == null) {
			result = Collections.emptyList();
		} else {
			URI uri = node.getURI();
			if(!node.isRoot() && (node instanceof CDOResource)) {
				// ensure that we search the UML resource, not the notation or di
				uri = uri.trimFileExtension().appendFileExtension(UMLResource.FILE_EXTENSION);

				if(!uri.equals(node.getURI())) {
					// does the UML resource exist?  If not, don't try to search it
					String path = CDOURIUtil.extractResourcePath(uri);
					if(!node.cdoView().hasResource(path)) {
						uri = null;
					}
				}
			} else if((node instanceof CDOResourceFolder) && !uri.hasTrailingPathSeparator()) {
				uri = uri.appendSegment(""); //$NON-NLS-1$
			}

			result = (uri == null) ? Collections.<URI> emptyList() : Collections.singletonList(uri);
		}

		return result;
	}
}

Back to the top