I stucked during migration from Wicket 1.5 to 6.6.
Earlier AjaxLink allowed to override getAjaxCallDecorator() method and wrap the script with preDecorateScript(CharSequence script).
Now recommended way is using AjaxCallListener with getPrecondition(Component component) method. But how can I wrap the source script using Component component parameter?
Dont know if i understand you correctly. We do it this way:
public class MyAjaxCallListener implements IAjaxCallListener{
#Override
public CharSequence getBeforeHandler(Component component) {
return null;
}
#Override
public CharSequence getPrecondition(Component component) {
return YOUR_SCRIPT;
}
// ... not needed overrides can return null
}
Then you add it through an Behavior to your AjaxLink.
ajaxLink.add(new AjaxEventBehavior("onclick") {
#Override
protected void onSubmit(AjaxRequestTarget target) {
//do stuff
}
#Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
super.updateAjaxAttributes(attributes);
attributes.getAjaxCallListeners().add(new MyAjaxCallListener());
}
});
Related
I have two methods which I need to run asynchronously in android.First I want to run getContactsList() and then populateList().I tried to use Asynctask but it skips the recyclerview populating code and then prints the next line in the log while it works well in onCreate.Please help me
private class TaskOne extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
Log.i("do in background", "running");
getContactList();
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
pDialog.cancel();
Log.i("onPostExecute", "running");
populateList();
isContacts_fetched();
Log.i("populateList", "finished");
}
}
calling in onCreate() methods
new TaskOne().execute();
populateList() code
public void populateList() {
Log.i("Populate List","Entered");
// Toast.makeText(this,String.valueOf(Common.selectedContactNos.size()),Toast.LENGTH_LONG).show();
displayRecyclerAdapter = new DisplayRecyclerAdapter(DisplayContacts.this);
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this);
recyclerView_contacts.setAdapter(displayRecyclerAdapter);
recyclerView_contacts.setLayoutManager(mLinearLayoutManager);
displayRecyclerAdapter.notifyDataSetChanged();
}
I have a Wicket component which is listening for some event (IEvent). If such event arrives, I want to re-render the component with a changed model. There are no active controls on the page, like AjaxLink, which can trigger the re-rendering.
Is there a way to refresh such kind of component?
I was thinking to somehow trigger an AJAX request from the onEvent method and add an AjaxBehavior to the mentioned component. But I don't know, how to trigger the AJAX request.
public class PersonPanel extends Panel implements Observer {
private WebMarkupContainer wrapper;
public PersonPanel(String id) {
super(id);
setDefaultModel(new CompoundPropertyModel<PersonInfo>(getModel()));
wrapper = new WebMarkupContainer("wrapper");
wrapper.setOutputMarkupId(true);
add(wrapper);
wrapper.add(new Label("personID"));
// some more content
}
private IModel<PersonInfo> getModel() {
return new LoadableDetachableModel<PersonInfo>() {
#Override
protected PersonInfo load() {
// model loading logic
}
};
}
#Override
public void onEvent(IEvent<?> event) {
logger.debug("\n Person Panel received an Event: " + event.getPayload());
// Re-rendering of "wrapper" should be triggered from here.
}
#Override
public void update(Observable observable, Object o) {
send(this, Broadcast.EXACT, "Observable cache has changed.");
}
}
Here is the solution, thanks to hint from martin-g, solved via WebSockets. See the methods update and onEvent, plus added WebSocketBehavior on the component:
public class PersonPanel extends Panel implements Observer {
private WebMarkupContainer wrapper;
public PersonPanel(String id) {
super(id);
setDefaultModel(new CompoundPropertyModel<PersonInfo>(getModel()));
wrapper = new WebMarkupContainer("wrapper");
wrapper.setOutputMarkupId(true);
add(wrapper);
wrapper.add(new Label("personID"));
// some more content
add(new WebSocketBehavior() {
});
observableCache.addObserver(this);
}
private IModel<PersonInfo> getModel() {
return new LoadableDetachableModel<PersonInfo>() {
#Override
protected PersonInfo load() {
// model loading logic
}
};
}
#Override
public void onEvent(IEvent<?> event) {
if (event.getPayload() instanceof WebSocketPushPayload) {
WebSocketPushPayload wsEvent = (WebSocketPushPayload) event.getPayload();
wsEvent.getHandler().add(wrapper);
}
}
#Override
public void update(Observable observable, Object o) {
WebSocketSettings webSocketSettings =
WebSocketSettings.Holder.get(getApplication());
WebSocketPushBroadcaster broadcaster =
new WebSocketPushBroadcaster(webSocketSettings.getConnectionRegistry());
broadcaster.broadcastAll(
getApplication(),
new WebSocketMessage("WebSocket message from the PersonPanel."));
}
}
You can find a full running example project, implemented in Wicket 8 and Gradle on Bitbucket:
sw-samuraj/blog-wicket-spring-rest
At the send side you can pass the AjaxRequestTarget with the payload of the event.
send(getPage(), Broadcast.DEPTH, new MyPayload(target));
and then on the receive side:
MyPayload payload = (MyPayload) event.getPayload();
payload.getTarget().add(this);
I have List box in new Neon Scout and I would like to validate value that was set.
I have implemented execValidateValue method :
#Override
protected Set<String> execValidateValue(final Set<String> rawValue) {
if (rawValue.contains(CONSTANT.UNKNOWN)) {
final Set<String> unknownSet = new HashSet<String>();
unknownSet.add(CONSTANT.UNKNOWN);
return super.execValidateValue(unknownSet);
}
return super.execValidateValue(rawValue);
}
but it doesn't seams to have any effect. While debugging I see that inside setValue(VALUE rawValue) method updateDisplayText(validatedValue) is called with right list of strings.
Why is that? Is there something that I did wrong?
Marko
You are right... If a value is changed during the validation (in execValidateValue(VALUE rawValue)) as suggested by the JavaDoc, the value is stored correctly in the Scout Model but the change is not reflected in the HTML-UI.
With the help of Samuel Renold, I have asked the team about it: The HTML-UI will be fixed to reflect the change in the UI. See bug 493778.
Test code for the Demo Widgets Application. Change the DefaultField in the ListBoxForm.
#Order(20)
public class DefaultField extends AbstractListBox<Color> {
#Override
protected Class<? extends ICodeType<?, Color>> getConfiguredCodeType() {
return ColorsCodeType.class;
}
#Override
protected Set<Color> execValidateValue(Set<Color> rawValue) {
System.out.println(">> execValidateValue");
printColors(rawValue);
if (rawValue != null && rawValue.contains(Color.RED)) {
return super.execValidateValue(Collections.singleton(Color.RED));
}
return super.execValidateValue(rawValue);
}
private void printColors(Set<Color> rawValue) {
if (rawValue != null) {
for (Color color : rawValue) {
System.out.print(color + ", ");
}
System.out.println("");
}
else {
System.out.println("null");
}
}
#Override
protected void execChangedValue() {
System.out.println(">> execValidateValue");
printColors(getValue());
}
#Override
protected int getConfiguredGridH() {
return 5;
}
#Override
protected String getConfiguredLabel() {
return TEXTS.get("Default");
}
}
The wrong behaviour can also be reproduced in Scout 4 (this release is end-of-life)
Im made a sort of autentication on my web application using GWT. So i make these functions in the GWTServiceImpl Class :
public class PageMenuLogin extends FlowPanel {
public PageMenuLogin() {
PageMenuLogin.getService().isSetSession(new AsyncCallback<String>() {
#Override
public void onFailure(Throwable caught) {
InlineLabel err=new InlineLabel();
err.setText("Errore Di Connessione");
PageMenuLogin.this.add(err);
}
#Override
public void onSuccess(String result) {
if(result.compareTo("")==0) {
designLogin();
} else {
designLogout(result);
}
}
});
}
public final void designLogin() {
final InlineLabel menu_err=new InlineLabel("");
menu_err.setStyleName("menu_err");
this.add(menu_err);
Button menu_login_button=new Button("Login");
this.add(menu_login_button);
menu_login_button.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
getService().checkLogin("nickname", "password", new AsyncCallback<Boolean>() {
#Override
public void onFailure(Throwable caught) {
menu_err.setText("Comunicazione Fallita");
}
#Override
public void onSuccess(Boolean result) {
if (result) {
// I LOAD THE PROFILE PAGE
} else {
menu_err.setText("Username e password non validi");
}
}
});
}
});
}
}
********************************************************************************
public class GWTServiceImpl extends RemoteServiceServlet implements GWTService {
HttpServletRequest request;
HttpSession session;
public String isSetSession() {
this.request = this.getThreadLocalRequest();
this.session=this.request.getSession();
if(this.session.getAttribute("nickname")==null) {
return "";
} else {
return (String)this.session.getAttribute("nickname");
}
}
public boolean checkLogin(String username, String password) {
if("i check on the database if the user exist") {
this.session.setAttribute("nickname", value);
return true;
}
return false;
}
}
On client side, i call the GWTServiceImpl functions (server side), i check the return values and i do some operations.
Is this the right way to work with session on GWT? Any suggestion/tips/helps would be appreciated :)
Thanks for your time!!!
EDIT
New GWTServiceImpl :
public class GWTServiceImpl extends RemoteServiceServlet implements GWTService {
HttpSession session;
public String isSetSession() {
HttpSession session=getThreadLocalRequest().getSession();
if(session.getAttribute("nickname")==null) {
return "";
} else {
return (String)session.getAttribute("nickname");
}
}
public boolean checkLogin(String nickname, String password) {
HttpSession session=getThreadLocalRequest().getSession();
Database mydb=Configuration.getDatabase();
mydb.connetti();
// faccio md5 ed escape
String log_check_user=nickname;
String log_check_pass=password;
// controllo che l'utente esista
ArrayList<String[]> db_result=null;
db_result=mydb.selectQuery("SELECT nickname FROM users WHERE nickname='"+log_check_user+"' AND password='"+log_check_pass+"'");
mydb.disconnetti();
if(!db_result.isEmpty()) {
session.setAttribute("nickname", nickname);
return true;
}
return false;
}
public boolean checkLogout() {
HttpSession session=getThreadLocalRequest().getSession();
session.invalidate();
return true;
}
}
Looks like it should work. I do a lot of work with GWT, although I often forego the use of the RemoteServiceServlet in favour of passing data back and forth via JSON.
A few suggestions, though. When you're calling a method or field within the same class, you don't need to include the this keyword. It doesn't hurt, but tends to make your code longer than it needs to be. Feel free to keep it though, if you find it makes things clearer for you.
Also, unless you've got methods that actually use the request, you don't need to create the request object; you can just do
session = getThreadLocalRequest().getSession();
A final suggestion: Since you're using the session in more than one method, it might be a good idea to initialize it right away; So, instead of initializing it in isSetSession(), you could just write HttpSession session = getThreadLocalRequest().getSession();, or initialize it in the class's constructor. As it stands now, if you happen to call checkLogin() before isSetSession(), you'll get a NullPointerException since session hasn't yet been initialized.
I would like to update a text field instantly when typing in a GWT TextBox. My problem is that ValueChangeEvent and ChangeEvent handlers only fire when the TextBox loses focus. I thought about using the KeyPressEvent but then nothing would happen when performing a copy paste with the mouse.
What's the simplest way to do that ?
You could catch the ONPASTE event and manually fire a ValueChangeEvent. Something like this:
public void onModuleLoad() {
final Label text = new Label();
final ExtendedTextBox box = new ExtendedTextBox();
box.addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
text.setText(event.getValue());
}
});
box.addKeyUpHandler(new KeyUpHandler() {
#Override
public void onKeyUp(KeyUpEvent event) {
text.setText(box.getText());
}
});
RootPanel.get().add(box);
RootPanel.get().add(text);
}
private class ExtendedTextBox extends TextBox {
public ExtendedTextBox() {
super();
sinkEvents(Event.ONPASTE);
}
#Override
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
switch (DOM.eventGetType(event)) {
case Event.ONPASTE:
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
#Override
public void execute() {
ValueChangeEvent.fire(ExtendedTextBox.this, getText());
}
});
break;
}
}
}
Tested on firefox 3.6.1.
As a general solution, what works for me (thx to gal-bracha comment):
Generally, GWT does not have classes to handle input event (described here
and here). So we need to implement it by ourselves:
Handler class:
import com.google.gwt.event.shared.EventHandler;
public interface InputHandler extends EventHandler {
void onInput(InputEvent event);
}
Event class:
import com.google.gwt.event.dom.client.DomEvent;
public class InputEvent extends DomEvent<InputHandler> {
private static final Type<InputHandler> TYPE = new Type<InputHandler>("input", new InputEvent());
public static Type<InputHandler> getType() {
return TYPE;
}
protected InputEvent() {
}
#Override
public final Type<InputHandler> getAssociatedType() {
return TYPE;
}
#Override
protected void dispatch(InputHandler handler) {
handler.onInput(this);
}
}
Usage:
box.addDomHandler(new InputHandler() {
#Override
public void onInput(InputEvent event) {
text.setText(box.getText());
}
},InputEvent.getType());
It works on every TextBox value change including pasting using context menu. It does not react on arrows, ctrl, shift etc...
This has been a major issue for me in the past. The keyupHandler wont work because the copy paste requires a second key press on the paste option which does not fire the event. the best i have been able to do is use the old changelistener not ideal but it does work.
I prefer use Elements than Widgets so this my way to handler.
Element input = Document.get().getElementById("my-input");
DOM.sinkBitlessEvent(input, "input");
DOM.setEventListener(input, event -> GWT.log("Event!"));
Why not use combination of both KeyUpHandler and a ChangeHandler on the TextBox?
Should take care of immediate feedback on each keystroke as well as copy paste case as well.
Just saw this question. Because I was facing the similar problem.
Did some hack and it worked for me.
You can use KeyUpHandler but use it with additional if block that checks
for length of textbox. If length of text box is > 0, do your thing.
Ex:
textBox.addKeyUpHandler(new KeyUpHandler() {
#Override
public void onKeyUp(KeyUpEvent keyUpEvent) {
if (textBox.getText().length() > 0) {
//do your stuff`enter code here`
}
}