YUIGWT
A Java GWT API for the great JavaScript YUI lirary
Project home page: http://code.google.com/p/yuigwt/
Example Gallery with online Java sources : http://cancerbero.vacau.com/yuigwt/
The project is very very new, but a general pattern for migrating all the YUI JavaScript API to Java was found, using 100% GWT Overlay types for a zero overhead Java API. A lot of components are already ready to be used, check the example gallery that will reflect current progress.
The blog of Sebastián Gurin about software development. "A place to store those little things that time will erase if not written...."
jueves, 9 de agosto de 2012
viernes, 20 de julio de 2012
Eclipse Java Code Templates for GWT related code automation
I often develop GWT libraries that port an existing javascript library like for example the raphael4gwt project that ports the javascript drawing library raphael to GWT / Java. I uses a lot of GWT Overlay types for dealing with native javascript objects from Java code.
GWT overlay types are not common java objects, in fact, at runtime, they "overlay" a native javascript object created outside GWT for example a DOM node. In my libraries I discovered that a good way to work with overlay types from java is like the following code.
Suppose I need to overlay a simple javascript object like the following in a java overlay type:
Now wuppose I want to create a GWT overlay type for this Stroke objects. In my experience I would like to define the overlay type, so I can create the same previous javascript object using the following java :
GWT overlay types are not common java objects, in fact, at runtime, they "overlay" a native javascript object created outside GWT for example a DOM node. In my libraries I discovered that a good way to work with overlay types from java is like the following code.
Suppose I need to overlay a simple javascript object like the following in a java overlay type:
{
color: "#ededed",
dashStyle: "none"
}
(this is an example taken from a real javascript library, YUI 3 - http://yuilibrary.com/yui/docs/api/classes/Circle.html#attr_stroke) Now wuppose I want to create a GWT overlay type for this Stroke objects. In my experience I would like to define the overlay type, so I can create the same previous javascript object using the following java :
Stroke stroke1 = Stroke.create().
color("#ededed").
dashStyle("none");
The overlay type must be something like the following java class:
package org.sgx.yui4gwt.yui.graphic;
import com.google.gwt.core.client.JavaScriptObject;
/**GWT Overlay type for yui graphic stroke objects -
* http://yuilibrary.com/yui/docs/api/classes/Circle.html#attr_stroke
* @author sg
*/
public class Stroke extends JavaScriptObject {
protected Stroke() {}
public static final String LINECAP_BUTT = "butt",
LINECAP_SQUARE = "square", LINECAP_ROUND = "round";
public static final String DASHSTYPE_NONE = "none",
DASHSTYPE_TODO = "...";
/** static constructor */
public static final native Stroke create()/*-{
return {};
}-*/;
/**
* The color of the stroke.
*
* @return
*/
public native final String color() /*-{
return this.color;
}-*/;
/**
* The color of the stroke.
*
* @param val
* @return this - for setter chaining
*/
public native final Stroke color(String val) /*-{
this.color = val;
return this;
}-*/;
/**
* Number that indicates the width of the stroke.
*
* @return
*/
public native final double weight() /*-{
return this.weight;
}-*/;
/**
* Number that indicates the width of the stroke.
*
* @param val
* @return this - for setter chaining
*/
public native final Stroke weight(double val) /*-{
this.weight = val;
return this;
}-*/;
/**
* Number between 0 and 1 that indicates the opacity of the stroke. The
* default value is 1.
*
* @return
*/
public native final double opacity() /*-{
return this.opacity;
}-*/;
/**
*
* @param val
* @return this - for setter chaining
*/
public native final Stroke opacity(double val) /*-{
this.opacity = val;
return this;
}-*/;
/**
*
* @return
*/
public native final String dashStyle() /*-{
return this.dashStyle;
}-*/;
/**
*
* @param val
* @return this - for setter chaining
*/
public native final Stroke dashStyle(String val) /*-{
this.dashStyle = val;
return this;
}-*/;
}
And now the fun part. Writing more and more of this kind of overlay types I discovered that I can automate the work with eclipse java code templates. Just create an eclipse java code template going to Preferences->Java->Editor->Templates, and create a new template called "gwtOverlay1" with the following body:/**
*
* @return
*/
public native final ${attributeType} ${attributeName}() /*-{
return this.${attributeName};
}-*/;
/**
*
* @param val
* @return this - for setter chaining
*/
public native final ${enclosing_type} ${attributeName}(${attributeType} val) /*-{
this.${attributeName} = val;
return this;
}-*/;
this way, when you need editing your overlay types in the eclipse java editor, to add another property to your overlay type, just use this template. type "gwt" and press ctrl+space. Choose "gwtOverlay1" and press enter. The template code will generated, and you can refactor the property type and property name.yuigwt java code templates
In http://code.google.com/p/yuigwt/, where I work 100% with these kind of overlay types, I'm automatizing the Java definition of YUI attributes using getters/setters methods created with Java code templates. This is the template for those cases.
/**
*
* @return
*/
public native final ${type} ${name}() /*-{
return this.get("${name}");
}-*/;
/**
*
* @param val
* @return this - for method chaining
*/
public native final ${enclosing_type} ${name}(${type} val) /*-{
this.set("${name}", val);
return this;
}-*/;
UIBinder @UIFactory
When writing YUIGWT with UIBinder (as documented in TODO LINK), if we reference a "YUIBinded" in another "YUIBinded" because "YUIBinded" required to pass a YUIContext as a constructor parameter, it is useful to autogenerate the @UIFactory method. This is a very personal note since I dot think anybody is working with this tech besides me. This is the template:
/**
* Will be used for creating ${T} instances of this UIBinder widget.
* @return a new ${T}
*/
@UiFactory
public ${T} make${T}() {
return new ${T}(y);
}
HTML Minimizator
I had to develop my own HTML code minimization application for my own specific needs, and here I would like to share the experience with the public.I called HTML minimizator and it is available online at htmlminimizator online web page.
A little background - while developing javascvript toolkits porting to Java GWT or to Java2Script frameworks I often need to copy javascript documentation to my javadocs classes comments for documenting java methods or classes that correspond to some javascript function or object.
For this I can copy the HTML sources of the javascript toolkit's documentation,
and paste it directly into the javadoc and it will look the same in generated html javadocs. This is really usefull and let me copy directly from the javascript toolkit documentation htmls.
For this, I want the javadoc size to be the minimun as possible and also I need to erase ALL html attributes like ids, hrefs, etc. Also I would like to eliminate any empty elements
That is what this little application HTML Minimizator try to do. It is a small GWT appilcation that let you paste XML valid code fragment, configure a little options and then get the minimified HTML.
I know there are a lot of HTML minimizators on the web, but none of them do an acceptable work for me, most of all because they won't eliminate attributes.
The project is hosted at https://code.google.com/p/gwteditors/ svn, since it uses my project gwteditors and it is very little for its own project.
For example, for default configuration, an html code like this:
A little background - while developing javascvript toolkits porting to Java GWT or to Java2Script frameworks I often need to copy javascript documentation to my javadocs classes comments for documenting java methods or classes that correspond to some javascript function or object.
For this I can copy the HTML sources of the javascript toolkit's documentation,
and paste it directly into the javadoc and it will look the same in generated html javadocs. This is really usefull and let me copy directly from the javascript toolkit documentation htmls.
For this, I want the javadoc size to be the minimun as possible and also I need to erase ALL html attributes like ids, hrefs, etc. Also I would like to eliminate any empty elements
That is what this little application HTML Minimizator try to do. It is a small GWT appilcation that let you paste XML valid code fragment, configure a little options and then get the minimified HTML.
I know there are a lot of HTML minimizators on the web, but none of them do an acceptable work for me, most of all because they won't eliminate attributes.
The project is hosted at https://code.google.com/p/gwteditors/ svn, since it uses my project gwteditors and it is very little for its own project.
For example, for default configuration, an html code like this:
< p>< code>Shape< /code> has the following implementations based on browser capability.< /p>
< ul id="yui_3_5_1_1_1342815626205_173">
< li>< a href="SVGShape.html">< code>SVGShape< /code>< /a>< /li>
< li id="yui_3_5_1_1_1342815626205_172">< a id="yui_3_5_1_1_1342815626205_171" href="VMLShape.html">< code>VMLShape< /code>< /a>< /li>
< li>< a href="CanvasShape.html">< code>CanvasShape< /code>< /a>< /li>
< /ul>
will be minimized to the following html code:
< p>< code>Shape< /code> has the following implementations based on browser capability.< /p>< ul>< li>< a>< code>SVGShape< /code>< /a>< /li>< li>< a>< code>VMLShape< /code>< /a>< /li>< li>< a>< code>CanvasShape< /code>< /a>< /li>< /ul>
lunes, 2 de julio de 2012
Disable all but one html element with jquery
They asked me to be able to disable all the html document but a single html element. Have done this with jquery like the following html snippet. The idea is to add 4 absolute positioned boxed "around" the given element, with big z-index with some transparency:
< !doctype html>
< html lang="es-UY">
< head>
< title>test< /title>
< script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript">< /script>
< /head>
< body>
< script>
/**
* disablingFocus - disable all the document instead a single element - put 4 masks around the given element
so it appear that all except that element is disabled. */
var lib = {
maskStyle: {
zIndex: 9998,
position: "absolute",
display: "block",
opacity: 0.5,
backgroundColor: "#111111"
},
mask: null,
disableAllBut: function(el) {
if(!lib.masks) {
lib.masks=[];
for(var i = 0; i< 4; i++) {
var maskEl = document.createElement("div");
document.body.appendChild(maskEl);
$(maskEl).attr({id: "disableAllButMask"+i});
$(maskEl).css(lib.maskStyle);
lib.masks.push(maskEl)
}
}
var x = $(el).offset().left,
y = $(el).offset().top,
w = $(el).width(),
h = $(el).height();
var maxWidth = 3333, //$(document.body).offset().width,
maxHeight = 3333; //$(document.body).offset().height;
$(lib.masks[0]).css({
"left": "0px", "top": "0px", "height": y+"px", "width": maxWidth+"px"
});
$(lib.masks[1]).css({
"left": "0px", "top": (y+h)+"px", "height": maxHeight+"px", "width": maxWidth+"px"
});
$(lib.masks[2]).css({
"left": 0+"px", "top": (y)+"px", "height": h+"px", "width": x+"px"
});
$(lib.masks[3]).css({
"left": (x+w)+"px", "top": (y)+"px", "height": h+"px", "width": maxWidth+"px"
})
},
"":""
};
window.disabler=lib;
window.disableAllBut=lib.disableAllBut;
< /script>
< style type="text/css">
< /style>
< div class="apagado">
hola 1
< table>
< tr>< td>hola hola< /td>
< td>
< div class="prendido">
hola 2< p>hhhhh< a href="">hshshs< /a>< /p>
< /div>
< /td>
< /tr>
< tr>
< div id='div3'>
hola 3 asd < span id="div4">asd< /span> as d
< /div>
< /tr>
< /table>
< /div>
< button onclick="disableAllBut(document.getElementById('div4'))">click me for the disabling< /button>
< /html>
viernes, 29 de junio de 2012
Disable HTML elements programatically
They asked me to disable / disable some html elements in the client side with javascript so the user cannot interact with cerrtain parts of an html document at some point. My sollution, based on jquery, is to put an html element in front of each elments we want to disable. This mask should be transparent and positioned absolutely with a big z-index. The following is an HTML document that demonstrates the problem and contains a simple implementation of a sollution. Hope it can be usefull to somebody needing something similar.
< !doctype html>
< html lang="es-UY">
< head>
< script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript">< /script>
< title>test< /title>
< /head>
< script type="text/javascript">
var lib = {
idCounter: 0,
disablers: [],
/**
* disable the given html elements by putting a mask html element with the same bounds in front of each given element. use disablers.enable for enabling them programatically.
* @param el - a reference to the elements to disable. For example, ".c1" will disable all elements of CSS class c1, "#el1" will disable the element id el1.
* @param maskCSS - optional - an object with extra CSS value for the mask, for example: {"background-color": "red", opacity: 0.2}
* @return the masks elements (array of jquery object) created
*/
disable: function(el, maskCSS) {
var masks = [];
$(el).each(function(){
masks.push(lib.disableOne(el, maskCSS));
});
return masks;
},
/**
* the same as disable, but treat the given object as a single element
*/
disableOne: function(el, maskCSS) {
var id = $(el).attr("id");
if(!id) {
lib.idCounter++;
id="disabler_"+lib.idCounter;
$(el).attr({"id": id});
}
if(!lib.disablers[id]) {
var dis = document.createElement("div");
document.body.appendChild(dis);
lib.disablers[id]=$(dis);
}
var dis = lib.disablers[id];
$(dis).css({
"position": "absolute", "z-index": 99999, "display":
"block", "left": $(el).offset().left, "top": $(el).offset().top,
"width": $(el).width(), "height": $(el).height(), "opacity": 0.4, "background-color": "black"
});
$(dis).show();
if(maskCSS)
$(dis).css(maskCSS);
return dis;
},
enable: function(el) {
if($(el).attr("id") & & lib.disablers[$(el).attr("id")]) {
var dis = lib.disablers[$(el).attr("id")];
dis.hide();
}
}
};
window.disabler=lib;
< /script>
< p>The following table contains a disabled element< /p>
< table>
< tr>< td id="el1">< button>hello< /button>< /td>< td>< p>alskjdlaksjd< /p>< /td>< /tr>
< tr>< td>< button onclick="window.disabler.enable('#el1')">click me for enable < /button>< /td>< td>< p>als< a href="">kjdlaksj< /a>d< /p>< /td>< /tr>
< /table>
< script type="text/javascript">
//examples:
//disable a single element with id "el1"
disabler.disable("#el1");
//disable all < a> elements:
disabler.disable("a");
< /script>
< /body>
< /html>
domingo, 10 de junio de 2012
JavaScript function inBetween
JavaScript function inBetween - be notified when a javascript function is called N times in between a time lapsus of T milliseconds.
This idea borned when reading the work of Ben Alman for function throttle : http://benalman.com/projects/jquery-throttle-debounce-plugin/
In that case the problem is to execute a function no more than once every T milliseconds. This is very helpful when you want to control how much an event handler is called back. I put a common example for events that are called intensively, like mousemove, or drag in my raphaël tutorial: http://cancerbero.vacau.com/ raphaeltut/index.html#sec- events-function-throttle There I explain very usefull cases of function throttle.
The thing is that function throttle made me think on the opposite problem: being notified when a function is called N times in a time lapsus of T milliseconds. This can be useful for registering double, triple, cuadruple clicks, and in general nth-clicks events.I call this, function inBetween (don't have a better name, suggestions accepted). See for example, how I would use inBetween with jquery for neing notified when a triple click occurs in between 1000 ms:
This is a complete working example: http://jsfiddle.net/RP7mW/11/.
Also there is a section in my raphaël tutorial for registering nth-click listeners to raphaël shapes using inBetween(). Link: http://cancerbero.vacau.com/raphaeltut/index.html#sec-events-inbetween
And this is the inBetween function code:
Hope this can be of some use for those who need some advance event - function stuff like this.
This idea borned when reading the work of Ben Alman for function throttle : http://benalman.com/projects/jquery-throttle-debounce-plugin/
In that case the problem is to execute a function no more than once every T milliseconds. This is very helpful when you want to control how much an event handler is called back. I put a common example for events that are called intensively, like mousemove, or drag in my raphaël tutorial: http://cancerbero.vacau.com/
The thing is that function throttle made me think on the opposite problem: being notified when a function is called N times in a time lapsus of T milliseconds. This can be useful for registering double, triple, cuadruple clicks, and in general nth-clicks events.I call this, function inBetween (don't have a better name, suggestions accepted). See for example, how I would use inBetween with jquery for neing notified when a triple click occurs in between 1000 ms:
function click3Hendler(evt) {
alert(this.clickCount+" clicks detected. last click xcoord: "+evt.clientX);
};
$("#someElement").click(inBetween(3, 1000, cb, {clickCount: 3}));
This is a complete working example: http://jsfiddle.net/RP7mW/11/.
Also there is a section in my raphaël tutorial for registering nth-click listeners to raphaël shapes using inBetween(). Link: http://cancerbero.vacau.com/raphaeltut/index.html#sec-events-inbetween
And this is the inBetween function code:
/**
* inBetween resolves the problem of being notified when a function is called N times in in between a time lapsus of T ms.
* @param n the amount of times.
* @param t the time lapsus in ms
* @param callback - the function to be called when the returned fcuntion is called at least n times in a lapsus of n ms.
* @return a new function - when that function is called n times in a lapsus
of t ms, then callback function will be called using the context object as
the callback context.
*/
function inBetween(n, t, callback, context) {
var sb = [];
sb.push("var that = arguments.callee; ")
sb.push("var thisTime = new Date().getTime(); ")
sb.push("var arr = that['ARR'];");
sb.push("if(!arr){");
sb.push(" arr = []; ");
sb.push(" for(var i = 0; i < that['N']; i++) arr.push(thisTime); ");
sb.push(" that['ARR'] = arr;");
sb.push(" that['COUNT']=0");
sb.push("}");
sb.push("that['COUNT']++; ");;
sb.push("arr.push(thisTime);");
sb.push("var lastTime = arr.shift();");
sb.push("if(that['COUNT'] >= that['N']) {");
sb.push(" that['COUNT']=1; ");
sb.push(" for(var i = 0; i < that['N']; i++) arr[i] = thisTime; ");
sb.push(" if(thisTime-lastTime < that['T']) ");
sb.push(" that['CB'].apply(that['CTX'], arguments); ");
sb.push("}");
var fn = new Function(sb.join(""));
fn['N']=n;
fn['T']=t;
fn['CB']=callback;
fn['CTX']=context;
return fn;
};
Hope this can be of some use for those who need some advance event - function stuff like this.
jueves, 17 de mayo de 2012
raphaelJs tutorial
I'm working on a raphaeljs tutorial. The tutorial is available here.
I made it in pure html and css, with a little javascript based on jquery.
It has nice dynamic features like
I made it in pure html and css, with a little javascript based on jquery.
It has nice dynamic features like
- references-links, this is links that opens in an iframe showing an anchor iframes with a method or property documentation of raphaeljs reference .
- code examples that can be runned online. A dialog with a raphal paper will be open with the example inside. The javascript code is prettyfied very using code-google-pretify
- auto creation of a table of content from h1, h2, h3, etc. HTML elements.
- auto creation of an index of terms, using class="index" in those html elements which contents I want to use as an index term.
Suscribirse a:
Entradas (Atom)