GWT: How to capture simple MouseOverEvent for CellTable ? - gwt

I found a code that handles mouseover event whenever a user hovers over any cell:
table.addCellPreviewHandler(new Handler<List<String>>()
{
#Override
public void onCellPreview(
CellPreviewEvent<List<String>> event)
{
if ("mouseover".equals(event.getNativeEvent().getType())) {
Element cellElement = event.getNativeEvent().getEventTarget().cast();
// play with element
System.out.println(" message !!");
}
}
});
When user hovers over title of columns, nothing happens.
Mouseover event should fire also when hovering over borders.

table.addDomHandler(new MouseOverHandler() {
#Override
public void onMouseOver(MouseOverEvent event) {
// handle the event
}
}, MouseOverEvent.getType());

Related

Double Click event for DataGrid

I added double click event for DataGrid, but it doesn't work correctly. The code handles a single click, but it does not handle double click.
Please help.
private DataGrid<Contract> table = new DataGrid<Contract>();
table.addCellPreviewHandler(new Handler<Contract>() {
#Override
public void onCellPreview(final CellPreviewEvent<Contract> event) {
if (BrowserEvents.DBLCLICK.equals(event.getNativeEvent().getType())) {
//it doesn't handle
Window.alert("Tro-lo-lo");
}
if (BrowserEvents.CLICK.equals(event.getNativeEvent().getType())) {
//it handles
Window.alert("Tru-la-la");
}
}
});
DataGrid has many things in common with CellTable. So solutions from this question must work for you too:
Using CellPreviewHandler count time between two clicks
Or you can add DoubleClickHandler using addDomHandler method
dataGrid.addDomHandler(new DoubleClickHandler() {
#SuppressWarnings("unchecked")
#Override
public void onDoubleClick(DoubleClickEvent event) {
DataGrid<YourDataProviderType> grid = (DataGrid<YourDataProviderType>) event.getSource();
int row = grid.getKeyboardSelectedRow();
YourDataProviderType item = grid.getVisibleItem(row);
Window.alert("Do Something Here");
}
}, DoubleClickEvent.getType());

GWT capturing keyboard events on Composite

I am trying to capture keyboard events when there isn't a widget with the focus. I tried adding this to a composite widget :
addDomHandler(new KeyPressHandler(){
public void onKeyPress(KeyPressEvent event) {
logger.info("onKeyPress: "+event);
}
}, KeyPressEvent.getType());
but I am not trapping the keyboard events. I'd like to be able to determine when the Ctrl key is press whilst a mousedown event is occuring. How can I do that?
Use MouseDownHandler:
MouseDownHandler mouseDownHandler = new MouseDownHandler() {
#Override
public void onMouseDown(MouseDownEvent event) {
if (event.isControlKeyDown()) {
// do something
}
}
};
myCompositeWidget.addDomHandler(mouseDownHandler, MouseDownEvent.getType());

How to capture doubleClickEvent in GWT CellTable

I'm trying to make a GWT CellTable catch events of type DoubleClickEvent, but while the CellTable correctly receives events of type ClickEvent when a row is clicked in the UI, it not see any DoubleClickEvent when the row is double-clicked.
So, if I click a row in the UI, the handler declared for ClickEvent is correctly triggered, but if I double click the handler declared for DoubleClickEvent is not triggered, instead.
Am I doing something wrong or CellTable itself cannot handle DoubleClickEvent at all?
In the latter case, what could be a good way to capture double-clicks in a table?
Below, the code for my CellTable declaration:
CellTable<ServiceTypeUI> contentTable = new CellTable<ServiceTypeUI>(10, style);
contentTable.setSelectionModel(new SingleSelectionModel<ServiceTypeUI>());
contentTable.addHandler(new DoubleClickHandler() { // HANDLER NOT CORRECTLY TRIGGERED
#Override
#SuppressWarnings("unchecked")
public void onDoubleClick(DoubleClickEvent event) {
presenter.doubleClickHandler(event);
}
}, DoubleClickEvent.getType());
contentTable.addHandler(new ClickHandler() { // HANDLER CORRECTLY TRIGGERED
#Override
#SuppressWarnings("unchecked")
public void onClick(ClickEvent event) {
presenter.clickHandler(event);
}
}, ClickEvent.getType());
I've also tried removing ClickEvent handler declaration and the SelectionModel declaration, to avoid that any of those capture the DoubleClickEvent event and treat it as a ClickEvent but the DoubleClickHandler has not been triggered even in this case.
CellTable<ServiceTypeUI> contentTable = new CellTable<ServiceTypeUI>(10, style);
contentTable.addHandler(new DoubleClickHandler() { // HANDLER NOT CORRECTLY TRIGGERED
#Override
#SuppressWarnings("unchecked")
public void onDoubleClick(DoubleClickEvent event) {
presenter.doubleClickHandler(event);
}
}, DoubleClickEvent.getType());
SingleSelectionModel<T> selectionModel
= new SingleSelectionModel<T>();
cellTable.setSelectionModel(selectionModel);
cellTable.addDomHandler(new DoubleClickHandler() {
#Override
public void onDoubleClick(final DoubleClickEvent event) {
T selected = selectionModel
.getSelectedObject();
if (selected != null) {
//DO YOUR STUFF
}
}
},
DoubleClickEvent.getType());
You have to replace the T with the your "ServiceTypeUI" . The value selected will be the object which was been chosen from the user.

style for GXT menu button

I see GXT Menu can only be set on CellButtonBase subclasses' instances through setMenu() method.
I'd like to show an image instead of a button and show a menu when user clicks that image. unfortunately, Image is not a subclass of CellButtonBase and thus I can't attach a GXT Menu to it.
so how can I make TextButton (which seems to be my only choice here) look like an image if I have to use it?
There's no documentation or examples on this subject. I asked on Sencha GXT forum support, but got no response.
ok, I found a way to do this without TextButton. add an Image and call menu.show(...) in click handler.
private void createMenu() {
menu = new Menu();
Image menuButtonImage = new Image(Resources.INSTANCE.nav_preferences());
menuButtonImage.addStyleName(CSS.header_bar_icon());
menuButtonImage.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
menu.showAt(getAbsoluteLeft(), getAbsoluteTop() + MENU_OFFSET_FROM_IMAGE_TOP);
}
});
menu.addShowHandler(new ShowEvent.ShowHandler() {
#Override
public void onShow(ShowEvent event) {
highlight();
}
});
menu.addHideHandler(new HideEvent.HideHandler() {
#Override
public void onHide(HideEvent event) {
removeHighlight();
}
});
menu.setStyleName(CSS.menu());
add(menuButtonImage);
}
private void addUserSettings() {
MenuItem userSettingsItem = new MenuItem("User Settings");
userSettingsItem.addSelectionHandler(new SelectionHandler<Item>() {
#Override
public void onSelection(SelectionEvent<Item> event) {
_coreLayout.showUserSettingsPage();
}
});
userSettingsItem.setStyleName(CSS.menu_item());
menu.add(userSettingsItem);
}
private void highlight() {
addStyleName(CSS.header_bar_icon_box_selected());
}
private void removeHighlight() {
removeStyleName(CSS.header_bar_icon_box_selected());
}

GWT: How to disable the anchor link event when clicked

I want to disable the anchor link event when it clicked one time. I used anchor.setenabled(false) but nothing happend. When I click the same button again the event e is true. I want false at that time.
public void onCellClick(GridPanel grid, int rowIndex, int colindex,EventObject e)
{
if(rowIndex==0 && colindex==2){
tomcatHandler = "Start";
anchorStart.setEnabled(false);
}else if(rowIndex==0 && colindex==3){
tomcatHandler = "Stop";
****anchorStop.setEnabled(false);
anchorStart.setEnabled(false);
anchorRestart.setEnabled(true);****
}else if(rowIndex==0 &&colindex==4){
tomcatHandler = "Restart";
anchorRestart.setEnabled(false);
}
AdminService.Util.getInstance().tomcat(tomcatHandler,new AsyncCallback<String>() {
#Override
public void onSuccess(String result) {
imageChangeEvent(result);
}
#Override
public void onFailure(Throwable caught) {
}
});}
Anchors in GWT have always had a problem with setEnabled() because HTML doesn't support such a property. A quick workaround is to create a new widget that subclasses GWT's Anchor, adding the following override:
#Override
public void onBrowserEvent(Event event) {
switch (DOM.eventGetType(event)) {
case Event.ONDBLCLICK:
case Event.ONFOCUS:
case Event.ONCLICK:
if (!isEnabled()) {
return;
}
break;
}
super.onBrowserEvent(event);
}
This disables the passing of the browser event to GWT's Anchor class (summarily disabling all related handlers) when the link is double clicked, focused or clicked and is in a disabled state.
Source
It doesn't seem to actually disable the anchor, but it does retain the status that has been set with anchor.setEnabled(), so just test that within your handler e.g.
myAnchor.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent evt) {
// write to filter and then call reload
if (((Anchor) evt.getSource()).isEnabled()) {
//do stuff
}
}
});