java
logs related to java
- 1 Google Web Toolkit
- 1.1 2009-5-21 documentation
- 1.2 2009-3-22 StartUp
- 1.3 2009-3-22 Google Web Toolkit in a Linux 64-bit environment
- 1.4 2009-3-22 Eclipse repeatedly crash
- 1.5 2009-4-3 how to call other javascript library by JSNI embedded in Java
- 1.6 2009-4-3 fireSelectionEvent() error
- 1.7 2009-4-14 use existing/external JAVA API
- 1.8 2009-4-17 google Maps API for GWT
- 1.9 2009-4-21 browser client x,y position upon scrolling
- 1.10 2009-4-21 judge string identity
- 1.11 2009-4-24 draw() of a GWT visualization widget isn't allowed to be customized in inheritance
- 1.12 2009-4-25 unused import from a un-included jar spoils GWT compilation
- 1.13 2009-4-25 double jsonify vs single jsonify
- 1.14 2009-4-25 (TypeError): b.contentWindow has no properties
- 1.15 2009-5-3 code-sharing between GWT applications
- 2 Syntax
1 Google Web Toolkit
1.1 2009-5-21 documentation
| Title | Date Published |
|---|---|
| GWT in Action: Easy Ajax with the Google Web Toolkit | 2007-6-5 |
| GWT in Practice | 2008-5-12 |
| Google Web Toolkit for Ajax | 2007-1-2 |
1.2 2009-3-22 StartUp
Create an Eclipse project (-out specifies the output directory):
projectCreator -eclipse StockWatcher -out StockWatcher
Creating a GWT application (client is a must-have in the finale name):
applicationCreator -eclipse StockWatcher -out StockWatcher -addToClassPath ~/script/gwt-maps/gwt-maps.jar,/home/crocea/script/gwt-visualization/gwt-visualization.jar -addModule com.google.gwt.maps.GoogleMaps,com.google.gwt.visualization.Visualization com.google.gwt.sample.stockwatcher.client.StockWatcher
-addToClassPath and -addModule are required if other APIs are to be used. ~ won't be recognized as HOME DIR in the 2nd class path in -addToClassPath.
-eclipse creates a debug launch configuration for the named eclipse project. It might stay the same for several applications under the same project.
1.2.1 2009-4-14 Static String Internationalization
add following to module xml file:
<inherits name="com.google.gwt.i18n.I18N"/>
The i18nCreator tool automates the generation of Constants interface subinterfaces like the one above. The tool generates Java code so that you only need to maintain the .properties files. It also works for ConstantsWithLookup and Messages classes.
~/Foo> i18nCreator -eclipse Foo -createMessages com.example.foo.client.MyConstants Created file src/com/example/foo/client/MyConstants.properties Created file MyConstants-i18n.launch Created file MyConstants-i18n
Note that MyConstants is declared as an interface, so you can not instantiate it directly with new. To use the internationalized constants, you create a Java instance of MyConstants using the GWT.create(Class) facility:
public void useMyConstants() {
MyConstants myConstants = (MyConstants) GWT.create(MyConstants.class);
Window.alert(myConstants.helloWorld());
}
1.3 2009-3-22 Google Web Toolkit in a Linux 64-bit environment
http://theworldofapenguin.blogspot.com/2008/05/google-web-toolkit-in-linux-64-bit.html
Unfortunatly GWT is only shipped as 32-bit and I am running a 64-bit Ubuntu 8.04. The SVN-repository of GWT include some pre-compiled libraries which are compiled for 32-bit Linux.
The easy way is to download and install a 32-bit Java from Sun. Following the GWT getting started got me up and running. But I wanted to use my usual Eclipse in 64-bit version.
So I started Eclipse and imported my lab project according to the instructions. Then it was time for some settings.
First, add the 32-bit Java as an JRE to Eclipse:
- Open the Window menu and select Preferences.
- Select Java and then Installed JREs in the tree menu to the left.
- Select add and pointed out where you installed your 32-bit Java (/usr/lib/jvm/ia32-java-6-sun) and closed all the dialogs.
Now its time to connect the GWT-project to the 32-bit Java.
- Right clicked on your imported GWT-project.
- Chose "Run As/Debug As" settings.
- In the new dialog that show up; select the JRE tab. Click on Alternate JRE and select your 32-bit java in the drop down.
Click now on the run button and check that everything works.
1.4 2009-3-22 Eclipse repeatedly crash
after importing the eclipse project generated by projectCreator, eclipse often crashes while doing something looking like compilation. Usually it happens after the GWT-project has been run from eclipse and/or compiled from the host browser.
It looks like despite setting the JRE to 32bit in "Run As/Debug As", the Builders in the project Properties is still linked to 64bit. So either remove that from Properties window or edit .project file and manually remove the <buildSpec> section.
1.5 2009-4-3 how to call other javascript library by JSNI embedded in Java
In direct javascript, the call would be new google.visualization.DataTable(eval("("+json+")"), 0.5);.
In JSNI, $wnd has to be added in front of the library to refer to the main window. don't know why:
private final native DataTable asDataTable(String json) /*-{
dataTable = new $wnd.google.visualization.DataTable(eval("("+json+")"), 0.5);
return dataTable;
}-*/;
If you forget $wnd, you'll get this:
[ERROR] Uncaught exception escaped
com.google.gwt.core.client.JavaScriptException: (ReferenceError): google is not defined
fileName: file:/usr/local/home_ubuntu/crocea/script/variation/web_interface/GWAWeb/src/edu/nordborglab/client/AccessionByName.java
lineNumber: 166
stack: ("{cols...
1.6 2009-4-3 fireSelectionEvent() error
For mysterious reason, my class, MapWithPhenotype, which extends AbstractVisualization can't do following:
MapWithPhenotype.this.fireSelectionEvent();
Got error in host mode:
[ERROR] Uncaught exception escaped
com.google.gwt.core.client.JavaScriptException: (TypeError): a has no properties
fileName: http://www.google.com/uds/api/visualization/1.0/106e5993e2d411fcd39f3f56caedfce4/default,table,motionchart+en.I.js
lineNumber: 269
stack: Pm(null,"select")@http://www.google.com/uds/api/visualization/1.0/106e5993e2d411fcd39f3f56caedfce4/default,table,motionchart+en.I.js:269
Rm(null,"select",null)@http://www.google.com/uds/api/visualization/1.0/106e5993e2d411fcd39f3f56caedfce4/default,table,motionchart+en.I.js:269
(null,"select",null)@http://www.google.com/uds/api/visualization/1.0/106e5993e2d411fcd39f3f56caedfce4/default,table,motionchart+en.I.js:277
(null)@jar:file:/usr/local/home_ubuntu/crocea/script/gwt-visualization-1.0.0/gwt-visualization.jar!/com/google/gwt/visualization/client/AbstractVisualization.java:76
public void com.google.gwt.maps.client.impl.EventImpl$VoidCallback.callbackWrapper()()@:0
([object Object])@transient source for com.google.gwt.maps.client.impl.__EventImplImpl:105
apply([object Object],[object Array])@:0
([object Array])@http://maps.google.com/intl/en_us/mapfiles/151e/maps2.api/main.js:365
([object Object],1)@http://maps.google.com/intl/en_us/mapfiles/151e/maps2.api/main.js:344
p([object Array],(function (d) {if (mn) {d.as(c);} else {try {d.as(c);} catch (e) {}}}))@http://maps.google.com/intl/en_us/mapfiles/151e/maps2.api/main.js:158
S([object Object],"click",[object Object])@http://maps.google.com/intl/en_us/mapfiles/151e/maps2.api/main.js:344
Pu([object Event])@http://maps.google.com/intl/en_us/mapfiles/151e/maps2.api/main.js:1296
@:0
at com.google.gwt.visualization.client.AbstractVisualization.fireSelectionEvent(Native Method)
at com.google.gwt.visualization.client.AbstractVisualization.fireSelectionEvent(AbstractVisualization.java:105)
Firebug would simply report a is null ... default,table,motionchart+en.I.js.
Selection.addSelectHandler(this, handler); won't work either in public final void addSelectHandler(SelectHandler handler) with similar error.
1.7 2009-4-14 use existing/external JAVA API
1.7.1 through source
Assume your project's module file is com/example/myproject/MyProject.gwt.xml. Place the source for the UUID class into com/example/myproject/module/UUID.java. Then add a line to MyProject.gwt.xml:
<source path="module" />
Then com/example/myproject/module/UUID.java is visible to the compiler as it is. In UUID.java, package com.example.myproject.module; is required, which makes it not as portable as super-source.
1.7.2 through super-source
Assume your project's module file is com/example/myproject/MyProject.gwt.xml. Place the source for the UUID class into com/example/myproject/jre/java/util/UUID.java. Then add a line to MyProject.gwt.xml:
<super-source path="jre" />
This tells the compiler to add all subfolders of com/example/myproject/jre/ to the source path, but to strip off the path prefix up to and including jre. As a result, com/google/myproject/gwt/jre/java/util/UUID.java will be visible to the compiler as java/util/UUID.java, which is the intended result.
1.8 2009-4-17 google Maps API for GWT
1.8.1 2009-4-17 map half blank
When I was using several maps in the same page under different tabs, the maps that are not in the immediate tab would show up as half blank. It happens in a google visualization map application as well (pure javascript, no GWT). Resizing the browser window would make the whole map appear.
Turns out calling map.setSize(width, height) upon tab selection would render the whole map as well. This is actually better than Resizing the browser window, because it only needs to be called once.
1.9 2009-4-21 browser client x,y position upon scrolling
If there is vertical/horizontal scrollbar and the user has scrolled down, panel's Y/X position does not take into account the scroll amount causing the context menu to appear somewhere else. So need to add the scroll position:
int left = evt.getClientX() + Window.getScrollLeft(); int top = evt.getClientY() + Window.getScrollTop();
1.10 2009-4-21 judge string identity
String object in java has special comparable function, equals(), which seems to be different from ==. I discerned it because == only works in the compiled javascript code, not in GWT shell (which i assume runs raw java).:
String col_id = dataTable.getColumnId(i);
if (col_id.equals("id"))
phenotype_id_idx = i;
1.11 2009-4-24 draw() of a GWT visualization widget isn't allowed to be customized in inheritance
The draw() function of GWT visualization widget is marked as final, which means no override. The way around it is to construct a class extending the AbstractVisualization class while having the intended GWT widget as a private member.
But if you don't intend to customize the draw(), class extension is supported.
1.12 2009-4-25 unused import from a un-included jar spoils GWT compilation
Several GWT applications reside in one GWT project, which has several jars included in eclipse environment. However some application's compile doesn't include every jar because it's not used. Importing libraries from those excluded jars (like the visualization GWT jar) in that application won't be marked as error in eclipse which is running project-wise setting.
Weird irrelevant errors surface under this kind of setting. For example, visualization.client.DataTable is imported but visualization jar is not included in application HaplotypeView. I get this error:
Computing all possible rebind results for 'edu.nordborglab.client.HaplotypeView'
Rebinding edu.nordborglab.client.HaplotypeView
Checking rule <generate-with class='com.google.gwt.user.rebind.ui.ImageBundleGenerator'/>
[ERROR] Unable to find type 'edu.nordborglab.client.HaplotypeView'
[ERROR] Hint: Previous compiler errors may have made this type unavailable
[ERROR] Hint: Check the inheritance chain from your module; it may not be inheriting a required module or
a module may not be adding its source path entries properly
[ERROR] Build failed
Checking rule sometimes becomes Checking rule <generate-with class='com.google.gwt.maps.jsio.rebind.JSFlyweightWrapperGenerator'/>.
1.13 2009-4-25 double jsonify vs single jsonify
double jsonify refers to the situation that on the server end (before sending data to client), a data structure is encoded into json structure, which gets jsonified again. single jsonify means the data structure undergoes only one jsonify before being sent out.
When the client receives the data, it has to first decode the json structure, for example by calling YAHOO.lang.JSON.parse. In my experience so far, double jsonify and single jsonify doesn't affect the outcome of this yahoo library, which is usually invoked in handwritten javascript code.
However, GWT's JSONParser.parse handles double jsonify and single jsonify differently. The decoded double jsonify gets a double quote (") on both ends of the string, which shall be removed beforehand:
JSONValue jsonValue = JSONParser.parse(response.getText()); String data = jsonValue.toString(); data = data.substring(1, data.length()-1);
1.14 2009-4-25 (TypeError): b.contentWindow has no properties
This error happens in the context like this:
[ERROR] Uncaught exception escaped com.google.gwt.core.client.JavaScriptException: (TypeError): b.contentWindow has no properties fileName: http://www.google.com/uds/api/visualization/1.0/.../default,columnchart,table,scatterchart,motionchart+en.I.js lineNumber: 729
It happened in the case that the visualization widget is not added/attached to the page (DOM) yet and can't find its parent window. This error is tricky, overlooked by the compiler.
1.15 2009-5-3 code-sharing between GWT applications
Two choices:
javascript-level. Embed the path of the compiled javascript code from one application into the other's html. Two javascript codes handle two different divisions (div). You can instruct one application to move the other's div to appropriate positions. Two applications are independent at the java level.
A tricky problem arises after the embedded is compiled and the host is not, the host page would be wholly supplanted by the embedded one. Don't know why. But a fresh re-compilation of the host solves the problem. Maybe name conflict in the generated js-codes/html-pages for shared modules between two apps.
java-level. just sharing a java class. At the javascript level, two are independent.
2 Syntax
2.1 2009-4-7 No Default Argument Values in Java
unlike python, php, c++, java doesn't allow default argument in functions (like void function(int a, int b=0);)
a solution posted on the web utilizing the null:
public void methodA(A arg1, B arg2, C arg3) {
arg1= arg1 != null ? arg1 : new A();
arg2= arg2 != null ? arg2 : new B();
arg3= arg3 != null ? arg3 : new C();
}
public void methodA(A arg1, B arg2) { C arg3=null; methodA(arg1, arg2, arg3); }
2.2 2009-4-8 How to convert a string to a number
somebody suggest (int)str1 should work too. below is from http://www.java-tips.org/java-se-tips/java.lang/how-to-convert-a-string-to-a-number.html:
byte b = Byte.parseByte("123");
short s = Short.parseShort("123");
int i = Integer.parseInt("123");
long l = Long.parseLong("123");
float f = Float.parseFloat("123.4");
double d = Double.parseDouble("123.4e10");
2.3 2009-4-24 Building a jar
All compiled classes are stored in directory bin/, package hierarchy starting from directory com/.:
jar cvf showcase.jar -C bin/ com/
jar created from java source code can't be used to compile with others (might be wrong, but at least eclipse won't recognize the package hierarchy.)
However, for GWT, the .class jar is not enough, the GWT compiler looks for both source code and .class. So after including the jar, include the source code directory by super-source directive in .gwt.html. This might be GWT-only, which might require the jar to include both source code and compiled classes.
2.4 2009-5-18 javadoc
http://www.tns.lcs.mit.edu/manuals/java-tools/javadoc.html
In order to let javadoc automatically generate documentation for the classes/functions, the documentation has to be placed right above the class or function (opposite in python) and use the special comment:
/**
* Return
*/
public char charAt(int index) {
...
}