Core Java Books

Monday 28 March 2011

Providing Mock API's at Runtime

This article is about how I provide a mock api's to my applications at runtime so I can test them without external services being involved.

Many of the application I work on make use of api's to servers, communications engines and so on. I find it useful to be able to test my application with mock API's so I have more control of the tests on it and can test it standalone. Also the mechanism provided allows me to run my application when a service has yet to be delivered but is defined. I could make use of dependency injection if I was to drag JEE api's into my build but I prefer the simple approach. It might not be perfect but it works.

Lets say I have an external interface that my application makes use of:


public interface Service {
    void addListener(final ServiceListener listener);
    void removeListener(final ServiceListener listener);
}


Now in my application I would typically construct a concrete class either directly  or via a factory method which implements the interface contract. Whilst testing I would like to actually provide another version of the implementing class which can be used to test my application with the interface:


public class MockService implements Service {

    private static List<ServiceListener> listeners = new ArrayList<ServiceListener>();

    public void addListener(ServiceListener listener) {
        listeners.add(listener);
    }

    public void removeListener(ServiceListener listener) {
        listeners.remove(listener);
    }

    public void newRequestReceived(final ExternalRequest request) {
        RequestEvent event = new RequestEvent(request);
        for(ServiceListener listener : listeners) {
            listener.notifyRequestEvent(event);
        }
    }

}

I provide the mock class making use of a command line property:


java -Dservice.api.classname=com.webbyit.MockService -jar myapp.jar



Now in the code of my app I make use of the property (maybe within a factory method) and construct the class provided rather than the default class:


private static Service getService() {
  

    synchronized (ServiceLock) {
        if (Service == null) { 

            String serviceClassName = System.getProperty("service.api.classname");

                if (serviceClassName == null) {
                     Service = new RealService();
                else {
                    try {
                        Service = (ServiceClass.forName(serviceClassName).newInstance();
                    catch (InstantiationException e) {
                        throwCallServiceInitialisationException(serviceClassName, e);
                    catch (IllegalAccessException e) {
                        throwCallServiceInitialisationException(serviceClassName, e);
                    catch (ClassNotFoundException e) {
                        throwCallServiceInitialisationException(serviceClassName, e);
                    }
                }
            }
        }
        return Service;
    } 

}

So what I have done is test for an alternative class being specified by a property (via the command line). If set the code attempts to construct the alternative class and use that instead of the default class usually used by the application.

Monday 21 March 2011

The Culture of the Locked Stationary Cupboard

This blog is a little bit off topic, it's a short observation of company culture and politics. Specifically this is about the culture of 'The Locked Stationary Cupboard'.

I should start with a little background information. I'm a software consultant, in short I hire out my services to customers and in general develop software for them. This more than often involves working at the customers business premises. Most customers I've dealt with employee 100+ people.

Now when I offer my services to a customer and work on their site I always use my own pens, pencils, calculator ...... however there are times when the pen runs out, the pencil jumps down a black hole and the calculator takes a walk in to the ether. Nothing strange here, all part and parcel of working for yourself. Things get very strange though when you ask a member of staff where the stationary cupboard is though!

I'd always thought of a stationary cupboard as a place where an employee could obtain that most basic of office tools, the pen and pencil. In a well stocked cupboard marker pens, staples, staplers and even rulers were available. All of these items of little monetary value but essential to any office workers tool set. It was in a companies interest to have a well stocked cupboard.

In recent times, and I have noticed this more in larger companies, the stationary cupboard takes on an status similar to that of the 'Ark of the Covenant'. You know it exists, you know you want to find it and open it but by god its not going to be an easy task.

Several obstacles are put in place to stop anyway unwary soul gaining access to the holly of holies. The main obstacle being the BridgeKeeper, you can only pass the bridge and obtain the magical key to the cupboard if you answer all the questions correctly.


  • What do you want.
  • Why do you want it.
  • How many do you want.
  • Has the project manager signed in blood.

Once the key has been obtained and the cupboard door is opened, you feel a sudden chill, the Eye of Sauron. He/She will makes sure that you will NOT take more than you requested. If you are even tempted to take and extra pen, maybe a marker you will never be able to cross the bridge and obtain the golden key again.



So what is it about the stationary cupboard that endows it with such reverence. It seems totally illogical, why would any company employ people, pay them, let them alter code but make it so damn hard to obtain a pen! In many cases developers are allowed to create and alter code that could potentially cause loss of wealth or life if it doesn't function correctly, but a simple request for the keys to the cupboard and they are treated with mistrust.

The crux of the matter I believe is that some companies just have a culture of mistrust. I suspect this is more so in older companies and the Googles of this world are a lot more open minded. Here in the UK we certainly historically come from a culture of 'us and them' with regards to employee and employer relationship. Industry is littered with companies which have gone to the wall because both sides did not trust each other.

My conclusion to this little rant is that any company that has no lock on the stationary cupboard is a forward thinking organisation that is heading in the right direction as regards employee relationships. On the other hand those that protect the jewels within the cupboard should look closely at there internal structure and ask themselves the question, does a pen really warrant all this hassle and mistrust!

Friday 18 March 2011

WebStart errors and Temporary Internet Files Setting

I've been working on a WebStart application for a few weeks and today it started failing to start. An error was raised about one of the jar files not being signed. I checked the jar and it was signed!

Turned out the error was down to a javaws setting (run 'javaws - viewer' to see the settings).





For some reason the 'Keep Temporary files on my computer' setting was not enabled. I read on various other forums that this had caused similar problems for other people. I enabled the setting, pressed deleted files just incase anything was hanging around and then started my application, it worked :)

So I'm puzzled as to how the setting became disabled as I did not do it. I'm also puzzled why this should cause the problem I was seeing, it seems to have happened to other people to so its not a one off. Some people reported the problem back in 1.4 Java, I'm running the latest 1.6.

Wednesday 16 March 2011

Java Tables: Simple Column to Row Highlighting

In this post I show a simple solution for highlighting table rows based on the value in a particular column on the row.





(Java 1.6 required!)

Firstly I make use of SwingX libraries which provide highlighting functionality. I replace the normal JTable with a JXTable and then add a Highlighter to the table.

The highlighter (code below) is given a column name and two maps for background and foreground colors. The map is keyed on a String with color values. The highlighter takes the value of the specified column on each row and that is used to get the colors required for highlighting



package com.webbyit.swing;

import java.awt.Color;
import java.awt.Component;
import java.util.Map;
import org.jdesktop.swingx.JXLabel;
import org.jdesktop.swingx.decorator.ComponentAdapter;
import org.jdesktop.swingx.decorator.HighlightPredicate;
import org.jdesktop.swingx.painter.Painter;
import static org.jdesktop.swingx.color.ColorUtil.blend;

/**
 *
 @author webbyit
 */
public class ColumnToRowHighlighter extends ColorBlendHighlighter {

    protected Map<String, Color> foregrounds;
    protected Map<String, Color> backgrounds;
    protected String columnName;

    public ColumnToRowHighlighter(String columnName, HighlightPredicate predicate) {
        this(columnName, predicate, null, null);
    }

    public ColumnToRowHighlighter(String columnName, HighlightPredicate predicate, Map<String, Color> backgrounds,
                                  Map<String, Color> foregrounds) {
        super(predicate);
        this.backgrounds = backgrounds;
        this.foregrounds = foregrounds;
        this.columnName = columnName;
    }

    public void setForegrounds(Map<String, Color> foregrounds) {
        this.foregrounds = foregrounds;
    }

    public void setBackgrounds(Map<String, Color> backgrounds) {
        this.backgrounds = backgrounds;
    }

    @Override
    protected void applyBackground(Component renderer, ComponentAdapter adapter) {
        if (backgrounds != null) {
            Color color = null;
            if (adapter.isSelected()) {
                color = getBackground();
            else {
                int columnIndex = adapter.getColumnIndex(columnName);
                String str = adapter.getFilteredStringAt(adapter.row, columnIndex);
                color = backgrounds.get(str);
            }
            if (color != null) {
                if (renderer instanceof JXLabel) {
                    // might have a background painter
                    Painter painter = ((JXLabelrenderer).getBackgroundPainter();
                    renderer.setBackground(blend(renderer.getBackground(), color));
                else {
                    renderer.setBackground(blend(renderer.getBackground(), color));
                }
            }
        }
    }

    @Override
    protected void applyForeground(Component renderer, ComponentAdapter adapter) {
        if (foregrounds != null) {
            Color color = null;
            if (adapter.isSelected()) {
                color = getSelectedForeground();
            else {
                int columnIndex = adapter.getColumnIndex(columnName);
                String str = adapter.getFilteredStringAt(adapter.row, columnIndex);
                color = foregrounds.get(str);
            }
            if (color != null) {
                renderer.setForeground(blend(renderer.getForeground(), color));
            }
        }
    }
}

To make use of the highlighter a JXTable must be used instead of a JTable, to be honest I don't use JTables anymore I always use JXTable. This is how the highlighter in the demo is added:



Map<String, Color> backgrounds = new HashMap<String, Color>();
backgrounds.put("Visa", Color.BLUE);
backgrounds.put("Cash", Color.RED);
backgrounds.put("Cheque", Color.BLACK);
Map<String, Color> foregrounds = new HashMap<String, Color>();
foregrounds.put("Visa", Color.WHITE);
foregrounds.put("Cash", Color.YELLOW);
foregrounds.put("Cheque", Color.ORANGE);

// Use default predicate
ColumnToRowHighlighter highlighter = new ColumnToRowHighlighter("Payment Method", HighlightPredicate.ALWAYS,
      backgrounds, foregrounds);
table.addHighlighter(highlighter);

Notice that the highlighter allows a predicate to be set. This is another feature which provides the capability to filter when a highlight is used or not. In the example for instance I could have provided a predicate which defines the highlights is only used for balances outstanding over 50. It's worth searching around for article elsewhere on SwingX and highlighting as its very flexible and very easy to use.

Enjoy.

Tuesday 15 March 2011

Displaying Application and Java Details in an About JDialog

I've decided that this post will offer a demonstration of a basic about dialog which displays basic information about the application and also about the java platform. Treat it as a starting point for you to extend and tweak.





Download the main frame and dialog source code to see how its done, its very simple. Please note I used Netbeans to create the code.

You could extend the code to display a company logo, provide a copy button on the Java details so they could be posted in an email ...................................

Enjoy :)

Thursday 10 March 2011

JInternalFrame's Parent & Child Modality

This post is about managing JInternalFrame's within a JDesktopPane, specifically the ability to popup child JInternalFrame's from a parent JInternalFrame's. Now why would you want to do this you may ask? Well on several applications I have worked on users open forms to view/enter data, these forms then offer extra buttons to view related data in other forms. In many cases I require that the new form blocks the parent form, in short becomes modal to the parent form (not other forms).

This post offers code and a demo of a simple extension to JInternalFrame which controls the child and parent relationship. It also offers non desktop modal dialogs, but dialogs that are only modal to the parent JInternalFrame.

The functionality the provided specifically is:

  1. Frames can open child frames. The parent frame becomes busied out (lightweight modal).
  2. Clicking on a busied out parent frame brings the top related child frame to the front.
  3. Multiple parent and child hierarchy's can be on the desktop at one time.
  4. A frame can open a dialog, the dialog can be either desktop modal or modal just to the frame hierarchy it was opened from.
  5. A frame modal dialog will be brought to the front if any of its parent frames are clicked on.
Please try out the demo, the list below gives some idea of the functionality.



Play around with the demo, the menu lets you open many frames that are not related. Each frame then has buttons to let you open different children. Notice how you can click on different children and children of children and also busied out parents, you will see that the top most child frame of the associated from clicked is always brought to the front, even if iconized.

I think many people will find this functionality useful in JDesktopPane related applications

Try it out:




You can download the code from here have a look and figure it out.

Basically I have overridden JInternalFrame and added functionality to listen out for child frames opening and closing. This caused glassplanes to be installed or removed as appropriate.  Check out the logic, its easy(ish) to follow. Let me know if you can think of other uses or extensions to this.


In future I will create a post to demonstrate full modality for internal frames ;)

UPDATE: AUG 2016 - Google have retired the Google Drive Archive :( If you wish to obtain the code please email me and I can send it via email or you can obtain the whole archive here.

Please give a recommend below if you liked the article.

Monday 7 March 2011

Nimbus L&F JDesktopPane Background Performance Tweak

This blog is for all the people out there using the Nimbus L&F and who find that it has a performance hit when using JDesktopPane components.

I had performance problems on a lightweight application using JDesktopPane's  and I tracked it down to the amount of processing going on when the background of the pane was repainted. Nimbus paints the background using what looks like vector type drawing routines to paint a fancy background pattern. This is all very impressive but on a client with a low(ish) spec processor this has a major impact whenever the user moved or resized JInternalFrame's.

I changed the background Painter used by the JDesktopPane to simply fill the pane with the Nimbus background color. This improved the performance significantly in my case and also left more processing available for the rest of the application to use.

Below is images of the before and after shots on the simple demo application.

Original Nimbus
Updated Background Painter




For the demo code I have simply overriden the updateUI() method on the JDesktopPane to simply provide a new Painter which does a simple fill using the default color.


package com.webbyit.swing;

import com.sun.java.swing.Painter;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.UIDefaults;
import javax.swing.UIManager;

/**
 * A <code>JDesktopPane</code> that displays a simple fill <code>Color</code> as the
 * background when the nimbus l&f is being used. This saves many process cycles over the normal
 * background painting that makes use of vector drawing.
 *
 @author webbyit
 */
public class SimpleFillDesktopPane extends JDesktopPane {

    @Override
    public void updateUI() {
        if ("Nimbus".equals(UIManager.getLookAndFeel().getName())) {
            UIDefaults map = new UIDefaults();
            Painter<JComponent> painter = new Painter<JComponent>() {

                @Override
                public void paint(Graphics2D g, JComponent c, int w, int h) {
                    // file using normal desktop color
                    g.setColor(UIManager.getDefaults().getColor("desktop"));
                    g.fillRect(00, w, h);
                }
            };
            map.put("DesktopPane[Enabled].backgroundPainter", painter);
            putClientProperty("Nimbus.Overrides", map);
        }
        super.updateUI();
    }
}



And here is a quick demo for you to try it on your box. You should find if you expand the window to be full frame and move the internal frame around it should redraw fairly quick on a low spec box.




(Java 1.6 required!)

Friday 4 March 2011

Using Java2D to Create Icons at Runtime

I needed some marker Icons to display unique points on a map. Each icon needed to have a unique id so I wanted a number or letter to be placed on each icon. Obviously I did not want to create and store hundreds of icons in a directory, the simple solution would be to create them on the fly using Java2D.

I'm posting a basic solution which creates some fairly basic marker icons but it is a good starting point for people to start creating more fancy looking markers/icons/images.

Basically I make use of Java2D to draw into BufferedImage and then use the image to construct an ImageIcon.

The example code can be downloaded and creates the following demo images:


Now obviously this isn't created by anyone with artistic talent but it shows the basics.


Click on the following button to try it out.



(Java 1.6 required!)

I'd be interested to hear about any other solutions and any neat ways of extending this.

Thursday 3 March 2011

Using Google Code to Host WebStart Applications

As you can see from my previous posts I like to provide examples applications and not just some code. Initially I wasn't sure how I could provide WebStart applications via my blog. After some investigation it turned out to be fairly easy, here are some notes about how to go about it.

To start with I obviously needed to create applications that can be delivered as WebStart applications. Personally I use Netbeans for all my development and this make it very easy. You simply select in your projects preferences that it will be packaged as a WebStart application and provide some basic information. When you build a WebStart application a JNLP file will be created and also on e or more Jar files.

Secondly I needed somewhere to host my files. I decided to use project hosting, which is provided by Google Code. You create a project and select the source revision method you wish to use. I picked SVN as its something I know well and is supported by default by Netbeans. Basically this allows you to store you project on the Google Code servers and update them whenever you want.

So now we have a JNLP file, a Jar or Jars and a way of storing these remotely on the Google Code servers.

Now in a JNLP file some of the fields need to specify the location of the JNLP and also the Jars to be used. You need to make sure these are set correctly. In the example below I have highlighted what I needed to change.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jnlp codebase="http://yourprojecthost.googlecode.com/svn/trunk/myprojectname" href="youjnlpfilename.jnlp" spec="1.0+">
    <information>
        <title>TitleofYourApp</title>
        <vendor>YourId</vendor>
        <homepage href=""/>
        <description>YourAppDescription</description>
        <description kind="short">Something useful</description>
    </information>
<update check="always"/>
    <resources>
<j2se version="1.5+"/>
<jar href="YourJar.jar" main="true"/>
    </resources>
    <application-desc main-class="xxx.xxx.xxxx.xxxx.youmainclass">
    </application-desc>
</jnlp>



You should change these fields as appropriate for where your application is hosted, the name of the application and the jars and main class to be used.

One other thing you need to make sure the mime type is set correctly for the JNLP file, otherwise any request to obtain it will just be passed a text file, we want it to kick off the Java VM and start running the application. Netbeans allows you to easily add mime types for files by adding it as a SVN property. This needs to be set so the Google Hosting Server know how to deliver the JNLP file back to any requester. The mime type should be set as:

Property name: svn:mime-type 
Property value: application/x-java-jnlp-file

If you don't use Netbeans then you can set the mime type using SVN:

$ svn propset svn:mime-type application/x-java-jnlp-file my.jnlp

Now we have the files hosted, the mime type is set we now need to add the link into our blog/webpage. Oracle/Sun provide a handy standard bit of script for this :) Just add the following into you page:

<script src="http://java.com/js/deployJava.js">
</script>
<script>
      deployJava.createWebStartLaunchButton("https://locationofyourjnlp.jnlp")
</script>


And in theory that is it :)

Enjoy

Please +1 this post or pass it on if you think it is useful.

Wednesday 2 March 2011

JTextFields and Regex Documents

I had planned on writing a post about auto complete JCheckBoxes but before this I've decided to jot down some notes on a simple regular expression document class I've written for handling text entry on text fields.

As they say there are many ways to skin a cat and this is just a simple way for me to restrict the number of characters entered into a fields and also the characters that can be typed by making use of a regular expression. I know there are other ways of doing this but I think this offers a nice example of what you can start doing with Document classes and how useful they can be. To get really fancy you could also investigate the JFormattedText component and see how you can restrict user input and focus traversal but for now I will comment on JTextField and keep things light.

In the examples I allow a user to enter a 'Caller Number', 10 characters are allowed. The characters can be any mixture of digits, space, + or -. They can also enter a 'Caller Code' and here they are allowed to enter up to 2 characters which can be A to E or 1 to 7.

The regex expressions used are:

"[0-9 \\-\\+]+"

and

"[A-E1-7]"

Try it out:




(Java 1.6 required!)

I achieved this by extending the Document class with a MaxLengthDocument class that restricts the number of characters than can be entered. I then extended the MaxLengthDocument class with RegexDocument class which makes use of the Matcher and Pattern classes in the Regex package.

I simply set the Document on  the two JTextField's, for example:

callerNumberTF.setDocument(new RegexDocument("[0-9 \\-\\+]+", 10));

See the code below.

The MaxLengthDocument:



package com.webbyit.swing;

import java.awt.Toolkit;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 * Simple Document class that restricts input on the associated text field to a
 * specified maximum length.
 *
 @author webbyit
 *
 */
public class MaxLengthDocument extends PlainDocument {

    /**
     * Maximum length of the text
     */
    private final int maxLength;

    /**
     * Default constructor.
     *
     @param maxLength
     *            the maximum number of characters that can be entered in the
     *            field
     */
    public MaxLengthDocument(final int maxLength) {
        super();
        this.maxLength = maxLength;
    }

    @Override
    public void insertString(final int offset, final String str,
                             final AttributeSet attrthrows BadLocationException {
        if (getLength() + str.length() > maxLength) {
            Toolkit.getDefaultToolkit().beep();
            return;
        }
        super.insertString(offset, str, attr);
    }
}

The RegexDocument:



package com.webbyit.swing;

import java.awt.Toolkit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;

/**
 * Class to provide restriction on user input using a regex pattern.
 *
 @author webbyit
 *
 */
public class RegexDocument extends MaxLengthDocument {

    protected final Pattern pattern;

    /**
     * Contructor used to construct a document object which pattern matches
     * strings as typed.
     *
     @param regex
     *            pattern to match on typed strings
     @param maxLength
     *            maximum length of full string
     */
    public RegexDocument(final String regex, final int maxLength) {
        super(maxLength);
        this.pattern = Pattern.compile(regex);
    }

    @Override
    public void insertString(final int offset, final String str,
                             final AttributeSet attrthrows BadLocationException {
        final Matcher matcher = pattern.matcher(str);
        if (matcher.matches()) {
            super.insertString(offset, str, attr);
        else {
            Toolkit.getDefaultToolkit().beep();
        }
    }


Now this should give you enough info to start delving into documents and working out how you want to use them :)

Enjoy