Tuesday, December 9, 2008

CJUG Meeting on Tuesday, December 16th 2008

Topic: The Seductions of Scala Scala is a new, statically-typed language for the JVM. Its advantages over Java include a succinct syntax, an improved object model and full support for functional programming (FP). In this code-heavy talk, I’ll show why Scala is so seductive, how it can improve your productivity, and why it might replace Java as the de facto language for the JVM.

Who: Dean Wampler

When: December 16th, 2008 6:00 PM - 8:00 PM (Networking from 6:00 PM - 6:30 PM) (Presentation from 6:30 PM - 8:00 PM)

Where: Lewis Towers Ballroom, Beane Hall Loyola University of Chicago 820 N Michigan Ave (enter on Pearson 111 E. Pearson) Chicago, IL 60611

Cost: FREE to current CJUG members and first time guests Join Now!

For full details and to RSVP go to http://www.cjug.org/Wiki.jsp?page=2008.12.16.downtown

Hope to see you there.

Monday, November 10, 2008

Layout Prototyping Part 1: XML

GUI Layout is hard, but is it harder then it should be in Java? Can we do something to better the situation? My next few blogs will be about prototyping different approaches in an attempt to find something better. For my first article I'm going to attempt to apply XML to the GUI layout. The goal is to approximate some of the features of MXML from Flex.

In a previous article I compared layout logic in Flex/Flex Builder to Java. I found only one layout manager that had no Java equivalent. I wrote CoordinateLayout to mimic absolute layout from Flex. Here is a very basic example of it's usage.


public class CoordinateLayoutDemo extends JPanel {

public CoordinateLayoutDemo() {
setLayout(new CoordinateLayout());

add(new JLabel("Last Name:"), "x=10, y=10");
add(new JTextField(), "x=87, y=8, width=223");

add(new JLabel("First Name:"), "x=318, y=10");
add(new JTextField(), "x=395, y=10, width=207");

add(new JLabel("Phone:"), "x=10, y=36");
add(new JTextField(), "x=87, y=34, width=223");

add(new JLabel("Email:"), "x=318, y=36");
add(new JTextField(), "x=395, y=34, width=207");

add(new JLabel("Address:"), "x=10, y=62");
add(new JTextField(), "x=87, y=60, width=515");

add(new JLabel("City:"), "x=10, y=88");
add(new JTextField(), "x=87, y=86, width=223");

add(new JLabel("State"), "x=318, y=88");
add(new JTextField(), "x=395, y=86, width=207");
}
}



This lets one layout a screen pixel by pixel. It also has an "anchor mode" that allows one to determine how a component will resize by specifying the location of an anchor on one the four sides.


public class CoordinateLayoutDemo2 extends JPanel {
/**
*
*/
private static final long serialVersionUID = 5744676397576694341L;

public CoordinateLayoutDemo2() {
setLayout(new CoordinateLayout());

add(new JScrollPane(new JTextArea()),
"left=35, top=40, right=10, bottom=10");
add(new JSlider(SwingConstants.VERTICAL), "bottom=10, left=10, top=40");
add(new JComboBox(), "right=287, left=35, top=10");
add(new JButton("Button"), "right=10, top=10");
}
}


This code results in the following screen.



The only other thing I need is XML bindings and I have functionality similar to absolute layout.

XMLBeans to the Rescue.

Without going into too much detail, XMLbeans takes an XSD (XML Schema Definition) and creates java classes that can parse XML that conforms to that schema.

Here is a tiny schema that represents CoordinateLayout.


<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="CoordinateLayout">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="item">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="x" type="xs:int" />
<xs:attribute name="y" type="xs:int" />
<xs:attribute name="width" type="xs:string" />
<xs:attribute name="height" type="xs:string" />
<xs:attribute name="top" type="xs:int" />
<xs:attribute name="left" type="xs:int" />
<xs:attribute name="bottom" type="xs:int" />
<xs:attribute name="right" type="xs:int" />
<xs:attribute name="vcenter" type="xs:int" />
<xs:attribute name="hcenter" type="xs:int" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

If I run this XSD through the XMLBeans schema compiler (scomp) I get a jar with classes that parse this schema. Now with a little drudgery to move the values from the schema generated objects to my layout I am able to represent my UI as an XML file.

Here is an XML file that conforms the XSD and arranges the components the same way as the first example.

<?xml version="1.0" encoding="UTF-8"?>
<CoordinateLayout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <item name="lastNameLabel" x="10" y="10" />
    <item name="lastName" x="87" y="8" width="223" />

    <item name="firstNameLabel" x="318" y="10" />
    <item name="firstName" x="395" y="10" width="207" />

    <item name="phoneLabel" x="10" y="36" />
    <item name="phone" x="87" y="34" width="223" />

    <item name="emailLabel" x="318" y="36" />
    <item name="email" x="395" y="34" width="207" />

    <item name="addressLabel" x="10" y="62" />
    <item name="address" x="87" y="60" width="515"/>

    <item name="cityLabel" x="10" y="88" />
    <item name="city" x="87" y="86" width="223"/>

    <item name="stateLabel" x="318" y="88"/>
    <item name="state" x="395" y="86" width="207"/>
</CoordinateLayout>


After all that I took a long hard look at my XML systems and asked myself two questions.

1. Is this roughly equivalent to absolute layout from Flex?
2. Does this make layout any easier?

The answer to number one is, yes. I consider it close enough to the the layout functionality of absolute layout.

Number two, well no. XML doesn't help. No matter which way you slice it specifying pixel locations is not the simplest way of laying out components.

Not wanting to abandon this XML idea I figured I'd apply it to other layout managers.

The first one I tried was TableLayout. I applied the same steps.

1. Create an XML schema that represents the TableLayout finctionality.
2. Use XMLBeans to generate a jar file with the parsing code.
3. Glue the XMLBeans generated object to TableLayout.

This allowed me to create the following XML file.


<?xml version="1.0" encoding="utf-8"?>
<tablelayout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    vgap="1" hgap="1">

    <columnsizes>
        <size>10</size><size>FILL</size><size>10</size><size>PREFERRED</size>
        <size>10</size><size>PREFERRED</size><size>10</size>
    </columnsizes>

    <rowsizes>
        <size>10</size><size>PREFERRED</size>
        <size>5</size><size>PREFERRED</size><size>10</size>
        <size>PREFERRED</size><size>5</size>
        <size>PREFERRED</size><size>10</size>
        <size>PREFERRED</size><size>5</size>
        <size>PREFERRED</size><size>10</size>
        <size>PREFERRED</size><size>10</size>
    </rowsizes>

    <item name="nameLabel" col1="1" row1="1" col2="5" row2="1" />
    <item name="name" col1="1" row1="3" col2="5" row2="3" />

    <item name="addLabel" col1="1" row1="5" col2="5" row2="5" />
    <item name="address" col1="1" row1="7" col2="5" row2="7" />

    <item name="cityLabel" col1="1" row1="9" />
    <item name="city" col1="1" row1="11" />

    <item name="stateLabel" col1="3" row1="9" />
    <item name="state" col1="3" row1="11" />

    <item name="zipLabel" col1="5" row1="9" />
    <item name="zip" col1="5" row1="11" />

    <item name="button" col1="1" row1="13" col2="5" row2="13" />
</tablelayout>


Which results in this:



I did the same thing with GridBagLayout.

<?xml version="1.0" encoding="utf-8"?>
<gridbag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <defaults anchor="EAST" top="5" left="5" right="5" />
    <item name="lastNameLabel" />
    <item name="lastNameTF" fill="HORIZONTAL" weightx="1" />
    <item name="firstNameLabel" />
    <item name="firstNameTF" fill="HORIZONTAL" weightx="1" gridwidth="REMAINDER" />
    <item name="phone" />
    <item name="phoneTF" fill="HORIZONTAL" weightx="1" />
    <item name="email"/>
    <item name="emailTF" fill="HORIZONTAL" weightx="1" gridwidth="REMAINDER" />
    <item name="address" />
    <item name="addressTF" fill="HORIZONTAL" weightx="1" gridwidth="REMAINDER" />
    <item name="city" bottom="5"/>
    <item name="cityTF" fill="HORIZONTAL" weightx="1" bottom="5"/>
    <item name="state" bottom="5"/>
    <item name="stateTF" fill="HORIZONTAL" weightx="1" bottom="5"/>
</gridbag>


Which actually looks like less code then writing this in Java. That's not hard though. Gridbag is rather cumbersome to use.

I decided to go through this process once more. This time with MigLayout. In the end I was disappointed. Not because it didn't work. Not because of any failure in MigLayout. Just because it was too simple. Now I could have built an XSD around the type-safe constraint objects available in MigLayout. Unfortunately that was going to make for some very verbose XML. Instead I just packaged up the string version of the constraints in some XML and called it a day.

<?xml version="1.0" encoding="utf-8"?>
<miglayout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    layout="ins 20" column="[para]0[][100lp, fill][60lp][95lp, fill]" row="">
   
    <item name="man" params="skip" />
    <item name="manSep" params="span 3, growx, wrap" />
   
    <item name="compLbl" params="skip" />
    <item name="company" params="span, growx" />
    <item name="contactLabel" params="skip" />
    <item name="contact" params="span, growx" />
    <item name="orderLabel" params="skip" />
    <item name="order" params="wrap para" />

    <item name="ins" params="skip" />
    <item name="insSep" params="span 3, growx, wrap" />
   
    <item name="namelabel" params="skip" />
    <item name="name" params="span, growx" />
    <item name="refNoLabel" params="skip" />
    <item name="refNo" params="wrap" />
    <item name="statLabel" params="skip" />
    <item name="stat" params="wrap para" />
   
    <item name="ship" params="skip" />
    <item name="shipSep" params="span 4, growx, wrap" />
   
    <item name="yardLabel" params="skip" />
    <item name="yard" params="span, growx" />
    <item name="regLabel" params="skip" />
    <item name="reg" params=""/>
    <item name="hullLabel" params="right" />
    <item name="hull" params="wrap" />
    <item name="typeLabel" params="skip" />
    <item name="type" params=""/>
</miglayout>


About five minuets after I finished I realized I created an XSD for what could be more simply represented as a properties file.

Here is a properties file version of the same layout.
layout = ins 20 
column = [para]0[][100lp, fill][60lp][95lp, fill]
row =
man = skip
manSep = span 3, growx, wrap

compLbl = skip
company = span, growx
contactLabel = skip
contact = span, growx
orderLabel = skip
order = wrap para

ins = skip
insSep = span 3, growx, wrap

namelabel = skip
name = span, growx
refNoLabel = skip
refNo = wrap
statLabel = skip
stat = wrap para

ship = skip
shipSep = span 4, growx, wrap

yardLabel = skip
yard = span, growx
regLabel = skip
reg =
hullLabel = right
hull = wrap
typeLabel = skip
type=


So what did I accomplish? Well I can say I successfully built a system that decouples layout parameters from components. Certainly decoupling can be a good design pattern. Gridbag showed that there was some benefit to the XML way of doing things. Yet MigLayout showed that in the end it's all about the capability of the layout manager itself and not the markup. I don't think just applying XML is going to make GUI layout easier all by itself.

I have a web start app demonstrating my prototype.



The source is also available.

xmllayoutdemo-src.zip

Wednesday, July 2, 2008

Lately I've been spending my spare time contributing code samples to the The Java Tutorial Community Portal. My hope is that one or more of these examples will be interesting enough to be included in the official tutorial. My latest addition is an example on how to override tool tip generation for a particular component. This example is a tabbed pane that shows a preview of a tab as a tooltip.

In this picture the mouse was hovering over the "images" tab.

There are techniques for installing custom universal tool tip providers. In this case I only wanted to change the behaviour for one particular component. To accomplish this I overrode the createToolTip() method of JTabbedPane with the following:

/**
* overide createToolTip() to return a JThumbnailToolTip
*/
@Override
public JToolTip createToolTip() {
Point loc = getMousePosition();
int index = indexAtLocation(loc.x, loc.y);

if (index != -1) {
Component comp = getComponentAt(index);

BufferedImage image = ImageFactory.toImage(comp);

JToolTip tip = new JImageToolTip(image, 0.5);

return tip;
} else {
return super.createToolTip();
}
}

This method returns an instance of JImageToolTip which displays an image of the hidden component instead of the tooltip text. The image of the component was generated by the toImage() method of my ImageFactory class.

/**
* Generates a buffered Image of the specified component.
*
* @param componet
* @return image 'snapshot' of the component and it's state at the time this
* method was called.
*/
public static BufferedImage toImage(Component componet) {
BufferedImage image = new BufferedImage(componet.getWidth(), componet
.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics graphics = image.getGraphics();
graphics.setColor(componet.getBackground());
graphics.fillRect(0, 0, componet.getWidth(), componet.getHeight());
componet.paint(graphics);
graphics.dispose();

return image;
}

The JImageToolTipThis also class was used to create a label that displays a thumbnail of an image and provides the full size image as a tooltip.

I've made the full source code available. Comments on this or any other code sample are welcomed and encouraged.

Source Code: ThumbnailTooltipDemo-src.zip

Collator - Faster Comparison for identical strings.

Tuesday, June 3, 2008

Java Tutorial Community - Jtree and Borders Code samples

I've been working away on building "code samples" for the Java Tutorial Community Portal. Each one is an example of a piece of code that at one time or another I've found useful. I'm not sure which ones will be interesting or even worthy of a full on tutorial. I'm just posting them and looking for feedback. I have four code samples today.

1. SwitchRootDemo - Demonstrates how to change the root of a default tree model.

2. NodeRolloverDemo - Demonstrates how to highlight a node in a tree when the mouse is over it.

3. FlexibleSearchTreeDemo - Demonstrates how to expand the searching functionality of JTree. Also has RegEx support.

4. CustomBorderDemo - Demonstrates how to use Java2D to create a custom border. 

I have a few other examples in the works for trees. I hope to post that in a few days.



Monday, May 19, 2008

Contributing to The Java Tutorials Community Project

I have been working on contributing to the Java Tutorials Community Project. I've put together some code samples as potential tutorial subjects.
  1. JTable - ColumnsDemo
  2. JTable - ColorEntireRowDemo

  3. JTable - MultiLineRows

  4. JTable - FrozenColumns

  5. JTree - DefaultTreeModelDemo

  6. JTree - CustomEditorDemo

The tutorials project has forums where you can comment on these proposed topics, propose new topics and to request revisions to any of the current Java tutorials. We need your help to create the kind of content that can help reduce the Java learning curve and help create better software.

Thursday, March 20, 2008

The secret life of a DefaultTreeModel

This is a draft of a small tutorial I have been attempting to submit to Sun for inclusion in the official Java tutorial. Unfortunately they are very busy lately and have not had the time to review my work. My intent is to bring to light some of the subtleties of the DefaulTreeModel API that are not covered in the Java tutorial on JTree.

This tutorial will revolve around a demo application designed to show the difference between the right and wrong way to interact with DefaulTreeModel. Right clicking on any node in the tree will bring up a menu with a range of options.



Adding and Removing nodes in a DefaultTreeModel.

A DefaulTreeModel collects DefaultMutableTreeNodes together into a tree structure. At first glance the child modification methods like add() and remove() from DefaultMutableTreeNode look as if they overlap with the insertNodeInto() and removeNodeFromParent() from DefaulTreeModel. When the methods from DefaultTreeModel are used events are fired that update any JTree this model may be associated with. Simply modifying the DefaultMutableTreeNode does not. This can be demonstrated by the following chunk of code.

Example of the wrong way to remove a node.
TreePath currentSelection = tree.getSelectionPath();
if (currentSelection != null) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode)
    currentSelection.getLastPathComponent();
    node.removeFromParent();
}


Calls to methods like add() or remove() from DefaultMutableTreeNode need to be followed by a call to the DefaultTreeModel to notify the JTree of model changes. In our example a call to nodeStructureChanged() on the parent of the node being removed would have allowed the tree to be updated correctly. For adding or removing a single node from a tree the simplest solution is to call the insertNodeInto and removeNodeFromParent methods on DefaultTreeModel.

Example of the correct way to remove a node.
TreePath currentSelection = tree.getSelectionPath();
if (currentSelection != null) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode)
        currentSelection.getLastPathComponent();
    DefaultTreeModel model = ((DefaultTreeModel) tree.getModel());
    model.removeNodeFromParent(node);
}


There may be situations where multiple additions or removals happen in large blocks. In these cases the model may not need to be notified of a change until the bulk operation is completed. This is exemplified by the following code which adds multiple children to a node.

...
/**
 *
 * Adds 2 children, "even" and "odd". Even contains all the even numbers from zero to 49.
 * Odd contains all the odd numbers from zero to 49.
 */
TreePath currentSelection = tree.getSelectionPath();
if (currentSelection != null) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode)
        currentSelection.getLastPathComponent();
    DefaultTreeModel model = ((DefaultTreeModel) tree.getModel());

    DefaultMutableTreeNode odd = new DefaultMutableTreeNode("odd");
    node.add(odd);
    DefaultMutableTreeNode even = new DefaultMutableTreeNode("even");
    node.add(even);
    for (int i = 0; i < 50; i++) {
        if (i % 2 == 0) {
            even.add(new DefaultMutableTreeNode(i));
        } else {
            odd.add(new DefaultMutableTreeNode(i));
        }
    }
    // The above changes may not seem to take effect until nodeStructureChanged is called
    model.nodeStructureChanged(node);
}
...


This example creates 52 new nodes. If they were inserted with insertNodeInto() then 52 TreeNodesInserted events would have been fired. This way only one fireTreeStructureChanged event is fired after all the nodes are modified. Depending upon the particular needs of your application this may save some event processing.

Modifying user objects in a DefaultMutableTreeNodes.

Just as a DefaultTreeModel needs to be notified when children are added or removed for a node, so too does it need to be notified when the user object returned by getUserObject() is modified. Failure to do so could lead to rendering issues in the JTree. This is an example of what a such a situation might look like.



To avoid this make sure to call nodeChanged() on the model after any change that might effect the rendering of that node. Here is an example of code that will update correctly.

...
TreePath currentSelection = tree.getSelectionPath();
if (currentSelection != null) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode)
    currentSelection.getLastPathComponent();

    node.setUserObject("THIS IS A VERY LOOOOOOOOOOOOOOOOOOOOONG STRING");

    DefaultTreeModel model = ((DefaultTreeModel) tree.getModel());
    model.nodeChanged(node);
}
...


When updated correctly the tree will display the entire string.



Conclusion
As a general rule any time the data associated with your DefaulTreeModel changes it is important to update the model. Proper use of the DefaulTreeModel will limit the number of times you must explicitly do this.

I've made the source code for the example available for download.

As I said this is a draft of a tutorial. I'm looking for feedback on what can be improved and added while staying within the scope of the DefaulTreeModel/DefaultMutableTreeNode.

Monday, March 10, 2008

Try and Try Again

While working on the code in my last post it occurred to me that reusable exception handling was decent idea. It might even be nice to create a version of try that takes a parameter that handed specific types of exceptions.

Example:
try {
FileReader fr = new FileReader("Readme.txt");
fr.read();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
A enhanced version might look like this.
try(new LogAndContinueExceptionHandler()){
FileReader fr = new FileReader("Readme.txt");
fr.read();
}
LogAndContinueExceptionHandler looks like this.
private static interface ExceptionHandler {
public void uncaughtException(Runnable source, Throwable e);
}
public class LogAndContinueExceptionHandler implements ExceptionHandler{
@Override
public void uncaughtException(Runnable source, final Throwable e) {
Logger.getLogger("ErrorLogger").log(Level.SEVERE, e.getMessage(), e);
}
}
If all you are going to do is log the exception, then maybe a default implementation in Logger makes sense.
try(Logger.logALL("MyLogName")){
FileReader fr = new FileReader("Readme.txt");
fr.read();
}
And for all of you who think checked exception are the bane of all existence there is this option.
public class ToRuntime implements ExceptionHandler{
@Override
public void uncaughtException(Runnable source, final Throwable e) {
throw new RuntimeException(e);
}
}
This rethrows any exception it gets a hold of as a runtime exception. This would drive me nuts, but some people really do hate checked exceptions.

It would be nice to be able to supply different logic for different types of exceptions, just like you do in a try/catch block. Also if one could handle some logic in the reusable section and override that by implementing a catch block you could get more flexibility.

Example:

try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
}

try(new ReflectionHandeler()){
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
}

public class ReflectionHandeler implements ExceptionHandler{
public void uncaughtException(Runnable source, final ClassNotFoundException e) {
// do something important
}

public void uncaughtException(Runnable source, final InstantiationException e) {
// do something important
}

public void uncaughtException(Runnable source, final IllegalAccessException e) {
// do something important
}
}

ReflectionHandeler might be useful someplace else... Ok this wasn't the best example, but you get the idea. Some get caught in the handler and some get caught in the catch.

Where a pattern for handling an exception or types of exceptions is repeatable building a reusable part can help unclutter and your code and make it more maintainable. Now if the task of handling a particular type of exception changes code need only be changed in one place, not scattered throughout the code in try blocks.

It's an idea. I may try to hack the javac compiler to support something like this... not that I'm sure even where to begin.

Sunday, March 9, 2008

More First Class Methods Code

I'm still experimenting with the prototype implementation of the FCM closures proposal. Here are a few utility methods that immediately came to mind.

withLock -- This one I've seen an example of online a few times. It automatically release the lock when the block of code finishes executing.
public static void withLock(Lock lock, Runnable task) {
lock.lock();
try {
task.run();
} catch (Exception e) {
throw e;
} finally {
lock.unlock();
}
}
Example Usage:
final ReentrantLock lock = new ReentrantLock();
withLock(lock, #{
// call some important locked code
});
Now I'm not too fond of unchecked Exceptions, but rethrowing is still better then swallowing every exception that escapes the Runnable. Here is my second attempt.
private static interface ExceptionHandler {
public void uncaughtException(Runnable source, Throwable e);
}

public static void withLock(Lock lock, Runnable task, ExceptionHandler eHandel) {
lock.lock();
try {
task.run();
} catch (Exception e) {
eHandel.uncaughtException(task, e);
} finally {
lock.unlock();
}
}
Example Usage:
withLock(lock, #{
// do some code that needs to be locked.
// throw an exception to test the event handler.
throw new RuntimeException("year is even error");
}, #(Runnable source, Throwable e){
// inline implemented event handler.
e.printStackTrace();
}
);
Once side effect of this is now one might be able to write reusable exception handling code. There have been plenty of times where a method I'm executing throws an exception, but there is no real way for me to recover. All I can do is log the failure and continue on. I end up writing the same try catch block over and over. It would be nice to write something like this once and reuse it.
public class LogAndContinueExceptionHandler implements ExceptionHandler{
public void uncaughtException(Runnable source, final Throwable e) {
SwingUtilities.invokeLater(#{
Logger.getLogger("ErrorLogger").log(Level.SEVERE, e.getMessage(), e);
});
}
}
Example Usage:
withLock(lock, #{
// do some code that needs to be locked.
// throw an exception to test the event handler.
throw new RuntimeException("year is even error");

}, new LogAndContinueExceptionHandler());
I used this idea when exploring another area that might benefit from closures. The EDT thread scheduling methods.

invokeAndWait
public static void invokeAndWait(Runnable r, ExceptionHandler eHandel){
try {
SwingUtilities.invokeAndWait(r);
catch (InterruptedException e) {
eHandel.uncaughtException(e);
} catch (InvocationTargetException e) {
eHandel.uncaughtException(r, e.getCause());
}
}

public class AlertAndContinueExceptionHandler implements ExceptionHandler{
public void uncaughtException(Runnable source, final Throwable e) {
Runnable showMessageBox = #{
JOptionPane.showMessageDialog(null, e.getMessage());
};
if(SwingUtilities.isEventDispatchThread()){
showMessageBox.run();
}else{
SwingUtilities.invokeLater(showMessageBox);
}
}
}
Example Usage:
invokeAndWait(#{
JOptionPane.showMessageDialog(null, "Hello World");
}, new AlertAndContinueExceptionHandler());
simpleSwingWorker -- The simpleSwingWorker method is an attempt to apply closures to the simple case of, "do something off the EDT then do something ON the EDT".
public static void simpleWorker(final Runnable offEDT, final Runnable onEDT){
Thread bGround = new Thread(#{
offEDT.run();
SwingUtilities.invokeLater(onEDT);
});
bGround.start();
}
Example Usage:
simpleWorker(#{
System.out.println("sleep");
Thread.currentThread().sleep(1000);
System.out.println("start");
}, #{
JOptionPane.showMessageDialog(null, "Hello World");
});
I also wrote a blocking version based on invokeAndWait.
public static void simpleBlockingWorker(final Runnable offEDT, final Runnable onEDT){
Thread bGround = new Thread(#{
offEDT.run();
SwingUtilities.invokeAndWait(onEDT);
});
bGround.start();
}

Hay wait a second, invokeAndWait throws exceptions right? And yet the code as it stands compiles without issue. I think I may have found a bug in the FCM prototype.

Some may look at these examples and point out that this type of functionality may really fall into the realm of "control structures". There is some debate in the Java community as to weather the common man should be allowed to write their own control structures. Some prefer to leave this responsibility with the language designers. It is my opinion that once you make this type of code easy, then you already have allowed the common man to make their own control structures. In that case you might as well legitimize the pattern and provide as much support as it possible.

I look forward to the day when there is a Java Control Abstraction (JCA) prototype to play around with.

Wednesday, March 5, 2008

Experimenting with FCM

Stephen Colebourne has a prototype implementation of the FCM closures proposal. I took some time to download and play with the new syntax and functionality.

I tried to focus on some of the functionality FCM provides that other closure proposals do not. Namely method and field literals. I think FCM can help create flexible models for swing components. In the past I've done this using plain reflection but FCM makes things easier.

This is a screenshot of the demo application.

It displays three lists and one table all of which are backed by the same collection of SuperHero beans.

I found that I could apply FCM in one of the first few lines of my demo.
SwingUtilities.invokeLater(#startApp());
This calls my startApp() method on the event dispatch thread. I have to admit that while I'm not a fan of in-lining this was preferable to the traditional method of building a Runnable and passing that to invokeLater.

Here is a very basic ListModel that can expose any public field of an object.
   /**
* Wraps a list of elements and exposes one field to the list model
* interface
*
* @param <E>
*/
private class FCMFieldListModel<T> extends AbstractListModel {

private List<T> backingList;
private Field targetField;

public FCMFieldListModel(List<T> data, Field target) {
backingList = data;
targetField = target;
}

@Override
public Object getElementAt(int index) {
try {
return targetField.get(backingList.get(index));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}

@Override
public int getSize() {
return backingList.size();
}

}
In my case I'm building a list of superheroes and wish to expose the last name field.

FCMFieldListModel<SuperHero> lastListModel = new FCMFieldListModel<SuperHero>(heroes, SuperHero#lastName);

The last parameter is the new field literal syntax from FCM. It allows me a type safe way of specifying which field I want to expose.

To create more list models that expose other fields I just need to create another FCMFieldListModel and expose a different field.

FCMFieldListModel<SuperHero> listModel = new FCMFieldListModel<SuperHero>(heroes, SuperHero#firstName);

Now most of the time a bean will not expose a field. Instead getter/setter methods are used to expose this information. FCM has method literals as well as field literals. The code for a FCMMethodListModel is very much like FCMFieldListModel but it takes a Method reference instead of a field refrence.

/**
* Wraps a list of elements and exposes one method to the list model
* interface
*
* @param <E>
*/
private class FCMMethodListModel<E> extends AbstractListModel {

private List<E> backingList;
private Method targetMethod ;

public FCMMethodListModel(List<E> data, Method target) {
backingList = data;
targetMethod = target;
}

@Override
public Object getElementAt(int index) {
try {
Object item = backingList.get(index);
return targetMethod.invoke(item);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}

@Override
public int getSize() {
return backingList.size();
}

}
Basically this lets you tell the list model which method of your bean to call to represent your bean in the list.

Finally I used this same technique to build a table model.
FCMTableModel<SuperHero> superTableModel = new FCMTableModel<SuperHero>(heroes,
SuperHero#getFirstName(), SuperHero#getLastName(),
SuperHero#getHeroName(), SuperHero#getFirstAppearance());

The constructor takes a list of SuperHeroes and a variable list of methods to use as columns. This code is all it takes to populate the table seen in the center of the screenshot.

The first appearance column of the super hero table is a date without a day. I decided to use an anonymous inner method to implement a cellrendrer to format the date.
TableColumn dateColumn = superHeroTable.getColumn(SuperHero#getFirstAppearance().getName());

// in-line cell renderer for the date column of the superhero table
dateColumn.setCellRenderer(#(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
if(isSelected){
dateRenderer.setBackground(UIManager.getColor("Table.selectionBackground"));
}else{
dateRenderer.setBackground(UIManager.getColor("Table.background"));
}
dateRenderer.setText(sdfDateColumn.format((Date) value));
return dateRenderer;
});
Overall I have to say I like FCM. Even in it's incomplete state I feel it's still a step forward. The method and field literals were very convenient and useful. I can see myself creating generalized adapter classes around this functionality. As I said before I'm not a fan of in-lining code. I tend not to in-line inner classes either. But the anonymous inner method syntax seemed straight forward enough. Also I much prefer using the # syntax to pass method to invokeLater.

My examples involved a bean and it's associated methods. Yet there is no way currently to specify that a method used as a parameter come from any specific class. It certainly would be an error to pass a FCMMethodListModel System.out#println(Object). I think this is one of the items on the list of things to do for FCM but I'm not certain.

I've made the source code for this demo available on google docs. Just remember that you need the prototype FCM implementation to run it.

Finally I'd just like to thank
Stephen for all his hard work in creating this prototype. It's been fun to experiment with it.

Note:
This is demo level code. It was written to play around with FCM and a such the models are not as robust as they could be. Their job is to adapt an unchanging list of beans using FCMs method literals. I will probably revisit FCMTableModel at some point to show how it could be enhanced. As it stands now they have no support for events. Also I'm not sure a cell renderer was a good choice for an anonymous inner method example. If you read the javadoc for DefaultTableCellRenderer it talks about overriding some of the JLabel methods for performance reasons.

Monday, January 14, 2008

Named Paramaters and Layout Managers

I had made a hasty comment on a java.net post that named parameters made a lot of scene for layout managers. Of course it started to bother me because while I felt I was right, I really had only ever used one language that had named parameters. That language had a lot of weird things in it and so I had to do some experimentation to see if I might actually be right. To that end I tried to implement something that looked syntactically like named parameters for GridBagLayout.

If Java supported a variable list of named parameters for layouts it might look like this:

add(new JLabel("test"), gridx:1, gridy:1, weight:1, fill:BOTH);

Now there is a couple ways to actually write this today.

Example : 1

GridBagConstraints constraints = new GridBagConstraints();
constraints.
gridx = 1;

constraints.
gridy = 1;

constraints.fill = GridBagConstraints.BOTH;
constraints.
weightx = 1;

add(new JLabel("test"), constraints);

This isn't so bad, but it's a little verbose, it really adds up when you have oh say a dozen or more controls in a container The next example is something I've see a certain commercial IDE create. This is just maddening to try to read.

Example 2:

add(new JLabel("test"), new GridBagConstraints(1,1,1,1,1,0,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));

To see if anything would actually be saved by named parameters I built GridBagFactory. It and it's related interfaces and helpers make the following syntax possible.

... create buttons in JPanel ...

public JavaTutorialDemo() {

GridBagFactory gbf = new GridBagFactory(this);
gbf.addDefaultConstraints(FILL.
HORIZONTAL);
gbf.add(
button1);
gbf.add(
button2);
gbf.add(
button3, GRID.WIDTH.REMAINDER);
gbf.add(
button4, PAD.y(40), GRID.WIDTH.REMAINDER);
gbf.add(
button5, INSETS.top(10), GRID.xy(1,2), GRID.WIDTH.REMAINDER, WEIGHT.y(1), ANCHOR.PAGE_END );

}

You can compare this to the original code in the Java tutorial . The relevant GridBag code weighs in at about 35 lines of code. GridBagFactory can do it in 6. Now code density is meaningless if it's not immediately clear what the line of code is doing. There is some noise with the declaration of the GridBagFactory object, but with a little inheritance and overriding I created GBFPanel. A JPanel integrated with a GridBagFactory.

... create buttons in JPanel ...
public
JavaTutorialDemo() {

addDefaultConstraints(FILL.HORIZONTAL);

add(button1);
add(
button2);
add(
button3, GRID.WIDTH.REMAINDER);
add(
button4, PAD.y(40), GRID.WIDTH.REMAINDER);
add(
button5, INSETS.top(10), GRID.xy(1,2), GRID.WIDTH.REMAINDER, WEIGHT.y(1), ANCHOR.PAGE_END );

}

Thats darn close to the named parameter syntax from the beginning. If we rewrite it in that style it becomes just a tad clearer.

... create buttons in JPanel ...
public
JavaTutorialDemo() {

addDefaultConstraints(FILL.HORIZONTAL);

add(button1);
add(
button2);
add(
button3, gridwidth:REMAINDER);
add(
button4, pady:40, gridwidth:REMAINDER);
add(
button5, top:10, gridx:1, gridy:2, gridwidth:REMAINDER, weighty:1, anchor:PAGE_END );

}

I tried to recreate a number of layout examples from other very powerful layouts, like MigLayout and PageLayout. In my test cases I came in really close in code density to those other layout managers. My point really is not about any particular layout manager but about named parameters. In this case it seems to increase readability and code density. So I feel better about my hasty statement, I think all layout managers would benefit from named parameters.

I'll post the full code for GridBagFactory just as soon as I find a place to host it.