GWT - enable button on KeyDownEvent in RichTextArea - gwt

I have following handler
textArea.addKeyDownHandler(new KeyDownHandler() {
#Override
public void onKeyDown(KeyDownEvent event) {
//here
}
});
I need to enable save button with id "idsave", but I am not able to refer the button.
I am new to GWT, any help would be appreciated.

Typically, you do not use element ids in GWT. If you created a button, you can simply use it:
private Button saveButton;
...
saveButton = new Button("Save");
textArea.addKeyDownHandler(new KeyDownHandler() {
#Override
public void onKeyDown(KeyDownEvent event) {
saveButton.setEnabled(true);
}
});

If you don't have the reference of the button then try with the id.
// get element by id
Element saveButtonElement = RootPanel.get("idsave").getElement();
// remove disabled attribute to make it enable
saveButtonElement.removeAttribute("disabled");

Related

How to disable AjaxLink button in wicket?

I'm trying to disable an AjaxLink button in wicket inside the
public void onClick(AjaxRequestTarget target) method;
I tried calling the setEnabled(false) directly but didn't work. some of the suggestions I saw online says call the isEnabled() or onConfigure() methods; but these cannot be implemented inside the onclick method; any help please?
{
buttonlabel.setDefaultModel(Model.of("Creating EWL"));
target.add(buttonlabel);
buttonlabel.setOutputMarkupPlaceholderTag(true);
boolean isWorkItemCreated = NewRecruitEWLUtil.createNewRecruitWorkItem(appInfo);
if (isWorkItemCreated)
{
buttonlabel.setDefaultModel(Model.of("EWL Created"));
target.add(buttonlabel);
buttonlabel.setOutputMarkupPlaceholderTag(true);
setEnabled(false);
target.add(this);
System.out.println("setEnabled ..." + isEnabled());
}
}
setEnabled(false) is the correct way! You also need to add the AjaxLink to the AjaxRequestTarget so it is repaint at the browser:
AjaxLink link = new AjaxLink("myLink") {
#Override
public void onClick(AjaxRequestTarget target) {
setEnabled(false);
target.add(this);
}
};
add(link);
link.setOutputMarkupId(true);

Add ClickHandler to every button

I am trying to implement a click logging system in GWT, so I know where people are going around my app.
I want to be able to do this automatically with out adding the handler to every single Button?
I tried in a Composite class:
this.addDomHandler(new ClickHandler() {...}, ClickEvent.getType());
But the ClickEvent didn't give me any specifics on what had been clicked. The below didn't work as well.
NodeList<Element> elements = Document.get().getElementsByTagName("a");
EventListener el = new EventListener() {
#Override
public void onBrowserEvent(Event event) {
System.out.println(event.toString());
}
};
for (int i = 0; i < elements.getLength(); i++) {
Element e = elements.getItem(i);
com.google.gwt.user.client.Element castedElem = (com.google.gwt.user.client.Element) e;
DOM.sinkEvents(castedElem, Event.ONCLICK);
DOM.setEventListener(castedElem, el);
}
Any tips?
Take a look here:
Notice every click in a gwt application
This will be called on every click in your applilcation.
So, if you have this code:
Event.addNativePreviewHandler(new NativePreviewHandler() {
#Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
if (event.getNativeEvent().getType().equals("click")) {
Element eventTarget = DOM.eventGetTarget(Event.as(event.getNativeEvent()));
// check if eventTarget is an a-tag
}
}
});
Any time the mouse is clicked, you will get an event. Exame the event to see, if an a-tag is clicked.
Hope that helps.

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());

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());
}