com.jgoodies.forms.factories
Class DefaultComponentFactory

java.lang.Object
  extended by com.jgoodies.forms.factories.DefaultComponentFactory
All Implemented Interfaces:
ComponentFactory

public final class DefaultComponentFactory
extends java.lang.Object
implements ComponentFactory

A singleton implementaton of the ComponentFactory interface that creates UI components as required by the PanelBuilder.

The texts used in methods #createLabel(String) and #createTitle(String) can contain an optional mnemonic marker. The mnemonic and mnemonic index are indicated by a single ampersand (&). For example "&Save", or "Save &as". To use the ampersand itself duplicate it, for example "Look&&Feel".

Version:
$Revision: 54721 $
Author:
Karsten Lentzsch

Nested Class Summary
private static class DefaultComponentFactory.TitledSeparatorLayout
          A layout for the title label and separator(s) in titled separators.
private static class DefaultComponentFactory.TitleLabel
          A label that uses the TitleBorder font and color.
 
Field Summary
private static DefaultComponentFactory INSTANCE
          Holds the single instance of this class.
private static char MNEMONIC_MARKER
          The character used to indicate the mnemonic position for labels.
 
Constructor Summary
private DefaultComponentFactory()
           
 
Method Summary
 javax.swing.JLabel createLabel(java.lang.String textWithMnemonic)
          Creates and returns a label with an optional mnemonic.
 javax.swing.JComponent createSeparator(javax.swing.JLabel label)
          Creates and returns a labeled separator.
 javax.swing.JComponent createSeparator(java.lang.String textWithMnemonic)
          Creates and returns a labeled separator with the label in the left-hand side.
 javax.swing.JComponent createSeparator(java.lang.String textWithMnemonic, int alignment)
          Creates and returns a labeled separator.
 javax.swing.JLabel createTitle(java.lang.String textWithMnemonic)
          Creates and returns a title label that uses the foreground color and font of a TitledBorder.
static DefaultComponentFactory getInstance()
          Returns the sole instance of this factory class.
private static void setTextAndMnemonic(javax.swing.JLabel label, java.lang.String textWithMnemonic)
          Sets the text of the given label and optionally a mnemonic.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INSTANCE

private static final DefaultComponentFactory INSTANCE
Holds the single instance of this class.


MNEMONIC_MARKER

private static final char MNEMONIC_MARKER
The character used to indicate the mnemonic position for labels.

See Also:
Constant Field Values
Constructor Detail

DefaultComponentFactory

private DefaultComponentFactory()
Method Detail

getInstance

public static DefaultComponentFactory getInstance()
Returns the sole instance of this factory class.

Returns:
the sole instance of this factory class

createLabel

public javax.swing.JLabel createLabel(java.lang.String textWithMnemonic)
Creates and returns a label with an optional mnemonic.

 createLabel("Name");       // No mnemonic
 createLabel("N&ame");      // Mnemonic is 'a'
 createLabel("Save &as");   // Mnemonic is the second 'a'
 createLabel("Look&&Feel"); // No mnemonic, text is Look&Feel
 

Specified by:
createLabel in interface ComponentFactory
Parameters:
textWithMnemonic - the label's text - may contain an ampersand (&) to mark a mnemonic
Returns:
an label with optional mnemonic

createTitle

public javax.swing.JLabel createTitle(java.lang.String textWithMnemonic)
Creates and returns a title label that uses the foreground color and font of a TitledBorder.

 createTitle("Name");       // No mnemonic
 createTitle("N&ame");      // Mnemonic is 'a'
 createTitle("Save &as");   // Mnemonic is the second 'a'
 createTitle("Look&&Feel"); // No mnemonic, text is Look&Feel
 

Specified by:
createTitle in interface ComponentFactory
Parameters:
textWithMnemonic - the label's text - may contain an ampersand (&) to mark a mnemonic
Returns:
an emphasized title label

createSeparator

public javax.swing.JComponent createSeparator(java.lang.String textWithMnemonic)
Creates and returns a labeled separator with the label in the left-hand side. Useful to separate paragraphs in a panel; often a better choice than a TitledBorder.

 createSeparator("Name");       // No mnemonic
 createSeparator("N&ame");      // Mnemonic is 'a'
 createSeparator("Save &as");   // Mnemonic is the second 'a'
 createSeparator("Look&&Feel"); // No mnemonic, text is Look&Feel
 

Parameters:
textWithMnemonic - the label's text - may contain an ampersand (&) to mark a mnemonic
Returns:
a title label with separator on the side

createSeparator

public javax.swing.JComponent createSeparator(java.lang.String textWithMnemonic,
                                              int alignment)
Creates and returns a labeled separator. Useful to separate paragraphs in a panel, which is often a better choice than a TitledBorder.

 final int LEFT = SwingConstants.LEFT;
 createSeparator("Name",       LEFT); // No mnemonic
 createSeparator("N&ame",      LEFT); // Mnemonic is 'a'
 createSeparator("Save &as",   LEFT); // Mnemonic is the second 'a'
 createSeparator("Look&&Feel", LEFT); // No mnemonic, text is Look&Feel
 

Specified by:
createSeparator in interface ComponentFactory
Parameters:
textWithMnemonic - the label's text - may contain an ampersand (&) to mark a mnemonic
alignment - text alignment, one of SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.RIGHT
Returns:
a separator with title label

createSeparator

public javax.swing.JComponent createSeparator(javax.swing.JLabel label)
Creates and returns a labeled separator. Useful to separate paragraphs in a panel, which is often a better choice than a TitledBorder.

The label's position is determined by the label's horizontal alignment, which must be one of: SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.RIGHT.

TODO: Since this method has been marked public in version 1.0.6, we need to precisely describe the semantic of this method.

TODO: Check if we can relax the constraint for the label alignment and also accept LEADING and TRAILING.

Parameters:
label - the title label component
Returns:
a separator with title label
Throws:
java.lang.NullPointerException - if the label is null
Since:
1.0.6

setTextAndMnemonic

private static void setTextAndMnemonic(javax.swing.JLabel label,
                                       java.lang.String textWithMnemonic)
Sets the text of the given label and optionally a mnemonic. The given text may contain an ampersand (&) to mark a mnemonic and its position. Such a marker indicates that the character that follows the ampersand shall be the mnemonic. If you want to use the ampersand itself duplicate it, for example "Look&&Feel".

Parameters:
label - the label that gets a mnemonic
textWithMnemonic - the text with optional mnemonic marker