Swing Style Aspect Part 2

Seems a few folks don’t get what my previous post about the Swing Style Aspect was all about, so I decied to answer a few of the comments in a post. First and foremost, this is merely a proof-of-concept and means of playing with AOP. Second, aspects provide another way of doing things and the results can certainly be achieved through other means. You don’t have to like or use AOP, but I do and I’m simply playing with idea here.

Two folks asked why I couldn’t simply use a factory class to vend styled JLabels as such:

JLabel name1 = FactoryJLabel.create(”Name 1″,”meduimGray”);

Well, I certainly could do that. In fact, the aspect implementation does exactly that under the hood. In some situtaions however, you may not be able use (or want to for that matter) a factory class to vend styled components. In my case, I’m using the JGoodies Binding framework to create bound JLabels:

JLable name1;
public void initComponents() {
  name1 = BasicComponentFactory.createLabel(model.getValue("name1"));

By adding in an aspect, I don’t need to refactor everything to make use of a style factory to create the label and then use the JGoodies Bindings class, which would allow me to perform the data bindings post-creation. In my situtaion, I felt an aspect was appropriate. Feel free to disagree, but I didn’t feel like refactoring 20-odd classes to implement a bolded label in Verdana at 16 point. The aspect got the task done quickly with the same results as refactoring to use a factory class. My point is, you may not always have the luxury of time to define a factory class and refactor a bunch of code.

Coolabana writes:

“I can’t see the benefit. What you do with aspect is what you usually do in a look and feel. Hence for me this is the wrong way. What is your motivation to solve this problem with aspects?”

Styling a JLabel to be have a bold font and be painted red to inidicate an error is not the responsibility of the look and feel. It is a style that is generally defined by the developer, and it is also a style that will probably span multiple look and feels. Second, an “error” style might be used accorss multiple components. The fact that the same code to create a styled JLable was being repeated accorss mutiple components, and the fact that the “styling” is not a core concern of the components and I felt like I recognized a crosscutting concern.

Maybe this will clear somethings up for folks, maybe not. But the comments, whether you agree or not, are always helpful.