Thanks Deepayan !
It worked great !

You can take a look at my commented sloppy code below.
It's mostly copy and paste from clickFocus.

If you ever get a chance I suggest you divide clickFocus in in two
functions:
  - trellis.clickFocus:
    Would behave like the function you have now but would call
xyClickFocus() to do most of the work.
    It would mostly call grid.location()
 - trellis.clickFocusXY(x,y)  (I am not refering to the code below, this
function would be different)
   Would do most of the work, and would be great to embed an R device into
any GUI.

I don't know if there is any other way to go around who owns the event loop,
either the REngine or the GUI where the R device is embedded.

Here is my modified sloppy code (I just have been playing with R for a
week):

xyclickFocus <-
        function(x,
                y,
                clip.off = FALSE,
                highlight = interactive(),
                ...,
                guess = TRUE,
                verbose = TRUE)
{
    library(lattice) ### New
    library(grid)     ### New
    trellis.unfocus()  ### Modified, from trellis.focus())
    layoutMatrix <- trellis.currentLayout()
    if (guess && sum(layoutMatrix > 0) == 1)
    {
        ## there's only one panel, so just select it
        w <- which(layoutMatrix > 0)
        focusRow <- row(layoutMatrix)[w]
        focusCol <- col(layoutMatrix)[w]
        if (verbose) message(sprintf("Selecting panel at position (%g, %g)",
focusRow, focusCol))
    }
    else if (all(layoutMatrix == 0))
    {
        warning("No panels available")
        return()
    }
    else
    {
        x <- grconvertX(x, "device", "npc") ### New
        y <- grconvertY(y, "device", "npc") ### New
        x <- unit(x, "native") ### New
        y <- unit(y, "native") ### New
        clickLoc = list(x=x,y=y)  ### Modified, changed from <-
grid.locator("npc")
        glayout <-lattice:::lattice.getStatus("layout.details")    ###
Modified, added lattice:::
        rowRange <- range(glayout$pos.heights$panel,
glayout$pos.heights$strip)
        colRange <- range(glayout$pos.widths$panel,
glayout$pos.widths$strip.left)
        layCols <-  glayout$page.layout$ncol
        layRows <- glayout$page.layout$nrow
        leftPad <-
convertX(sum(glayout$page.layout$widths[1:(colRange[1]-1)]), "npc",
valueOnly = TRUE)
        rightPad <-
convertX(sum(glayout$page.layout$widths[(colRange[2]+1):layCols]), "npc",
valueOnly = TRUE)
        topPad <-
convertY(sum(glayout$page.layout$heights[1:(rowRange[1]-1)]), "npc",
valueOnly = TRUE)
        botPad <-
convertY(sum(glayout$page.layout$heights[(rowRange[2]+1):layRows]), "npc",
valueOnly = TRUE)
        message("Click on panel to focus")
        if (is.null(clickLoc)) return()
        clickXScaled <- (as.numeric(clickLoc$x) - leftPad) / (1 - leftPad -
rightPad)
        focusCol <- ceiling(clickXScaled * ncol(layoutMatrix))
        clickYScaled <- (as.numeric(clickLoc$y) - botPad) / (1 - botPad -
topPad)
        focusRow <- ceiling(clickYScaled * nrow(layoutMatrix))
        if (lattice:::lattice.getStatus("as.table")) focusRow <-
nrow(layoutMatrix) - focusRow + 1   ### Modified, added lattice:::
    }
    if ((focusCol >= 1) && (focusCol <= ncol(layoutMatrix)) &&
            (focusRow >= 1) && (focusRow <= nrow(layoutMatrix)) &&
            layoutMatrix[focusRow, focusCol] > 0)
    {
        trellis.focus("panel", column = focusCol, row = focusRow,
                clip.off = clip.off, highlight = highlight,
                ...)
    }
    else
    {
        focusCol <- focusRow <- 0
    }
    invisible(list(col=focusCol, row=focusRow))
}


On Thu, Oct 23, 2008 at 2:55 PM, Deepayan Sarkar
<deepayan.sarkar@gmail.com>wrote:

> On Thu, Oct 23, 2008 at 12:26 PM, Daniel Kornhauser
> <dkor@northwestern.edu> wrote:
> > Hi:
> >
> > I would like to find out the panel of a xyplot matrix where a mouse
> clicked.
> >
> > I know this functionality is already bundled in trellis.focus but I can't
> > use it because I am coding a stand alone application in Java using with
> > GDCanvas as a graphics device.
> >
> > I tried calling trellis.focus from Java with:
> >   re.eval("t = trellis.focus()"));
> > However it did not work for an embedded GDCanvas. The above call to the
> > trellis.focus() returns null no matter where I click.
> >
> > (Side note: the functionality must be implemented in the GDCanvas because
> > trellis.focus does work correctly in JGR)
> >
> > I wish to handle all the user GUI events in Java to evaluate different
> > commands in an Rengine.
> > With a GDCanvas, it's straightforward to get the x y position of a mouse
> > click with the standard e.getX() and e.getY()
> > So my handler would look like:
> >    public void mouseClicked(MouseEvent e) {
> >        System.out.println("Clicked" + e.getX() + " " + e.getY());
> >        System.out.println(re.eval("t = trellis.focus()"));
> >        System.out.println(re.eval("t"));
> >       // I would update the panel here ...
> >    }
> >
> > I am only using Java because:
> > - I am proficient in Java GUI programmer
> > - It is multiplatform
> >
> > Another way of posing the question is:
> > Given the coordinates from grid.location how can I find out the plane of
> an
> > xyplot matrix where the user clicked.
> >
> > I have tried to figure out how to do it from code using
> trellis.clickFocus,
> > but I have have only been playing with R for a week, so I am quite lost
> > reading R code.
>
> The approach in trellis.clickFocus should get you close. One important
> point is that
>
> clickLoc <- grid.locator("npc")
>
> returns the location in NPC, whereas your e.getX() and e.getY() would
> be in device coordinates. help(grconvertX) should tell you how to make
> the conversion.
>
> -Deepayan
>

	[[alternative HTML version deleted]]

