čtvrtek, 25. dubna 2013

Guava ClassPath Suite

Sometimes, one needs to run just a subset of JUnit test. The very good library ClassPathSuite (cpsuite) address this problem and is able to select test based on a class name, or another class properties (annotation, super class, etc.)

In our project, we have needed a little bit more complicated filter conditions than it offers. Afters studying ClassPathSuite and  Google Guava internals, I just figure out, some kind of more flexible ClassPathSuite can be created using Guava Predicate and ClassPath classes. So I have coded just one core file, GuavaClassPathSuite which does what we need - scans the class path for JUnit tests and filter them by user defined Predicates. I have pushed the solution to GitHub as gcpsuite, Guava ClassPath Suite, project. (Not very distinct name, but points to the Guava ClassPath). Either use it as a library, or simply copy&paste the GuavaClassPathSuite.java, write your predicates and enjoy!

čtvrtek, 13. září 2012

Java Swing: MigLayout, JScrollPane and JTextField Showing Horizontal Scrollbar

MigLayout is a great layout manager for Java Swing. However, it needs some time to discover it's nooks and crannies. One common problem is with components inside JScrollPane.

We place all components inside the JPanel, which has a JScrollPane as parent. And we use MigLayout mostly with fill and grow parameters. Such layout ensures that a user can access all components by scrolling when she has a small screen.

But I have a faces a problem with JTextField containing a very long text. I usually use MigLayuot

growx, width 50::

constraints for text fields. However, when a text files contains a very long String, then it forces JScrollPane to to enlarge it's width and show the horizontal scrollbar. The user usually need the text field to be as long as possible, but not to overflow the screen width, if possible. After a while, I have found a proper and simple solution, just set the preferred size as minimum:

growx, width 50:50:

Note: I have described the problem using component constraints, the solution is similar with row constraints

fill, grow 50:50:

pátek, 20. ledna 2012

Eclipse: Manual and Automatic Code Formatting

I like automatic code formatting in the Eclipse IDE. For Java see the settings in Window -> Preferences -> Java -> Code Style -> Formatter. I Just hit CTRL+SHIFT+F time from time and let the IDE do the boring job for me.

Occasionally, I want the different result then the Eclipse Formatter produce. Especially when a method has many parameters. Sometimes I like to have all of them on one line, sometimes I like to break them into multiple times. Of course, the Eclipse automatic formatter cannot be configured to meet wishes.

The Eclipse formatter has a nice option On/Off Tags:
  • @formatter:off
  • @formatter:on
But it's tedious and ugly to write these tags into the code. (Even Code templates does not simply usage of On/Off Tags very much). Much more simpler solution is to append the Java comment // ant the and of line, which you want to break. For example, I have configured the Eclipse formatter to have all method parameters on a single line. But when I write
this.someMethod(param1, //
    param2, //
    param3, //
    param4, //
    param5);
Then the CTRL+SHIFT+F reformatting let this piece of the code as is.

čtvrtek, 6. října 2011

Simple Javascript SCORM 2004 API

Just a very very simple implementation of SCORM 2004 API for LMS (RTS) developers. It implements just basic SCORM 2004 features. Hope, someone else may find it useful, see http://sites.google.com/site/xmedeko/code/misc/simple-javascript-scorm-2004-api.

úterý, 31. května 2011

Java zsync

The very first implementation of zsync in Java: http://sourceforge.net/projects/jazsync/. It has no release yet, check out the source code from SVN.

středa, 11. května 2011

ZK-DL Released!

ZK-DL, an extension to the ZKoss framework (ZK 5), has been released under LGPL just a few weeks ago!. ZK_DL provides especially:

  1. Integration with Spring.
  2. Flexible listbox, combobox and lovbox (a kind of combobox) components with automatic sorting and searching build upon the Hibernate Criteria API.
  3. And much more, see http://zk.datalite.cz/zk-dl.

The documentation is at http://zk.datalite.cz/zk-dl and the source code can be found at http://code.google.com/p/zk-dl/.

I have used DL-ZK with EJB3 on JBoss 5.1 successfully. So I had not taken the advantage of ZK-DL Spring integration (IMHO better than original ZK Spring integration). Anyway the ZK-DL listobox, combobox and lovbox components has greatly simplified our development and provided a rich yet simple interface to our users.

Many thanks to Jiří Bubník and Karel Čemus for their valuable help and cooperation. I wish DL-ZK long life and have many users!

čtvrtek, 10. března 2011

JPA Queries - A Few Methods to Simplify the Life with JPA/JPQL

The JPA Query.getSingleResult() method is usable only when the JPQL has exactly one result. Yeah, you can catch NoResultException or NonUniqueResultException exceptions, but the try ... catch block make the code less readable. So I have coded three static method to overcome this JPA deficiency:
public class Queries {
    /**
     * @param query
     * @return {@code true}, the query has one or more results, otherwise returns {@code false}.
     */
    public static boolean hasAnyResult(Query query) {
        query.setMaxResults(1);
        final List<?> list = query.getResultList();
        return list.size() > 0;
    }

    /**
     * @param query
     * @return The only one result or {@code null}, when the query has no result.
     * @throws NonUniqueResultException
     *             when the query has more than one result.
     */
    public static Object singleResultOrNull(Query query) {
        query.setMaxResults(2);
        final List<?> list = query.getResultList();
        final int size = list.size();
        if (size <= 0) {
            return null;
        }
        if (size > 1) {
            throw new NonUniqueResultException("result returns more than one element");
        }

        // return first element from the list
        return list.get(0);
    }

    /**
     * @param query
     * @return The first result or {@code null}, when the query has no result.
     */
    public static Object firstResultOrNull(Query query) {
        query.setMaxResults(1);
        final List<?> list = query.getResultList();
        final int size = list.size();
        if (size <= 0) {
            return null;
        }
        if (size > 1) { // should not happened, Hibernate bug?
            Logger.getLogger(Queries.class).error("firstResultOrNull more rows returned. setMaxResults(1) does not work?");
        }

        return list.get(0);
    }

    /**
     * Constructor. No instances.
     */
    private Queries() {
    }
}
The hasAnyResult is the boolean query method - you may test if something exists in database. The best is to use it with some JPQL query returning only id of an entity, e.g.:
Queries.hasAnyResult(em.createQuery("SELECT id FROM Person WHERE ..."));
I think the usage of the singleResultOrNull is obvious. Use it, when you are sure the result returns one or no result. The exception NonUniqueResultException should warn you that something is wrong, wither the query or the data.

The firstResultOrNull returns just the first result of the query. Typically, use it with queries with "ORDER BY" statement, like "get the Person with lowest/highest salary."