Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0357b6187e3b62abb0112212844d2bcf06d7ca79 (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
/*****************************************************************************
 * Copyright (c) 2009, 2015 CEA LIST, LIFL, 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:
 *  Cedric Dumoulin  Cedric.dumoulin@lifl.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 431953 (pre-requisite refactoring of ModelSet service start-up)
 *  Christian W. Damus (CEA) - bug 437217
 *  Christian W. Damus - bug 479999
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.core.sasheditor.editor;

import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.ISashWindowsContentProvider;
import org.eclipse.papyrus.infra.core.sasheditor.internal.IMultiEditorManager;
import org.eclipse.papyrus.infra.core.sasheditor.internal.SashWindowsContainer;
import org.eclipse.papyrus.infra.core.sasheditor.internal.eclipsecopy.MultiPageSelectionProvider;
import org.eclipse.papyrus.infra.tools.util.PlatformHelper;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchPartConstants;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

/**
 * Base editor that can be subclassed to provide a multi page editor with sash windows.
 *
 * @author dumoulin
 */
public abstract class AbstractMultiPageSashEditor extends EditorPart implements IMultiPageEditorPart, IMultiEditorManager {

	/** The parent composite of my sash container. */
	private Composite parentComposite;

	/** The pageProvider */
	private ISashWindowsContentProvider pageProvider;

	/** The sash windows system :-) */
	private SashWindowsContainer sashContainer;

	/**
	 * Synchronizer in charge of synchronizing tab names with IEditorPart title.
	 */
	private SashTabDecorationSynchronizer tabsSynchronizer;

	/**
	 * Listener on editor internal pages.
	 * Used to relay the firePropertyChange() event.
	 */
	private IPageLifeCycleEventsListener2 pageLifeCycleEventListener = new DefaultPageLifeCycleEventListener() {

		@Override
		public void pageFirePropertyChange(IPage page, int propertyId) {
			// relay the event.
			firePropertyChange(propertyId);
		};

		/**
		 * Calling getActiveEditor().getEditorInput() will return a different value since the
		 * active editor has now changed. Notify the Eclipse Platform listeners by firing an
		 * appropriate notification.
		 * 
		 * @see org.eclipse.papyrus.infra.core.sasheditor.editor.DefaultPageLifeCycleEventListener#pageChanged(org.eclipse.papyrus.infra.core.sasheditor.editor.IPage)
		 *
		 * @param newPage
		 */
		@Override
		public void pageActivated(IPage newPage) {
			firePropertyChange(IWorkbenchPartConstants.PROP_INPUT);
		};


	};

	/**
	 * Listener on double click events from {@link ISashWindowsContainer}'tabs.
	 */
	private TabMouseEventListener tabMouseEventListener;

	/**
	 * get the contentProvider. Create it if necessary.
	 *
	 * @return
	 */
	protected ISashWindowsContentProvider getContentProvider() {
		if (pageProvider == null) {
			pageProvider = createPageProvider();
		}

		return pageProvider;
	}

	/**
	 * Set the contentProvider if not set. If it is already set, this method do not change it.
	 *
	 * @param contentProvider
	 */
	protected void setContentProvider(ISashWindowsContentProvider contentProvider) {
		if (pageProvider == null) {
			pageProvider = contentProvider;
		}
	}

	/**
	 * Create the provider.
	 * Subclass must implements this method. It should return the provider used by the editor.
	 *
	 */
	abstract protected ISashWindowsContentProvider createPageProvider();

	/**
	 * Handles a property change notification from a nested editor. The default implementation simply forwards the change to
	 * listeners on this multi-page editor by calling <code>firePropertyChange</code> with the same property id. For example, if
	 * the dirty state of a nested editor changes (property id <code>IEditorPart.PROP_DIRTY</code>), this method handles it
	 * by firing a property change event for <code>IEditorPart.PROP_DIRTY</code> to property listeners on this multi-page
	 * editor.
	 * <p>
	 * Subclasses may extend or reimplement this method.
	 * </p>
	 *
	 * @copiedfrom org.eclipse.ui.part.MultiPageEditorPart.handlePropertyChange(int)
	 *
	 * @param propertyId
	 *            the id of the property that changed
	 */
	protected void handlePropertyChange(int propertyId) {
		firePropertyChange(propertyId);
	}

	/**
	 * The <code>MultiPageEditorPart</code> implementation of this <code>IEditorPart</code> method sets its site to the given
	 * site, its input to the given input, and the site's selection provider to a <code>MultiPageSelectionProvider</code>.
	 * Subclasses may extend this method.
	 *
	 * @copiedfrom org.eclipse.ui.part.MultiPageEditorPart
	 * @param site
	 *            The site for which this part is being created; must not be <code>null</code>.
	 * @param input
	 *            The input on which this editor should be created; must not be <code>null</code>.
	 * @throws PartInitException
	 *             If the initialization of the part fails -- currently never.
	 */
	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		setSite(site);
		setInput(input);
	}

	/**
	 * Create the part controls. {@inheritDoc}
	 */
	@Override
	public void createPartControl(Composite parent) {
		parentComposite = parent;

		getSite().setSelectionProvider(new MultiPageSelectionProvider(this));

		activate();
	}

	/**
	 * Method to activate the editor.
	 * Called immediately after createPartControl() is complete.
	 * To be implemented by subclasses. Default implementation do nothing.
	 */
	protected void activate() {

		// Create and initialize sash windows
		sashContainer = new SashWindowsContainer(this);
		sashContainer.setContentProvider(getContentProvider());
		sashContainer.createPartControl(parentComposite);

		// Add double click menu
		tabMouseEventListener = new TabMouseEventListener(sashContainer, getSite());

		tabsSynchronizer = new SashTabDecorationSynchronizer(sashContainer);

		// Listen on page life cycle to relay property change events
		// we don't need to remove the handler from sashContainer on dispose, because sashContainer
		// is disposed itself.
		sashContainer.addPageLifeCycleListener(pageLifeCycleEventListener);
	}

	/**
	 * Method to deactivate the editor.
	 * Called when dispose() is called.
	 * To be implemented by subclasses. Default implementation do nothing.
	 */
	protected void deactivate() {

		// Could be deactivated by a call from dispose().
		// In this case, the tabsSynchronizer can already be null, because no activation was done.
		if (tabsSynchronizer == null) {
			return;
		}
		tabsSynchronizer.dispose();
		tabsSynchronizer = null;

		if (tabMouseEventListener != null) {
			tabMouseEventListener.dispose(sashContainer);
			tabMouseEventListener = null;
		}

		if (sashContainer != null) {
			sashContainer.dispose();
		}
		pageProvider = null;
	}

	/**
	 * Dispose the Editor. Also dispose the sashsystem.
	 *
	 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
	 *
	 */
	@Override
	public void dispose() {
		deactivate();

		// The selection provider keeps a reference to "this". It is not disposed.
		getSite().setSelectionProvider(null);

		super.dispose();
	}

	/**
	 * Refresh the sash windows system
	 */
	protected void refreshTabs() {
		Display.getDefault().syncExec(new Runnable() {

			@Override
			public void run() {
				doRefreshTabs();
			}
		});
	}

	private void doRefreshTabs() {
		if (sashContainer != null) {
			sashContainer.refreshTabs();
		}
	}

	/**
	 * If there is an adapter registered against the subclass of MultiPageEditorPart return that. Otherwise, delegate to the
	 * internal editor.
	 *
	 * @copiedfrom org.eclipse.ui.part.MultiPageEditorPart
	 *
	 * @see org.eclipse.ui.part.WorkbenchPart#getAdapter(java.lang.Class)
	 */
	@Override
	public Object getAdapter(Class adapter) {

		// Get the content provider if requested.
		if (ISashWindowsContentProvider.class == adapter) {
			return getContentProvider();
		}

		// Get the content provider if requested.
		if (ISashWindowsContainer.class == adapter) {
			return sashContainer;
		}

		// Look in hierarchy
		Object result = super.getAdapter(adapter);

		if (result == null) {
			IEditorPart innerEditor = getActiveEditor();
			// see bug 138823 - prevent some subclasses from causing
			// an infinite loop
			if (innerEditor != null && innerEditor != this) {
				result = PlatformHelper.getAdapter(innerEditor, adapter);
			}
		}
		return result;
	}

	/**
	 * Needed by MultiPageActionBarContributor and MultiPageSelectionProvider.
	 */
	@Override
	public IEditorPart getActiveEditor() {
		if ((sashContainer == null) || sashContainer.isDisposed()) {
			return null;
		}

		return sashContainer.getActiveEditor();
	}

	/**
	 * Get the {@link ISashWindowsContainer}.
	 * Note the the ISashWindowsContainer can also be acuired with getAdapter(ISashWindowsContainer.class).
	 */
	public ISashWindowsContainer getISashWindowsContainer() {
		return sashContainer;
	}

	/**
	 *
	 * {@inheritDoc}
	 */
	@Override
	public void setFocus() {
		sashContainer.setFocus();
	}

}

Back to the top