Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5cf339c79e20f18f333e0ea44c7ac4fec26e6b3b (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*****************************************************************************
 * 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 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.multipage.editor;

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

import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.papyrus.infra.core.sasheditor.Activator;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IAbstractPanelModel;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IComponentModel;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IEditorModel;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IPageModel;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.ISashPanelModel;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.ISashWindowsContentProvider;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.ITabFolderModel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.MultiPageEditorPart;

/**
 * A MultiPageEditor implementation accepting IMultiSashContentProvider as content provider.
 * This implementation extends the regular eclipse MultiPageEditorPart.
 * The refresh() method allows to refresh the tabs.
 *
 * The implementation do not listen on model change. This can be done by subclasses.
 * To add a new tab, one should add it as a model in the ContentProvider. The addPage()
 * methods should not be used for this purpose.
 *
 * This implementation is intended for debug and testing purpose : it can be used in place
 * of the AbstractMultiPageSashEditor. It takes the same arguments and ContentProvider, but
 * it only allows one folder and don't deal with multisashes.
 * Also, the implementation use the regular Eclipse EditorSite management. This allows to check for problems from this
 * site management.
 *
 * @author dumoulin
 * @deprecated
 * @since 4.4
 */
@Deprecated
public abstract class MultiPageEditor extends MultiPageEditorPart {

	/** The pageProvider */
	private ISashWindowsContentProvider pageProvider;

	/**
	 * The tabfolder model providing the pages that must be shown.
	 */
	private ITabFolderModel tabFolderModel;

	/** Ordered set of currently shown diagrams */
	protected List<IPageModel> currentTabs = new ArrayList<>();

	/**
	 * Constructor.
	 */
	public MultiPageEditor() {
		super();
	}

	/**
	 * This method is called at the end of createPartControl().
	 * Just intercept the call in order to call activate().
	 * Create the part controls. {@inheritDoc}
	 */
	@Override
	protected void initializePageSwitching() {

		super.initializePageSwitching();
		activate();
	}

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

	}

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

	}

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

	@Override
	public Object getAdapter(Class adapter) {

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

		return super.getAdapter(adapter);
	}

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

		return pageProvider;
	}



	/**
	 * @param pageProvider
	 *            the pageProvider to set
	 */
	protected void setContentProvider(ISashWindowsContentProvider pageProvider) {
		this.pageProvider = pageProvider;
	}

	/**
	 * Add a page containing the Component described by the provided model.
	 *
	 * @param tabItem
	 */
	protected int addPage(IComponentModel tabItem) {
		Composite composite = tabItem.createPartControl(getContainer());

		int index = addPage(composite);
		setPageText(index, tabItem.getTabTitle());
		Image image = tabItem.getTabIcon();
		if (image != null) {
			setPageImage(index, image);
		}
		return index;
	}

	/**
	 * Add the editor corresponding to the model to the folder.
	 *
	 * @param editorModel
	 */
	protected int addPage(IEditorModel editorModel) {
		try {
			IEditorPart editor = editorModel.createIEditorPart();
			int index = addPage(editor, getEditorInput());
			setPageText(index, editorModel.getTabTitle());
			Image image = editorModel.getTabIcon();
			if (image != null) {
				setPageImage(index, image);
			}
			return index;
		} catch (PartInitException e) {
			ErrorDialog.openError(
					getSite().getShell(),
					"Error creating nested text editor",
					null,
					e.getStatus());
			return -1;
		}

	}

	/**
	 * Creates the pages of the multi-page editor.
	 */
	@Override
	protected void createPages() {
		// get the page descriptions
		pageProvider = getContentProvider();
		// Get the current tabFolder
		//
		tabFolderModel = lookupFolder();

		refreshTabs();

		// // iterate over pages to be show
		// for( Object rawPageModel : tabFolderModel.getChildren() )
		// {
		// // Get the model interface
		// ITabItemModel tabItem = tabFolderModel.createChildSashModel( rawPageModel);
		// if(tabItem instanceof IEditorModel )
		// {
		// addPage((IEditorModel)tabItem );
		// }
		// else if(tabItem instanceof IComponentModel )
		// {
		// addPage((IComponentModel)tabItem );
		// }
		// else
		// {
		// System.err.println("Can't create page for model '" + tabItem + "'. Skipp it.");
		// }
		// }
	}

	/**
	 * Lookup for a folder in the SashModel. Return the first folder found.
	 *
	 * @return
	 */
	private ITabFolderModel lookupFolder() {
		if (pageProvider == null) {
			return null;
		}

		Object rawModel = pageProvider.getRootModel();
		IAbstractPanelModel panelModel = pageProvider.createChildSashModel(rawModel);

		return lookupFolder(panelModel);
	}

	/**
	 * Recursively search in sash models for a FolderModel.
	 * Return the first encountered folder.
	 *
	 * @param panelModel
	 * @return
	 */
	private ITabFolderModel lookupFolder(IAbstractPanelModel panelModel) {

		if (panelModel instanceof ITabFolderModel) {
			return (ITabFolderModel) panelModel;
		} else {
			ISashPanelModel sashModel = (ISashPanelModel) panelModel;
			// Iterate on children
			for (Object child : sashModel.getChildren()) {
				IAbstractPanelModel childModel = pageProvider.createChildSashModel(child);
				ITabFolderModel res = lookupFolder(childModel);
				if (res != null) {
					return res;
				}
			}
		}
		// Not found
		return null;
	}

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

	/**
	 * Refresh the tabs order.
	 * This method should be called after the model list is modified.
	 */
	protected void refreshTabs() {
		// get list of diagrams to be displayed
		List<?> newModels = tabFolderModel.getChildren();

		// Check if each model has an open pageEditor. If not, create the editor.
		Iterator<?> newIter = newModels.iterator();
		while (newIter.hasNext()) {
			Object model = newIter.next();
			if (!tabExistsFor(model)) { // create a new editor for the model
				IPageModel pageModel = tabFolderModel.createChildSashModel(model);
				if (pageModel != null) {
					addEditor(pageModel);
				}
			}
		}

		// If open editor count is the same has models count,
		// all models have an editor. So, end the refresh process.
		if (newModels.size() == getPageCount()) {
			return;
		}

		// There is some extra editors ! remove them.
		// remove extra editors : for each open editor, check if its model is in
		// the list of required models.
		List<IPageModel> toBeRemoved = new ArrayList<>();
		Iterator<IPageModel> currentIter = currentTabs.iterator();
		while (currentIter.hasNext()) {
			IPageModel model = currentIter.next();
			if (!newModels.contains(model.getRawModel())) { // remove editor
				toBeRemoved.add(model);
			}
		}

		// Remove editor from the editors displayed by the multi editor
		Iterator<IPageModel> removeIter = toBeRemoved.iterator();
		while (removeIter.hasNext()) {
			IPageModel model = removeIter.next();
			removeEditor(model);
		}
	}

	/**
	 * Check if an editor exists for the specified model.
	 *
	 * @param model
	 *            the diagram (model) that should be displayed
	 * @return <code>true</code> if the editor exists for this model
	 */
	private boolean tabExistsFor(Object model) {
		for (IPageModel tabItem : currentTabs) {
			if (tabItem.getRawModel() == model) {
				return true;
			}
		}
		return false;
	}


	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.ui.part.MultiPageEditorPart#removePage(int)
	 */
	@Override
	public void removePage(int pageIndex) {
		super.removePage(pageIndex);
		// synchronize the list of currently shown models.
		currentTabs.remove(pageIndex);
	}

	/**
	 * Removes the editor associated to the specified model.
	 *
	 * @param model
	 *            the diagram (model) displayed in the editor
	 */
	private void removeEditor(IPageModel model) {
		int index = currentTabs.indexOf(model);
		removePage(index);
	}

	/**
	 * Add a new editor at the end of existing editors.
	 * First, create the editor, then add it to the tabs.
	 *
	 * @param contentProvider
	 *            the diagram (model) to be displayed in the editor
	 */
	private void addEditor(IPageModel tabItem) {
		// Check if an editor already exists
		if (tabExistsFor(tabItem)) {
			if (Activator.log.isDebugEnabled()) {
				Activator.log.debug("Editor already exists for '" + tabItem + "'");
			}
			return;
		}

		int editorIndex = -1;
		if (tabItem instanceof IEditorModel) {
			editorIndex = addPage((IEditorModel) tabItem);
		} else if (tabItem instanceof IComponentModel) {
			editorIndex = addPage((IComponentModel) tabItem);
		} else {
			System.err.println("Can't create page for model '" + tabItem + "'. Skipp it.");
			return;
		}

		// Add the model in the list of current tabs.
		currentTabs.add(tabItem);
		// set active page
		setActivePage(editorIndex);

	}


}

Back to the top