Customizations

In this chapter we will describe how EMF Parsley let you customize the standard behaviours. A DSL is provided to easily customize most common feature, but you can customize all aspect by doing manually. As a matter of fact each customization is explained in a single section, with the details on the DSL (if available) and the class for overriding by Java.

As we hinted in the Overview, if you want to provide a specific implementation via Java, you can use the Google Guice injection mechanism, but overriding the specific class with your own implementation. Note that an explicit contructor is needed, with the @Inject annotation to make Guice correctly works.

Providers

Viewer Label Provider

The Label Prorvider as in JFace allows to specify the representation of a given Object. EMF Parsley provides an implementation that uses the information provided via the DSL, as you can see in the snippet below.

labelProvider{
    text{
        Book -> "Book:"+title
        Borrower -> "Borrower: "+firstName
    }
    image{
        Book -> "book.png"
    }
}

However if you wnat to use your own implementation, you can do that by providing a specific implementation of ILabelProvider and injecting it in the spefic module (TODO). EMF Parsley provides such an implementation with the class ViewerLabelProvider (src) that is inteded to be surclassed by the programmer to provides specific implementations like in the example below.

public class CustomLibraryLabelProvider extends ViewerLabelProvider {

    @Inject
    public CustomLibraryLabelProvider(AdapterFactoryLabelProvider delegate) {
        super(delegate);
    }

    public String text(Book book) {
        return "Book: " + book.getTitle();
    }

    public String image(Book book) {
        return "book.png";
    }

    public String text(Borrower b) {
        return "Borrower: " + b.getFirstName();
    }
}

Viewer Content Provider

As in Jface, the Content Provider is used to get the elements to represent and for tree their children. EMF Parsley provides an implementation that uses the DSL as in the code below.

viewerContentProvider{
    elements{
        Library -> books
    }
    children{
        Library -> books
        Book b-> {
            new ArrayList()=>[
                add(b.author)
                addAll(b.borrowers)
            ]
        }
    }
}

The developer can also provide a specific implementation of IContentProvider by injecting it in the spefic module (TODO). EMF Parsley provides an implementation with the class ViewerContentProvider (src) that can be easily used to specify the children of all object on the tree, like in the example below.

public class CustomLibraryViewerContentProvider extends ViewerContentProvider {

    @Inject
    public CustomLibraryViewerContentProvider(AdapterFactory adapterFactory) {
        super(adapterFactory);
    }

     public Object elements(Library library) {
        return library.getBooks();
      }

    public Object children(Library library) {
        return library.getBooks();
    }

    public Object children(Book book) {
        ArrayList<Object> children = new ArrayList<Object>();
        Writer author = book.getAuthor();
        if (author != null) {
            children.add(author);
        }
        children.addAll(book.getBorrowers());
        return children;
    }
}

Features Provider

EMF Parsley uses thi kinf of provider wherever a list of feature is requested for a certain EClass. The default is to return the list of all the features in the EClass, but the programmer can customize it (for instance, by returning only a superset, or using a different order) on an EClass-based strategy. Thus you can use the DSL to specify that list, as in the snipped below.

featuresProvider{
    features{
        Book -> title, author, category, pages         
    }
}

If we look at Java, there are more chances to customize this behaviour, but we need to go deep in some details of the Feature Provider implementation.

When the framework builds components according to the EStructuralFeature (src)s of a given EClass (src) it relies on an injected FeaturesProvider (src). The default behavior is to simply return all the features of the a given EClass, in the order they are defined in the EClass, as implemented by the method defaultFeatures in FeaturesProvider (src).

You can set the mappings, i.e., specify the structural features you want to be used given an EClass, by implementing the method buildMap, which receives the FeaturesProvider.EClassToEStructuralFeatureMap (src) that can be filled with the method mapTo; for instance, using the EMF extended library example, this customization will return only the name and address features for Library, the firstName, lastName and address for Person, and the firstName, lastName and books (but not address) for Writer (which inherits from Person).

import static org.eclipse.emf.examples.extlibrary.EXTLibraryPackage.Literals.*;
import org.eclipse.emf.parsley.ui.provider.EStructuralFeaturesProvider;

public class LibraryEStructuralFeaturesProvider extends
        FeaturesProvider {

    @Override
    protected void buildMap(EClassToEStructuralFeatureMap map) {
        super.buildMap(map);
        map.mapTo(LIBRARY,
                LIBRARY__NAME, ADDRESSABLE__ADDRESS);
        map.mapTo(PERSON, PERSON__FIRST_NAME, PERSON__LAST_NAME, ADDRESSABLE__ADDRESS);
        map.mapTo(WRITER, PERSON__FIRST_NAME, PERSON__LAST_NAME, WRITER__BOOKS);
    }
}

Another possibility is to build a map which relies on Strings both for the EClass (src) and for the list of EStructuralFeature (src); note that the name of the EClass (src) should be obtained by using getInstanceClassName(); you can also combine the two approaches.

Table Features Provider

As an extension, you can use the TableFeaturesProvider (src): the customizations will be applied only to Tables, not to Forms.

Feature Caption Provider

The FeatureCaptionProvider (src) provides captions for the features in Tables and Forms. Here you can see an example of the DSL.

featureCaptionProvider{
    text{
        Book:author -> "Wrote by:"
        Writer:name -> "Name:"
    }
}

The FeatureCaptionProvider (src) provides captions for the features. It can be customized, with injection (see Injection paragraph), to customize the caption label on the left of each control in a form and the headers in a table's columns. The framework use a polimorphic mechanism to find customizations, so that It can be written a method with a specific signature build by the keyword 'text' followed by the EClass and the EStructuralFeature. All parts of the name are separated by an underscore character and the method must accept a parameter of type EStructuralFeature.

In the following example we specify the caption text for the feature 'Author' of Book and the feature 'Name' for Writer.

public String text_Book_author(final EStructuralFeature feature) {
    return "Wrote by:";
}

public String text_Writer_name(final EStructuralFeature feature) {
    return "Name:";
}

Form Feature Caption Provider

The FormFeatureCaptionProvider (src) can be used if you want to define the description only for the form. For example using the Tree Form your definition will not be used in the tree.

In this case you can also define a method the returns directly the control, like in the example below. In such methods there is another parameter that is the parent composite.

public Label label_Writer_name(Composite parent, EStructuralFeature feature) {
    Label label = defaultLabel(parent, feature);
    label.setBackground(getFormToolkit().getColors().getColor(IFormColors.TITLE));
    return label;
}

Proposal Provider

Some controls use a list of proposal to help the end user experince: for example the combo box has a list of proposal, but also the simple text can use the proposal to assist and correct the hand-writed values. For each feature it can be specified a list of proposals via the DSl.

proposals{
    Book:author -> {
        defaultProposals(feature).
            filter(Writer).
            filter[name.startsWith("F")].toList
    }
}

This customization can be done also in Java, by extending the class ProposalCreator (src) and implementing the method public List<?> proposals_Book_author(Book book) {...}

Contextual Menu

Contextual menu can be added to any viewer by usinf the ViewerInitializer (src).

@Inject ViewerInitializer viewerInitializer;
(...)

treeActionBarContributor.initialize(editingDomain);
viewerInitializer.addContextMenu(treeFormComposite.getViewer(),
treeActionBarContributor, editingDomain, this);
treeFormComposite.getViewer().addSelectionChangedListener(treeActionBarContributor);

Menu Builder

EMF Parsley logically separate the menu into 2 parts. The first section contains all common edit commands such as copy and paste. The second section regards EMF specific commands, such as for example new child. You can use the DSL to fully customize the menu, as in the example below.

menuBuilder{
    menus{
        Library-> #[
            submenu("Edit",#[
                actionCopy,
                actionCut,
                separator,
                actionPaste
            ])
        ]
    }
    emfMenus{
        Library -> #[
            actionAdd("Add a new book", books,
                EXTLibraryFactory.eINSTANCE.createBook => [
                    title="new book"
                ]
            )
        ]
    }
}

You can customize menu also via Java, by extending the EditingMenuBuilder (src).

Factories

Form Control Factory

EMF Parsley let you customize the form controls via the DSL as in the following example.

formControlFactory {
control {
    Library : name -> { }
    Writer : books -> 
        createLabel(
            books.map[title].join(", "))
    Writer : name -> { createLabel(parent, "") }
        target { observeText }
    Writer : firstName -> 
        toolkit.createLabel(parent, "")
        target observeText(SWT::Modify)
    Borrower : firstName -> {
        createText(firstName, SWT::MULTI, SWT::BORDER,
                            SWT::WRAP, SWT::V_SCROLL)
    }
}
}

If you want to customize the controls on the detail via Java, you can extend the class FormControlFactory (src). Using the same polimorphic mechanism of the labels, the programmer can write a method with the keyword 'control' followed by the EClass and EStructuralFeature undescore-character-separated. In the signature of the method must be both the DataBinding Context and the Feature Observable that can be used for databinding.

public Control control_Writer_name(DataBindingContext dbc,IObservableValue featureObservable) {
    //Creating the control
    Text text = getToolkit().createText(getParent(), "");
    text.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TREE_BORDER);
    text.setBackground(getToolkit().getColors().getColor(IFormColors.TITLE));
    //Binding the control to the feature observable
    dbc.bindValue(SWTObservables.observeText(text, SWT.Modify),    featureObservable);
    return text;
}

Dialog Control Factory

If you want to customize controls in Dialog, you can use the specific DSL section dialogControlFactory:

dialogControlFactory {
    control {
        ...
    }
}