Differences

This shows you the differences between two versions of the page.

Link to this comparison view

tutorial:extend_controller [2020/04/16 10:56] (current)
Line 1: Line 1:
 +<WRAP adihi><​html><​center></​html>​Controller (UI component composition) can be extended and their extensions easily incorporated by the xml file.<​html></​center></​html></​WRAP>​
 +\\
 +===== Example: Extend a specific '​CComboController'​ for property '​rating'​ =====
 +We want to create a new <wrap adicode>​CComboController</​wrap>​ which manages background colors: <hi #​E0E8F1>​**light blue**</​hi>​ if value is '​R',​ <hi #​FF0000>​**Red**</​hi>​ if value is 'NC 17', default otherwise.\\
 +In Eclipse IDE, open file <wrap adicode>​$projectDirectory/​resources/​xml/​include/​detail/​FilmDI.xml</​wrap>​.
  
 +===== Specify a new controller for Rating field =====
 +<columns 100% l 350px middle>
 +{{:​tutorial:​select_rating_controller_class.png?​350 | Specify a new controller for Rating field}}
 +<​newcolumn left>
 +  - open file <wrap adicode>​$projectDirectory/​resources/​xml/​include/​detail/​FilmDI.xml</​wrap>​ with AXML editor.
 +  - Select field Select <wrap adicode>​CCombo rating</​wrap>​ field.
 +  - Select <wrap adicode>​Life cycle elements</​wrap>​ shelf in the <wrap adicode>​outline</​wrap>​ page.
 +  - Click on <wrap adicode>​controllerClassName</​wrap>​ hypelink.
 +
 +A new wizard window is opened for the creation of a new class.
 +</​columns>​
 +\\
 +===== Create the new controller class =====
 +<columns 100% l 350px middle>
 +{{:​tutorial:​create_rating_controller.png?​350 | Create the new controller class}}
 +<​newcolumn left>
 +A new <wrap adicode>​Java Class wizard</​wrap>​ is starting with a default values for source folder, package and superclass.  ​
 +  - Specify class name as <wrap adicode>​RatingCComboController</​wrap>​.
 +  - Click on <wrap adicode>​Finish</​wrap>​ button.
 +  - Select <wrap adicode>​Life cycle elements</​wrap>​ shelf in the <wrap adicode>​outline</​wrap>​ page.
 +  - Click on <wrap adicode>​controllerClassName</​wrap>​ hypelink.
 +A new Java editor window is open for <wrap adicode>​RatingCComboController</​wrap>​ Java class.
 +  - Copy following code inside editor.
 +  - Save <wrap adicode>​RatingCComboController</​wrap>​ Java class editor.
 +  - Save <wrap adicode>​FilmDI.axml</​wrap>​ AXML editor.
 +</​columns>​
 +<sxh java first-line: 1; title: excerpt from '​RatingCComboController.java'​ class.>
 +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.events.SelectionAdapter;​
 +import org.eclipse.swt.events.SelectionEvent;​
 + 
 +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() {
 +        String value = getCCombo().getText();​
 +        // Use '​setCssBackground'​ rather than '​getControl().setBackground'​ to be compliant with css process.
 +        // Otherwise when changing css theme current color is lost.
 +        if ("​NC-17"​.equals(value))
 +            setCssBackground("#​adichatz-common",​ "​error-foreground-color"​);​
 +        else if ("​R"​.equals(value))
 +            setCssBackground("#​adichatz-common",​ "​title-background"​);​
 +        else
 +            setCssBackground(null,​ null);
 +    }
 +}</​sxh>​
 +\\ \\
 +Save changes in new **RatingCComboController.java** file then in **FilmDI.axml** file.\\ \\
 +<WRAP indic>
 +In your application,​ open a new editor for the detail of a <wrap adicode>​Film</​wrap>,​ background color of <wrap adicode>​rating CComboController</​wrap>​ changes following value of the property.\\
 +<​html><​center></​html>​**You do not need to close and reopen application to see changes at runtime.**<​html></​center></​html>​
 +</​WRAP>​
 +