Another Custom Wicket Component - DateDropDownField
Submitted by rowell on Mon, 04/11/2011 - 23:30I needed a date drop down component (see below) for my current project. Unfortunately, I couldn't find a built-in Wicket component that does the job. So why not write my own???
Java Code
- public class DateDropDownField extends FormComponentPanel<Date>
- {
- private static final long serialVersionUID = -839727513016106766L;
- private int month, day, year;
- private final DropDownChoice<Integer> monthField;
- private final DropDownChoice<Integer> dayField;
- private final DropDownChoice<Integer> yearField;
- {
- super(id, model);
- // create month field
- monthField = new DropDownChoice<Integer>("month",
- new PropertyModel<Integer>(this, "month"), // bind model
- getMonths(), // list of choices
- new MonthRenderer()); // renderer
- monthField.setLabel(new Model<String>("month"));
- monthField.add(new RangeValidator<Integer>(0, 11)); // add a simple validator
- // create day field
- dayField = new DropDownChoice<Integer>("day",
- new PropertyModel<Integer>(this, "day"), getDays());
- dayField.setLabel(new Model<String>("day"));
- // create year field
- yearField = new DropDownChoice<Integer>("year",
- new PropertyModel<Integer>(this, "year"), getYears());
- yearField.setLabel(new Model<String>("year"));
- add(monthField);
- add(dayField);
- add(yearField);
- }
- @Override
- public void onBeforeRender()
- {
- // initialize model
- if(date == null)
- {
- setModelObject(date);
- }
- // synchronize main model and drop down components
- cal.setTime(date);
- super.onBeforeRender();
- }
- @Override
- public void convertInput()
- {
- try
- {
- // get values from individual components
- int month = monthField.getConvertedInput();
- int day = dayField.getConvertedInput();
- int year = yearField.getConvertedInput();
- // TODO check for valid date (example - 02/31/11 is invalid)
- // update the model from converted input
- setConvertedInput(calendar.getTime());
- }
- setConvertedInput(null);
- }
- }
- // month choices
- private List<Integer> getMonths() {...}
- // day choices
- private List<Integer> getDays() {...}
- // year choices
- private List<Integer> getYears() {...}
- // render months as names instead of numbers
- private class MonthRenderer extends ChoiceRenderer<Integer>{
- private static final long serialVersionUID = -9037815586384085976L;
- @Override
- {
- return format.format(cal.getTime());
- }
- }
- }
HTML Code
- <html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd" >
- <body>
- <wicket:panel>
- <select wicket:id="month"></select>
- <select wicket:id="day"></select>
- <select wicket:id="year"></select>
- </wicket:panel>
- </body>
- </html>
Again, Wicket saved the day. Here's another reusable and self-contained component. To use this anywhere in your code, you simply need to add this new component to a form, panel, etc. No need to duplicate code or html markup!
Java Code
Extending Wicket's AjaxFallbackDefaultDataTable
Submitted by rowell on Sun, 04/03/2011 - 09:20I was looking for a Wicket datatable component with sorting, paging and ajax support. Fortunately, I found this component.The only problem is, I want to be able to apply a different background style when selecting each row (hover) and execute some action when the entire row is clicked. These features are not supported out-of-the-box.
Luckily, Wicket makes it very easy to extend components. All I had to do was hook into the method that creates each row object and add the new behavior. Here's the code...
Java Code
- public class CustomAjaxFallbackDefaultDataTable<T> extends AjaxFallbackDefaultDataTable<T>
- {
- // Constructor
- List<IColumn<T>> columns,
- ISortableDataProvider<T> dataProvider,
- int rowsPerPage)
- {
- super(id, columns, dataProvider, rowsPerPage);
- }
- @Override
- {
- // let super class create row item as usual
- final Item<T> item = super.newRowItem(id, index, model);
- // add style on mouse over
- item.add(new SimpleAttributeModifier("onmouseover",
- "this.style.backgroundColor='#80b6ed';"));
- // remove style on mouse out
- item.add(new SimpleAttributeModifier("onmouseout",
- "this.style.backgroundColor='';"));
- // execute when row is clicked
- item.add(new AjaxEventBehavior("onclick"){
- protected void onEvent(AjaxRequestTarget target){
- executeOnClick(target, item.getModel());
- }
- });
- return item;
- }
- public void executeOnClick(final AjaxRequestTarget target, final IModel<T> rowItem)
- {
- // execute server side code
- // execute client side code
- target.appendJavascript("alert('" + message + "');");
- }
- }
Now the table looks something like this...
As you can see, I was able to extend a component with minimal effort. In addition, this component is reusable. The user can simply override the executeOnClick method if a different action is required when the row is clicked. Try that with JSF =)
Drupal / Java Integration
Submitted by rowell on Mon, 12/13/2010 - 20:59Drupal is one of the best open source content management systems currently available. One of Drupal's strengths is its module development architecture. It allows developers to extend Drupal's core features in a non-invasive and elegant manner. My only problem with Drupal is that it is written in .... PHP.
I am not a PHP expert, but I am an experienced Java Developer. I have written and deployed many Java enterprise applications in production. I am very familiar with a number of Java frameworks and technologies such as Spring, Struts, JSF, Wicket, JPA, JMS, Web Services, etc. I am also proficient on several Java development/testing tools. However, I do not know of any Java open source projects that can provide all of Drupal's features out-of-the-box.
Luckily, I found a way to integrate Drupal/PHP and Java using the Quercus PHP engine. Because Quercus can run PHP scripts and expose Java methods in PHP, I was able to write Drupal modules by calling Java methods inside PHP functions. I can write most of the business logic in Java and only use PHP to write the Drupal "hooks"!.
I was also able to integrate other Java frameworks such as Spring, Vaadin and Wicket. I used SiteMesh to decorate the Wicket/Vaadin pages using the same Drupal theme.
Check out my POC web application here!