GWTDesigner does not load the code in super() constructor.

GWT Designer allows you to quickly create the modules, composites, panels, remote services and other elements that comprise Google Web Tookit applications.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

GWTDesigner does not load the code in super() constructor.

Postby kelvin » Wed Mar 28, 2007 6:19 pm

I wonder why the GWTDesigner does not load the code in super() constructor.

with the following code sample, in the Design mode of SubClass.java, it only show a empty absolute panel.

AbstractSuperClass.java

Code: Select all
package test.client;

import com.google.gwt.user.client.ui.Composite;

public abstract class AbstractSuperClass extends Composite {

   public AbstractSuperClass() {
      init();
      show();
   }

   protected void init() {
   }

   public void show() {
   }
}


SubClass.java
Code: Select all
package test.client;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Label;

public class SubClass extends AbstractSuperClass {

   private AbsolutePanel absolutePanel;
   private Label lengthLabel;
   
//   public SubClass() {
//      init();
//      show();      
//   }
   
   protected void init() {
      super.init();
      
      absolutePanel = new AbsolutePanel();
      initWidget(absolutePanel);

      lengthLabel = new Label("Length");
   }

   public void show() {
      absolutePanel.setStyleName("test");
      absolutePanel.setSize("200px", "200px");

      absolutePanel.add(lengthLabel, 10, 22);
   }
}


However, if i remove the comment of the SubClass(), the design view shows the "Length" label in the absolute panel correctly.

To my understanding, java always calls its super() constructor but i don't know why i must explicity call the init() and show() explicitly in the SubClass constructor.

This behavior of GWTDesigner restrict the possibility of inheriting a superclass because the init() and show() will be call twice or even more in runtime.
kelvin
 
Posts: 14
Joined: Tue Mar 13, 2007 7:57 am

Re: GWTDesigner does not load the code in super() constructo

Postby Eric Clayberg » Thu Mar 29, 2007 1:55 pm

kelvin wrote:I wonder why the GWTDesigner does not load the code in super() constructor.

GWT Designer does execute code in a super constructor when editing a subclass. You can see a good example of this in the Visual Inheritance Example we posted earlier.

The problem with your example is that the super() constructor doesn't actually do anything (e.g., the init() and show() methods in the superclass are empty).

kelvin wrote:with the following code sample, in the Design mode of SubClass.java, it only show a empty absolute panel.
...
However, if i remove the comment of the SubClass(), the design view shows the "Length" label in the absolute panel correctly.

Designer does not execute code in the current class. Instead, it parses the class that you are currently editing. It does not parse any other class, so it has no way to know about any execution flow that happens external to the class being edited. The local constructor is what Designer begins with when analyzing a class. The local constructor is parsed and any methods directly accessible from that constructor are parsed and presented in the design view. If a method isn't linked back to the local constructor (as in your example), it is ignored.

kelvin wrote:This behavior of GWTDesigner restrict the possibility of inheriting a superclass because the init() and show() will be call twice or even more in runtime.

Not at all. There are lots of ways around this:

1) You could follow the pattern shown in our Visual Inheritance Example

2) You could make the init() and show() methods private so that each class calls its local version

3) You could add a simple check for duplicate invocation like this:

Code: Select all
public class MyDataComposite extends AbstractDataComposite {
   private boolean m_initialized;
   private AbsolutePanel absolutePanel;
   private Label lengthLabel;
   public MyDataComposite() {
      init();
      show();
   }
   protected void init() {
      if (m_initialized)
         return;
      m_initialized = true;
      //
      super.init();
      absolutePanel = new AbsolutePanel();
      initWidget(absolutePanel);
      lengthLabel = new Label("Length");
   }
   public void show() {
      if (m_initialized)
         return;
      //
      absolutePanel.setStyleName("test");
      absolutePanel.setSize("200px", "200px");
      absolutePanel.add(lengthLabel, 10, 22);
   }
}

4) You could use an isDesignTime() check to ignore the calls to init() and show() at runtime:

Code: Select all
public class SubClass extends AbstractSuperClass {
   private AbsolutePanel absolutePanel;
   private Label lengthLabel;
   private static final boolean isDesignTime() {
      return false;
   }
   public SubClass() {
      super();
      if (!isDesignTime()) return;
      init();
      show();
   }
   protected void init() {
      super.init();
      absolutePanel = new AbsolutePanel();
      initWidget(absolutePanel);
      lengthLabel = new Label("Length");
   }
   public void show() {
      absolutePanel.setStyleName("test");
      absolutePanel.setSize("200px", "200px");
      absolutePanel.add(lengthLabel, 10, 22);
   }
}
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Postby kelvin » Thu Mar 29, 2007 5:45 pm

The problem with your example is that the super() constructor doesn't actually do anything (e.g., the init() and show() methods in the superclass are empty).


Although the init() and show() methods in the superclass are empty, the SubClass.java does overrided them. to my java understanding, once the SubClass being instantiated, it calls its super() constructor() and run the code in the super constructor. in this case, run the init() and show(). As init() and show() have been overrided, SubClass.init() and show() should be called instead of AbstractSuperClass.init() and AbstractSuperClass.show(). therefore, the lengthLabel should be shown.

Actually, SubClass.init() and SubClass.show() is not totally ignored. with my code sample provided, in the Design mode of SubClass.java, it shows the empty absolutePanel with style "test" (absolutePanel is initialize in SubClass.init() and the style is defined in SubClass.show(), however, the code statement "absolutePanel.add(lengthLabel, 10, 22) in the SubClass.show();" is ignored by GWT Designer
kelvin
 
Posts: 14
Joined: Tue Mar 13, 2007 7:57 am

Postby Eric Clayberg » Thu Mar 29, 2007 8:19 pm

kelvin wrote:Although the init() and show() methods in the superclass are empty, the SubClass.java does overrided them. to my java understanding, once the SubClass being instantiated, it calls its super() constructor() and run the code in the super constructor.

If Designer were actually running the code in SubClass, that would be true, but it is not. The SubClass class is never actually instantiated, and its code is never run. The class is, however, parsed and it is the results of that parsing effort that are displayed in the design view.

kelvin wrote:As init() and show() have been overrided, SubClass.init() and show() should be called instead

Again, the SubClass class is never instantiated so its methods are never called.

kelvin wrote:Actually, SubClass.init() and SubClass.show() is not totally ignored.

As long as they are reachable from the constructor of the class being edited, they are parsed and the results are shown in the design view.

kelvin wrote:the code statement "absolutePanel.add(lengthLabel, 10, 22) in the SubClass.show();" is ignored by GWT Designer

As long as the show() method is reachable from the local constructor (as in my two example variations), that statement will be rendered jsut fine.

As I stated in my last message, there are a lot of ways that you can set up visual inheritance using GWT Designer. The way that you want to do it just happens not to be one of them at the moment. The particular pattern that you want to use may be something that we support in the future.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA


Return to GWT Designer

Who is online

Users browsing this forum: No registered users and 3 guests