Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 552c2574bf3a3bc47fe896760f796c9772a19a36 (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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*****************************************************************************
 * Copyright (c) 2013, 2014 Cedric Dumoulin, CEA, 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 (CEA) - bug 433371
 *  Céline Janssens (ALL4TEC) celine.janssens@all4tec.net - Bug 415638
 *  Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Bug 440754
 *****************************************************************************/

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

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IPageManager;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.service.ILocalPageService;
import org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.Activator;
import org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.IOpenable;
import org.eclipse.papyrus.infra.core.sashwindows.di.DiFactory;
import org.eclipse.papyrus.infra.core.sashwindows.di.PageRef;
import org.eclipse.papyrus.infra.core.sashwindows.di.SashModel;
import org.eclipse.papyrus.infra.core.sashwindows.di.SashWindowsMngr;
import org.eclipse.papyrus.infra.core.sashwindows.di.TabFolder;
import org.eclipse.swt.widgets.Display;
import org.osgi.framework.FrameworkUtil;


/**
 * This class provide high level method to manage the Sash pages through its DI
 * implementation.
 *
 * @author cedric dumoulin
 *
 */
public class PageManagerImpl extends PageMngrImpl implements IPageManager {

	/**
	 * Constructor.
	 * Use a default {@link ICurrentFolderAndPageMngr} that alwayrs use the first
	 * folder as the current folder. Futhermore, the default implementation doesn't
	 * allow to set the active folder. <br>
	 * Suitable for tests
	 *
	 * @param diSashModel
	 *            The model onto which operation of this class act.
	 * @param contentChangedEventProvider
	 *            A class listening on changes on the internal model and delivering events to registered listeners. This implementation
	 *            need the class in order to deactivate event triggering during the operations.
	 *
	 */
	protected PageManagerImpl(SashWindowsMngr diSashModel, ContentChangedEventProvider contentChangedEventProvider) {
		super(diSashModel, contentChangedEventProvider);
	}

	/**
	 * Constructor.
	 *
	 * @param diSashModel
	 *            the di sash model
	 * @param contentChangedEventProvider
	 *            the content changed event provider
	 * @param folderAndPageMngr
	 *            the folder and page mngr
	 */
	protected PageManagerImpl(SashWindowsMngr diSashModel, ContentChangedEventProvider contentChangedEventProvider, ICurrentFolderAndPageMngr folderAndPageMngr) {
		super(diSashModel, contentChangedEventProvider, folderAndPageMngr);
	}

	/**
	 * @see org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IPageManager#closeAllOpenedPages(java.lang.Object)
	 *
	 * @param pageIdentifier
	 */
	@Override
	public void closeAllOpenedPages(Object pageIdentifier) {
		while (isOpen(pageIdentifier)) {
			closePage(pageIdentifier);
		}
	}

	/**
	 * @see org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.internal.PageMngrImpl#addPage(java.lang.Object)
	 *
	 * @param pageIdentifier
	 */
	@Override
	public void addPage(Object pageIdentifier) {
		// Nothing
	}

	/**
	 * Reload the Diagram
	 * This used when a resource is reloaded, the related diagrams are reloaded as well
	 * 
	 * @see org.eclipse.papyrus.infra.services.controlmode.listener.LoadResourceSnippet
	 * 
	 * @param diagramProxy
	 *            Identifier of the page to reload
	 */
	@Override
	public void reloadPage(Object diagramProxy) {

		if (diagramProxy instanceof EObject) {

			PageRef proxyRef = diSashModel.getSashModel().lookupPage(diagramProxy);

			if (proxyRef.eContainer() instanceof TabFolder) {
				TabFolder folder = (TabFolder) proxyRef.eContainer();


				if (folder != null) {
					if (folder.getChildren() != null) {
						// get the initial index of the Diagram Tab
						int i = folder.getChildren().indexOf(proxyRef);

						if (i >= 0) {
							// Create a new Page
							PageRef newRef = DiFactory.eINSTANCE.createPageRef();
							newRef.setEmfPageIdentifier((EObject) diagramProxy);
							// Replace the previous by the new one
							folder.getChildren().set(i, newRef);
						}
					}
				}
			}
		}
	}

	/**
	 * @see org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.internal.PageMngrImpl#allPages()
	 *
	 * @return
	 */
	@Override
	public List<Object> allPages() {
		// FIXME: Temporary, naive code. Need to implement a mechanism to contribute page providers
		List<Object> result = new LinkedList<Object>();
		List<Resource> notationResources = getResources("notation");
		for (Resource notationResource : notationResources) {
			for (EObject content : notationResource.getContents()) {
				if (isPage(content)) {
					result.add(content);
				}
			}
		}


		return result;
	}

	/**
	 * Gets the local pages.
	 *
	 * @param model
	 *            the model
	 * @return the local pages
	 * @see org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IPageManager#allLocalPages()
	 */
	@Override
	public List<Object> allLocalPages(ILocalPageService service) {
		List<Object> result = new LinkedList<Object>();

		List<Resource> notationResources = getResources("notation");
		for (Resource notationResource : notationResources) {
			for (EObject content : notationResource.getContents()) {
				if (isPage(content)) {

					if (service.isLocalPage(content)) {
						result.add(content);
					}
				}
			}
		}

		return result;
	}


	/**
	 * 
	 * @see org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IPageManager#getAssociatedPages(org.eclipse.emf.common.util.URI)
	 *
	 * @param uriTrim
	 * @return
	 */
	public List<Object> getAssociatedPages(Object uriTrim) {

		List<Object> list = new ArrayList<Object>();

		SashModel sashModel = diSashModel.getSashModel();
		Iterator<?> iter = sashModel.eAllContents();

		while (iter.hasNext()) {
			Object next = iter.next();
			if (next instanceof PageRef) {
				PageRef pageRef = (PageRef) next;

				// pageRef is one of the pages referred into the sash resource
				if (pageRef != null) {
					EObject pageID = pageRef.getEmfPageIdentifier();
					if (pageID != null) {
						URI uriContainer;
						if (pageID.eIsProxy()) {
							InternalEObject internal = (InternalEObject) pageID;
							uriContainer = internal.eProxyURI().trimFragment().trimFileExtension();

						} else {
							uriContainer = pageID.eResource().getURI().trimFileExtension();
						}
						if (uriTrim instanceof URI) {
							if (uriContainer.equals((URI) uriTrim)) {
								list.add(pageID);
							}
						}
					}
				}

			}

		}
		return list;
	}

	/**
	 * Gets the resources.
	 *
	 * @param fileExtension
	 *            the file extension
	 * @return the resources
	 */
	private List<Resource> getResources(String fileExtension) {
		List<Resource> resourcesList = new LinkedList<Resource>();

		// Get resource set from Di Sash model
		ResourceSet resourceSet = diSashModel.eResource().getResourceSet();

		for (Resource resource : resourceSet.getResources()) {

			// Verify if the resource exist and is loaded
			if (resource != null && resource.isLoaded()) {
				// Verify if its extension correspond
				if (fileExtension.equals(resource.getURI().fileExtension())) {
					resourcesList.add(resource);
				}

			}
		}

		return resourcesList;
	}

	/**
	 * Checks if is page.
	 *
	 * @param content
	 *            the content
	 * @return true, if is page
	 */
	private boolean isPage(EObject content) {
		return Platform.getAdapterManager().getAdapter(content, IOpenable.class) != null;
	}

	/**
	 * @see org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.internal.PageMngrImpl#openPage(java.lang.Object)
	 *
	 * @param pageIdentifier
	 */
	@Override
	public void openPage(Object pageIdentifier) {
		diSashModel.getSashModel().addPage(getCurrentFolder(), pageIdentifier);
	}

	/**
	 * @see org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IPageManager#selectPage(java.lang.Object)
	 *
	 * @param pageIdentifier
	 */
	@Override
	public void selectPage(final Object pageIdentifier) {
		Display.getDefault().syncExec(new Runnable() {

			@Override
			public void run() {
				folderAndPageMngr.setActivePage(pageIdentifier);
			}
		});
	}

	/**
	 * @see org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.internal.PageMngrImpl#removePage(java.lang.Object)
	 *
	 * @param pageIdentifier
	 */
	@Override
	public void removePage(Object pageIdentifier) {
		closeAllOpenedPages(pageIdentifier);
	}

	/**
	 * Sets the current folder and page mngr.
	 *
	 * @param currentFolderAndPageMngr
	 *            the new current folder and page mngr
	 */
	public void setCurrentFolderAndPageMngr(ICurrentFolderAndPageMngr currentFolderAndPageMngr) {
		this.folderAndPageMngr = currentFolderAndPageMngr;

	}

	/**
	 * Executes an operation on my internal sash model.
	 *
	 * @param <T>
	 *            the generic type
	 * @param sashModelOperation
	 *            the operation to execute
	 * @return the operation's result
	 * @throws IllegalAccessException
	 *             on attempt to execute an operation defined by a client bundle
	 */
	public <T> T execute(SashModelOperation<T> sashModelOperation) throws IllegalAccessException {
		T result;

		if (FrameworkUtil.getBundle(sashModelOperation.getClass()) != Activator.getDefault().getBundle()) {
			throw new IllegalAccessException("Attempt to access bundle-private API."); //$NON-NLS-1$
		}

		ContentChangedEventProvider eventProvider = getContentChangedEventProvider();
		final boolean deliver = eventProvider.isDeliver();

		eventProvider.setDeliver(false);
		try {
			result = sashModelOperation.execute(diSashModel);
		} finally {
			eventProvider.setDeliver(deliver);
		}

		return result;
	}

	//
	// Private interfaces
	//

	/**
	 * An operation on the internal sash model of a page manager.
	 *
	 * @param <T>
	 *            the generic type
	 */
	public static interface SashModelOperation<T> {

		/**
		 * Execute.
		 *
		 * @param sashWindowsManager
		 *            the sash windows manager
		 * @return the t
		 */
		T execute(SashWindowsMngr sashWindowsManager);
	}

}

Back to the top