GWT: AcceptsOneWidget vs Composite vs IsWidget

GWT has many identically named (and seemingly identically defined) types:

  • Widget
  • AcceptsOneWidget
  • Composite
  • IsWidget
  • SimplePanel

When / where is it appropriate to use each of them? What is their relationship with RootPanel ?

+8
java mvp gwt
source share
2 answers

Let the interfaces separate from the classes first.

Interfaces are great for bullying (which allows you to test your application without the need for a sluggish GWTTestCase ):

  • IsWidget : when all you need is a widget handle that is independent of the Widget class. This is usually used with MVP as a way of presenting a view.
  • AcceptsOneWidget : when you need a placeholder for one widget (in the form of IsWidget ). This is typically used for operations to insert a view ( IsWidget ) into a given slot ( AcceptsOneWidget ).

All the classes you listed are extended by Widget , so they rely on JSNI and (most of the time) should run in the GWT environment (for unit tests, which means GWTTestCase ):

  • Widget : the base of all widgets. Implements IsWidget returning from asWidget() .
  • Composite : the base class when you need to create a widget created from other widgets, hiding their implementation. Although you can extend an existing widget, it's best to hide it inside the Composite so that you only expose the API that you need / should show. Composite is "composition, not inheritance" and encapsulation. Examples of composites in standard widgets include TabPanel (built from TabBar and DeckPanel ), DateBox (built from TextBox and DatePicker in PopupPanel ), ValueListBox , which wraps ListBox or ValuePicker , which wraps CellList . In many cases, given that panels accept IsWidget , you can simply implement IsWidget to expand Composite , but it is sometimes useful to have a true Widget .
  • SimplePanel panel that implements AcceptsOneWidget , useful as a slot when using actions (but you can also easily implement AcceptsOneWidget to insert into any panel)

Talking about this, Google recently published a GWT-Mockito that connects Mockito to GWT.create() and uses the classloader manual to overwrite JSNI methods and remove final modifiers so you can directly use widgets in tests without the need for GWTTestCase or MVP.

So, overall, it depends on how you approach your code, how you use the architecture of your application. If you use MVP, stick with the interfaces ( IsWidget , AcceptsOneWidget ) in your presenter so that you can easily make fun of your opinions in your tests.
Otherwise, or if you want a “simplified MVP” where the view is a UiBinder template, try GWT-Mockito for your tests and use widgets directly.
Of course, you can mix both approaches in one application. And in any case, create your own widgets as a Widget for low-level things (rarely required) and Composite or IsWidget for everything else, and not to extend existing widgets.

+11
source share

Everything is confused for you.

Widget . It allows you to interact with users. (e.g. Button )

Panels . These are widgets that may contain other panels / widgets. May be referred to as a widget container.

AcceptsOneWidget : implemented by panels that will accept only one widget. (e.g. SimplePanel )

Composite : a type of widget that can wrap another widget by hiding widget wrapped methods. The component is useful for creating a single widget from a combination of several other widgets contained in one panel.

IsWidget : an interface implemented by almost all known widgets. It provides access to this widget.

SimplePanel : a panel containing only one widget.

RootPanel . It is the base panel to which all other panels are added.

+4
source share

All Articles