Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d6f0f81e9d1cf4862c16627fb181e03ab6e56c87 (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
/*****************************************************************************
 * Copyright (c) 2014 Montages AG, 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:
 *  Montages AG - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 437217
 *
 *****************************************************************************/
package org.eclipse.papyrus.editor;

import org.eclipse.gef.ui.views.palette.PaletteView;
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.IPageChangedListener;
import org.eclipse.papyrus.infra.core.sasheditor.internal.ActivePageTracker.IActiveEditorChangedListener;
import org.eclipse.papyrus.infra.core.sasheditor.internal.PagePart;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;

/**
 * Synchronizes the detached palette view with the contents provided by the active inner editor
 */
/* package */class PapyrusPaletteSynchronizer implements IActiveEditorChangedListener, IPageChangedListener {

	private final PapyrusMultiDiagramEditor myMultiDiagramEditor;

	private IEditorPart myLastActivePart;

	public PapyrusPaletteSynchronizer(PapyrusMultiDiagramEditor multiEditor) {
		myMultiDiagramEditor = multiEditor;

		// Handle the initial page selection
		synchronizePaletteView(multiEditor.getISashWindowsContainer().getActiveSashWindowsPage());
	}

	@Override
	public void activeEditorChanged(PagePart oldEditor, PagePart newEditor) {
		synchronizePaletteView(newEditor);
	}

	@Override
	public void pageChanged(IPage newPage) {
		synchronizePaletteView(newPage);
	}

	/**
	 * If {@link PaletteView} is available, synchronizes it with the content provided by active
	 * inner page
	 *
	 * @param activePage
	 *            inner page to synchronize palette view with
	 */
	private void synchronizePaletteView(IPage activePage) {
		PaletteView paletteView = findPaletteView();
		if (paletteView == null) {
			return;
		}

		// IEditorPage is not granted, it may be, e.g ErrorComponentPart
		IEditorPart activePart = activePage instanceof IEditorPage ? ((IEditorPage) activePage).getIEditorPart() : null;
		if (activePart == myLastActivePart) {
			return;
		}

		if (activePart == null) {
			paletteView.partClosed(myLastActivePart);
		} else {
			// multi-editor may be activated outside of this code
			// and palette view may already remember its (now outdated) palette
			// sending partClosed will enforce palette view to request fresh version
			paletteView.partClosed(myMultiDiagramEditor);
			paletteView.partActivated(activePart);
		}
		myLastActivePart = activePart;
	}

	/**
	 * Called when host editor is disposed, cleans up
	 */
	public void dispose() {
		PaletteView paletteView = findPaletteView();
		if (paletteView == null) {
			return;
		}
		if (myLastActivePart != null) {
			paletteView.partClosed(myLastActivePart);
		} else {
			paletteView.partClosed(myMultiDiagramEditor);
		}
	}

	/**
	 * Finds the {@link PaletteView} at the same page as the host multi-editor
	 *
	 * @return palette view or <code>null</code> if not found
	 */
	private PaletteView findPaletteView() {
		IWorkbenchPage samePage = myMultiDiagramEditor.getSite().getPage();
		return (PaletteView) samePage.findView(PaletteView.ID);
	}

}

Back to the top