Wicket Composite Behavior - wicket

I have created a composite behavior - basically two AjaxFormComponentUpdatingBehaviors in one (onBlur and onKeyUp). It is available here : http://tny.cz/0ff0ead2
When rendering a component to which this Behavior has been added, I get an exception stating that "[the] Behavior _ was not registered with this component". The exception occurs when the blur.onComponentTag()method is called from within the behavior'sonComponentTag().
Does anyone have any idea why this is? I should mention that I am using Wicket 1.4.21.
Thanks,
A

The exception originates in RequestCycle#urlFor():
public final CharSequence urlFor(final Component component, final IBehavior behaviour,
final RequestListenerInterface listener)
{
int index = component.getBehaviorsRawList().indexOf(behaviour);
if (index == -1)
{
throw new IllegalArgumentException("Behavior " + this +
" was not registered with this component: " + component.toString());
}
As Michael already commented, your wrapped behaviors have to be added to the component, just calling #bind() is not enough.

In your composite behavior's #bind() method you call another #bind() methods for your blur and keyUp, but, as #Sven already mentioned, calling this method not enough to do the trick, because it creates link to component for behaviors, but component still don't know anything about them.
When you call component.add(behaviors), there are internal calls for #bind() methods of every behavior. And this is what you actually need.
So, just replace :
blur.bind(component);
keyUp.bind(component);
by following:
component.add (blur, keyUp);

Related

Vaadin Flow: How to tell if a Component is attached

How does one reliably find out if a given Component is actually present in the DOM?
Until now I used Component.getUI().isPresent() which is supposed to able to determine if the Component is attached to a UI.
It also might be that I ran into an edge case as the Components in question are encapsulated in a ComponentRenderer which is managed by a Grid.
I need to access these Componets via JavaScript like this:
void setValue(Component comp, Value value){
Runnable callJs = comp.getUI().ifPresent(ui -> ui.getPage().executeJs("someCall($0)", value));
if(comp.isAttached()){
// execute it right away
callJs.run();
} else {
// execute onAttach
comp.addAttachListener(evt -> callJs.run());
}
}
After some digging I stumbled across the StateNode API:
comp.getElement().getNode().isAttached()

easy way of showing errors in a wicket form

I am a wicket beginner and find it extremely hard to implement more advanced actions in it.
So I've got a form with input fields in it and a submit button. I've written my own validator with its own logic. now when the validator fails the page simply reloads and the wrong value is lost. what I want is:
inputs with wrong input(where the validator failed) should be graphically different, that is get its own style - can i do it in the validator itself? or do I have to implement a visitor? how do i register it for this very form? No error message is necessary.
the 'wrong input' should not be lost and should be visible to the user.
I'd be eternally thankful for a simple solution (with some code snippets please)
What you need is a Validator + Behavior.
The behavior is needed to modify the FormComponent's markup when it is invalid.
Fortunately they mix very well:
public class MyValidator extends Behavior implements IValidator<SomeType> {...}
Implement IValidator#validate(IValidatable) to validate (you already have this) and override Behavior#onComponentTag(Component, ComponentTag) to set a CSS class when invalid, e.g.:
#Override public void onComponentTag(Component c, ComponentTag tag) {
FormComponent fc = (FormComponent) c;
if (!fc.isValid()) {
tag.append("class", "error", " ");
}
}
Make sure you have a .css file loaded that defines .error with the desired styles. You can override Behavior#renderHead(Component, IHeaderResponse response) {response.render(CssHeaderItem.forReference(new CssResourceReference(SomeScope.class, "res/css/styles.css")))} for that.
I hope things a cleaner now.
Have fun!

How to make SuggestBox to reliably RPC-call to Server DB in GWTP (Gwt platform) Framework?

I spent many hours to search this info "How to make SuggestBox to reliably RPC-call to Server DB in GWTP (Gwt platform) Framework" but couldn't find any answer about it.
In fact, there were some answers but they were for people who do not use GWTP. For example, i found a website (http://jagadesh4java.blogspot.com.au/2009/03/hi-every-one-this-is-my-first-blog.html) that guide to code SuggestBox & RPC & it suggests these classes:
-Client Side:
+ interface SuggestService extends RemoteService
+ interface SuggestServiceAsync
+ class Suggestions implements IsSerializable, Suggestion
+ class SuggestionOracle extends SuggestOracle
-Server Side:
+ class SuggestServiceImpl extends RemoteServiceServlet implements
SuggestService
I tried to follow up that website but i got error:
[WARN] failed SelectChannelConnector#127.0.0.1:8888
java.net.BindException: Address already in use: bind..........
The above guide clearly was not for people who use GWTP.
My task is I have a dictionary that contains 200k of English words & I want to have a suggest box that when user types any char or word it will look up into DB & suggest accordingly. Ex, when user types "c" it will suggest "cat, car, cut, etc", when typing "car" it will suggest "car service", "carbon", etc.
So I come up with my own solution, even it works but I am feeling I am not doing right thing. My solution is quite simple that I just bring the data from DB down & add them into MultiWordSuggestOracle. Whenever it found a list of word in DB, it won't clear the old data but just keep adding the new list into MultiWordSuggestOracle. However, my program will not constantly call to DB everytime user types a char, but it will call to DB only if the wordInTheMultiWordSuggestOracleList.indexOf(suggestBox.getText(),0)>0. However, there is no way to loop each string in the MultiWordSuggestOracle, so I used List<String> accumulatedSuggestedWordsList=new ArrayList<String>() to store data. Pls see ex:
private final MultiWordSuggestOracle mySuggestions = new MultiWordSuggestOracle();
private List<String> accumulatedSuggestedWordsList=new ArrayList<String>();
private void updateSuggestions(List<String> suggestedWordsList) {
// call some service to load the suggestions
for(int i=0;i<suggestedWordsList.size(); i++){
mySuggestions.add(suggestedWordsList.get(i));
accumulatedSuggestedWordsList.add(suggestedWordsList.get(i));
}
}
#Override
protected void onBind() {
super.onBind();
final SuggestBox suggestBox = new SuggestBox(mySuggestions);
getView().getShowingTriplePanel().add(suggestBox);
suggestBox.addKeyDownHandler(new KeyDownHandler(){
#Override
public void onKeyDown(KeyDownEvent event) {
// TODO Auto-generated method stub
String word=suggestBox.getText();
int index=-1;
for(int i=0; i<accumulatedSuggestedWordsList.size();i++){
String w=accumulatedSuggestedWordsList.get(i);
index=w.indexOf(word,0);
if(index>0)
break;
}
if(index==0 || index==-1){
GetWordFromDictionary action=new tWordFromDictionary(suggestBox.getText());
action.setActionType("getSuggestedWords");
dispatchAsync.execute(action, getWordFromDictionaryCallback);
}
}
});
}
private AsyncCallback<GetWordFromDictionaryResult> getWordFromDictionaryCallback=new AsyncCallback<GetWordFromDictionaryResult>(){
#Override
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
}
#Override
public void onSuccess(GetWordFromDictionaryResult result) {
// TODO Auto-generated method stub
List<String> suggestedWordsFromDictionaryList=result.getSuggestedWordsFromDictionaryList();
updateSuggestions(suggestedWordsFromDictionaryList);
}
};
The Result: it works but the suggest only show up if i type the "Backspace" button. For ex, when i type the word "car" then no suggested list popup, it only popup "car service, car sale, etc" when i hit the backspace button.
So, can u evaluate my solution? I am feeling i am not doing right. If i am not doing right thing, can u provide a SuggestBox PRC for GWTPframework?
Very Important Note:
How to build a Reliable SuggestBox PRC that prevents the a denial of service attack on our own servers?
What if there are too many calls generated by many people rapidly typing in a suggest box?
Actually I just found an error:
SQL Exception: Data source rejected establishment of connection, message from server: "Too many connections" --> so there must be something wrong with my solution
I knew why i got "Too many connections" error. For example, when i type "ambassador" into the suggest box, & i saw my server call the Db 9 times continuously.
-1st call, it will search any word like 'a%'
-2nd call, it will search any word like 'am%'
-3nd call, it will search any word like 'amb%'
The first problem is that it create too many calls at 1 time, second it is not effective cos the first time call like 'a%' may already contains word that will be called at 2nd time like 'am%', so it duplicate the data. Question is how to code to avoid this ineffectiveness.
Someone suggests to use RPCSuggestOracle.java (https://code.google.com/p/google-web-toolkit-incubator/source/browse/trunk/src/com/google/gwt/widgetideas/client/RPCSuggestOracle.java?spec=svn1310&r=1310)
If you can provide an example of using RPCSuggestOracle.java, that will be great.
I hope your answer will help a lot of other people.
There were an old inspiring blog post, from the Lombardi Development, that I remember addresses almost all questions you are looking for. It took me a while to find that out but, fortunately, it has simply been moved! And the sources are available. Have a look.
Although being old, things in that post still applies. In particular:
use a single connection to avoid explosion of requests, and left free the other ones for other tasks (i.e. avoid to use all the 2-to-8 max parallel browser http connections);
reuse data from a previous requests (i.e., if your request is a substring of the previous one, you may already have the suggestions, hence just filter them client-side).
Other things that come to my mind are:
use a Timer to simulate a little delay in case of fast writers, so you call the server only after a bit (probably an over optimization, but still an idea);
allow to fetch suggestions only on a minimum input length (say, min 3 characters). If you have a lot of possible suggestions, the data returned might be expensive even to parse, specially if - for the search - you decide to adopt a contains instead of startswith strategy;
in case you still have tons of suggestions, you could try to implement a lazy load SuggestionDisplay that simply show you the first, say, 50 suggestions and then, on scroll, all the others in an incremental way using the same input string.
Can't say anything from the GWTP part, I've never used it. But AFAICS seems just like GWT-RPC + dispatch mechanism (command pattern) like the old gwt-dispatch. Should't be hard to use instead of vanilla GWT-RPC.
Also have a look at the other 2 previous articles linked in the one above. Might contain some other useful tips.
Use key Up handler instead of key down handler may be it will solve your problem.
this is because keyDown event is fired before rendering the character.

add addEventListener to IE7 via .htc

I know in IE8 you can extend the Element Interface so you can abstract attachEvent/detachEvent, such as...
if (!window.addEventListener) {
// Internet Explorer 8 provides access to its 'Element' Interface…
window.Element.prototype.addEventListener = function(type, listener, useCapture) {
this.attachEvent('on' + type, listener);
}
window.Element.prototype.removeEventListener = function(type, listener, useCapture) {
this.detachEvent('on' + type, listener);
}
}
...but I'm not sure how to implement this in IE7, although I've heard it's possible via a .htc file?
Can any way show me how exactly?
Prototype.js, AFAIK, got around this by adding its methods directly to an element (whenever called via $()), instead of its prototype, which doesn't work.
If you're looking for a hack to make this work have a look here: http://blog.motane.lu/2007/09/20/elementprototype-in-ie/

Using Reflection to Remove Entity from RIA Services EntityCollection?

To facilitate control reuse we created a solution with three separate projects: a control library, Silverlight client, and ASP.NET backend. The control library has no reference to the RIA Services-generated data model classes so when it needs to interact with it, we use reflection.
This has worked fine so far but I've hit a bump. I have a DataGrid control where the user can select a row, press the 'delete' button, and it should remove the entity from the collection. In the DataGrid class I have the following method:
private void RemoveEntity(Entity entity)
{
// Use reflection to remove the item from the collection
Type sourceType = typeof(System.Windows.Ria.EntityCollection<>);
Type genericType = sourceType.MakeGenericType(entity.GetType());
System.Reflection.MethodInfo removeMethod = genericType.GetMethod("Remove");
removeMethod.Invoke(this._dataGrid.ItemsSource, new object[] { entity });
// Equivalent to: ('Foo' derives from Entity)
// EntityCollection<Foo> ec;
// ec.Remove(entity);
}
This works on the client side but on the domain service the following error gets generated during the Submit() method:
"The UPDATE statement conflicted with
the FOREIGN KEY constraint
"********". The conflict occurred in
database "********", table "********",
column '********'. The statement has
been terminated."
One thing I noticed is the UpdateFoo() service method is being called instead of the DeleteFoo() method on the domain service. Further inspection shows the entity is going into the ModifiedEntities ChangeSet instead of the RemovedEntities ChangeSet. I don't know if that's the problem but it doesn't seem right.
Any help would be appreciated, thanks,
UPDATE
I've determined that the problem is definitely coming from the reflection call to the EntityCollection.Remove() method. For some reason calling it causes the entity's EntityState property to change to EntityState.Modified instead of EntityState.Deleted as it should.
Even if I try to remove from the collection by completely circumventing the DataGrid I get the exact same issue:
Entity selectedEntity = this.DataContext.GetType().GetProperty("SelectedEntity").GetValue(this.DataContext, null) as Entity;
object foo = selectedEntity.GetType().GetProperty("Foo").GetValue(selectedEntity, null);
foo.GetType().InvokeMember("Remove", BindingFlags.InvokeMethod, null, foo, new object[] { entity });
As a test, I tried modifying the UpdateFoo() domain service method to implement a delete and it worked successfully to delete the entity. This indicates that the RIA service call is working correctly, it's just calling the wrong method (Update instead of Delete.)
public void UpdateFoo(Foo currentFoo)
{
// Original update implementation
//if ((currentFoo.EntityState == EntityState.Detached))
// this.ObjectContext.AttachAsModified(currentFoo, this.ChangeSet.GetOriginal(currentFoo));
// Delete implementation substituted in
Foo foo = this.ChangeSet.GetOriginal(currentFoo);
if ((foo.EntityState == EntityState.Detached))
this.ObjectContext.Attach(foo);
this.ObjectContext.DeleteObject(foo);
}
I've been researching a similar issue.
I believe the issue is you are calling remove with a reference for an EntityCollections within the DomainContext as the root reference rather than using the DomainContext itself as the root.
So...
ParentEntityCollection.EntityCollectionForTEntity.Remove(TEntity);
Produces the EntityState.Modified instead of EntityState.Deleted
Try instead...
DomainContext.EntityCollectionForTEntity.Remove(TEntity);
I think this will produce the result you are seeking.
Hope this helps.
What is the "column" in the "FOREIGN KEY constraint" error? Is this a field in the grid row and collection that coorosponds to that column? Is it possible that the entity you are trying to remove is a column in the row rather than the row itself which is causing an update to the row (to null the column) rather than to delete the row?
I read your update and looks like you've determined that the problem is the reflection.
Have you tried to take the reflection out of the picture?
As in:
private void RemoveEntity(Entity entity)
{
// Use reflection to remove the item from the collection
Type sourceType = typeof(System.Windows.Ria.EntityCollection<>);
Type genericType = sourceType.MakeGenericType(entity.GetType());
// Make sure we have the right type
// and let the framework take care of the proper invoke routine
if (genericType.IsAssignableFrom(this._dataGrid.ItemsSource.GetType()))
((Object) this._dataGrid.ItemsSource).Remove(entity);
}
Yes, I know it's ugly, but some times...
Edited to add
I've updated the code to remove the is keyword.
Now about using the object to make the call to the Remove method, I believe it might work due the late binding of it.