Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3dd300d014319718f0b60eedea651df9e35e96ba (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
317
318
319
320
321
322
323
324
/*****************************************************************************
 * Copyright (c) 2009 CEA LIST & LIFL 
 *
 *    
 * 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
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.core.sasheditor.editor;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.NullProgressMonitor;
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.eclipsecopy.MultiPageSelectionProvider;
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.PartInitException;
import org.eclipse.ui.internal.util.Util;
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 pageProvider */
	private ISashWindowsContentProvider pageProvider;

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

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

	/**
	 * 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);
		site.setSelectionProvider(new MultiPageSelectionProvider(this));
	}

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

		// Create and intialize sash windows
		sashContainer = new SashWindowsContainer(this);
		sashContainer.setContentProvider(getContentProvider());
		sashContainer.createPartControl(parent);
		activate();
	}

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

		tabsSynchronizer = new SashTabDecorationSynchronizer(sashContainer);
	}

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

		tabsSynchronizer.dispose();
	}

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

	/**
	 * Refresh the sash windows system
	 */
	protected void refreshTabs() {
		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);
		// restrict delegating to the UI thread for bug 144851
		if(result == null && Display.getCurrent() != null) {
			IEditorPart innerEditor = getActiveEditor();
			// see bug 138823 - prevent some subclasses from causing
			// an infinite loop
			if(innerEditor != null && innerEditor != this) {
				result = Util.getAdapter(innerEditor, adapter);
			}
		}
		return result;
	}

	/**
	 * Needed by MultiPageActionBarContributor and MultiPageSelectionProvider.
	 */
	public IEditorPart getActiveEditor() {
		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();

	}

	/**
	 * Overrides isDirty.
	 * 
	 * {@inheritDoc}
	 * 
	 * TODO Move this method aways. This method is too tightly coupled to the
	 * Papyrus GMF UML IEditor. It doesn't work on other kind of IEditor. It
	 * introduce problems in IEditor of other kinds
	 * 
	 * @see org.eclipse.papyrus.infra.core.sasheditor.editor.ISashWindowsContainer#isDirty()
	 * @deprecated A SaveAndDirtyService is used instead.
	 */
	@Override
	public boolean isDirty() {
		// return sashContainer.isDirty();
		EditorVisitor visitor = new EditorVisitor();
		sashContainer.visit(visitor);

		for(IEditorPart editorPart : visitor.getPages()) {
			if(editorPart.isDirty()) {
				return true;
			}
		}

		return false;
	}

	/**
	 * Notify all the editors that the multi editor has been saved.<BR>
	 * Fires the PROP_DIRTY property change.
	 * 
	 * TODO Move this method aways. This method is too tightly coupled to the
	 * Papyrus GMF UML IEditor. It doesn't work on other kind of IEditor. It
	 * introduce problems in IEditor of other kinds
	 * 
	 * @deprecated A SaveAndDirtyService is used instead.
	 */
	protected void markSaveLocation() {
		// return sashContainer.isDirty();
		EditorVisitor visitor = new EditorVisitor();
		sashContainer.visit(visitor);

		for(IEditorPart editorPart : visitor.getPages()) {
			editorPart.doSave(new NullProgressMonitor());
		}
		firePropertyChange(PROP_DIRTY);
	}

	/**
	 * A visitor allowing to collect the available IEditor. TODO : Remove
	 * 
	 * @author dumoulin
	 * 
	 */
	protected class EditorVisitor implements IPageVisitor {

		private List<IEditorPart> pages = new ArrayList<IEditorPart>();

		/**
		 * Get collected pages.
		 * 
		 * @return
		 */
		public List<IEditorPart> getPages() {
			return pages;
		}

		/**
		 * 
		 */
		public void accept(IComponentPage page) {
			// Do nothing
		}

		/**
		 * 
		 */
		public void accept(IEditorPage page) {

			IEditorPart editor = page.getIEditorPart();
			pages.add(editor);
		}
	}
}

Back to the top