How to concatenate 2 textboxes in wicket? - wicket

So i got these 2 text boxes and I'm trying to concatenate them together and show the result in label. I found an example and did it like in the example but something is wrong. So maybe some one can see what I am doing wrong, because i have just started and don't understand how to do it properly.
public class HomePage extends WebPage {
private String fNumber="Big";
private String sNumber=" text!";
private String sResult=fNumber+sNumber;
public HomePage() {
PropertyModel<String> firstNumber = new PropertyModel<String>(this, "fNumber");
PropertyModel<String> secondNumber = new PropertyModel<String>(this, "sNumber");
add(new Label("message", "HelloWorld!"));
add(new Label("result", sResult));
Form<?> form = new Form("form");
form.add(new TextField<String>("firstNumber", firstNumber));
form.add(new TextField<String>("secondNumber", secondNumber));
add(form);
}
}
soo i have made this
` add(new Label("message", "HelloWorld!"));
add(new Label("result", new Model(numb.getsResult())));
Form<?> form = new Form("form") ;
form.add(new TextField<String>("firstNumber", new Model(numb.setfNumber())));
form.add(new TextField<String>("secondNumber",new Model(numb.setsNumber())));
add(form);`
and i have a class that has 3 string fields and getters and setters and sii that much i have understood last comment explained some things maybe some one know how to fix this.

You need to "recalculate" your result. The Wicket way would be to define a Model for your Label that does the concatenation.
add(new Label("result", new IModel<String>(){
#Override
public void detach() {
// do nothing
}
#Override
public String getObject() {
return fNumber + sNumber;
}
#Override
public void setObject(String object) {
// do nothing
}
}));
Additionally you must use the PropertyModels from the example.

To concatenate two strings I usually use StringBuilder:
PropertyModel firstNumber = new PropertyModel(this,"fNumber");
PropertyModel secondNumber = new PropertyModel(this,"sNumber");
PropertyModel resultNumber = new PropertyModel(this,"sResult");
StringBuilder sResult = new StringBuilder((String) firstNumber.getObject());
sResult.append((String) secondNumber.getObject());
resultNumber.setObject(sResult.toString());
Also, please read the link from biziclop as it should help you significantly.

Related

passing parameters to wicket panel

firstly wish you all a very happy new year.
i am moving my data tables in to a panel from webpage. i was using pageparameters to pass required parameters on clicking the link for retrieving data and displaying it in the next page. now that i have moved those tables in to a panel i am not sure how to forward those parameters in panels.
My calling Method:
final TextField<String> jobnumber = new TextField<String>("jobnumber ", Model.of(""));
jobnumber .setRequired(true);
final TextField<String> jobtype= new TextField<String>("jobtype", Model.of(""));
jobtype.setRequired(true);
Form form = new Form("form") {
#Override
public void onSubmit() {
final String jobnumber = jobnumber .getModelObject();
final String jobtype= jobtype.getModelObject();
PageParameters params= new PageParameters();
params.add("jobnumber ", jobnumber );
params.add("jobtype", jobtype);
new Job("jobs", params);
}
};
Button button = new Button("button");
form.add(button);
form.add(jobnumber);
form.add(jobtype);
add(form);
My Panel Constructor:
public class Job extends Panel {
public Job(String id, **PageParameters params**) {
super(id);
String jobnumber = params.get("jobNumber").toString();
String jobtype= params.get("jobtype").toString();
add(new Label("jobNumberLabel", jobnumber));
add(new Label("jobtypeLabel", jobtype));
list = retrieveJob(jobnumber, jobtype);
add(new ListView("agilejobs1", list) {
#Override
protected void populateItem(ListItem item) {
final Job job = (Job) item.getModelObject();
item.add(new Label("jobNumber", job.getJobNumber()));
item.add(new Label("jobdesc", job.getJobdesc()));
item.add(new Label("jobcount", job.getJobCount()));
}
});
}
}
i have tried to replace PageParameters with IModel but it did not work.
Please suggest.
Thanks
I would use a IModel<List<Job>>. You can create one in your panel like this:
public class JobPanel extends Panel {
public JobPanel(String id, String jobnumber, String jobtype) {
super(id);
IModel<List<Job>> jobListModel = new ListModel<Job>(retrieveJob(jobnumber,jobtype));
add(new ListView("agilejobs1", jobList) {
#Override
protected void populateItem(ListItem item) {
final Job job = (Job) item.getModelObject();
item.add(new Label("jobNumber", job.getJobNumber()));
item.add(new Label("jobdesc", job.getJobdesc()));
item.add(new Label("jobcount", job.getJobCount()));
}
});
}}
However, it would be better to construct the IModel<List<Jobs>> outside of the Panel and just pass it as an argument. The creation/setting of the model can be done in an Page, an onSubmit() in a Form or whatever method you like. Then the code becomes much cleaner, as the JobPanel will only be responsible for showing the data.

unit-testing Wicket input components

I just wrote my first Wicket component :) It contains a ListView with some Radio input fields. Now I want to unit test if a selected value makes its way to the model.
As WicketTester.newFormTester("myForm") expects a form, I try to create a form on the fly:
public void testDataBinding()
{
Model model = ...
MyRadioComponent myRadioComponent = new MyRadioComponent (...);
Form form = new Form("myForm", ...);
form.add(myRadioComponent);
WicketTester wicketTester = new WicketTester();
wicketTester.startComponentInPage(form);
// FormTester formTester = wicketTester.newFormTester("myForm");
// ...
}
Now wicketTester.startComponentInPage(form) results in:
Failed: Component [myForm] (path = [0:x]) must be applied to a tag of type [form],
not: '<span wicket:id="myForm" id="myForm3">'
Any idea how to fix this and/or how to test such an input component the right way?
OK, in detail the solution now looks like this:
public FormTester createFormTester(Component c) {
final WicketTester wicketTester = new WicketTester();
final FormPage page = new FormPage(c);
wicketTester.startPage(page);
return wicketTester.newFormTester(page.getPathToForm());
}
private static class FormPage extends WebPage implements IMarkupResourceStreamProvider {
private final Form<Void> form;
private final Component c;
private FormPage(final Component c) {
this.c = c;
add(form = new Form<>("form"));
form.add(c);
}
public String getPathToForm() {
return form.getPageRelativePath();
}
#Override
public IResourceStream getMarkupResourceStream(MarkupContainer container, Class<?> containerClass) {
return new StringResourceStream(
"<html><body>"
+ "<form wicket:id='" + form.getId() + "'><span wicket:id='" + c.getId() + "'/></form>"
+ "</body></html>");
}
}
I believe startComponentInPage uses a <span> for its component. Wicket checks that Forms are attached to <form> tags which is why you get this error.
You need to create your own test page with a <form> inside it. See org.apache.wicket.markup.html.form.NumberTextFieldTest for an example of inline markup. Otherwise, create a Form test page class with associated html markup file.

Google Web Toolkit - How to prevent duplication of the records?

I am new in GWT. I am trying to use the cell table to do this. Here is my questions:
Name Gender
Ali M
Abu M
Siti F
page 1
Name Gender
Siti F
Noor F
Ahmad F
page 2
I use simple pager to do the paging function. Everything is ok except next page.
When i click next page, siti record appear 2 times.
How to prevent the name Siti not appear in page 2? Below are my code:
private static class Contact{
private final String name;
private final String gender;
public Contact(String name, String gender){
this.name = name;
this.gender = gender;
}
}
private static final List<Contact> CONTACTS = Arrays.asList(
new Contact("Ali","M"),
new Contact("Abu","M"),
new Contact("Siti","F"),
new Contact("Noor","F"),
new Contact("Ahmad","M")
);
public void onModuleLoad(){
final CellTable<Contact> table = new CellTable<Contact>();
table.setPageSize(3);
TextColumn<Contact> nameColumn = new TextColumn<Contact>(){
#Override
public String getValue(Contact object) {
return object.name;
}
};
TextColumn<Contact> genderColumn = new TextColumn<Contact>(){
#Override
public String getValue(Contact object) {
return object.gender;
}
};
table.addColumn(nameColumn, "Name");
table.addColumn(genderColumn, "Gender");
AsyncDataProvider<Contact> provider = new AsyncDataProvider<Contact>(){
#Override
protected void onRangeChanged(HasData<Contact> display) {
int start = display.getVisibleRange().getStart();
int end = start + display.getVisibleRange().getLength();
end = end >= CONTACTS.size() ? CONTACTS.size() : end;
List<Contact> sub = CONTACTS.subList(start,end);
updateRowData(start,sub);
}
};
provider.addDataDisplay(table);
provider.updateRowCount(CONTACTS.size(), true);
SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
SimplePager pager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0, true);
pager.setDisplay(table);
Please help me to solve this problem. Thanks.
You have faced most probably the gwt last page problem, described in the linked questions:
GWT - celltable with simple pager issue
SimplePager row count is working incorrectly
The solution here is to set:
setRangeLimited(false)
and the last page is paged correctly, ie. it contains only Noor and Ahmad.
So in conclusion: actually no duplication is present here, but a bug on pagination in case of the last page. You will observe the same behavior with also other amounts of data, but on my view point it would be always a last page issue only.

trying to add some link cell in my GWT cellTable

I am trying to add a Link in my cell table (I just want the item to be underlined and mouse symbol change on hover)
and on click I just want to give a window Alert .
for that i have tried these Options : ( but no luck )
1)
final Hyperlink hyp = new Hyperlink("test", "test");
Column<EmployerJobs, Hyperlink> test = new Column<EmployerJobs, Hyperlink>(new HyperLinkCell())
{
#Override
public Hyperlink getValue(EmployerJobs object)
{
return hyp;
}
};
Problem with option 1 is , it takes me to navigation page "test", whereas I dont want to go any other page i just want a window alert.
2)
Column<EmployerJobs, SafeHtml> test = new Column<EmployerJobs, SafeHtml>(new SafeHtmlCell())
{
#Override
public SafeHtml getValue(EmployerJobs object)
{
SafeHtmlBuilder sb = new SafeHtmlBuilder();
sb.appendEscaped("test");
return sb.toSafeHtml();
}
};
problem with option 2 is I dont know what exactly to return here and its not getting underlined.
3) at last i am trying to add anchor in my celltable with a compositecell(as ideally i want three different anchors in my ONE cell)
final Anchor anc = new Anchor();
ArrayList list = new ArrayList();
list.add(anc);
CompositeCell ancCell = new CompositeCell(list);
Column testColumn1 = new Column<EmployerJobs, Anchor>(ancCell) {
#Override
public Anchor getValue(EmployerJobs object) {
return anc;
}
};
Option 3 is giving some exception .
If you can help me get working any of the above option, I'll be grateful
Thanks
You are doing it totally wrong. You need to use ActionCell for stuff like this or create your own cell. Example code:
ActionCell.Delegate<String> delegate = new ActionCell.Delegate<String>(){
public void execute(String value) { //this method will be executed as soon as someone clicks the cell
Window.alert(value);
}
};
ActionCell<String> cell = new ActionCell<String>(safeHtmlTitle,delegate){
#Override
public void render(com.google.gwt.cell.client.Cell.Context context, //we need to render link instead of default button
String value, SafeHtmlBuilder sb) {
sb.appendHtmlConstant("<a href='#'>");
sb.appendEscaped(value);
sb.appendHtmlConstant("</a>");
}
};
Column testColumn1 = new Column<EmployerJobs, String>(cell) {
#Override
public String getValue(EmployerJobs object) {
//we have to return a value which will be passed into the actioncell
return object.name;
}
};
I recommend to read official documentation for Cell Widgets, since it is pretty much everything what you need to know about cell widgets.

How do I add items to GWT ListBox in Uibinder .ui.xml template ?

How to add the listbox items using UiBinder?
It is possible since february 2011 version:
http://code.google.com/p/google-web-toolkit/issues/detail?id=4654
Following this patch you are now able to add items following this syntax:
<g:ListBox>
<g:item value='1'>
first item
</g:item>
<g:item value='2'>
second item
</g:item>
</g:ListBox>
This is a listbox of translations of an enumeration, I suppose this also works for a listbox with string values (version of GWT: 2.1.0)
You only need the renderer for translating the enumeration values.
//UI XML
<g:ValueListBox ui:field="requesterType"/>
//JAVA CODE
#UiField(provided = true)
ValueListBox<RequesterType> requesterType = new ValueListBox<RequesterType>(requesterTypeRenderer);
static EnumRenderer<RequesterType> requesterTypeRenderer = new EnumRenderer<RequesterType>();
public Constructor() {
requesterTypeRenderer.setEmptyValue(Translations.translateEmptyValue(RequesterType.class));
requesterType.setAcceptableValues(Arrays.asList(EnumUtil.getRequesterTypes()));
}
/**
* Translates enum entries. Use setEmptyValue() if you want to have a custom empty value. Default empty value is "".
*
* #param <T>
* an enumeration entry which is to be registered in {#link Translations}
*/
public class EnumRenderer<T extends Enum<?>> extends AbstractRenderer<T> {
private String emptyValue = "";
#Override
public String render(T object) {
if (object == null)
return emptyValue;
return Translations.translate(object);
}
public void setEmptyValue(String emptyValue) {
this.emptyValue = emptyValue;
}
}
GWT ValueListbox otherwise know as a ComboBox or Dropdown component.
Another example that also demonstrates populating the list.
UiBinder...
<g:ValueListBox ui:field="subCategory"/>
Editor...
#UiField(provided = true)
ValueListBox<String> subCategory = new ValueListBox<String>(
new Renderer<String>() {
#Override
public String render(String object) {
String s = "Cats";
if (object != null) {
s = object.toString();
}
return s;
}
#Override
public void render(String object, Appendable appendable)
throws IOException {
render(object);
}
});
Constructor...
List<String> values = new ArrayList<String>();
values.add("Animal Shelters and Rescues");
values.add("Birds");
values.add("Cats");
values.add("Dogs");
values.add("Other Pets");
values.add("Rabbits");
subCategory.setAcceptableValues(values);