Define control model

    The 1 minute and 50 second GIF video below demonstrates how to define and use a SWT control model through a model called BlueButton.

    The core elements that define the SWT model are summarized as follows:

  • Need to implement the create() method. Create the Java object of the control in the create() method, set the properties of the Java object according to the model, call the create() method of the child node, and put the Java object in the global context.
  • Register the model under the general index of the SWT control so that it can be used when writing SWT applications.

 

The process of writing the code for create() is omitted in the video. create is written in Groovy. The code is as follows.

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;

import xworker.swt.widgets.ControlCreator;

//Create button object, parent is putted by parent model
def button = new Button(parent, SWT.NONE);
button.setBackground(button.getDisplay().getSystemColor(SWT.COLOR_BLUE));

//Apply control common features
ControlCreator.init(button, self, actionContext);

//Set button attributes with model
button.setText(self.getMetadata().getLabel());

//Invoke children's create method, put button as parent
actionContext.peek().put("parent", button);
for(child : self.getChilds()){
     child.doAction("create", actionContext);
}

//Put button to global context
actionContext.g().put(self.getMetadata().getName(), button);

//Return
return button;

 

Copyright ©  2007-2019 XWorker.org  版权所有  沪ICP备08000575号