Creating a custom widget

ZIETrans provides a Create Widget wizard to help you create custom widgets. You can start the wizard in several ways:
  • From the File > New > ZIETrans Widget menu in ZIETrans Toolkit
  • From the ZIETrans > New > Widget menu in ZIETrans Toolkit
  • From the context (right click) menu of a ZIETrans project, select New ZIETrans > Widget

There are two panels in the Create Widget wizard. On the first panel, you provide the name of the project, the name of the new widget, and the name of the Java™ package for the widget. Optionally, you can select a check box to include stub methods that allow you to define a GUI panel for configuring the settings used by the new widget (see ZIETrans Toolkit support for custom component and widget settings for more information). On the second panel, enter the name you want displayed for the new widget in the ZIETrans Toolkit and select the components to associate with the widget.

The wizard provides the following required elements of a custom widget:
  • Extends the widget abstract class and implements the SwtRenderer interface: public class MyCustomWidget extends Widget implements SwtRenderer.

    See Extending widget classes for more information.

  • Adds the constructor method:
    public MyCustomWidget(ComponentElement[] arg0, Properties arg1) {
    		super(arg0, arg1);
    }
  • Adds the following method to generate SWT for RCP project output.
    
    public Control[] drawSwt(Composite parent) {
            SwtElementFactory elementFactory = 
                     SwtElementFactory.newInstance(contextAttributes, 
                                       settings, parent.getDisplay());
            
            // Construct controls using SwtElementFactory instance
            
            return new Control[0];
        }

    The SwtElementFactory should be used to construct SWT controls since it handles associating new controls with the underlying data model and applying the styles of the associated template.

  • Adds the source code for the widget into the Source folder of your project
  • Compiles the new widget .java file, if you have Build Automatically selected in the Eclipse workbench preferences (Window > Preferences > General > Workspace) or the Project menu. If the widget is not compiled into a .class file, it is not available for use in the ZIETrans Toolkit.
  • Registers the new widget in the ComponentWidget.xml file. See Registering your component or widget for more information about registering widgets.
If you selected the check box to include ZIETrans Toolkit graphical user interface support methods, enabling you to modify the settings of the widget, the Create Widget wizard adds the following methods:
  • Method to return the number of pages in the property settings:
    public int getPropertyPageCount() {
    		return (1);
    }
  • Method to return the settings that can be customized:
    public Vector getCustomProperties(int iPageNumber, Properties properties,
    			ResourceBundle bundle) {
    		return (null);
    }
  • Method to return the default values of the settings that can be customized:
    public Properties getDefaultValues(int iPageNumber) {
    		return (super.getDefaultValues(iPageNumber));
    	}

See ZIETrans Toolkit support for custom component and widget settings for more information about the methods necessary to support your custom widget.