GWT - event.getX() and event.getClientX() - gwt

I added click handler to flowpanel as follows
this.addDomHandler(new ClickHandler(){
#Override
public void onClick(ClickEvent event) {
Window.alert("event.getX()="+event.getX()+" event.getY()="+event.getY());
Window.alert("event.getClientX()="+event.getClientX()+" event.getClientY()="+event.getClientY());
}
},ClickEvent.getType());
... as I could get it getX() returns mouse position within flowpanel but getClientX() returns another value and I couldn't get the value is coming from. So my question is what is the getClientXY() methods are used for?
P.S
GWT 2.2/2.3

The javadoc is clear:
getClientX: Gets the mouse x-position within the browser window's client area.
getX: Gets the mouse x-position relative to the event's current target element.
So, you can use getClientX to get the absolute left position of some element.

Related

Caret position of Eclipse text editor

I'm writing a little plugin to move the caret position of an Eclipse text editor to the other side of a selected block. Problem is that I don't find a nice way to discover whether the selection is left-to-right or right-to-left.
I understand there are these alternatives:
Use a CarretListener in some way. It seems unnecessary and I don't want to.
Get hold of the underlaying StyledText and compare selection with the caret position. Seems to break abstraction because I have to know how the editor is implemented. Another disadvantage is that you would have to use widgetOffset2ModelOffset methods on the text viewer to adjust positions.
Can't I get the caret position from my ITextEditor or ISelectionProvider or something?
Here is my code:
public class SwapCursorSelectionHandler extends AbstractHandler {
public Object execute( ExecutionEvent event )
{
ITextEditor editor;
try {
editor = (ITextEditor) HandlerUtil.getActivePartChecked( event );
} catch ( ExecutionException exc ) {
throw new RuntimeException( exc );
}
ITextSelection sel = (ITextSelection) editor.getSelectionProvider().getSelection();
// How to find out if sel is left-to-right or right-to-left?!
editor.selectAndReveal( ... );
return null;
}
}
Update: There doesn't seem to be a way to do this without using the StyledText. I think this is weird and I considers placing a bug report suggesting that selection direction information should be added to ITextSelection. Before I do this it would be interesting to get the opinion on people here at SO about this proposal.
EDIT: This solution turned out to be wrong! Thanks to willkil for pointing it out.
This not very elegant piece of code was the most canonical way to achieve this I could find. It uses ITextEditor.getAdapter(ITextOperationTarget.class) and JFaceTextUtil. This implies that it depends on a particular editor implementation, but at least I don't have to touch it myself or mess with the widget2model methods.
public Object execute(ExecutionEvent event) {
try {
ITextViewer viewer = (ITextViewer)
((ITextEditor) HandlerUtil.getActivePartChecked(event))
.getAdapter(ITextOperationTarget.class);
int caretOffset = JFaceTextUtil.getOffsetForCursorLocation(viewer);
} catch (ExecutionException exc) {
throw new RuntimeException(exc);
}
return null;
}
The best way to get the current cursor position is via ITextViewer.getTextWidget().getCaretOffset(). Here's an example that prints various text positions in an implementation of IContentAssistProcessor that I was working on:
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
int widgetCaretOffset = viewer.getTextWidget().getCaretOffset();
if (viewer instanceof ITextViewerExtension5) {
ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
System.out.println(extension.widgetOffset2ModelOffset(widgetCaretOffset));
}
System.out.println(JFaceTextUtil.getOffsetForCursorLocation(viewer));
System.out.println(offset);
System.out.println(widgetCaretOffset);
System.out.println(viewer.getSelectedRange());
}
I placed the caret at a random place in a document, then moved the mouse close to the beginning of the first line, then triggered the content assistant with various selections. In my case, the textViewer does not implement ITextViewerExtension5, so only four lines print. The output of the code above is below:
With nothing selected:
6
794
794
Point {794, 0}
With a left-to-right selection created by shift-right (caret blinking on right side of selection):
6
794
799
Point {794, 5}
Note that the caret position is 799, which equals 794 + 5.
With a left-to-right selection created by shift-left (caret blinking on left side of selection):
6
794
794
Point {794, 5}
Note that the caret position equals the selection offset.
Also note, although it's not relevant to this question, that the offset parameter in IContentAssistProcessor. computeCompletionProposals() is always the offset of the selection, not the caret.
If you have an ITextEditor instead of an ITextViewer, you can get the ITextViewer via the method from another answer to this question and from an answer to a different question:
ITextEditor editor;
ITextOperationTarget target = (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
if (target instanceof ITextViewer) {
ITextViewer viewer = (ITextViewer) target;
int widgetCaretOffset = viewer.getTextWidget().getCaretOffset();
if (viewer instanceof ITextViewerExtension5) {
ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
System.out.println(extension.widgetOffset2ModelOffset(widgetCaretOffset));
}
System.out.println(JFaceTextUtil.getOffsetForCursorLocation(viewer));
System.out.println(offset);
System.out.println(widgetCaretOffset);
System.out.println(viewer.getSelectedRange());
}

GWT overlapping images

i have one window panel and i want to set image in it.so i do,
Window window = new Window();
Panel panel = new Panel();
AbsolutePanel absolutePanel = new AbsolutePanel();
Image image = new Image("img/heat_map.jpg");
absolutePanel.add(image);
Image ap1Image = new Image("img/end.PNG");
ap1Image.getElement().getStyle().setMargin(1, Unit.PX);
absolutePanel.add(ap1Image);
panel.add(absolutePanel);
window.add(panel);
but i stuck in code as i can't overlap another small icon image on main image(heat_map).
i want onclick event on that icon image.but i can't overlap images in window panel.please help me out.
It seems that you using something like GXT not pure GWT. But anyway - AbsolutePanel should implement something like add(Widget, int left, int top) method so you need use it instead of simple add(widget)
First thing is in your code is you can not instantiate GWT Window Class since the constructor Window() is not visible.
Second thing is there is no add method in window class.
And finally to overlap your images one on another you need to apply Some CSS
(Z-index..positions )
CSS Divs overlapping, how do I force one above the other?
And finally
you can simply add a click handler to image.
imageIcon.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// Do something....
}
});
Good luck.

How to set the Droppable/Draggable Event on the mouse cursor

I've implemented the Drag-n-Drop effects in Wicket using the Ajax Behavior. If I dragg the Image over the tree nodes, the position of droppable accept is in the middle of image. How to set this position (event) on the cursor?
Thank you.
Also I found it. The solution is:
DroppableAjaxBehavior b = new DroppableAjaxBehavior() {
#Override
public void onDrop(Component droppedComponent, AjaxRequestTarget target) {
//do something to handle event
}
};
b.setTolerance(ToleranceEnum.POINTER);

GWT: How to center element containing CellTable?

I'm trying to center an element that contains a CellTable. The actual
centering logic works okay, but I'm having problems with all those
attaching/detaching events. Basically, I'm doing this in my container
widget:
#Override
public void onLoad() {
super.onLoad();
center();
}
However, it seems that onLoad on the container does not mean that all
children have loaded, so... the actual centering routine is called too
early and Element.getOffsetWidth/getOffsetHeight are both returning 0.
This results in the container being displayed with the left upper corner
in the center of the screen.
Same thing happens if I use an AttachEvent.Handler on the CellTable.
So... is there any event on CellTable, or on Widget or whatever that
allows me to trigger an action when the DOM subtree has been attached to
the DOM?
Thanks in advance.
Take a look at scheduleDeferred. A deferred command is executed after the browser event loop returns.
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
#Override
public void execute() {
center();
}
});
Override onAttach instead of onLoad. onAttach default implementation calls onLoad followed by doAttachChildren (which calls onAttach on each child widget), so the following code should call center after the children have been attached:
#Override
public void onAttach() {
super.onAttach();
center();
}
(BTW, the default implementation of onLoad is a no-op)

I need to know when a VerticalPanel changes size

I'm using gwt-dnd to implement drag-and-drop functionality in my GWT program. To get scrolling to work right, I need
<ScrollPanel>
<AbsolutePanel>
<VerticalPanel>
<!-- lots of draggable widgets -->
</VerticalPanel>
</AbsolutePanel>
</ScrollPanel>
I have to manually set the size of the AbsolutePanel to be large enough to contain the VerticalPanel. When I add widgets to the VerticalPanel, though, the size reported by VerticalPanel.getOffsetHeight() isn't immediately updated - I guess it has to be rendered by the browser first. So I can't immediately update the AbsolutePanel's size, and it ends up being too small. Argh!
My stop-gap solution is to set up a timer to resize the panel 500ms later. By then, getOffsetHeight will usually be returning the updated values. Is there any way to immediately preview the size change, or anything? Or, alternatively, can I force a render loop immediately so that I can get the new size without setting up a timer that's bound to be error-prone?
This is a common problem with DOM manipulations. The offsetHeight doesn't update until a short time after components are added. I like to handle this using a recursive timer until a pre-condition is violated. E.g. In your case let there be a function which adds components and will be defined as below:
public void addComponent(Widget w)
{
final int verticalPanelHeight = verticalPanel.getOffsetHeight();
verticalPanel.add(w);
final Timer t = new Timer(){
public void run()
{
if(verticalPanelHeight != verticalPanel.getOffsetHeight())
absolutePanel.setHeight(verticalPanel.getOffsetHeight() + 10 + "px");
else
this.schedule(100);
}
};
t.schedule(100);
}