Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2d242f0b08028479fc9a84bc73eaee7e6963a1f5 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*****************************************************************************
 * Copyright (c) 2013, 2016 Cedric Dumoulin, 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:
 *  Cedric Dumoulin  Cedric.dumoulin@lifl.fr - Initial API and implementation
 *  Christian W. Damus - bugs 469188, 485220
 *
 *****************************************************************************/

package org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider;

import java.util.Map;

import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IContentChangedProvider;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.ISashWindowsContentProvider;
import org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.internal.ContentChangedEventProvider;
import org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.internal.CurrentFolderAndPageManager;
import org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.internal.DiContentProvider;
import org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.internal.ICurrentFolderAndPageMngr;
import org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.internal.PageManagerImpl;
import org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.internal.TransactionalDiContentProvider;
import org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.internal.TransactionalPageManagerImpl;
import org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.utils.TransactionHelper;
import org.eclipse.papyrus.infra.core.sasheditor.editor.ISashWindowsContainer;
import org.eclipse.papyrus.infra.core.sashwindows.di.SashWindowsMngr;
import org.eclipse.papyrus.infra.core.sashwindows.di.service.IPageManager;
import org.eclipse.papyrus.infra.core.sashwindows.di.util.DiUtils;


/**
 * Class used as main entry point to access a sash model build on EMF / di
 * This class allows to get facade objects on the diSashModel.
 *
 * @author cedric dumoulin
 *
 */
public class DiSashModelManager {

	/** The EMF model used to store the sash windows structure and pages */
	protected SashWindowsMngr sashWindowMngr;

	/**
	 * Instance of the {@link PageManagerImpl}.
	 */
	private final PageManagerImpl pageMngr;

	/**
	 * Instance of the DiContentProvider used to manipulate SashModel.
	 */
	private final DiContentProvider contentProvider;

	private final TransactionalDiContentProvider transDiContentProvider;

	private final IPageModelFactory pageModelFactory;

	/**
	 * Object used externally listen to model changes.
	 * The object is also used internally to control how events are fired (limit multiple events).
	 */
	protected ContentChangedEventProvider contentChangedEventProvider;

	/** The factory used to create IPageModel */
	// private IPageModelFactory pageModelFactory;


	/**
	 *
	 * Constructor.
	 *
	 * @param pageModelFactory
	 * @param diResource
	 * @param currentFolderAndPageMngr
	 */
	public DiSashModelManager(IPageModelFactory pageModelFactory, final Resource diResource, TransactionalEditingDomain editingDomain, ICurrentFolderAndPageMngr currentFolderAndPageMngr) {
		// this.pageModelFactory = pageModelFactory;

		// lookup the SashModel
		sashWindowMngr = lookupSashWindowMngr(diResource);
		// If no SashWindow structure is found, create a new one using a transaction.
		if (sashWindowMngr == null) {
			// Create a default model and attach it to resource.
			sashWindowMngr = createDefaultSashModel();
			// Create a command
			RecordingCommand cmd = new RecordingCommand(editingDomain) {

				@Override
				protected void doExecute() {
					diResource.getContents().add(sashWindowMngr);
				}
			};
			// Execute command
			editingDomain.getCommandStack().execute(cmd);
		}

		this.pageModelFactory = pageModelFactory;

		contentProvider = new DiContentProvider(sashWindowMngr.getSashModel(), pageModelFactory, contentChangedEventProvider);
		// Create the TransactionalDiContentProvider
		transDiContentProvider = new TransactionalDiContentProvider(getDiContentProvider(), editingDomain);

		// Create the TransactionalPageMngrImpl
		pageMngr = new TransactionalPageManagerImpl(sashWindowMngr, contentChangedEventProvider, currentFolderAndPageMngr);
	}

	/**
	 *
	 * Constructor.
	 *
	 * @param pageModelFactory
	 * @param diResource
	 * @param currentFolderAndPageMngr
	 */
	public DiSashModelManager(IPageModelFactory pageModelFactory, final Resource diResource, TransactionalEditingDomain editingDomain) {

		// lookup the SashModel
		sashWindowMngr = lookupSashWindowMngr(diResource);
		// If no SashWindow structure is found, create a new one using a transaction.
		if (sashWindowMngr == null) {
			// Create a default model and attach it to resource.
			sashWindowMngr = createDefaultSashModel();

			try {
				TransactionHelper.run(editingDomain, new Runnable() {

					@Override
					public void run() {
						diResource.getContents().add(sashWindowMngr);

					};
				});
			} catch (Exception ex) {
				Activator.log.error(ex);
			}
		}

		this.pageModelFactory = pageModelFactory;

		contentProvider = new DiContentProvider(sashWindowMngr.getSashModel(), pageModelFactory, getContentChangedEventProvider());
		// Create the TransactionalDiContentProvider
		transDiContentProvider = new TransactionalDiContentProvider(getDiContentProvider(), editingDomain);

		// Create the TransactionalPageMngrImpl
		pageMngr = new TransactionalPageManagerImpl(sashWindowMngr, getContentChangedEventProvider());

	}

	/**
	 *
	 * @param currentFolderAndPageMngr
	 */
	public void setCurrentFolderAndPageMngr(ICurrentFolderAndPageMngr currentFolderAndPageMngr) {
		pageMngr.setCurrentFolderAndPageMngr(currentFolderAndPageMngr);
	}

	/**
	 * Set the CurrentFolderAndPageManager as an instance of {@link CurrentFolderAndPageManager}
	 *
	 * @param currentFolderAndPageMngr
	 */
	public void setCurrentFolderAndPageMngr(ISashWindowsContainer sashWindowsContainer) {
		pageMngr.setCurrentFolderAndPageMngr(new CurrentFolderAndPageManager(sashWindowsContainer));
	}

	/**
	 *
	 * Constructor.
	 * Only create a {@link IPageMngr} impl. Do not create the DiContentProvider as there is no factory provided.
	 * Internal use.
	 *
	 * @param pageModelFactory
	 * @param diResource
	 */
	// private DiSashModelManager(final Resource diResource, TransactionalEditingDomain editingDomain, ICurrentFolderAndPageMngr currentFolderAndPageMngr) {
	// this(null, diResource, editingDomain, currentFolderAndPageMngr);
	//
	// }


	/**
	 * Get the internal EMF implementation.
	 * Intended to be used by tests.
	 *
	 * @return the sashWindowMngr
	 */
	protected SashWindowsMngr getDiSashWindowsMngr() {
		return sashWindowMngr;
	}


	/**
	 * Return the internal implementation of ContentProvider.
	 * Create if if needed.
	 * This method should not be subclassed
	 *
	 * @return the contentProvider
	 */
	protected DiContentProvider getDiContentProvider() {
		return contentProvider;
	}

	/**
	 * Return the internal implementation of ContentProvider.
	 * Create if if needed.
	 * This method should not be subclassed
	 *
	 * @return the contentProvider
	 */
	protected TransactionalDiContentProvider getTransactionalDiContentProvider() {
		return transDiContentProvider;
	}

	/**
	 * Return the internal implementation of PageMngr.
	 * Create if if needed.
	 * This method should not be subclassed
	 *
	 * @return the PageMngrImpl
	 */
	protected final PageManagerImpl getPageManagerImpl() {
		return pageMngr;
	}

	/**
	 * Return the internal implementation of ContentChangedEventProvider.
	 * Create if if needed.
	 * This method should not be subclassed
	 *
	 * @return the PageMngrImpl
	 */
	protected final ContentChangedEventProvider getContentChangedEventProvider() {
		if (contentChangedEventProvider == null) {
			contentChangedEventProvider = new ContentChangedEventProvider(sashWindowMngr);
		}

		return contentChangedEventProvider;
	}

	/**
	 * Get the IPageManager providing basic methods to manage Pages in the sash model.
	 *
	 * @return
	 */
	public IPageManager getIPageManager() {
		return getPageManagerImpl();
	}

	/**
	 * Get the ContentProvider used by the SashWindows system.
	 * This class can also be used to accurately manage the sash model.
	 *
	 * @return
	 */
	public ISashWindowsContentProvider getISashWindowsContentProvider() {

		return getTransactionalDiContentProvider();

	}

	/**
	 * Get the ContentChangedProvider for the SashModel
	 *
	 * @return
	 */
	public IContentChangedProvider getSashModelContentChangedProvider() {
		return getContentChangedEventProvider();
	}

	/**
	 * Create a default model with one window and one folder.
	 *
	 * @param diResource
	 * @return
	 */
	protected SashWindowsMngr createDefaultSashModel() {
		SashWindowsMngr result = DiUtils.createDefaultSashWindowsMngr();

		if (result.getSashModel() != null) {
			// Default this to true for new models, which store the sash model
			// in the workspace private area by default
			result.getSashModel().setRestoreActivePage(true);
		}

		return result;
	}

	/**
	 * Lookup for the SashModel object in the resource
	 *
	 * @param diResource
	 * @return
	 */
	protected SashWindowsMngr lookupSashWindowMngr(Resource diResource) {

		return DiUtils.lookupSashWindowsMngr(diResource);
	}

	/*
	 * Create an instance of IPageMngr acting on the provided resource.
	 * This instance is suitable to add, remove, close or open diagrams.
	 *
	 * @param diResource
	 *
	 * @return The non transactional version of the IPageMngr
	 */
	// public static IPageMngr createIPageMngr(Resource diResource) {
	//
	// // Create an instance of the DiSashModelMngr with no factory.
	// // The factory is not needed since we don't get the ISashWindowsContentProvider.
	// return new DiSashModelManager(null, diResource).getIPageMngr();
	//
	// }

	/**
	 * Finds a the EditorIDs that can open the given pageIdentifier
	 *
	 * @param pageIdentifier
	 * @return A map of (ID -> Label) for the matching editors
	 *
	 * @see {@link IPageManager#DEFAULT_EDITOR}
	 */
	public Map<String, String> getEditorIDsFor(Object pageIdentifier) {
		return pageModelFactory.getEditorIDsFor(pageIdentifier);
	}

}

Back to the top