Skip to main content
summaryrefslogtreecommitdiffstats
blob: 88ea64e0ecac9ac5accd49df0c1d1cfe1f5894bc (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
/*****************************************************************************
 * Copyright (c) 2010 CEA LIST.
 *    
 * 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:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.views.properties.tabbed.core.view;

import java.util.List;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.papyrus.views.properties.runtime.Activator;
import org.eclipse.papyrus.views.properties.runtime.view.IConfigurableDescriptor;
import org.eclipse.papyrus.views.properties.runtime.view.IFragmentDescriptor;
import org.eclipse.papyrus.views.properties.runtime.view.constraints.IConstraintDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.AbstractSectionDescriptor;
import org.eclipse.ui.views.properties.tabbed.ISection;
import org.w3c.dom.Node;


/**
 * Descriptor for sections using controllers.
 */
public class DynamicSectionDescriptor extends AbstractSectionDescriptor implements IEnhancedFilter, IConfigurableDescriptor {

	/** semantic resolver */
	public static final String SEMANTIC_RESOLVER = "Semantic";

	/** id of the graphic resolver */
	public static final String GRAPHIC_RESOLVER = "Graphic";

	/** id of the edit part resolver */
	public static final String EDIT_PART_RESOLVER = "EditPart";

	/** section class managing the content */
	protected ISection section;

	/** id of the section */
	protected final String id;

	/** id of the tab contributed */
	protected final String tabId;

	/** stores the unparsed configuration, waiting for the section to be used */
	protected Node unparsedSectionNode;

	/** indicates if the section has already been parsed */
	protected boolean unparsed = true;

	/** indicates if the parse was correct */
	protected boolean parseSectionFailed = false;

	/** list of constraints on this element. This could be OCL, java constraints for example */
	protected final List<IConstraintDescriptor> constraints;

	/** size of selection */
	protected final int selectionSize;

	/** object adapter id */
	protected final String adapterId;

	/** list of replaced ids */
	protected final List<String> replacedSectionIds;

	/** section which should be before this one */
	protected final String afterSectionId;

	/** list of fragments to display in the section */
	protected final List<IFragmentDescriptor> fragmentDescriptors;



	/**
	 * Creates a new DynamicSectionDescriptor.
	 * 
	 * @param id
	 *        unique identifier of this section descriptor
	 * @param tabId
	 *        id of the tab where this section should be added
	 * @param constraints
	 *        the constraints placed on this section, so it should be displayed or not
	 * @param selectionSize
	 *        size of the selection
	 * @param adapterID
	 *        name of the adapter
	 * @param replacedSectionIds
	 *        id of the section replaced by this one
	 * @param afterSectionId
	 *        identifier of the section which should be placed before this one
	 * @param fragmentDescriptors
	 *        fragments to display in the section
	 */
	public DynamicSectionDescriptor(String id, String tabId, List<IConstraintDescriptor> constraints, int selectionSize, String adapterID, List<String> replacedSectionIds, String afterSectionId, List<IFragmentDescriptor> fragmentDescriptors) {
		this.id = id;
		this.tabId = tabId;
		this.constraints = constraints;
		this.selectionSize = selectionSize;
		this.adapterId = adapterID;
		this.replacedSectionIds = replacedSectionIds;
		this.afterSectionId = afterSectionId;
		this.fragmentDescriptors = fragmentDescriptors;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int getEnablesFor() {
		return selectionSize;
	}

	/**
	 * {@inheritDoc}
	 */
	public String getId() {
		return id;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getAfterSection() {
		if(afterSectionId == null) {
			return super.getAfterSection();
		}
		return afterSectionId;
	}

	/**
	 * Returns the adapterId for this section descriptor
	 * 
	 * @return the adapterId for this section descriptor
	 */
	public String getAdapterId() {
		return adapterId;
	}

	/**
	 * {@inheritDoc}
	 */
	public ISection getSectionClass() {
		return new DynamicSection(fragmentDescriptors, getAdapterId());
	}


	/**
	 * {@inheritDoc}
	 */
	public String getTargetTab() {
		return tabId;
	}

	/**
	 * Sets the unparsed content for the descriptor.
	 * 
	 * @param sectionNode
	 *        the node configuring the described section.
	 */
	public void setUnparsedContent(Node sectionNode) {
		this.unparsedSectionNode = sectionNode;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public IEnhancedFilter getFilter() {
		return this;
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean select(Object toTest) {
		return SectionDispatcher.getInstance().isSectionDisplayed(this, toTest);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean appliesTo(IWorkbenchPart part, ISelection selection) {
		// check all 
		List<DynamicSectionDescriptor> filteredDescriptors = SectionDispatcher.getInstance().getDisplayedSections(part, selection);

		// now, compare the current descriptor. Is it in the list of filtered descriptors ?
		return filteredDescriptors.contains(this);
	}

	/**
	 * Determines if this section applies to the selection, without inheritance issue. The section, even if overriden by another visible section, will
	 * return <code>true</code>
	 * 
	 * @param part
	 *        the current workbench part.
	 * @param selection
	 *        the selection.
	 * @return <code>true</code> if this section applies to the current
	 *         selection.
	 */
	public boolean appliesToWithoutSectionInheritance(IWorkbenchPart part, ISelection selection) {
		return super.appliesTo(part, selection);
	}

	/**
	 * Returns the constraints
	 * 
	 * @return the constraints
	 */
	public List<IConstraintDescriptor> getConstraints() {
		return constraints;
	}

	/**
	 * Returns the object adapated to the property view (ex: edit part into the underlying model element
	 * 
	 * @param objectToAdapt
	 *        the object to adapt
	 * @return the object adapted to the property view
	 */
	public Object getAdaptedObject(Object objectToAdapt) {
		if(SEMANTIC_RESOLVER.equals(adapterId)) {
			return resolveSemanticElement(objectToAdapt);
		}
		if(GRAPHIC_RESOLVER.equals(adapterId)) {
			return resolveGraphicElement(objectToAdapt);
		}
		if(EDIT_PART_RESOLVER.equals(adapterId)) {
			return resolveEditPart(objectToAdapt);
		}
		return objectToAdapt;
	}

	/**
	 * Resolves the semantic element for the specified object
	 * 
	 * @param objectToAdapt
	 *        the object to retrieve
	 * @return the semantic element for the specified object
	 */
	protected EObject resolveSemanticElement(Object objectToAdapt) {
		if(objectToAdapt instanceof EObject) {
			return (EObject)objectToAdapt;
		} else if(objectToAdapt instanceof IAdaptable) {
			IAdaptable adaptable = (IAdaptable)objectToAdapt;
			if(adaptable.getAdapter(EObject.class) != null) {
				return (EObject)adaptable.getAdapter(EObject.class);
			}
		}
		return null;
	}

	/**
	 * Resolves the edit part for the specified object
	 * 
	 * @param objectToAdapt
	 *        the object to retrieve
	 * @return the edit part for the specified object
	 */
	protected EditPart resolveEditPart(Object objectToAdapt) {
		if(objectToAdapt instanceof EditPart) {
			return (EditPart)objectToAdapt;
		} else if(objectToAdapt instanceof IAdaptable) {
			IAdaptable adaptable = (IAdaptable)objectToAdapt;
			if(adaptable.getAdapter(EditPart.class) != null) {
				return (EditPart)adaptable.getAdapter(EditPart.class);
			}
		}
		return null;
	}

	/**
	 * Resolves the semantic element for the specified object
	 * 
	 * @param objectToAdapt
	 *        the object to retrieve
	 * @return the semantic element for the specified object
	 */
	protected View resolveGraphicElement(Object objectToAdapt) {
		if(objectToAdapt instanceof View) {
			return (View)objectToAdapt;
		} else if(objectToAdapt instanceof IAdaptable) {
			IAdaptable adaptable = (IAdaptable)objectToAdapt;
			if(adaptable.getAdapter(View.class) != null) {
				return (View)adaptable.getAdapter(View.class);
			}
		}
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean selectWithoutVisibility(Object objectToTest) {
		// all constraints provided by the section descriptor should be valid.
		List<IConstraintDescriptor> constraintDescriptors = getConstraints();
		if(constraintDescriptors == null || constraintDescriptors.isEmpty()) {
			// something went wrong during definition or parsing, ignore this section
			Activator.log.warn("No constraints found for descriptor : " + this.getText());
			return false;
		}

		// adapt the selection using adapters provided by the section descriptor
		Object adaptedObject = getAdaptedObject(objectToTest);

		for(IConstraintDescriptor constraintDescriptor : constraintDescriptors) {
			if(!constraintDescriptor.select(adaptedObject)) {
				return false;
			}
		}

		// then, is this section in the hidden views (preferences ?)for this kind of object ?
		return true;
	}


	/**
	 * Returns the replacedSectionIds
	 * 
	 * @return the replacedSectionIds
	 */
	public List<String> getReplacedSectionIds() {
		return replacedSectionIds;
	}

	/**
	 * {@inheritDoc}
	 */
	public String getText() {
		return "Section: " + getId() + " in tab: " + getTargetTab();
	}

	/**
	 * {@inheritDoc}
	 */
	public Image getImage() {
		return org.eclipse.papyrus.views.properties.tabbed.core.Activator.getImage("/icons/Section.gif");
	}

	/**
	 * {@inheritDoc}
	 */
	public SectionDescriptorState createState(boolean readOnly) {
		return new SectionDescriptorState(this, readOnly);
	}

	/**
	 * Returns the list of fragment descriptors for this section
	 * 
	 * @return the list of fragment descriptors for this section
	 */
	public List<IFragmentDescriptor> getFragmentDescriptors() {
		return fragmentDescriptors;
	}
}

Back to the top