Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 673384fe5d6f1e27b56bbeba0c9ed44b72b87f29 (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
/*****************************************************************************
 * Copyright (c) 2010 LIFL & CEA LIST.
 *
 *
 * 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 (LIFL) cedric.dumoulin@lifl.fr - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.views.modelexplorer.core.ui.pagebookview;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.papyrus.infra.ui.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.views.modelexplorer.Activator;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.IPage;
import org.eclipse.ui.part.IPageBookViewPage;
import org.eclipse.ui.part.PageBook;
import org.eclipse.ui.part.PageBookView;


/**
 * Base class for PageBookView in Papyrus.
 * The PageBook mechanism can be moved in the core, as it can be reused by other Views.
 *
 * @author cedric dumoulin
 *
 */
public abstract class MultiViewPageBookView extends PageBookView {

	/**
	 * Remember the IMemento
	 */
	private IMemento memento;

	/**
	 * @see org.eclipse.ui.part.PageBookView#createDefaultPage(org.eclipse.ui.part.PageBook)
	 *
	 * @param book
	 * @return
	 */
	@Override
	protected IPageBookViewPage createDefaultPage(PageBook book) {
		IPageBookViewPage page = new DefaultPage();
		// Set Site
		initPage(page);
		page.createControl(book);

		return page;
	}

	/**
	 * Subclass must implement this method.
	 * The subclass implementation look like this:
	 * <ul>
	 * <li>ViewPartPage page = new MyPage();</li>
	 * <li>call initPage(page, part);</li>
	 * <li>call page.createControl(getPageBook());</li>
	 * <li>return new PageRec(part, page);</li>
	 * </ul>
	 *
	 * @param part
	 * @return
	 */
	@Override
	abstract protected PageRec doCreatePage(IWorkbenchPart part);

	/**
	 * @see org.eclipse.ui.part.PageBookView#doDestroyPage(org.eclipse.ui.IWorkbenchPart, org.eclipse.ui.part.PageBookView.PageRec)
	 *
	 * @param part
	 * @param pageRecord
	 */
	@Override
	protected void doDestroyPage(IWorkbenchPart part, PageRec pageRecord) {

		// Dispose the associated page.
		pageRecord.page.dispose();

	}

	/**
	 * Check if the currently active part is a {@link IMultiDiagramEditor}.
	 * If true, return it.
	 *
	 * @see org.eclipse.ui.part.PageBookView#getBootstrapPart()
	 *
	 * @return The currently active editor if it is a IMultiDiagramEditor, null otherwise.
	 */
	@Override
	protected IWorkbenchPart getBootstrapPart() {


		IWorkbenchPart part = null;
		try {
			IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
			if (activeWorkbenchWindow != null) {
				IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
				if (activePage != null) {
					part = activePage.getActiveEditor();
				}
			}
		} catch (NullPointerException e) {
			// An element is not active yet
			return null;
		}
		if (isImportant(part)) {
			return part;
		}
		// The current active part is not for us.
		return null;
	}

	/**
	 * return true if the activated part is a {@link IMultiDiagramEditor}.
	 *
	 * @see org.eclipse.ui.part.PageBookView#isImportant(org.eclipse.ui.IWorkbenchPart)
	 *
	 * @param part
	 * @return
	 */
	@Override
	protected boolean isImportant(IWorkbenchPart part) {


		if (part instanceof IMultiDiagramEditor) {
			return true;
		}

		return false;
	}

	/**
	 * Record the IMemento to use it when pages are created
	 *
	 * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite, org.eclipse.ui.IMemento)
	 *
	 * @param site
	 * @param memento
	 * @throws PartInitException
	 */
	@Override
	public void init(IViewSite site, IMemento memento) throws PartInitException {
		super.init(site, memento);
		this.memento = memento;
	}

	/**
	 * Create a Site that is also a {@link IViewSite}.
	 *
	 * @see org.eclipse.ui.part.PageBookView#initPage(org.eclipse.ui.part.IPageBookViewPage)
	 *
	 * @param page
	 */
	protected void initPage(ViewPartPage page, IWorkbenchPart part) {

		try {
			page.init(part, new ViewPageSite(getViewSite()), memento, getConfigurationElement());
		} catch (PartInitException e) {
			Activator.log.error("initPage", e); //$NON-NLS-1$
		} catch (CoreException e) {
			Activator.log.error("initPage", e); //$NON-NLS-1$
		}

	}

	/**
	 * Get the currently nested active view.
	 *
	 * @return The active Viewer, or null if none.
	 */
	public IViewPart getActiveView() {
		IPage page = getCurrentPage();
		if (page instanceof ViewPartPage) {
			return ((ViewPartPage) page).getViewer();
		}

		// not found
		return null;
	}
}

Back to the top