Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ad2c941a82c82ac13d625e58de22b9f878a6b079 (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
/*****************************************************************************
 * Copyright (c) 2013, 2017 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) - bug 422257
 *   Eike Stepper (CEA) - bug 466520
 *
 *****************************************************************************/
package org.eclipse.papyrus.cdo.internal.ui.wizards;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.explorer.CDOExplorerUtil;
import org.eclipse.emf.cdo.explorer.checkouts.CDOCheckout;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.papyrus.cdo.internal.core.CDOProxyResolvingResourceSet;
import org.eclipse.papyrus.cdo.internal.core.CDOUtils;
import org.eclipse.papyrus.cdo.internal.ui.Activator;
import org.eclipse.papyrus.cdo.internal.ui.views.DIModel;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;


/**
 * This is the LocalRepositoryView type. Enjoy.
 */
public class LocalRepositoryView {

	private final Map<CDOCheckout, CDOView> localViews = Maps.newHashMap();

	private ResourceSet rset = null;

	public LocalRepositoryView() {
		super();
	}

	/**
	 * Disposes of all of the local {@link CDOView}s that I have cached.
	 */
	public void dispose() {
		for (Map.Entry<CDOCheckout, CDOView> next : localViews.entrySet()) {
			CDOView view = next.getValue();
			try {
				CDOUtils.unload(view);
			} catch (Exception e) {
				Activator.error("Exception in unloading CDO repository view.", e); //$NON-NLS-1$
			} finally {
				view.close();
			}
		}

		localViews.clear();

		if (rset != null) {
			try {
				EMFHelper.unload(rset);
			} catch (Exception e) {
				Activator.error("Exception in unloading CDO repository view's resource set.", e); //$NON-NLS-1$
			} finally {
				rset = null;
			}
		}
	}

	/**
	 * Obtain a view of the specified {@code selection} in the context of a local cache of distinct {@link CDOView}s.
	 *
	 * @param selection
	 *            a selection of objects in a CDO view
	 *
	 * @return a new selection consisting of the same objects as presented in my local CDO views
	 */
	public IStructuredSelection translate(IStructuredSelection selection) {
		return new StructuredSelection(translate(selection.toList()));
	}

	/**
	 * Obtain a view of the specified {@code objects} in the context of a local cache of distinct {@link CDOView}s.
	 *
	 * @param objects
	 *            a bunch of objects in a CDO view
	 *
	 * @return a new list consisting of the same objects as presented in my local CDO views
	 */
	public List<Object> translate(Collection<?> objects) {
		List<Object> result = Lists.newArrayListWithCapacity(objects.size());

		for (Object next : objects) {
			if (next instanceof EObject) {
				result.add(translate((EObject) next));
			} else if (next instanceof DIModel) {
				DIModel di = (DIModel) next;

				result.add(DIModel.getInstance(translate(di.getResource()), true));
			}
		}

		return result;
	}

	/**
	 * Obtain a view of the specified {@code object} in the context of a local cache of distinct {@link CDOView}s.
	 *
	 * @param objects
	 *            a modeled object in a CDO view
	 *
	 * @return the same object as presented in my local CDO views
	 */
	public <T extends EObject> T translate(T object) {
		T result;

		if (object instanceof CDOObject) {
			CDOObject cdo = (CDOObject) object;
			CDOView view = cdo.cdoView();
			if (view == null) {
				result = object;
			} else {
				CDOCheckout checkout = CDOExplorerUtil.getCheckout(view);
				CDOView localView = localViews.get(checkout);
				if (localView == null) {
					localView = checkout.openView(true, getResourceSet());
					localViews.put(checkout, localView);
				}

				result = localView.getObject(object);
			}
		} else {
			result = object;
		}

		return result;
	}

	protected ResourceSet getResourceSet() {
		if (rset == null) {
			rset = createResourceSet();
		}

		return rset;
	}

	protected ResourceSet createResourceSet() {
		return new CDOProxyResolvingResourceSet();
	}
}

Back to the top