@Gadget.Content

17 views
Skip to first unread message

MockerTim

unread,
Sep 14, 2010, 1:38:13 AM9/14/10
to Google API Libraries for GWT
There is a class com.google.gwt.gadgets.client.Gadget in Gadgets API
Library 1.2.
There are some annotations defined in this class.
The one I'm interested in is @Gadget.Content
(http://gwt-google-apis.googlecode.com/svn/javadoc/gadgets/1.2/com/
google/gwt/gadgets/client/Gadget.Content.html).
Can anyone provide a simple example of this annotation usage?
Thanks a lot!

Eric Ayers

unread,
Sep 15, 2010, 3:09:32 PM9/15/10
to gwt-goo...@googlegroups.com
I don't have any public examples, but here is the general idea:


import com.google.gwt.gadgets.client.ContentSection;
import com.google.gwt.gadgets.client.Gadget;
import com.google.gwt.gadgets.client.Gadget.Content;
...

@ModulePrefs(...)
@Content(contents = {HomeView.class, OtherView.class})
public class MyGadget extends Gadget {

  @ContentType(views = {"home"})
  public class HomeView extends  ContentSection<MyGadget> {
@Override
public void init() {
..code to run when View1 is active...
}
}

@ContentType(views = {"other"})
public class OtherView extends ContentSection<MyGadget> {
@Override
public void init() {
..code to run when View1 is active...
}
}

}


--
You are subscribed to the Google Groups "GWT-Google-Apis" group.
For more options, visit http://groups.google.com/group/gwt-google-apis?hl=en



--
Eric Z. Ayers
Google Web Toolkit, Atlanta, GA USA

Tim

unread,
Sep 20, 2010, 4:58:45 AM9/20/10
to Google API Libraries for GWT
Thank you for your reply, Eric!

The genral idea is clear to me now. But the is a problem.

When I try to compile corresponding code the compiler says (see
MyGadget2.java code down this page):

Type mismatch: cannot convert from Class<HomeView> to Class<? extends
ContentSection<?>>.

and points to this line:

@Content(contents = {HomeView.class, OtherView.class})

And I failed to fix this.

Next, for convenience, I publish my complete code.

################## com.mygadget.MyGadget2.gwt.xml: ################

<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='mygadget2'>
<inherits name='com.google.gwt.user.User'/>
<inherits name='com.google.gwt.gadgets.Gadgets' />

<entry-point class='com.mygadget.client.MyGadget2'/>
</module>

###################################################################

######################### MyGadget2.java: #########################

package com.mygadget.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.Gadget;
import com.google.gwt.gadgets.client.Gadget.AllowHtmlQuirksMode;
import com.google.gwt.gadgets.client.Gadget.InjectModulePrefs;
import com.google.gwt.gadgets.client.Gadget.ModulePrefs;
import com.google.gwt.gadgets.client.Gadget.UseLongManifestName;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

@ModulePrefs(title = "MyGadget2")
@Gadget.Content(contents = {HomeContentSection.class,
CanvasContentSection.class})
@UseLongManifestName(false)
@AllowHtmlQuirksMode(false)
public class MyGadget2 extends Gadget<MyUserPreferences> {
@Override
protected void init(MyUserPreferences preferences) {

}
}

###################################################################

#################### HomeContentSection.java: #####################

package com.mygadget.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.ContentSection;
import com.google.gwt.gadgets.client.Gadget;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

@Gadget.ContentType(type = "html", views = {"home"})
public class HomeContentSection<T extends Gadget<MyUserPreferences>>
extends ContentSection<Gadget<MyUserPreferences>> {

public HomeContentSection () {

}

@Override
public void init(Gadget<MyUserPreferences> gadget) {
Button simpleButton = new Button("HomeView");
simpleButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Window.alert("Hello Home World!");
}
});
RootPanel.get().add(simpleButton);
}
}

###################################################################

################### CanvasContentSection.java: ####################

package com.mygadget.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.ContentSection;
import com.google.gwt.gadgets.client.Gadget;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

@Gadget.ContentType(type = "html", views = {"canvas"})
public class CanvasContentSection<T extends Gadget<MyUserPreferences>>
extends ContentSection<Gadget<MyUserPreferences>> {

@Override
public void init(Gadget<MyUserPreferences> gadget) {
Button simpleButton = new Button("CanvasView");
simpleButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Window.alert("Hello Canvas World!");
}
});
RootPanel.get().add(simpleButton);
}
}

#########################THE END OF CODE###########################

Eric Ayers

unread,
Sep 20, 2010, 9:38:03 AM9/20/10
to gwt-goo...@googlegroups.com
On Mon, Sep 20, 2010 at 4:58 AM, Tim <t.sh...@gmail.com> wrote:
Thank you for your reply, Eric!

The genral idea is clear to me now. But the is a problem.

When I try to compile corresponding code the compiler says (see
MyGadget2.java code down this page):

Type mismatch: cannot convert from Class<HomeView> to Class<? extends
ContentSection<?>>.

and points to this line:

@Content(contents = {HomeView.class, OtherView.class})

And I failed to fix this.

I didn't see your declaration for the HomeView class, but it should look something like:

public class HomeView extends ContentSection<MyGadget2> {

Tim

unread,
Sep 22, 2010, 8:05:08 AM9/22/10
to Google API Libraries for GWT
Your latter post helped me to fix my mistake.

Now I can't understand how to make those views work together.

What can I do with MyGadget gadget variable (that is passed to
HomeView in
init method) to change gadget's interior?

All my code in HomeView and CanvasView do nothing!
That makes me crazy!

Here is my latest code:

################## com.mygadget.MyGadget2.gwt.xml: #######

<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='mygadget2'>
<inherits name='com.google.gwt.user.User' />
<inherits name='com.google.gwt.gadgets.Gadgets' />
<set-property name="user.agent" value="gecko1_8" />
<entry-point class='com.mygadget.client.MyGadget2' />
</module>

##########################################################

######################### MyGadget2.java: ################

package com.mygadget.client;

import com.google.gwt.gadgets.client.Gadget;
import com.google.gwt.gadgets.client.Gadget.AllowHtmlQuirksMode;
import com.google.gwt.gadgets.client.Gadget.ModulePrefs;
import com.google.gwt.gadgets.client.Gadget.UseLongManifestName;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

@ModulePrefs(title = "MyGadget2")
@Gadget.Content(contents = {HomeView.class, CanvasView.class})
@UseLongManifestName(false)
@AllowHtmlQuirksMode(false)
public class MyGadget2 extends Gadget<MyUserPreferences> {
@Override
protected void init(MyUserPreferences preferences) {
this.prefs = preferences;
Button simpleButton = new Button("Default");

RootPanel.get().add(simpleButton);
}

RootPanel getRootPanel() {
return defaultRootPanel;
}

private RootPanel defaultRootPanel = RootPanel.get();
}

#########################################################

#################### HomeView.java: #####################

package com.mygadget.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.ContentSection;
import com.google.gwt.gadgets.client.Gadget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

@Gadget.ContentType(type = "html", views = {"home"})
public class HomeView extends ContentSection<MyGadget2> {
MyGadget2 gadget;
@Override
public void init(MyGadget2 gadget) {
this.gadget = gadget;

Button simpleButton = new Button("HomeView");
simpleButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {

}
});

RootPanel.get().add(simpleButton);
}
}
#########################################################

################### CanvasView.java: ####################

package com.mygadget.client;

import com.google.gwt.gadgets.client.ContentSection;
import com.google.gwt.gadgets.client.Gadget;

@Gadget.ContentType(type = "html", views = {"canvas"})
public class CanvasView extends ContentSection<MyGadget2> {

@Override
public void init(MyGadget2 gadget) {

gadget.getRootPanel().clear();
}
}

#########################THE END OF CODE#################

On 20 сен, 17:38, Eric Ayers <zun...@google.com> wrote:

Eric Ayers

unread,
Sep 22, 2010, 8:45:17 AM9/22/10
to gwt-goo...@googlegroups.com
Have you read the docs for view support in Gadgets?  If not, maybe that would help - these bindings are intended to be wrappers that give access to all the same functionality:

http://code.google.com/apis/gadgets/docs/ui.html#views

Tim

unread,
Sep 22, 2010, 8:58:42 AM9/22/10
to Google API Libraries for GWT
Of course, I've read those docs.

Sorry for wasting your time...
> > > > > > The...
>
> продолжение >>

Eric Ayers

unread,
Sep 22, 2010, 9:30:01 AM9/22/10
to gwt-goo...@googlegroups.com
The reason I ask is not to brush you off, but because there is documentation in other places (like the open social website, myspace and orkut documentation).  

In the GWT bindings, the view init() class will completely take the place of the Gadget class's init if that view is currently active.  So, you shouldn't expect MyGadget2.init() and HomeView.init() to both be called in the same invocation.

Here is another piece of the puzzle that might help you debug why your view code isn't being called.  In your Gadget subclass you can add:

 private ViewFeature viewFeature;

  class Foo implements NeedsViews {
    @Override
    public void initializeFeature(ViewFeature feature) {
      viewFeature = feature;
    }
  }

With access to 'viewFeature' you can help debug your problem by seeing which views the container supports and what the current view is.  


-Eric.


--
You are subscribed to the Google Groups "GWT-Google-Apis" group.
For more options, visit http://groups.google.com/group/gwt-google-apis?hl=en

Tim

unread,
Sep 24, 2010, 5:49:02 AM9/24/10
to Google API Libraries for GWT


On 22 сен, 17:30, Eric Ayers <zun...@google.com> wrote:
> The reason I ask is not to brush you off, but because there is documentation
> in other places (like the open social website, myspace and orkut
> documentation).
>
> In the GWT bindings, the view init() class will completely take the place of
> the Gadget class's init if that view is currently active. So, you shouldn't
> expect MyGadget2.init() and HomeView.init() to both be called in the same
> invocation.
>

In my case the problem is as follows:
Method MyGadget2.init() is always called, and both methods
HomeView.init()
and CanvasView.init() are never called.
In the same time the injections that are made to those two views with

@Gadget.InjectContent are always done as they should be done.

For example, if I declare my Views as follows:

@ContentType(type = "html", views = {"home", "canvas"}) // should be
applied
// to both views: "home" & "canvas"
@InjectContent(files = "../HomeContent.txt")
public class HomeView extends ContentSection<MyGadget2> {
@Override
public void init(MyGadget2 gadget) {
// some code ...
}
}

@ContentType(type = "html", views = {"home", "canvas"})
@InjectContent(files = {"../CanvasContent.txt"})
public class CanvasView extends ContentSection<MyGadget2> {
@Override
public void init(MyGadget2 gadget) {
// some code ...
}
}

what init method should be called?

In my case the contents of both HomeContent.txt & CanvasContent.txt
files are injected but code of both init() methods is ignored.

It's always ignored. Even if views variable of @ContentType contains
different view names for different View classes (I probed all
possible
combinations).


MyGadget2 class declaration is always the same:

@ModulePrefs(title = "MyGadget2")
@Content(contents = {HomeView.class, CanvasView.class})
@UseLongManifestName(false)
@AllowHtmlQuirksMode(false)
public class MyGadget2 extends Gadget<MyUserPreferences> {
@Override
protected void init(MyUserPreferences preferences) {
// some code ...
}
}


Maybe I'am missing some declaration in my gwt module file or somewhere
else?


> Here is another piece of the puzzle that might help you debug why your view
> code isn't being called. In your Gadget subclass you can add:
>
> private ViewFeature viewFeature;
>
> class Foo implements NeedsViews {
> @Override
> public void initializeFeature(ViewFeature feature) {
> viewFeature = feature;
> }
> }
>
> With access to 'viewFeature' you can help debug your problem by seeing which
> views the container supports and what the current view is.
>

I use this feature, but it can't help me determine why my view's
init() methods are never called.

I tried to google examples of
com.google.gwt.gadgets.client.ContentSection
usages, but I've found nothing.

I tried to search the source code of
com.google.gwt.gadgets.rebind.ContentSectionGenerator.
It's obvious that contents of files declared in @Gadget.Inject content
are
added to gadget's declaration xml. But I haven't found any
ContentSection.init() method calls.
> > Gadget<MyUserPreferences>>...
>
> продолжение >>

Eric Ayers

unread,
Sep 24, 2010, 9:46:20 AM9/24/10
to gwt-goo...@googlegroups.com, sas...@gmail.com
I don't know.  This is a really complex example and I'm not sure we planned for it.  I think the idea of the alternate views was to give a way to override the normal logic of the main gadget init when an alternate view was enabled.  The alternate init functions are used to create different content sections in your gadget spec, so I'm not sure what defining 2 for the same view would mean, or what the calling order would be.

Is it necessary to assign the same view to more than one @ContentType annotation?  Does the simpler case of simply assigning one ContentSection<MyGadget2> to a single view work for you?

If you look at the ViewFeature code snippet I sent you earlier, you should be able to debug which views are active in your gadget container.  Have you confirmed that the names 'home' and 'canvas' are supported by the container according to the viewFeature variable returned when you implement NeedsViews?



 
>
> продолжение >>

--
You are subscribed to the Google Groups "GWT-Google-Apis" group.
For more options, visit http://groups.google.com/group/gwt-google-apis?hl=en

Tim

unread,
Sep 30, 2010, 7:10:52 AM9/30/10
to Google API Libraries for GWT


On 24 сен, 17:46, Eric Ayers <zun...@google.com> wrote:
Sorry for not answering for a long time.

I created a project on Google Code and added all my code there.

Here is that project's wiki page:

http://code.google.com/p/sugarcurve/wiki/MyGadget2?ts=1285843717&updated=MyGadget2

It contains more clear (I hope so) description of my problem and
answers your questions.
> > > > > > > > Next, for convenience, I publish my complete code....
>
> продолжение >>

Eric Ayers

unread,
Sep 30, 2010, 10:27:42 AM9/30/10
to gwt-goo...@googlegroups.com
OK, this is kind of a dumb suggestion, but by looking at some other
apps, I see they don't modify the UI directly inside of init(). Is
there another way you could confirm that init() entry points are or
aren't being called? Say having them set a global variable that you
can then check after pressing a button?

Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages