Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5495148f3d6071b6f7d0d644ed55cfac1fbe8cdc (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
/*****************************************************************************
 * Copyright (c) 2010, 2014 CEA LIST and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *  Cedric Dumoulin  Cedric.dumoulin@lifl.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.core.sasheditor.editor;


import org.eclipse.papyrus.infra.core.sasheditor.Activator;
import org.eclipse.papyrus.infra.core.sasheditor.internal.SashWindowsContainer;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IPropertyListener;
import org.eclipse.ui.IWorkbenchPart;


/**
 * This class take care to refresh a Sash Tab (name and icon) when its model change.
 * The class listen to IEditor property change, and refresh the corresponding tab
 * accordingly.
 * The class also listen on {@link SashWindowsContainer} page's life cycle in order to
 * attach/remove the listener on pages.
 *
 * The class can be extended.
 *
 * @author cedric dumoulin
 *
 */
public class SashTabDecorationSynchronizer {

	/**
	 * The container to synchronize.
	 */
	private SashWindowsContainer container;

	/**
	 * Listener on Container'pages life cycle.
	 */
	IPageLifeCycleEventsListener pageLifeCycleListener = new IPageLifeCycleEventsListener() {

		@Override
		public void pageChanged(IPage newPage) {
		}

		@Override
		public void pageOpened(IPage page) {
			attachPage(page);

		}

		@Override
		public void pageDeactivated(IPage page) {
		}

		@Override
		public void pageClosed(IPage page) {
			detachPage(page);

		}

		@Override
		public void pageActivated(IPage page) {
		}

		@Override
		public void pageAboutToBeOpened(IPage page) {
		}

		@Override
		public void pageAboutToBeClosed(IPage page) {
		}
	};

	/**
	 * Listener on changes of an {@link IEditorPart}
	 */
	private IPropertyListener editorPartPropertyListener = new IPropertyListener() {

		@Override
		public void propertyChanged(Object source, int propId) {
			if (propId == IWorkbenchPart.PROP_TITLE && source instanceof IEditorPart)
			{
				refreshContainerTabForPage((IEditorPart) source);
			}

		}
	};

	/**
	 *
	 * Constructor.
	 *
	 * @param container
	 *            The container to synchronize.
	 *
	 */
	public SashTabDecorationSynchronizer(SashWindowsContainer container) {
		this.container = container;
		attachContainerPages();
		attachListeners();
	}

	/**
	 * Detach any listeners.
	 */
	public void dispose() {
		detachListeners();
		detachContainerPages();
	}

	/**
	 * Attach listeners requested by the class.
	 */
	protected void attachListeners() {
		container.addPageLifeCycleListener(pageLifeCycleListener);
	}

	/**
	 * Detach listeners requested by the class.
	 */
	protected void detachListeners() {
		if (!container.isDisposed()) {
			container.removePageLifeCycleListener(pageLifeCycleListener);
		}
	}

	/**
	 * Start listening on change for the specified page.
	 *
	 * @param page
	 */
	protected void attachPage(IPage page) {
		if (page instanceof IEditorPage)
		{
			Activator.log.debug("attachPage( " + page + " )");
			IEditorPage editorPage = (IEditorPage) page;
			editorPage.getIEditorPart().addPropertyListener(editorPartPropertyListener);
		}

	}

	/**
	 * Stop listening on change for the specified page.
	 *
	 * @param page
	 */
	protected void detachPage(IPage page) {
		// IEditorPage are already disposed, so we don't need to remove anything
		// if( page instanceof IEditorPage )
		// {
		// log.fine("attachPage( " + page + " )");
		// IEditorPage editorPage = (IEditorPage)page;
		// editorPage.getIEditorPart().removePropertyListener(editorPartPropertyListener);
		// }
	}

	protected void refreshContainerTabForPage(IEditorPart source) {
		// lookup page
		IPage page = container.lookupIPageByIEditorPart(source);
		refreshContainerTabForPage(page);

	}

	/**
	 * Refresh the tab for the specified page
	 *
	 * @param page
	 */
	protected void refreshContainerTabForPage(IPage page) {
		container.refreshPageTab(page);
	}

	/**
	 * Iterate on pages owned by the container, and call detachPage for each.
	 */
	private void detachContainerPages() {
		// Visit all pages of the container.
		if (!container.isDisposed()) {
			container.visit(new DetachVisitor());
		}

	}

	/**
	 * Iterate on pages owned by the container, and call detachPage for each.
	 */
	private void attachContainerPages() {
		// Visit all pages of the container.
		container.visit(new AttachVisitor());

	}

	/**
	 * Visitor to visit all pages of the container and attach the page.
	 *
	 * @author dumoulin
	 *
	 */
	private class AttachVisitor implements IPageVisitor {

		@Override
		public void accept(IComponentPage page) {
		}

		@Override
		public void accept(IEditorPage page) {
			attachPage(page);

		}

	}

	/**
	 * Visitor to visit all pages of the container and attach the page.
	 *
	 * @author dumoulin
	 *
	 */
	private class DetachVisitor implements IPageVisitor {

		@Override
		public void accept(IComponentPage page) {
		}

		@Override
		public void accept(IEditorPage page) {
			detachPage(page);

		}

	}
}

Back to the top