Thursday, April 22, 2010

RichFaces - "beautiful" code

I'm extending the rich:calendar control today trying to put its definition into a table (for reasons that are not important here). The outcome should be as easy as input in one cell and the icon in another. Sounds easy, right?

Nothing could be further from the truth!

How I'd envision generation of HTML in such a case would be a container class having things like name, attributes collection, possibly even child controls (if it's a container for other controls) with one method "render(where...)" that would make use of those contained properties.

But it turns out that the guys from RichFaces didn't get the memo, that code duplication is bad. In fact they missed it so much that getting the whole thing to do what I want it to do takes a copy and paste, then refactoring and at the end implementation of the stuff I need.

Here's an example of how the render for example attributes of a control:

getUtils().writeAttribute(writer, "accesskey",
component.getAttributes().get("accesskey"));
getUtils().writeAttribute(writer, "class", "rich-calendar-input " +
convertToString(component.getAttributes().get("inputClass")));
getUtils().writeAttribute(writer, "disabled",
variables.getVariable("disabled"));
getUtils().writeAttribute(writer, "id",
convertToString(clientId) + "InputDate");
getUtils().writeAttribute(writer, "maxlength",
component.getAttributes().get("maxlength"));
getUtils().writeAttribute(writer, "name",
convertToString(clientId) + "InputDate");
getUtils().writeAttribute(writer, "onblur",
component.getAttributes().get("oninputblur"));
getUtils().writeAttribute(writer, "onchange",
component.getAttributes().get("oninputchange"));
getUtils().writeAttribute(writer, "onclick",
component.getAttributes().get("oninputclick"));
getUtils().writeAttribute(writer, "onfocus",
component.getAttributes().get("oninputfocus"));
getUtils().writeAttribute(writer, "onkeydown",
component.getAttributes().get("oninputkeydown"));
getUtils().writeAttribute(writer, "onkeypress",
component.getAttributes().get("oninputkeypress"));
getUtils().writeAttribute(writer, "onkeyup",
component.getAttributes().get("oninputkeyup"));
getUtils().writeAttribute(writer, "onmouseout",
component.getAttributes().get("oninputmouseout"));
getUtils().writeAttribute(writer, "onmouseover",
component.getAttributes().get("oninputmouseover"));
getUtils().writeAttribute(writer, "onselect",
component.getAttributes().get("oninputselect"));
getUtils().writeAttribute(writer, "size",
component.getAttributes().get("inputSize"));
getUtils().writeAttribute(writer, "style",
"vertical-align: middle; " + convertToString(component.getAttributes().get("inputStyle")));
getUtils().writeAttribute(writer, "tabindex",
component.getAttributes().get("tabindex"));
getUtils().writeAttribute(writer, "type",
variables.getVariable("type"));
getUtils().writeAttribute(writer, "value",
getInputValue(context, component));

I mean who does that in the 21th century in an object-oriented programming language?!?!?!?!?

Here's another example of pure madness:

writer.writeText(convertToString(
"dayListTableId: '" +
convertToString(clientId) +
"Day', \n weekNumberBarId: '" +
convertToString(clientId) +
"WeekNum', \n weekDayBarId: '" +
convertToString(clientId) +
"WeekDay',\n currentDate: " +
convertToString(getCurrentDate(context, component, currentDate)) +
", \n selectedDate: " +
convertToString(getSelectedDate(context, component)) +
", \n datePattern: '" +
convertToString(component.getDatePattern()) +
"',\n jointPoint: '" +
convertToString(component.getJointPoint()) +
"',\n direction: '" +
convertToString(component.getDirection()) +
"',\n boundaryDatesMode:'" +
convertToString(component.getBoundaryDatesMode()) +
"',\n popup: " +
convertToString(component.isPopup()) +
",\n enableManualInput: " +
convertToString(component.getAttributes().get("enableManualInput")) +
",\n showInput: " +
convertToString(component.getAttributes().get("showInput")) +
",\n disabled: " +
convertToString(component.isDisabled()) +
",\n readonly: " +
convertToString(component.getAttributes().get("readonly")) +
",\n ajaxSingle: " +
convertToString(component.getAttributes().get("ajaxSingle")) +
",\n verticalOffset:" +
convertToString(component.getVerticalOffset()) +
",\n horizontalOffset: " +
convertToString(component.getHorizontalOffset()) +
",\n style:'z-index: " +
convertToString(component.getAttributes().get("zindex")) +
"; " +
convertToString(component.getAttributes().get("style")) +
"',\n firstWeekDay: " +
convertToString(getFirstWeekDay(context, component)) +
", \n minDaysInFirstWeek: " +
convertToString(getMinDaysInFirstWeek(context, component)) +
",\n todayControlMode:'" +
convertToString(component.getAttributes().get("todayControlMode")) +
"',\n showHeader:" +
convertToString(component.getAttributes().get("showHeader")) +
",\n showFooter:" +
convertToString(component.getAttributes().get("showFooter")) +
",\n showWeeksBar:" +
convertToString(component.getAttributes().get("showWeeksBar")) +
",\n showWeekDaysBar:" +
convertToString(component.getAttributes().get("showWeekDaysBar")) +
",\n showApplyButton:" +
convertToString(component.getAttributes().get("showApplyButton")) +
",\n resetTimeOnDateSelect:" +
convertToString(component.getAttributes().get("resetTimeOnDateSelect")) +
",\n defaultTime:" +
convertToString(getPreparedDefaultTime(component))), null);


Finally please note that both fragments come from the same single method and that's just a fraction of that method!

Now you tell me: would you ever use stuff that looks like that underneath the skin?

Wednesday, April 14, 2010

JSF - a component framework only the mother can love

Hi there,

this is another JSF nonsense - the component creation style.

I'm just sick an tired of going over the renderers that don't render anything, the component tags (like it's not enough to have just one of those guys)... I mean come on!!!

For those of you that say that JSF 2.0 make a difference I rush to clarify that it is so not true - the component model didn't change even a bit! What they added is Facelets which is what most people were already using anyway.

What's screwd though is that Facelets is a templating engine and they try to use it for component creation. That's grouse!!!

Tuesday, July 14, 2009

Trinidad + Richfaces = Problem (solution)

Finally after 3 days of googling and a trial-and-error approach I've found out how to overcome the problem with links in a table being stripped from javascript code after an Ajax call (for example for pagination or sorting columns).

The answer to that is a4j:htmlCommandLink (instead of h:commandLink) inside an a4j:form (instead of h:form). Don't ask me why - it's to complex to describe the actual bits and pieces behind some sick concepts in those libraries. The final fact is that h:commandLink ain't working but the a4j:htmlCommandLink is. Period.

Later today I'll attach a working project with the solution. For now you're just going to have to trust me on that one :D

Monday, July 13, 2009

RichFaces 3.3.1 Bug

Another day got lost because of some guy trying to make things that work better. This time it's sorting of columns generated from list. All used to work until version 3.3.0.CR1 and then, all of the sudden, it stopped. You can click the damn header all you want and nothing happens.

Components involved: rich:dataTable and rich:columns

O my gosh how I hate those days...

Saturday, July 11, 2009

RichFaces + Trinidad = problems

I've been fighting for over 3 days on that so you might wanna listen to the story not to make the same mistake I did.

Some time ago someone decided to use JSF libraries in our project. That same someone decided to use RichFaces (a nice library indeed) and Trinidad (the most terrible piece of code I've ever encountered) in one project.
As the story continued we've been using both of those libraries and it worked quite nice right up to the point where I wanted to use rich:datascroller on a rich:dataTable component containing some action links.

Just so that you know what I'm talking about I'm giving you 2 small projects that show the problem. One without Trinidad configuration (this one is working just fine) and the other one with Trinidad.

example-01-working.zip
example-01-not_working.zip

Basically the Ajax rendering engine from RichFaces is being crushed down with the one from Trinidad. They just hate eachother!

I've never ever expected that 2 libraries might have such a negative impact on eachother! What a waste of time!!!

Thursday, June 25, 2009

JSF + HTML - a sudden divorce

Now here's a neat one: try mixing HTML and JSF markup in one place. It's all nice and dandy unless there's no html->jsf->html->jsf nesting. In that case the JSF tags are omitted! Here's an explanation why that was the case in JSF 1.1 and that it's supposedly fixed in 1.2 but I've tried it out and it's still not working.

FYI: that works just fine on multiple level of nesting in ASP.NET!

Friday, June 5, 2009

Trinidad nonsense

Today I've spent almost 6 hours trying to find out the CSS responsible for introducing an 8 pixel top margin for body. The case is even stranger in that the mysterious margin appears only in Internet Explorer. Imagine the 8px disruption in absolute positioning of the elements on the page - it just looks bad.

Finally I've found the problem: Trinidad introduces some stylesheets that do exactly that.

Well it wouldn't be much of an issue if it were not for the fact that this damn CSS is included as the last thing in the head!!! So you can do anything you like - body will always have this damn 8 pixels margin in IE only no matter what.

I'd like to find out who the butthead that invented this piece of sh** is. I mean, come on!!! Someone must have been stoned to do this kind of thing!

The worst thing is I have not damn idea how to fix it other than using some sort of on-load method to correct the CSS for body element. jQuery works well, but this is the ugliest hack I've ever come across.