8.47% is a Resource: statistics about Wicket

Wicket 1.3.3 statistics class cloudFrameworks are growing with every release. Classes are changed, removed and added. In this series I zoom in on some well known projects and analyze their class names with completely meaningless statistics. Now up: Wicket 1.3.3!

To get these statistics, I wrote a script that analyzed all classes. They get chopped up on word boundaries, so for ContextAwareFactoryBean the words Context, Aware, Factory and Bean are counted. From the output I generated a class cloud.

Wicket likes Requests for Pages or Resources

8.47% of the total number of classes in Wicket contain the word Resource. In absolute numbers, 68 classes (of a total of 802) have this word in their name. This is quite a low percentage for the top result, compared to the Spring statistics where the top result Bean is present in 12.31% of all classes.

From the top ten, it’s easy to spot that Wicket is a web framework, with Pages, Web, Ajax and Resources in many class names.

Wicket loves Wicket

A bit remarkable regarding Wicket statistics: about 4.24% of all classes have the string ‘Wicket’ in their name. Relative to the number of Spring classes that have Spring in their name, this is 2.3 times as much!

In general, the class names of Wicket are well distributed. This probably originates in the fact that Wicket is very specialized on one subject and is not a general purpose platform.

Class Cloud (click to enlarge)

Wicket 1.3.3 statistics Class Cloud

Top 10 of partial class names

Wicket 1.3.3 statistics bar chart

  • Resource: 68
  • Request: 62
  • Page: 56
  • Abstract: 42
  • Target: 37
  • Stream: 36
  • Validator: 36
  • Web: 36
  • Component: 34
  • Ajax: 34

Longest class name

The grand prize goes to: BookmarkablePageRequestTargetUrlCodingStrategy, with 46 characters!

The BookmarkablePageRequestTargetUrlCodingStrategy encodes and decodes mounts for a single bookmarkable page class (source).

Stay tuned for more useless statistics for other well known projects! If you have suggestions for which projects you want to see, please let me know in the comments!

1942 classes: statistics about Spring 2.5

Spring 2.5 statistics class cloudFrameworks are growing with every release. Classes are changed, removed and added. In this series I zoom in on some well known projects and analyze their class names with completely meaningless statistics. First: Spring 2.5.

To get these statistics, I wrote a script that analyzed all classes. They get chopped up on word boundaries, so for ContextAwareFactoryBean the words Context, Aware, Factory and Bean are counted. From the output I generated a Class Cloud.

Spring is a framework of Factories, Beans, and Exceptions

There are 1942 classes currently in Spring. Of those, 213 classes contain the word Factory. Even more classes contain the word Bean. Spring is clearly a framework that loves factories and beans. There is even a FactoryBean class.

Another high score partial class name is the Exception. Exactly 197 classes have that in their class name. From the humble BindException to the fine-grained JdbcUpdateAffectedIncorrectNumberOfRowsException.

Class Cloud (click to enlarge)

Spring 2.5 statistics Class Cloud

Top 10 of partial class names

  • Bean: 239
  • Factory: 213
  • Exception: 197
  • Abstract: 158
  • Context: 112
  • Source: 97
  • Utils: 89
  • Transaction: 85
  • Resolver: 79
  • Request: 78

Longest class name

The grand prize goes to two classes: AbstractInterruptibleBatchPreparedStatementSetter and AbstractTransactionalDataSourceSpringContextTests

Both have a length of 49 characters!

Stay tuned for more useless statistics for other well known projects! If you have suggestions for which projects you want to see, please let me know in the comments!

4 steps to add Scala to your Maven Java projects

ScalaIn this tutorial you learn how to integrate Scala in your existing Maven Java projects. From configuring the project’s pom file to adding your first Scala code. Tip: Try to hide this inside a large code commit, and your co-workers will not notice you have added Scala support.. surprise!

Step 1: Configure the Maven Scala Plugin in your pom.xml

To building the Scala source code into Java bytecode, you can use the Maven Scala Plugin.
Add the <plugins> section somewhere in your pom.xml (in the <build> section):

<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
               <artifactId>maven-scala-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
<project>

Alternative Step 1: Circular dependencies

Update: Do you need to build circular dependencies between Java and Scala? Put this in your pom.xml instead.

Add the <plugins> section somewhere in your pom.xml (in the <build> section):

<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
               <artifactId>maven-scala-plugin</artifactId>
 
                <executions>
                    <execution>
                        <id>compile</id>
                        <goals>
                        <goal>compile</goal>
                        </goals>
                        <phase>compile</phase>
                    </execution>
 
                    <execution>
                        <id>test-compile</id>
                        <goals>
                        <goal>testCompile</goal>
                        </goals>
                        <phase>test-compile</phase>
                    </execution>
 
                    <execution>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
<project>

Step 2: Add Maven Scala Plugin repository

Before the Maven Scala Plugin can be loaded, your Maven installation needs to know where to find the plugin and the other Scala dependencies. These items can be found in the Scala Tools repository. Add the following to your project-specific pom.xml. (You can also choose to add this repository to your ~/.m2/settings.xml)

Add this to your pom.xml in the <project> section:

<pluginRepositories>
    <pluginRepository>
        <id>scala</id>
        <name>Scala Tools</name>
        <url>http://scala-tools.org/repo-releases/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>
<repositories>
    <repository>
        <id>scala</id>
        <name>Scala Tools</name>
        <url>http://scala-tools.org/repo-releases/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

Step 3: Add Scala dependency to your pom.xml

Now that all Scala repositories and the Maven Scala plugin is configured, you can add the Scala dependency to your project’s pom.xml. Add the following code to the >dependencies> section of your pom.xml:

<dependency>
    <groupId>org.scala-lang</groupId>
    <artifactId>scala-library</artifactId>
    <version>2.7.2</version>
</dependency>

Step 4: Add some Scala code

The Maven Scala plugin expects your Scala code in /src/main/scala
Create the scala directory.
Put the following code in a file called /src/main/scala/demo.scala:

object HelloWorld {
  def main(args: Array[String]) {
    println("Hello, world!")
  }
}

Ready!

You can now execute mvn package to build your project, mvn test to test your project, and the Scala class will get built. Let me know if you got it working, I’d love to hear your comments.

Scala basics: What is Scala?

Scala

Many people do not know about the programming language Scala, or why they should care. I think you should care, because Scala has some nice secrets from which you can learn a lot. Scala is a great excuse for learning new things. Even if you never use Scala again, it can be a nice addition to any programmer’s toolbox.

No matter what language you are programming in, it is a good habit to look at new developments. New ways of doing stuff keeps your brain alive. You become a better programmer with your existing tools. And maybe you get some new tools in the process.

So, onto the first question, What is Scala?

What is Scala?

Scala is a relatively young programming language. It has the following distinguishing features:

  • Java compatible bytecode
  • Functional programming language

Java compatible bytecode

Scala is a bit special because it runs on the Java virtual machine. This means that Scala code will compile into Java compatible bytecode. For the Java programmers, this means you can integrate Scala directly into your Java projects. This means that it is possible to create a Java interface and implement it in Scala. Or call Scala methods from Java. It also means that you can use a truckload of existing Java libraries with Scala.

This is very neat, as you do not have to convert entire projects to Scala. You can use Scala in the spots where it fits, and use Java as the main language. This makes Scala look a lot more interesting, especially in big Java shops.

Functional programming language

Scala is a functional programming language. To give a tip-of-the-iceberg-idea: every function is an object. Scala gives the possibility to pass values, but also to pass calculations to methods. This is very powerful and opens a lot of doors to all kinds of new programming constructs.

In Scala, you can make the following constructs very easily:

  • Anonymous functions
  • Pass a function as argument to another function
  • Nested functions
  • Function as return value of a function

The Hello World example

As any post about a new programming language, I cannot omit the Hello World example.

So, download Scala, copy/paste the following code and get started!

object HelloWorld {
  def main(args: Array[String]) {
    println("Hello, world!")
  }
}

Further reading

User friendly form validation with Wicket

By default Wicket shows error messages together in a single place in the HTML form. This has some drawbacks to usability, especially if you have long forms with lots of fields. Read further for a tutorial exploring possibilities to improve the location of the error on the page, thereby improving usability.

The default FeedbackPanel shows all errors in one place. When you enter a wrong value in an input field below the fold, the input field is a mile away from the error above the form. This makes it unclear which error message corresponds to which field.

form-usability-scanning.png

The image on the left shows that a lot of page scanning is needed even with moderate sized forms.

With more than a few fields, the user is confused as which error corresponds to which field. It is a big problem when your e-commerce site scares away many potential clients who can’t complete your web forms!

The default form

Before I describe how to create a more user friendly form, first the ‘default’ form. The following is an example of a standard Wicket form. This kind of form is the one you get without doing any ‘special’ magic things. Place your mouse cursor over the image to see the default error messages above the form.

Default Wicket form with FeedbackPanel at the top

This form will be the starting point for our improvements.

The improved form

This is what we are going to make:

Better and improved Wicket form with FeedbackLabels throughout

As you can see, the error messages are directly next to the component that caused the error. This reduces the scanning of the page to link the error message to the right form component.

Step 1: Introducing the FeedbackLabel

The FeedbackLabel is a custom component I’ve written for this tutorial. With this custom label, you can show important feedback messages related to a FormComponent.

This has the advantage that you can place your Feedback messages in any place you want.

Add this to the Java part of the form:

// This shows feedback when the name input is not correct.
FeedbackLabel nameFeedbackLabel = new FeedbackLabel("name.feedback", name);
form.add(nameFeedbackLabel);
 
FeedbackLabel colorFeedbackLabel = new FeedbackLabel("color.feedback", color, customText);
form.add(colorFeedbackLabel);

Add another SPAN tag for every feedback label. You can place this near the relevant form component within the form.

Step 2: Removing the FeedbackPanel completely? No, filtering!

At first, it looks like you can remove the FeedbackPanel completely. But, then you will not be able to use the info() method to display text inside the FeedbackPanel! Therefore we need a way to filter the FeedbackMessages so error messages are not shown. I have written a FeedbackMessageFilter to accomplish that. It filters out the unwanted error messages.

// filteredErrorLevels will not be shown in the FeedbackPanel
int[] filteredErrorLevels = new int[]{FeedbackMessage.ERROR};
feedback.setFilter(new ErrorLevelsFeedbackMessageFilter(filteredErrorLevels));

I have included the ErrorLevelsFeedbackMessageFilter in the project files, so you can reuse this in your existing projects. Remember if you use the error() method, you cannot use this filter! Instead, create your own filter that filters based on components.

Step 3: Adding some style, the ComponentVisualErrorBehavior™

I always love extremely long class names, like the BookmarkablePageRequestTargetUrlCodingStrategy or the SharedResourceRequestTargetUrlCodingStrategy. It makes my day to introduce a class name consisting of four words, the ComponentVisualErrorBehavior™.

This behavior changes the CSS styles for components that are invalid according to the Wicket form validation. You see in the screenshot that the textfield has a red line around it. This is the result of the ComponentVisualErrorBehavior. You can easily change the styles that are applied.

form-usability-tutorial-namefield.png

To add the ComponentVisualErrorBehavior to your component, just add one line in your Java code:

name.add(new ComponentVisualErrorBehavior("onblur", nameFeedbackLabel));

“onblur” stands for the event that triggers this Behavior. The nameFeedbackLabel will also be updated when this is triggered, so that it will show the relevant error (or nothing if the input is valid).

Download

Download the complete example project and start experimenting! (run with mvn jetty:run and connect to http://localhost:8080/demo)

Let me know how and where you use it in the comments!