Tor Egil Hoftun Kvæstad

— Joy in creation

InsetsCreator: A Case of Over-Engineering

Published: 2011-11-20 Last edited: 2011-11-20

While I was working on the editor for MinStory, I for some reason skim-read the Gnome Human Interface Guidelines. It had a section on spacing between components in dialogs, which I found interesting, since all the components in my dialogs were mashed together end-to-end.

In most of the dialogs in question, I'm using GridBagLayout, which uses java.awt.Insets to space the components properly. The first thing I did was to create two constants, INNER_SPACING and OUTER_SPACING, which define the space between components that are logically connected, and those that are not, respectively.

However, instead of using the two constants directly in the creation of Insets for each dialog, I came up with InsetsCreator. It is an enum, which includes values such as OUTER_LEFT, that has an OUTER_SPACING on the left, and INNER_BOTTOM, that has an INNER_SPACING on the bottom.

The values of InsetsCreator cannot be used directly, however. Each one only defines the spacing in one direction (I was going to remedy that later), and none of them are of the Insets class that is necessary.

In order to get something that I can actually use, InsetsCreator uses the method createInsets. It takes an arbitrary number of InsetsCreator values, finds the highest numbers for the spacing in each direction (top, left, bottom, right) and creates an Insets object with those spacings.

When used, it looks this:

  Insets top = InsetsCreator.createInsets(
					  InsetsCreator.OUTER_TOP,
					  InsetsCreator.OUTER_LEFT,
					  InsetsCreator.OUTER_RIGHT);

I've written the above in present tense, but the InsetsCreator has since been removed, and can now only be found in an earlier revision. I now use the constants directly, and the above code currently looks like this:

  Insets top = new Insets(
			  GUIConstants.OUTER_SPACING,
			  GUIConstants.OUTER_SPACING,
			  0,
			  GUIConstants.OUTER_SPACING);