Friday, December 3, 2010

DB Importer

I often need to use an existing database from Java. I like JPA, so I usually want to generate JPA @Entity classes for the existing tables. There are some tools out there that does this, but they are not exactly easy to use. I tried several, and I could not get them to work in a reasonable time. (I have more important things to do than to fight a complicated tool.)

I also want to configure the import, for instance to generate @Version and @GeneratedValue annotations, to tweak the generated class names, to generate @ManyToMany relations from connection tables, etc.

So, I decided to write a tool myself. (Why spend a day getting a tool to work when you can make your own in a few weeks?)

I have already made one tool like this: HiberObjects Import DB Pro, but I realize that most programmers don't want to use UML. So, I made a similar tool but without UML.

The code generation can be configured with a Groovy script. To keep this simple, I use plain Java syntax in the script.

The tool is an Eclipse plug-in called DB Importer. It will be available in a free edition and a commercial edition. DB Importer is currently in Beta. Please try it out and let me know what you think.

Tuesday, July 20, 2010

Make it or break it

Some times I am working on a project alone doing both design, planning, programming and testing. I have found it useful to separate these different kinds of work as much as possible. That's because programmers are not good testers. Programming and testing requires totally opposite mindsets:
  • Success as a programmer is to get something to work (make it)
  • Success as a tester is to find something that doesn't work (break it)
If I start testing while I'm in programming mode, I will not try very hard to break it, so lots of errors will slip through.

But on the other hand, I can easily get into programming from testing. When I am testing, I am working with an example, and the example helps me to focus my programming. But I try to avoid jumping from programming to testing anyway, because it is hard to get back to effective testing again.

1. Design
I get the best results if I start with designing a few examples of how the functionality will be used. The examples help me to focus and remember all details. I will also use them as my test specification. Then I create automatic tests based on these examples.

2. Implementation
The automatic tests will remind me what I need to do, so it's no problem to take lunch or end the work day now. But often I am so focused that I just continue with implementation until all the automatic tests pass. After this I take lunch or a long break before I start manual testing.

3. Manual testing
I don't write a test specification for myself, I just run through the examples I designed in the beginning. If I find any errors, I don't fix them right away, but write them down in a test log. When I have tested everything, I can get back to programming again. Since I have some automatic tests in place, it's usually easy to add some more tests that captures the errors.

Wednesday, April 21, 2010

Selecting columns or rows but not cells in JTable

Here is a snippet to enable selection of either columns or rows, but not cells, in a JTable:
   public Integer findColumn(JTable table, Point p)
{
JTableHeader tableHeader = table.getTableHeader();
p.translate(-tableHeader.getX(), -tableHeader.getY());
if(p.x >= 0 && p.x < tableHeader.getSize().width &&
p.y >= 0 && p.y < tableHeader.getSize().height)
{
int x = p.x;
for(int i = 0; i < table.getColumnCount(); i++)
{
String columnName = table.getColumnName(i);
TableColumn column = table.getColumn(columnName);
x -= column.getWidth();
if(x < 0)
{
return column.getModelIndex();
}
}
}

return null;
}

public void enableRowSelection(final JTable table)
{
table.getTableHeader().addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
table.getSelectionModel().clearSelection();
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(false);
int column = findColumn(table, e.getPoint());
table.getColumnModel().getSelectionModel().
setSelectionInterval(column, column);
}
});
table.addMouseListener(new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent e)
{
table.setColumnSelectionAllowed(false);
table.setRowSelectionAllowed(true);
table.getColumnModel().getSelectionModel().
clearSelection();
}
});
}

Some adjustment may be needed to enable selection of single/multiple columns or rows.