3

I have some code which uses java.awt.Color. I want to translate my Java Code to GWT. So I will emulate java.awt.Color in my GWT Project.

One approach is to write a module called java.AWT.gwt.xml whose source path is awt and a class called java.awt.Color.

The other approach is to create a folder com.google.gwt.emul.java.awt and create java.awt.Color class inside that path. Eclipse will show errors. But compiler will work.

Which one is appropriate way to add java.awt.Color for GWT?

1 Answer 1

6

You'd rather create a subfolder (say 'super') in whichever module you want (probably the module for the code you want to make "translatable") and declare it as a super-source in your module's gwt.xml:

<super-source path="super" />

And you'd put your java/awt/Color.java in there.

The Eclipse errors are normal, it's not source code that should be compiled (by javac, to a *.class file), only code for the GWT compile, which works from the *.java file. So you'd want to exclude the super subfolder from your build path.

That's it!

See also the Overriding one package implementation with another subsection at http://code.google.com/webtoolkit/doc/latest/DevGuideOrganizingProjects.html#DevGuideModuleXml

Sign up to request clarification or add additional context in comments.

6 Comments

Is there a problem in my first approach. The first approach will also work in development mode.
The only issue would be that there's more risk that the class get compiled (to a *.class) and mess up your classpath. Using a super-source is the preferred approach. You could reuse the one from the com.google.gwt.enum.Enum module, but I believe it's best to use your own module, so that it's not automatically included in the GWT compile just by being in the class path; you also have to inherit the module, which makes it clearer what could happen.
I have a problem in development mode. Assume
I have a problem in development mode. Assume Color class same as java version but it has a method called toGwtColor which convert java.awt.Color to CssColor. In that case, the eclipse compiler will warn me that Color does not have toGwtColor method. If I change the classpath order of Color project over jre, the warning will be gone. When the project is GWT compiled, Everything works OK.
Do not add methods to an emulated class (unless you use it form another emulated class); instead, use a helper method in another class. That other class might use GWT.isScript to use different code paths on DevMode vs. "web mode", or have an "emulated" counter-part (in your super-source) that uses your toGwtColor from your emulated Color. You'll annotate the emulated class with @com.google.gwt.core.client.GwtScriptOnly so it's only used in web mode, and the "non-emulated" helper class will be used in DevMode.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.