GWT - click event on day in DatePicker - gwt

In GWT I want to handle the click event, but only when user clicks on a day.
There's a valueChangeHandler but it fires when value change, so when I click two times at the same day it will fire once.
Other option is to use addHandler or addDomHandler in which I can add ClickHandler, but it fires always, no matter if I click a day or if I pass to other month.
Both options for me are not functional.
Any ideas how to do it?

Use the addvalueChangeHandler then after that set the value to something else. It's a hack but it works for me.
calendarWidget.addValueChangeHandler(new ValueChangeHandler<Date>() {
#Override
public void onValueChange(ValueChangeEvent<Date> event) {
calendarWidget.setValue(new Date(0));
}
}

Firstly , you should describe more details . I don't know exactly what you really want to do.
Here my advices.
(1). Use Cookies to save infomation of current used user and set expire day to 1day.
(2). Don't use addNewHandler. You should use global variable of one handler instance and initialize it to NULL . At first tought to this , you would be initialized it. That may creat only one time and you can check is it NULL . eg:
private CloseHandler handler;
.......
if (handler == null) {
handler = new CloseHandler<PopupPanel>() {
public void onClose(final CloseEvent<PopupPanel> event) {
........
}
};
}
(3). You can also use simple handler as you with and if fired event to it , save in a Map with current time and unique value (eg:sequence of login user.etc). When after hit it again , you retrive and match the times of both current and previous.
Be useful for you..

You can also add handler on your DateBox.getDatePicker() and your DateBox.getTextBox().
You can add another handlers as like this...
dateBox.addHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Window.alert("click");
}
}, ClickEvent.getType());

Related

Is it possible/how to display a message on the FeedbackPanel more than one time

I have a org.apache.wicket.markup.html.panel.FeedbackPanel in my panelA class. The feedback panel is instantiated in a panelA constructor with one message to display -> feedbackPanel.info("displayFirstTime"). I am navigating to a new page and later to the previous panelA with a command
target.getPage().get(BasePage.CONTENT_PANEL_ID).replaceWith(panelA);
but the message "displayFirstTime" won't be displayed on the feedback panel again.
I have made it with overriding a panel onBeforeRender method
#Override
public void onBeforeRender() {
super.onBeforeRender();
if (again_displayCondition) {
this.info("displayFirstTime");
}
}
but it's not a clean solution.
Is it possible or how to make it, that when moving to a panelA page for the 2nd time the feedback message will be also displayed ?
Wicket uses application.getApplicationSettings().getFeedbackMessageCleanupFilter() to delete the feedback messages at the end of the request cycle.
By default it will delete all already rendered messages.
You can setup a custom cleanup filter that may leave some of the messages, e.g. if they implement some interface. For example:
component.info(new DoNotDeleteMe("The actual message here."));
and your filter will have to check:
#Override
public boolean accept(FeedbackMessage message)
{
if (message.getMessage() instanceOf DoNotDeleteMe) {
return false;
}
return message.isRendered();
}
Make sure you implement DoNotDeleteMe#toString() so that it renders properly. Or you will have to use a custom FeedbackPanel too.
DoNotDeleteMe must implement java.io.Serializable!

gwt checkbox fire handler when setValue(true)

I have a checkbox with a valueChangeHandler on it. It works when user check the checkbox.
For some reason, I need to set a value to this checkbox in my code, like this :
checkbox.setValue(true), the checkbox is perfectly checked visually but my problem is that it doesn't fire my valueChangeHandler.
checkBox.setValue(true);
checkBox.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
#Override
public void onValueChange(ValueChangeEvent<Boolean> event) {
...
}
});
Is there another handler that can be fire when I set a value ? Or another way to dot this?
Thanks
EDIT : I also tried checkbox.setValue(true,true) but it doesn't work.
RESOLVED : the setValue MUST BE after the registration of the handler. Thanks
That's the difference between setValue(Boolean) vs. setValue(Boolean,boolean)
checkBox.setValue(true, true);

GWT ListBox detect when value is re-selected

I'm having a problem using a GWT listbox. I have a case where the user selects a value from a listBox, but it can become invalidated if they change data in a related field. To validate the listBox, the user has to either select a new value, or confirm their old selection by selecting the same value again. I can't figure out how to determine if they have selected the same value so that I can restyle the listBox to look validated.
The valueChanged handler only detects if a new value is selected. The clickHandler and focusHandler fire too often because they fire when the user isn't selecting a value. Any ideas?
You can improve the clickHandler with something like this :
ignoreClick = true;
lastSelection = -1 ;
....
listBox.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
if (!ignoreClick) {
lastSelection = listBox.getSelectedIndex();
}
ignoreClick = !ignoreClick;
}
});
I tried it and the event was only fired if you selected an item. But you should rethink your user interface , like said above.

Block gwt DisclosurePanel on open state

How may I block a gwt DisclosurePanel on the open state ?
I mean, how can I prevent this DisclosurePanel to close if the user click the header more than once ?
(My header is a textBox, I want the user to enter a text, and the panel should remain open if the user unfocus the textBox and focus newly by clicking it. The DisclosurePanel content has a "cancel" button that closes the panel)
Thank you very much.
I edit my question after 2 first answers: I would like to avoid to reopen the DisclosurePanel once closed to avoid flashing effect. I actually want to prevent the DisclosurePanel to close. Maybe sinkEvents can help me... if so, how? Thanks.
A NativePreviewHandler receives all events before they are fired to their handlers. By registering a nativePreviewHandler the first time your disclosurePanel is opened, you can cancel the click event. You can later decide to remove this handler by preventClose.removeHandler();
HandlerRegistration preventClose = null;
....
panel.addOpenHandler(new OpenHandler<DisclosurePanel>() {
#Override
public void onOpen(OpenEvent<DisclosurePanel> event) {
if (preventClose == null){
preventClose = Event.addNativePreviewHandler(new NativePreviewHandler() {
#Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
if (event.getTypeInt()==Event.ONCLICK && event.getNativeEvent().getEventTarget() == panel.getHeader().getElement().cast())
event.cancel();
}
});
}
}
});
The obvious answer is review the javadoc here: https://google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/gwt/user/client/ui/DisclosurePanel.html
There is a setOpen() method that: Changes the visible state of this DisclosurePanel.
Set it to false from a click event to capture the user action.
The JavaDoc is right here: https://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/user/client/ui/DisclosurePanel.html
jamesDrinkard pointed the old 1.5 javadoc.
You can use the addCloseHandler(CloseHandler<DisclosurePanel> handler) method to add a handler so when the user tries to close it you can reopen it again with setOpen().
Maybe not the best way, but it worked for me (maybe just one of both will work too):
dPanel.setOpen(true);
dPanel.addOpenHandler(new OpenHandler<DisclosurePanel>() {
#Override
public void onOpen(OpenEvent<DisclosurePanel> event) {
dPanel.setOpen(true);
}
});
dPanel.addCloseHandler(new CloseHandler<DisclosurePanel>() {
#Override
public void onClose(CloseEvent<DisclosurePanel> event) {
dPanel.setOpen(true);
}
});

GWT: handling more than one events on Label

I want handle events on a Label when user holds down some key (Ctrl) and then clicks the mouse button together (Ctrl + mouse click), like open some window etc...
How could i do that in GWT? Should i get add two handlers or can do it with one?
thank you.
al
In your click handler you can check if the Ctrl key is pressed when the event was fired, see example below. You also might want to check for the specific mouse button the user clicked on. I've also added that to the example:
yourLabel.addClickHandler(new ClickHandler() {
if(NativeEvent.BUTTON_LEFT == event.getNativeButton() &&
event.isControlKeyDown()) {
//do what you want
}
});
Or for older version of GWT instead of event.isControlKeyDown use event.getNativeEvent().getCtrlKey(), which returns a boolean value true if the control key is pressed when this event is fired.
Edit: this code is buggy, please look at Hilbrand's answer
To be honest, I don't think you can do it with 1 or 2 handlers. I think you would need 3 handler.
A KeyDownHandler that sets a boolean you can later read form the MouseDownHandler
A MouseDownHandler that does what you want
A KeyUpHandler that resets the value of the boolean in the KeyDownHandler
boolean ctrlPressed;
yourLabel.addDomHandler(new KeyDownHandler() {
public void onKeyDown(KeyDownEvent event) {
if(event.getAssociatedType().equals(KeyCodes.KEY_CTRL))
ctrlPressed=true;
}
}, KeyDownEvent.getType());
yourLabel.addDomHandler(new KeyUpHandler() {
public void onKeyUp(KeyUpEvent event) {
if(event.getAssociatedType().equals(KeyCodes.KEY_CTRL))
ctrlPressed=false;
}
}, KeyUpEvent.getType());
yourLabel.addClickHandler(new ClickHandler() {
if(ctrlPressed) {
//do what you want
}
});