Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 357632

Summary: Add EMF.Edit / EMF.Editor support for property sheet
Product: [Modeling] Graphiti Reporter: Ivar Refsdal <refsdal.ivar>
Component: CoreAssignee: Project Inbox <graphiti-inbox>
Status: CLOSED DUPLICATE QA Contact:
Severity: enhancement    
Priority: P3 CC: michael.wenz
Version: 0.8.0   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:

Description Ivar Refsdal CLA 2011-09-14 09:45:34 EDT
Build Identifier: 

Currently one needs to code the property sheet by hand.
But this can already be generated by the ecore model's .genmodel, and the result is a well functioning editor.

However, inserting the property sheet is a little tricky (personally I had to learn about Emf.Edit, Emf.Editor, extension points, etc, etc.). One needs to edit DiagramEditorInternal and modify the getAdapter() function.
The classes return from this function must also override selectionChanged and removing the graphical objects, only returning the domain objects.

Example code for getAdapter:
@SuppressWarnings("restriction")
	@Override
	public Object getAdapter(@SuppressWarnings("rawtypes") Class type) {
		// existing code here...
		if (type == IPropertySheetPage.class) {
				AdapterFactoryEditingDomain ed = (AdapterFactoryEditingDomain) getEditingDomain();
				GraphitiExtendedPropertySheetPage propertySheetPage = new GraphitiExtendedPropertySheetPage(ed);
				propertySheetPage.setPropertySourceProvider(new AdapterFactoryContentProvider(ed.getAdapterFactory()));
				return propertySheetPage;
		}
		return super.getAdapter(type);
	}

and for the custom propertysheet class:
public class GraphitiExtendedPropertySheetPage extends ExtendedPropertySheetPage {

	public GraphitiExtendedPropertySheetPage(AdapterFactoryEditingDomain editingDomain) {
		super(editingDomain);
	}

	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection sel) {

		validation: {

			if (!(sel instanceof IStructuredSelection)) {
				break validation;
			}

			StructuredSelection selection = (StructuredSelection) sel;
			PictogramElement obj = getSelectedPictogramElement(selection);

			if (!(obj instanceof PictogramElement)) {
				break validation;
			}

			PictogramElement pictogramElement = (PictogramElement) obj;
			EObject businessObject = Graphiti.getLinkService().getBusinessObjectForLinkedPictogramElement(
					pictogramElement);
			if (businessObject == null) {
				break validation;
			}

			Object[] objects = new Object[] { businessObject };
			sel = new TreeSelection(new TreePath(objects));
		}

	super.selectionChanged(part, sel);
	}

	/**
	 * Copied, slightly modified.
	 * 
	 * @return the selected pictogram element.
	 */
	protected PictogramElement getSelectedPictogramElement(StructuredSelection selection) {

			Object firstElement = selection.getFirstElement();

			if (firstElement instanceof PictogramElement) {
				return (PictogramElement) firstElement;
			}

			EditPart editPart = null;
			if (firstElement instanceof EditPart) {
				editPart = (EditPart) firstElement;
			} else if (firstElement instanceof IAdaptable) {
				editPart = (EditPart) ((IAdaptable) firstElement).getAdapter(EditPart.class);
			}
			if (editPart != null && editPart.getModel() instanceof PictogramElement) {
				return (PictogramElement) editPart.getModel();
			}
		return null;
	}

See getPropertySheetPage() XXXEditor.java in the generated Emf.Editor package for a starting point.

Thanks.

Reproducible: Always
Comment 1 Ivar Refsdal CLA 2011-09-14 10:05:56 EDT
Could this be considered a duplicate of https://bugs.eclipse.org/bugs/show_bug.cgi?id=341898 ?
Comment 2 Michael Wenz CLA 2011-09-15 07:15:32 EDT
Not exactly a duplicate, but it targets more or less the same thing and it needs to be aligned in any case, so I would like to merge the two bugs.

Here we have more details on embedding generated EMF property sheets while https://bugs.eclipse.org/bugs/show_bug.cgi?id=357632 is more general.

*** This bug has been marked as a duplicate of bug 341898 ***