Controller (UI component composition) can be extended and their extensions easily incorporated by the xml file.


Example: Extend a specific 'CComboController' for property 'rating'

We want to create a new CComboController which manages background colors: Yellow if value is 'NC 17', Red if value is 'R', default otherwise.
In Eclipse IDE, open file $projectDirectory/resources/xml/include/detail/FilmDI.xml.

1) Change element:

<ccombo values="G, PG, PG-13, R, NC-17" property="rating" style="SWT.READ_ONLY | SWT.BORDER" id="rating"/>
by
<ccombo values="G, PG, PG-13, R, NC-17" property="rating" style="SWT.READ_ONLY | SWT.BORDER" id="rating" controllerClassName="org.mycompany.myProject.gencode.custom.RatingCComboController"/>

2) In directory $projectDirectory/src, create package org.mycompany.myProject.gencode.custom and class RatingCComboController like this:

package org.mycompany.myproject.gencode.custom;

import org.adichatz.engine.controller.IContainerController;
import org.adichatz.engine.controller.field.CComboController;
import org.adichatz.engine.core.ControllerCore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Display;

public class RatingCComboController extends CComboController {

	public RatingCComboController(String id,
			IContainerController parentController, ControllerCore genCode) {
		super(id, parentController, genCode);
	}

	@Override
	public void createControl() {
		super.createControl();
		getCCombo().addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				setRatingBackGroundColor();
			}
		});
	}

	@Override
	public void setValue(Object value) {
		super.setValue(value);
		setRatingBackGroundColor();
	}

	private void setRatingBackGroundColor() {
		if ("NC-17".equals(getValue()))
			getCCombo().setBackground(
					Display.getCurrent().getSystemColor(SWT.COLOR_RED));
		else if ("R".equals(getValue()))
			getCCombo().setBackground(
					Display.getCurrent().getSystemColor(SWT.COLOR_YELLOW));
		else
			getCCombo().setBackground(null);
	}
}

In your application, open a new editor for the detail of a Film, background color of rating CComboController changes following value of the property.
Be sure that the editor was not already open for the Film, otherwise the editor use the old version without the last change.

You do not need to close and reopen application to see changes at runtime.

2014/10/28 10:54
features/extend_controller.txt · Last modified: 2015/10/05 17:52 (external edit)