Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 168a4877f9b0da42e2bad65a324f980e947b24c7 (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
/*****************************************************************************
 * Copyright (c) 2015 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

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

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

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.utils.IPageUtils;
import org.eclipse.papyrus.infra.core.sasheditor.editor.IComponentPage;
import org.eclipse.papyrus.infra.core.sasheditor.editor.IEditorPage;
import org.eclipse.papyrus.infra.core.sasheditor.editor.IPage;
import org.eclipse.papyrus.infra.core.sasheditor.editor.IPageVisitor;
import org.eclipse.papyrus.infra.core.sasheditor.editor.ISashWindowsContainer;
import org.eclipse.papyrus.infra.core.sashwindows.di.service.IPageManager;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ISources;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * A convenient aggregation of objects needed for processing page management commands
 * in the context of the Papyrus multi-diagram editor.
 */
class PageContext {
	final IEditorPart editor;
	final IPageManager pageManager;
	final ISashWindowsContainer sashContainer;
	final Object currentPageIdentifier;
	final IPage currentPage;

	PageContext(ExecutionEvent event) {
		this(HandlerUtil.getActiveEditor(event));
	}

	PageContext(Object evaluationContext) {
		this((IEditorPart) HandlerUtil.getVariable(evaluationContext, ISources.ACTIVE_EDITOR_NAME));
	}

	PageContext(IEditorPart activeEditor) {
		super();

		editor = activeEditor;
		sashContainer = (activeEditor == null) ? null : activeEditor.getAdapter(ISashWindowsContainer.class);

		if ((sashContainer == null) || sashContainer.isDisposed()) {
			pageManager = null;
			currentPage = null;
			currentPageIdentifier = null;
		} else {
			pageManager = activeEditor.getAdapter(IPageManager.class);
			currentPage = sashContainer.getActiveSashWindowsPage();
			currentPageIdentifier = (currentPage == null) ? null : IPageUtils.getRawModel(currentPage);
		}
	}

	boolean isValid() {
		return (pageManager != null) && (currentPageIdentifier != null) && (sashContainer != null);
	}

	int getOpenPageCount() {
		return getOpenPages().size();
	}

	Collection<IPage> getOpenPages() {
		Collection<IPage> result;

		if (!isValid()) {
			result = Collections.emptyList();
		} else {
			result = new java.util.ArrayList<>();
			sashContainer.visit(new IPageVisitor() {

				@Override
				public void accept(IEditorPage page) {
					result.add(page);
				}

				@Override
				public void accept(IComponentPage page) {
					result.add(page);
				}
			});
		}

		return result;
	}
}

Back to the top