Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aa14f4fefb79e288fe66f08aa5b53ca647cac3e3 (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
/*****************************************************************************
 * 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 - bug 469188
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.core.sasheditor.internal;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.papyrus.infra.core.sasheditor.editor.ICloseablePart;
import org.eclipse.papyrus.infra.core.sasheditor.editor.IPage;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Widget;

/**
 * This class represent a leaf part that is a part containing an editor or a component.
 *
 * @author dumoulin
 */
public abstract class PagePart extends AbstractPart implements IPage, IAdaptable {

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

	/** Parent part of this Part */
	protected TabFolderPart parent;


	/**
	 * Constructor.
	 *
	 * @param sashWindowsContainer
	 */
	public PagePart(TabFolderPart parent, Object rawModel) {
		super(parent.getSashWindowContainer());
		this.parent = parent;
		this.rawModel = rawModel;
	}

	/**
	 * @since 2.0
	 */
	@Override
	public <T> T getAdapter(Class<T> adapter) {
		return Platform.getAdapterManager().getAdapter(this, adapter);
	}

	/**
	 * @return the parent
	 */
	protected TabFolderPart getParent() {
		return parent;
	}


	/**
	 * Get the {@link TabItemPart} that is associated to this Page. <br>
	 * The {@link TabItemPart} represents the tab in the folder.
	 *
	 * @return
	 */
	protected TabItemPart getAssociatedTabItemPart() {

		// TabItemPart is not directly accessible. We should ask it to
		// the parent.
		return getParent().lookupAssociatedTabItemPart(this);
	}

	/**
	 * Orphan this node. The parent is set to null, but control is left unchanged.
	 * The node can be reattached with reparent(). Change garbage state to {@link GarbageState.ORPHANED}.
	 * This method as no effect if the Page has already been reparented.
	 *
	 * @see
	 * @return the parent
	 */
	public void orphan() {
		// orphan only if we are in COLLECTED state
		if (garbageState == GarbageState.UNVISITED) {
			garbageState = GarbageState.ORPHANED;
			parent = null;
		}
	}

	/**
	 * Mark this Page as UNCHANGED.
	 * The PAge should be in the COLLECTED state.
	 *
	 * @see
	 * @return the parent
	 */
	public void unchanged() {
		// orphan only if we are in COLLECTED state
		if (garbageState == GarbageState.UNVISITED || garbageState == GarbageState.ORPHANED) {
			garbageState = GarbageState.UNCHANGED;
		} else {
			// Bad state, this is an internal error
			// TODO : log a warning ?
			throw new IllegalStateException("Try to change state from " + garbageState.toString() + " to UNCHANGED. This is forbidden."); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	/**
	 * Visit this part.
	 *
	 * @param visitor
	 * @return true if the visit should continue, false otherwise.
	 */
	abstract boolean visit(IPartVisitor visitor);

	/**
	 * Locates the part that intersects the given point and that have the expected type.
	 * For a leaf, return the leaf if it is of the expected type.
	 *
	 * @param position
	 * @param expectedTileType
	 * @return
	 */
	public AbstractPart findPartAt(Point position, Class<?> expectedTileType) {

		if (expectedTileType == this.getClass()) {
			return this;
		}

		return null;
	}

	/**
	 * Create the control of this Part, and children's controls.
	 *
	 * @param parent
	 */
	public abstract void createPartControl(Composite parent);

	/**
	 * Get the control associated to this Part.
	 *
	 * @return
	 */
	@Override
	public abstract Control getControl();

	/**
	 * reparent this Part with the specified new parent.
	 * The part is marked as reparented.
	 *
	 * @param parent
	 */
	public abstract void reparent(TabFolderPart parent);

	/**
	 * Return the {@link Widget} of the tab associated to this page.
	 *
	 * @return The {@link Widget} of the tab.
	 */
	public Widget getTabWidget() {
		return getAssociatedTabItemPart().control;
	}

	/**
	 * Add the tree of parts starting from this part.
	 * As we are a leaf, add itself only.
	 *
	 * @param partMap
	 */
	public void fillPartMap(PartLists partMap) {
		partMap.addLeaf(this);
		garbageState = GarbageState.UNVISITED;
	}

	/**
	 * Get the raw model associated to this Part.
	 *
	 * @return
	 */
	@Override
	public Object getRawModel() {
		return rawModel;
	}

	/**
	 * Return a title for this part. This title can be used by parent to be shown
	 * in tabs ...
	 * To be implemented by subclasses.
	 *
	 * @return The title or null.
	 */
	@Override
	public String getPageTitle() {
		return null;
	}

	/**
	 * Return a icon for this part. This title can be used by parent to be shown
	 * in tabs ...
	 * To be implemented by subclasses.
	 *
	 * @return The icon or null.
	 */
	@Override
	public Image getPageIcon() {
		return null;
	}

	/**
	 * Refresh the tab of this page (I.e the name and icon in the tab).
	 */
	public void refreshTab() {
		getParent().refreshPageTab(this);
	}

	/**
	 * Queries whether I should be permitted to be closed.
	 * 
	 * @return whether my containing tab should show the close widget
	 * @since 2.0
	 */
	public boolean canClose() {
		ICloseablePart closeable = getAdapter(ICloseablePart.class);
		return (closeable == null)
				|| (closeable == this) // Avoid unbounded re-entry into this method!
				|| closeable.canClose();
	}

	/**
	 * Set focus on the SWT control associated to this PagePart.
	 * Used by the ActivePageTracker.
	 */
	public abstract void setFocus();

	/**
	 * Dispose the part. <br/>
	 * The part and its associated resource are disposed.
	 *
	 */
	public abstract void dispose();

	/**
	 * Dispose this part and all its children.
	 * The method is called recursively on children of the part.
	 */
	public abstract void disposeThisAndChildren();

	/**
	 * Garbage the part. <br/>
	 * This method is called by the sashwindows garbage mechanism. It means that the part has been marked as "garbage" or
	 * is now unreachable. It is no longer used by the {@link SashWindowsContainer}, and it should be garbage. <br/>
	 * All resources used by this node should be reclaimed, but not its subnodes.
	 *
	 */
	public abstract void garbage();

	/**
	 * Return true if the part is associated to the specified rawModel.
	 * Return false otherwise.
	 *
	 * @param realModel
	 * @return
	 */
	public boolean isPartFor(Object realModel) {
		return this.rawModel == realModel;
	}



}

Back to the top