How to validate a textfield in pageablelistview in java wicket - java

i have added a pageable listview with two components,one is text field and other is a dropdown. Normal way of checking for validation is by adding a feedback panel.But when same method is used in grid its giving an error saying that feedback component with wicket:id="xy" has already been parsed.
is there any way to validate those components?
JAVA CODE HTML CODE

Somewhere in the code (e.g. your overridden Form#onSubmit() or Form#onError(), one of your Ajax behaviors, etc.) you add the component with wicket ID "FeedbackSubQuestions_" again, even though this component has already been added before. Something like the following:
public class MyPage extends WebPage {
public MyPage() {
this.add(new FeedbackPanel("FeedbackSubQuestions_"));
this.add(new Form<Void>("myForm"){
#Override
protected void onError() {
// This call the produce the error as the feedback component is already in the component hierarchy
MyPage.this.add(new FeedbackPanel("FeedbackSubQuestions_"));
}
});
...
Adding all the form components with validation and their ajax behaviors
...
}
}
In wicket, once you construct the component hierarchy, subsequent interactions with the page do not need to reinitialise any of it anymore. That is, once you add the feedback panel in order for it to display your feedback you do not need to add this panel again. If there are errors to be displayed (e.g. a form validation fails, or Component#error(String) is called manually) the feedback will pick those up automatically when the feedback panel is being rendered again (e.g. when you add it to your ajax response or the whole page is being re-rendered as part of the form submission process).

Related

Vaadin wizard addon event fire twice

I am using Vaadin wizard addon and I have a problem with the following case:
When the user only presses forward/next step, there is no problem. However, if the user wants to go to the previous step, I am accidentally adding a Button click listener to the same event(That is my assumption. I have debugged the program and saw that if the user goes to the previous page, the event fires twice)
I have tried to remove the event listener before going to the next page, however, I could not find a method to remove all of the event listeners once. Also, I don't know where to remove them, since I could not find a function executed before the user is moved to the next page in Vaadin wizard.
I am following this example:
https://github.com/tehapo/WizardsForVaadin/tree/master/wizards-for-vaadin-demo/src/main/java/org/vaadin/teemu/wizards
Is there a method to remove all of the ClickListeners?
If it exists, where should I add that functionality?
Also, I am using ListDataProvider and NativeSelect components too.
NativeSelect has HasValue.ValueChangeListener<String> listener and in the default implementation, I Could not find a method such that I can use this:
NativeSelect<String> select = new NativeSelect<>("List");
select.addValueChangeListener(new HasValue.ValueChangeListener<String>() {
// some overwritten valuechange method
}
select.removeValueChangeListener(); // This does not exist
I am setting the click listener in the public Component getContent() {} method
In Vaadin 8 you need to use the Registration interface to remove Listeners.
When you add a Listener it will return the Registration:
final Registration registration = select.addValueChangeListener(this::doSomething);
And then to remove it:
registration.remove();

About JButton listener with controller and view

I'm doing a Swing app in Java with a database. I got different JButton which delete, add or modified row in a JTable.
I'm stuck because I don't know where I can manage my action listener.
I have a class named DaoClef where I will execute an SQL statement, send my table model etc...
I got a view where all my button are set but I don't know what to do in my controller.
For now I got this in the view:
btnNew.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
// check for selected row first
if (t.getSelectedRow() != -1) {
// remove selected row from the model
String sqlMaj = "DELETE FROM clefs WHERE IdClef = ?";
try (PreparedStatement pst = Connect.getConnection().prepareStatement(sqlMaj) )
{
pst.setInt(1, (int) t.getValueAt(t.getSelectedRow(), 0));
pst.executeUpdate();
t.addNotify();
}
catch (SQLException e)
{
}
}
}
});
But it's not good because the SQL statement need to be execute in a method in the controller.
Can you give me some clues?
From an MVC perspective, the issue here is that the Model and the View are mixed together. Since the Controller is supposed to act as an intermediary between the two, it has no role here.
The View, presenting the GUI (windows, buttons, text fields, etc.) to the user, should (ideally, more on this later) tell the Controller when a delete button has been pressed and its corresponding id. Then the Controller should pass this info to the Model which, in turn, will carry out the communication with the database using its connection and prepared statements.
Here's a good explanation on the MVC pattern:
Model-View-Controller: Does the user interact with the View or with the Controller?
Note (or maybe omit) that there's some controversy about different takes on the MVC pattern as shown in the different answers here.
There is also a good answer on the topic of MVC in Swing here:
Java and GUI - Where do ActionListeners belong according to MVC pattern?
Although the example given may be a little complex as the author tries to more strictly separate View and Controller. I'd go on and read the comments as well since they add further clarification.
I tried finding a simpler example but most didn't include action listeners. Here's a couple that do:
a) https://gist.github.com/Sammy30/7ebc606e7bb76cefac0f
b) http://www.fredosaurus.com/notes-java/GUI/structure/40mvc.html
The action listeners get added from the Controller unlike the first example linked. It's simpler but has the disadvantage of having Controller and View intertwined, which doesn't allow switching the latter easily (i.e., without modifying the Controller), which is one of the advantages of MVC.
The steps would be:
Preparation:
The View and Model get created (the Model may set its connection to the database now)
The Controller gets created with instances of the View and Model as properties
The Controller gets ready by setting action listeners in the View (or tells the View to set them, in which case the View should contact the Controller whenever an event happens)
Usage:
The user presses a button in the View
The Controller notices the button press through its action listener (or the View tells the Controller which button has been pressed)
The Controller decides what to do with the event. In this case, tell the Model that a record needs to be deleted
The Model receives the order and modifies the database, and returns a value with the updated rows count
The Model closes the ResultSet, Statement, and Connection

How to get notifications when xtext's EMF model changes with proper values

We have a FormEditor containing four pages: three FormPages and fourth page is XTextEditor as a source page.
Whenever user makes any changes (e.g. changing value in text box) on FormPages, we change EMF model content inside XTextDocument.modify() method as given below:
xtextEditor.getDocument().modify(new IUnitOfWork.Void<XtextResource>() {
#Override
public void process(XtextResource state) throws Exception {
IParseResult parseResult = state.getParseResult();
Assert.isNotNull(parseResult);
EObject rootASTElement = parseResult.getRootASTElement();
if (rootASTElement instanceof MyModel) {
XyzType t = ((MyModel) rootASTElement).getXyzType();
t.setName(name); <- ‘name’ is the new value entered on FormPage text box
}
}
});
Now, we want to get notifications in FormPages, whenever EMF model gets changed when user makes some changes on source page i.e. XTextEditor.
We tried adding IXtextModelListener and IXtextDocumentContentObserver to IXtextDocument; but these get called for every character entered in XTextEditor.
Our requirement is to get notifications only when values in EMF model get changed (and not for text formatting e.g. when whitespace is inserted/removed).
Can somebody please provide some pointers?
Regards,
Akhil
You can use the EMF Client Platform (ECP), which adds an implementation of an Observer Bus to an EMF model.
They implement an own validation service which does what you described:
ecp.view.validation
This is the validation service, which monitors the domain model and
calculates validation errors.
The validation service already uses the Observer Bus of ECP to register to EMF change events. The Observer Bus itself is implemented as an EContentAdapter listening to every change of the model. It already filters the change events and provides them following the Observer Bus pattern to the event bus which you can register. There you only get the events you registered to and not all events as for the EContentAdapter which you have to filter for yourself.
I think they mainly use it to validate models to show results in their EMF Forms GUI. However, you can use the services also standalone.

SmartGWT: dynamically adding custom widget to VStack

I need some widgets that share the same code base, thus I introduced an abstract class providing these shared members and methods and created an implementation which adds additional functionality. but every time I wanted to add this to a VStack I got the following error:
java.lang.AssertionError: A widget that has an existing parent widget may not be added to the detach list
in order to verify if this is b/c of the class hierarchy I created the same in a method, but I'm still receiving the same error. the code below should describe what I want to achieve.
public class Test extends VStack {
// constructor that adds button and a clickhandler for the button
// that will call addComplex()
public void addComplex() {
HStack stack = new HStack();
stack.setHeight("22px");
stack.addMember(new IButton("remove"));
stack.addMember(new ListBox());
DynamicForm form = new DynamicForm();
form.setFields(new TextItem());
stack.addMember(form);
addMember(stack);
}
}
when I just call
addMember(new Label(""));
I get no error.
furthermore, considering that the stuff in the method addComplex is in a separate class, and I add a new instance in the constructor of this Test-class, no error is thrown. the error is only thrown when I want to add the HStack via a button click
why can't I add this HStack to my VStack?
Update
it was b/c of the ListBox, which is a GWT component, and no SmartGWT component. this was the detail I missed.
can anybody tell me: why has a GWT widget an existing parent, and a SmartGWT widget does not? or is the error message just bogus?
the problem was that ListBox is from GWT, not SmartGWT. replacing it with the appropriate SmartGWT widget it works like a charm.
I have found, as a general rule:
Thou shalt not mix GWT and SmartGWT if thou seeketh functionality beyond the basic displaying of thine widgets.
I ran into this same exception when attempting to hide/show a DynamicForm or a VLayout that contained some plain GWT widgets... The page loaded fine, but I ran into trouble when I tried to add some hide/show interaction functionality...

How to control submit url action in Wicket form?

I have a wicket web application with Page mounted to bookmarkable alias. The page contains a form object with submit action.
The problem is that though the form belongs to the page the action url doesn't contain page alias, but rather created in cryptic form of wicket action.
Is there a way to adjust that behavior, so link will be like page_alias/submit ?
...
setRenderStrategy(IRequestCycleSettings.ONE_PASS_RENDER);
mountBookmarkablePage("/resetpwd", ResetPasswordPage.class);
...
public ResetPasswordPage(final String id, final PageParameters parameters) {
final Form form = new StatelessForm();
form.add(new Button("submit") {
public void onSubmit() {
...
});
add(form);
If you subclass StatelessForm instead of Form, this will take you part of the way. Rather than having something like
action="myapp/?wicket:interface=:1:eventEditor::IFormSubmitListener::"
with the page containing the form mounted at a bookmarkable URL, you'll get something like, for example,
action="myapp/mount/path/some/params/?wicket:interface=:0:eventEditor::IFormSubmitListener::"
This uses a MixedParamUrlCodingStrategy for the mount in WebApplication.init()
You can then override encodeUrlInHiddenFields() to return true, which will give you a clean URL in the action attribute.
However, all this doesn't really change the way Wicket works with forms, i. e., you still have some Wicket-specific state data in the client's markup. The reason why this is so hard, I believe, is that Wicket is meant to help you build a web app that has state. I noticed that Wicket does a lot of stuff (like comparing submitted form values with what the model's getters return before the setters are called) behind the scenes, that I know too little about to be comfortable when just cutting it out.
You can use Wicket to provide RESTful web services, though, as outlined in this blog post. There's also a project on Google code called wicket-rest that expands on this idea. Note that this seems to work as simple as it does because it just never uses the whole component based UI building stuff.
The guy who wrote this post had a different problem, but it helped me understand Wicket forms a little better anyway.
you can hide a lot of the request mumbo jumbo by using a HybridUrlCodingStrategy like so:
mount(new HybridUrlCodingStrategy("/resetpwd", ResetPasswordPage.class));
Then when the click submit, assuming you don't redirect to a new page, the url would change from
mysite.com/DocRoot/resetpwd
to
mysite.com/DocRoot/resetpwd.1
or if you really want it to be mysite.com/DocRoot/resetpwd/submit you could create a new bookmarkable page, say ResetPasswordResult.class, set your response page to that and mount it at "/resetpwd/submit"
You might look at other encoding strategies to see if their is another that suits you better:
http://cwiki.apache.org/WICKET/url-coding-strategies.html
You can take a look at http://day-to-day-stuff.blogspot.com/2008/10/wicket-extreme-consistent-urls.html and try to adapt that for Forms.

Categories