Apache Wicket - Forms - wicket

Hello I am new to Apache Wicket, can someone help me find some documentation or examples related to Apache Wicket Forms where I can navigate between a flow of screens having Text fields, Drop downs, Back and Next button.
I tried a simple example with two screens where i can navigate between the two screens. The issue that i am facing is I am unable to see the form data that i entered in the first screen on click of "Back" button from my second page and also I am not able to see the data that i entered in second screen on clicking "Next" button from 1st screen.

You could use org.apache.wicket.extensions.wizard.Wizard.
First define your wizard steps like this:
private final class UserFieldsStep extends WizardStep {
public UserFieldsStep() {
super(new ResourceModel("adduser.title"), new ResourceModel("adduser.summary"));
add(new RequiredTextField("user.name"));
add(new RequiredTextField("user.email").add(EmailAddressValidator.getInstance()));
add(new TextArea("user.description"));
}
}
And then
public class AddUserWizard extends Wizard {
public AddUserWizard(String id) {
super(id);
....
WizardModel model = new WizardModel();
model.add(new UserNameStep());
// initialize the wizard with the wizard model we just built
init(model);
}
}
More examples are available here: http://examples7x.wicket.apache.org/wicket-examples/wizard/
http://examples7x.wicket.apache.org/wicket-examples/wizard/wicket/bookmarkable/org.apache.wicket.examples.source.SourcesPage?2&SourcesPage_class=org.apache.wicket.examples.wizard.WizardPage

Related

How to redirect to another page in GWT application

I have a client class UserLogin which also represents my entrypoint . I ask for a password and login to the user, I make my server side check and I get a response in the form of a string representing an xml file that I must parse and generate the widget with the coded information this xml .
I'm stuck here: if the user is recognized , I want to redirect to a second page "home" which will present the user with the component provides the answer. Only I do not know how to do that normally in class that generates the home I have to have the onModuleLoad () method that will parse the response from the server and display the desired elements.
GWT is Single Page Web Application,to Navigating one Screen to other screen just clear layout Container and add the newly created Container to RootPanel.See example code,
HorizontalPanel hp=new HorizontalPanel();
Button btn=new Button("Login");
hp.add(new TextBox());
hp.add(new PasswordTextBox());
hp.add(btn);
RootPanel.get().add(hp);
btn.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent clickEvent) {
RootPanel.get().clear();
RootPanel.get().add(new screen());
}
});

how to dianmically add dialog to webpage in wicket?

I have write my own dialog and on my Index page have a AjaxLink
when AjaxLink is clicked. I will behave to open my dialog instance.
this is my code
add(new AjaxLink("open.working.date.dialog") {
#Override
public void onClick(AjaxRequestTarget target) {
WorkingDateDialog dialog = new WorkingDateDialog("working.date.dialog", getString("index.working.date.dialog.title"), true);
Index.this.add(dialog);
dialog.open(target);
}
});
of course, on my web page html markup I don't have reference component id working.date.dialog and it will throw exception.
but when I replace Index.this.add(dialog); by this.add(dialog); or by target.add(dialog); the dialog won't work.
There any other way to add dynamically dialog to page?
In jquery I can do that easily by just append dialog html to body then open it by jquery.
thanks for your all helpful!
One option is to add the dialog in the constructor of the page and hide it initially. Then later when clicking the link just mark it as visible and add it to the AjaxRequestTarget.
Another option is to add a dummy/empty WebMarkupContainer with the same component id and later replace it with the dialog in #onClick().
You are going against the wicket way of doing things here, which will cause you much pain. :-) First of all WorkingDateDialog needs to extend ModalWindow. Assuming it does, here are my suggestions.
Right above your ajax link add this code:
final WorkingDateDialog dialog = new WorkingDateDialog("working.date.dialog", getString("index.working.date.dialog.title"), true);
add(dialog);
Then your ajax link becomes:
add(new AjaxLink("open.working.date.dialog") {
#Override
public void onClick(AjaxRequestTarget target) {
dialog.show(target);
}
});
So you always add your ModalWindow instance to your page hierarchy. It' just not visible until someone clicks the link.
I hope this helps you. Please let me know if you have any questions.

Actionbar and ListActivity in one Activity

I have a program which shows lists of files in directories on the SDCARD and when the user clicks on a file it opens the PDF up. For instance one activity will display the contents of one folder.
I have been wanting to add the ActionBar for navigation and can get the ActionBar to work in its own activity and my ListActivity to work in its own activity, however I cannot get them to work together.
I am not sure how to combine the two so that I end up with one activity that displays the action bar and the file list in the one Activity?
The Actionbar Activity starts with
public class SecondActivity extends FragmentActivity implements ActionBar.OnNavigationListener {
The Activity that displays the clickable list starts with
public class MainActivity extends ListActivity {
My android app programming is quite basic and self taught so I am probably missing something obvious. Thanks in advance
I have posted an alternative solution that separates the concerns of the List and the ActionBar. Makes the code a little tidier IMO.
I think you can extend your class to ListActivity and override method onCreateOptionsMenu to bind your action bar to the activity:
public class MainActivity extends ListActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_action_bar_name_here, menu);
return (super.onCreateOptionsMenu(menu));
}
// your other methods
}

Wicket: Website hierarchy

Thats a general question about: How to structure my website with all its pages and components (here panels) in wicket. I would like to show my attempt and hope someone can give me advice, wheather its a good way or if there is a better one.
My Structure is like:
Root: HomePage
Page1 extends HomePage
Page2 extends HomePage
Page3 extends HomePage
The pages wrap the content and its own navigation. They get init in HomePage.html with wicket child.
Now, when I define a new Panel for Page1, I have to define a link for it as well. For the link onClick() I set the panel to which it refers to visible and all other panels to invisible. Also I have to define on the Page1 the panel, which gets shown when I navigate to Page1. All other panels are invisible.
Is this a good attempt or is there a better way? Now I initialize ever panel and just hold them invisible.
A shot at answering your questions... This assumes you're using Wicket 1.4.x.
First, you can have many levels of Page classes, often mimicking the overlap of the design and function. For example, say you have an application where people "Write", "Browse" and "Read" user-created books.
RootPage - common headers/footers, javascript imports
AbstractWritePage extends RootPage - for anything regarding authoring
AbstractBrowsePage extends RootPage - browsing
AbstractReadPage extends ReadPage - reading
Then, I implement something like:
FullLibraryPage extends AbstractBrowsePage
FilterSearchPage extends AbstractBrowsePage
In the long run, it gets complicated, but very powerful.
Secondly, your Panel components that go visible/invisible. If you're using Wicket 1.4.x, you should look at the overrideable method onConfigure() for each Panel. In this panel, you can set the visibility like:
#Override
protected void onConfigure() {
super.onConfigure();
setVisible(!navTriggered);
}
where navTriggered is a boolean value residing in the containing page. Then, your link could do a simple:
#Override
protected void onClick(AjaxRequestTarget target) {
navTriggered = false;
target.addComponent(/* Appropriate panels; see below for multiples */);
}
The advantage to this is that you can have multiple panels triggered by the same boolean variable. There is nothing wrong with creating all of your panels at page creation time, even if they start out invisible.
Finally, if you have a lot of panels that need to be changed/triggered/etc, consider pairing an IVisitor with a marking interface. Something like...
public class Panel1 implements MyPanelGroup { ... }
public class Panel2 implements MyPanelGroup { ... }
Then, you can use an IVisitor to visit every instance of MyPanelGroup in a page and do something with visibility (either set visibility, add it to the AjaxRequestTarget, etc).
Hope that answers something :)

How to keep business logic seperate within GWT Composites?

I'm currently building a GWT login screen and I managed to get a basic version of the login structure working.
I created the following pieces:
An entry point class (actually it was given).
A main application screen composite.
A login-screen composite.
I have the following logic in my entry point method:
MyApplication mainWindow = null;
public void onModuleLoad() {
LoginScreen loginScreen = new LoginScreen() {
#Override
public String onLogin(String username, String password) {
boolean passwordWasOk = rpcCheckUsernamePassword(username,password); // mechanism not important for this question
if (passwordWasOk) {
RootPanel.get().remove(0);
mainWindow = new MyApplication();
// Using root layout panel as main window is a layout type composite
RootLayoutPanel.get().add(mainWindow);
return null;
} else {
return "password was incorrect";
}
}
};
RootPanel.get().add(loginScreen);
}
So, I created a method in the LoginScreen composite that is called when the user clicks the 'Sign-In' button. If the onLogin method fails its validation of the username and password, then a narrative can be returned to the login composite so that it can update the user. The login screen will stay on the screen until the user uses a correct username/password combination.
My question is, is this the correct way to use composites? Where should my login logic ideally reside. Is a better alternative to inject some sort of login handler object in the constructor of the composite or via a setter or is the method I used quite normal?
As I'm planning to write quite a lot of code, I want to get the coding style correct from the outset.
Any feedback is greatly appreciated.
Thanks,
For complex projects you'd want to use the Model-View-Presenter (MVP) design pattern. It allows you to separate the rendering logic (views) from the business logic. To get you started, the official documentation has two articles about it plus there's that presentation by Ray Ryan that started the whole GWT + MVP = <3 haze :) After that I'd recommend browsing through MVP related questions on SO and GWT's Google Group.