Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1fe3e61b67fd6b99f0cb600a0a15c40f5a27fa56 (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
/*****************************************************************************
 * Copyright (c) 2008 CEA LIST.
 *
 *
 * 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.internal;

import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IAbstractPanelModel;
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.Point;
import org.eclipse.swt.widgets.Composite;

/**
 * Part used as root. This is the one with no parent and no model. This class is intended for local
 * use only.
 *
 * @author dumoulin
 *
 * @param T
 *            Common ancestor for the model provided for the sash windows by the application.
 *            This is the type used externally by the application. Sash implementation don't use this type,
 *            it just carry it to ask for the appropriate wrapper. Concrete implementation can specify
 *            a type.
 */
public class RootPart extends AbstractPart implements IPanelParent {

	/** The SWT container used as parent of all */
	private Composite container;

	/**
	 * The first SWT controled part;
	 */
	private AbstractPanelPart child;

	/** Raw model associated to this part. We store it because the PartModel do not provide it */
	// private Object rawModel;

	/**
	 * Constructor.
	 */
	public RootPart(SashWindowsContainer sashWindowsContainer) {
		super(sashWindowsContainer);
	}

	/**
	 * Create the SWT controls.
	 * This Root as no control. Children are created by the {@link #synchronize2(PartLists)} method.
	 */
	public void createPartControl(Composite parent) {
		this.container = parent;
	}

	/**
	 * Dispose this part and its children.
	 */
	public void disposeThisAndChildren() {
		if (child != null) {
			child.disposeThisAndChildren();
		}

		// Detach properties to help GC
		child = null;
	}

	/**
	 * Get control associated to this part.
	 */
	@Override
	public Composite getControl() {
		return container;
	}


	/**
	 * Create the part for the specified child model.
	 * The controls are also builds.
	 *
	 * TODO: delegate to sashContainer.
	 *
	 * @param rootPart
	 * @param partModel
	 * @return
	 */
	private AbstractPanelPart createChildPart(Object rawModel) {

		// Create the child PartModel. Delegate creation to the root PartModel.
		IAbstractPanelModel model = getPartModel().createChildSashModel(rawModel);

		AbstractPanelPart createdPart;
		if (model instanceof ITabFolderModel) {
			createdPart = new TabFolderPart(this, (ITabFolderModel) model, rawModel);
		} else if (model instanceof ISashPanelModel) {
			createdPart = new SashPanelPart(this, (ISashPanelModel) model, rawModel);
		} else {
			// error
			throw new IllegalArgumentException("Can't create child part for model of type '" //$NON-NLS-1$
					+ model.getClass().getName()
					+ "'"); //$NON-NLS-1$
			// TODO: Return an error Part showing the exception instead of throwing it ?
		}

		// Create controls
		createdPart.createPartControl(getControl());
		return createdPart;

	}

	/**
	 * Get the PartModel associated to this Part.
	 *
	 * @return
	 */
	private ISashWindowsContentProvider getPartModel() {
		// The associated model for a root is the ContentProvider.
		return getSashWindowContainer().getContentProvider();
	}

	/**
	 * Synchronize the part and its children.
	 *
	 * @param partMap
	 */
	public void synchronize2(PartLists partMap) {

		// Synchronize locally the child
		synchronizeChild(partMap);
		// Synchronize recursively subchilds.
		if (child != null) {
			child.synchronize2(partMap);
		}

	}

	/**
	 * Synchronize locally the child
	 *
	 * @param partMap
	 */
	private void synchronizeChild(PartLists partMap) {

		// Get the new raw model
		Object rawModel = getContentProvider().getRootModel();

		// Check if old child exist
		// If exist, check if the current part is associated to the checked model
		//
		if (child != null) {
			// If the tile is already for the model, there is nothing to do.
			if (child.isPartFor(rawModel)) {
				child.unchanged();
				return;
			}
			// The current tile is not for the model: mark it as orphan
			child.orphan();
		}

		// The child tile need to be updated. Do it.
		// First check if already exist in the map
		AbstractPanelPart newPart = partMap.findPartFor(rawModel);
		if (newPart != null) {
			// Reparent the tile
			newPart.reparent(this, getControl());
		} else {
			// create the tile and its control
			newPart = createChildPart(rawModel);
		}

		// Now, put the tile on the right side
		setChild(newPart);
	}

	/**
	 * Set the child. If a child already exist at the specified index, it is lost.
	 *
	 * @param newTile
	 */
	private void setChild(AbstractPanelPart newTile) {
		child = newTile;
	}

	/**
	 * Fill partMap with the children.
	 *
	 * @param partMap
	 */
	public void fillPartMap(PartLists partMap) {
		if (child != null) {
			child.fillPartMap(partMap);
		}

	}

	/**
	 * Find The AbstractPart under the specified position.
	 */
	public AbstractPart findPart(Point toFind) {
		if (child != null) {
			try {
				return child.findPart(toFind);
			} catch (NotFoundException e) {
				System.err.println(e.getMessage());
				return null;
			}
		} else {
			return null;
		}
	}

	// /**
	// * Locates the part that intersects the given point and that have the expected type
	// *
	// * @param toFind
	// * @return
	// */
	// public AbstractPart findPartAt(Point toFind, Class<?> tileType) {
	// return child.findPartAt(toFind, tileType);
	// }

	/**
	 *
	 */
	public AbstractPart findPart(Object control) {
		if (child != null) {
			return child.findPart(control);
		} else {
			return null;
		}
	}

	// /**
	// * @see org.eclipse.papyrus.infra.core.sasheditor.sash.ITilePart#getDropTarget(java.lang.Object, org.eclipse.papyrus.infra.core.sasheditor.sash.TabFolderPart, org.eclipse.swt.graphics.Point)
	// */
	// public IDropTarget getDropTarget(Object draggedObject, TabFolderPart sourcePart, Point position) {
	// return child.getDropTarget(draggedObject, sourcePart, position);
	// }

	/**
	 * Do nothing. This node can't be orphaned
	 *
	 */
	public void orphan() {
		// Do nothing. This node can't be orphaned
	}

	/**
	 * Always return false. This Part can't be orphaned.
	 *
	 * @see org.eclipse.papyrus.infra.core.sasheditor.internal.AbstractPart#isOrphaned()
	 *
	 * @return
	 */
	@Override
	public boolean isOrphaned() {
		return false;
	}

	/**
	 * Get the Garbage state.
	 *
	 * @see org.eclipse.papyrus.infra.core.sasheditor.internal.AbstractPart#getGarbageState()
	 *
	 * @return
	 */
	@Override
	public GarbageState getGarbageState() {
		return GarbageState.UNVISITED;
	}

	/**
	 * Accept the provided visitor.
	 * Call the corresponding accept method in the visitor.
	 *
	 * @param visitor
	 * @return
	 */
	public boolean visit(IPartVisitor visitor) {
		return visitor.accept(this);
	}

	/**
	 * Visit the children of this Tile.
	 *
	 * @param visitor
	 */
	public boolean visitChildren(IPartVisitor visitor) {
		if (child != null) {
			return child.visit(visitor);
		}

		// Return the default value
		return true;
	}

	/**
	 * Show tile status.
	 */
	protected void showStatus() {
		org.eclipse.papyrus.infra.core.sasheditor.Activator.log.debug("rootPart (1)" //$NON-NLS-1$
				+ ", disposed=" + container.isDisposed() //$NON-NLS-1$
				+ ", visible=" + container.isVisible() //$NON-NLS-1$
				+ ", " + this); //$NON-NLS-1$
	}


}

Back to the top