My Launchpad‎ > ‎

Dats Util library

Dats Util Library v1.2.0 (LGPL)

Requirements : You need at least Java Runtime Environment (JRE) 5 installed.

This library is really just a small utility library I'm building on for mostly my own use. It's a pretty odd mixture of a couple of fairly useful things :

Library Features:

An EventManager that makes events work C# style.
    Get rid of those annoying event loops! Treat an event as a single method call

A MappedTableModel
    To quickly display a JTable of objects in a more simple to manage Object Oriented way.

A simple JTable implementation
    Simply enhances a normal JTable slightly.

Swing Date and Time components
    More elegant date selection than a couple of drop-down boxes, and easier to use to boot!

ObjectString
    Turn any object into a easily readable string for logs or debugging

FileCopier
    System level file copying, because they should have had a file.copy(dest)!

TimeSpan
    Making a length of time a first class concept - geared for database storage.

Simplistic Money, Percent and Stamp (time-elapsed) classes
    Have a standard way of representing these tricky concepts.


Below, I will quickly describe the way to use most of the library, I encourage you to download the source and have a browse for any other
EventManager Usage :

For this example, we'll use the default Listener interface that comes with datsutil, but you can use any interface you like.

Let's start off with a use case. My StockController needs to fire an event when stock gets low.. so :
	public class StockController {
1: Listener stockLowEvent = EventManager.newEvent(Listener.class);

public void addStockLowListener(Listener listener) {
2: EventManager.addListener(stockLowEvent, listener);
}

public void removeStockLowListener(MouseEvent listener) {
3: EventManager.addListener(stockLowEvent, listener);
}

public void checkStock(Stock stock) {
if (stock.belowStockThreshold()) {
4: stockLowEvent.update();
}
}
}
And that demonstrates the EventManager! In line 1: we setup the event, calling a static factory method from EventManager.

Lines 2: and 3: we use the EventManager to add and remove listeners from the event.

And the magic is at line 4: Fire the event on all listeners.. look ma, no loops! :)
MappedTableModel Usage :

I find creating TableModels very annoying, I want things to map directly to my data. So, say I have a Stock class :
	public class Stock {
public String getName() { return name; }
public int getQty() { return qty; }
public boolean isProtected() { return Security.isProtected(this); }
}
Note: The important parts are the getters and setters (for editing capabilities), not the properties, as I'm demonstrating with isProtected()

Let's put a List<Stock> into a new JTable in my JFrame :
	public class MyFrame extends JFrame {
private MappedTableModel<Stock> model;
...

public void construct() {
...

1: model = new MappedTableModel<Stock>(Stock.class);
2: model.bindColumn("Stock Type", "name", true);
3: model.bindColumn("Quantity Left", "qty", true);
4: model.bindColumn("Protected?", "protected", true);

publicationTable = new JTable(model);
frame.add(new JScrollPane(publicationTable));

model.setItems(getStockList());
...
}

public List<Stock> getStockList() { ... }
...
}
Line 1: sets up the table model and connects it to the Stock class.

Lines 2, 3 and 4: bind each column with a title (param 1), property (JavaBean style, param 2) and whether it's readonly or not. If you pass false for the third parameter, you need to have a setter for the field you want to make editable.

And there you have it. No fuss POJO to JTable mapping! :)

The rest of the components are small and basic enough that I don't have to give an overview, best is just to play around and find out how they work, everything should be self explanatory. Of course, if you have any hassles, feel free to give me a shout!
ċ
datsutil-1.2.0-bin.zip
(177k)
Deon Moolman,
Mar 1, 2010, 8:55 PM
ċ
datsutil-1.2.0-src.zip
(159k)
Deon Moolman,
Mar 1, 2010, 8:55 PM
Comments