We had EMF generated model classes and a Util class with lot of helper methods. Both were exposed to clients as an API. Most of the methods in the Util classes were like:
//API - Util class
Book getBook(Writer writer, String title);
//Customer code
Util.getBook(daveSteinberg, "Eclipse Modeling Framework");
These methods would have been more appropriate in the Writer class itself rather than in a Util class, and it will be more natural to code like:
daveSteinberg.getBook("Eclipse Modeling Framework");
This tip is about how to add such methods in the generated code.
The first way is simple. Just edit the generated java code and add these methods. EMF is smart enough to identify this method and keeps it safe during regeneration of code. But if you were like me, who consider the generated Java files are as good as class files and don't want them checked into the repository, you can follow the second way.
In the eCore Editor for your model, right click the EClass and add a new child EOperation.
Go to the Properties view and specify the name, this will be the name of your method. The EType represents the return type of the method.
If your method has any parameters, then add EParameter children to the EOperation & specify their types and names.

To add the code, add an EAnnotation to the EOperation.
In the properties view, set the Source to "http://www.eclipse.org/emf/2002/GenModel".
Add a details entry to the EAnnotation.
In the properties view set the key to 'body' and value to the code that you want to be generated.
Reload your .genmodel from .ecore and generate the code:
May 5, 2008
Adding util methods to the generated EMF classes
Posted by
Prakash G.R.
at
10:26 PM
0
comments
Labels: eclipse, EMF, guidelines
May 3, 2008
Single column TableViewer and TableColumnLayout
If you have a single column TableViewer (which is commonly used because ListViewer won't show you the images), the single coloumn won't take the entire space. For example if you run the TableViewer snippet, the output is like this:
To get rid of the other spurious coloum that appears on the right, you have to use the TableColumnLayout. Modifying the code to use that:
public Snippet001TableViewer(Shell shell) {
Composite tableComposite = new Composite(shell, SWT.NONE);
final TableViewer v = new TableViewer(tableComposite);
v.setLabelProvider(new LabelProvider());
v.setContentProvider(new MyContentProvider());
MyModel[] model = createModel();
v.setInput(model);
v.getTable().setLinesVisible(true);
TableColumn singleColumn = new TableColumn(v.getTable(), SWT.NONE);
TableColumnLayout tableColumnLayout = new TableColumnLayout();
tableColumnLayout.setColumnData(singleColumn, new ColumnWeightData(100));
tableComposite.setLayout(tableColumnLayout);
}
Remember, the TableColumnLayout should be applied on the composite that holds the Table. And the composite should contain only the table and nothing else. Here is the result:
Posted by
Prakash G.R.
at
12:58 PM
0
comments
Labels: guidelines, jface, swt
