Skip to main content

event handlers in jsr 226

7 replies [Last post]
shydisturbedboy
Offline
Joined: 2007-09-29
Points: 0

how do i register event handlers on jsr 226? i tried this:

doc = svgImage.getDocument();
svg = (SVGSVGElement)doc.getDocumentElement();
EventTarget t = (EventTarget)svg;
t.addEventListener("click", new LayerManager(), false);

but this only registers the root.. i want to register all elements. do i have to register the elements one by one? please help me..

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
venkyn
Offline
Joined: 2007-04-19
Points: 0

The problem, as you've stated, is you need pointer events and so it will work on a touch tone phone. On other phones you need to create your own focus management and synthesize the pointer events.
This Picture decorator demo
https://meapplicationdevelopers.dev.java.net/uiLabs/PictureDecorator.html
does its focus management, using a cursor. It keeps track of where the cursor is, and
gives focus to the element beneath it, when the select button is pressed. The code for this demo is available at
https://meapplicationdevelopers.dev.java.net/source/browse/meapplication...

shydisturbedboy
Offline
Joined: 2007-09-29
Points: 0

Thank you!!! i forgot about this example.. Thanks again!!!

shydisturbedboy
Offline
Joined: 2007-09-29
Points: 0

anyone?

Terrence Barr - Evangelist, Java Mobile & Embedded

Hi again,

Vincent is currently on vacation and I haven't tried yet to
reproduce your problem.

I'll try to find someone else who can help.

-- Terrence

meappdev-interest@mobileandembedded.org wrote:
> anyone?
> [Message sent by forum member 'shydisturbedboy' (shydisturbedboy)]
>
> http://forums.java.net/jive/thread.jspa?messageID=239033
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@meapplicationdevelopers.dev.java.net
> For additional commands, e-mail: interest-help@meapplicationdevelopers.dev.java.net
>
>
[terrence.barr.vcf]
---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@meapplicationdevelopers.dev.java.net
For additional commands, e-mail: interest-help@meapplicationdevelopers.dev.java.net

shydisturbedboy
Offline
Joined: 2007-09-29
Points: 0

can someone please help me... i still do not know the answer and the solution..

i have new questions.. does "click" only work with phones with pointers?
"DomActivate" activates the element, right? what does "activate" mean?
when i add DOMActivate at the root, only the root is accessed and not the other elements. why? how do i access all elements with one addEventListener()?

vhardy
Offline
Joined: 2004-02-18
Points: 0

You are doing the right operation to register an event handler.

When you register on the root element, you'll get events for all of the children elements target of the event which you register for. If you register for "click", you should get a "click" event every time one of the children elements is clicked.

Example:





With the event handler that you registered, you'll get a callback if the user clicks on "rectA", "rectB" or "circleA". This is because of the 'bubbling' event mechanism, see:

http://www.w3.org/TR/xml-events/#s_intro

shydisturbedboy
Offline
Joined: 2007-09-29
Points: 0

thanks for the link.. but in my midlet the root is the only one that is registered..
can you please take a look at my sample midlet? i'll post the code below..

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.m2g.SVGImage;
import javax.microedition.m2g.ScalableGraphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import org.w3c.dom.Document;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.svg.SVGElement;
import org.w3c.dom.svg.SVGPoint;
import org.w3c.dom.svg.SVGSVGElement;

public class Test extends MIDlet implements CommandListener {

public Test() {
}

protected void startApp() throws MIDletStateChangeException {
InputStream imageStream = Test.class.getResourceAsStream("/test.svg");
SVGImage svgImage;

try {
try {
svgImage = (SVGImage)SVGImage.createImage(imageStream, null);
}
finally {
try {
imageStream.close();
} catch (IOException e) {
// ignore
}
}
} catch (IOException e) {
destroyApp(false);
notifyDestroyed();
return;
}

MyCanvas c = new MyCanvas(svgImage);

getDisplay().setCurrent(c);
}

public Display getDisplay() {
return Display.getDisplay(this);
}

protected void pauseApp() {
}

protected void destroyApp(boolean b) throws MIDletStateChangeException {
}

public void commandAction(Command command, Displayable displayable) {
}

class MyCanvas extends Canvas {

private ScalableGraphics sg;
private SVGImage svgImage;
private Document doc;
private SVGSVGElement svg;

public MyCanvas(SVGImage svgImage) {
this.svgImage = svgImage;

svgImage.setViewportWidth(getWidth());
svgImage.setViewportHeight(getHeight());

sg = ScalableGraphics.createInstance();
sg.setRenderingQuality(sg.RENDERING_QUALITY_LOW);

doc = svgImage.getDocument();
svg = (SVGSVGElement)doc.getDocumentElement();
LayerManager layerManager = new LayerManager();

EventTarget t = (EventTarget)svg;
t.addEventListener("click", new LayerManager(), false);
}

protected void paint(Graphics g) {
// *** clear the display
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());

// *** render the SVG image
sg.bindTarget(g);
sg.setTransparency(1f);
sg.render(0, 35, svgImage);
sg.releaseTarget();

g.setColor(255,0,0);
g.fillRect(115, 155, 10, 10);
}

protected void keyRepeated(int keyCode) {
keyPressed(keyCode);
}

protected void keyPressed(int keyCode) {
SVGPoint origin = svg.getCurrentTranslate();
switch(keyCode) {
case -1:// up
origin.setY(origin.getY() + 3f);
break;
case -2:// down
origin.setY(origin.getY() - 3f);
break;
case -3:// left
origin.setX(origin.getX() + 3f);
break;
case -4:// right
origin.setX(origin.getX() - 3f);
break;
case -5:
svgImage.dispatchMouseEvent("click", 120, 160);
break;
}
repaint();
}
}

class LayerManager implements EventListener {
public void handleEvent(Event evt) {
SVGElement targetElement = (SVGElement)evt.getCurrentTarget();
System.out.println("TARGET: " + targetElement.getTrait("id"));
System.out.println(evt.getType());
}
}

}

and here is the svg file:



]>
viewBox="0 0 185.042 252.064" xml:space="preserve">