de.randgestalten.toolbar
Class AbstractToolbarBuilder<V extends AbstractToolbarBuilder,T extends JComponent>

java.lang.Object
  extended by de.randgestalten.toolbar.AbstractToolbarBuilder<V,T>
All Implemented Interfaces:
SwingConstants
Direct Known Subclasses:
AbstractSegmentedToolbarBuilder

public abstract class AbstractToolbarBuilder<V extends AbstractToolbarBuilder,T extends JComponent>
extends Object
implements SwingConstants

AbstractToolbarBuilder provides the basic abilities to create a custom TBuilder. If you extend this, be careful with the generic signature of the class. If you add methods to your new class, you have to make sure that all methods properly return your new implementation. Thats why the generics on the class are so ugly :)

You have to specify the return type for all builder methods and the type of the component that is created by the builder. See AbstractSegmentedToolbarBuilder for an example.

The builder can use a custom component factory (use withComponentFactory(de.randgestalten.toolbar.AbstractToolbarBuilder.ComponentFactory) to create components (i.e. Buttons from Actions, Seperators and group containers).

You can switch to custom ButtonUI implementations using withUI(Class). The UI is applied if you add Buttons to the toolbar.

See ToolbarBuilder for a simple usage example.

Author:
Thasso Griebel (thasso.griebel@gmail.com)

Nested Class Summary
static interface AbstractToolbarBuilder.ComponentFactory
          The ConponentFactory is used to create group containers, Seperators and Buttons from actions.
protected static class AbstractToolbarBuilder.DefaultComponentFactory
          Default implementation.
 
Field Summary
protected  V builder
          Reference to the builder
protected  AbstractToolbarBuilder.ComponentFactory componentFactory
          The current component factory
protected  Map<String,Object> componentProperties
          Store component properties that are applied to all components
protected  boolean focusPainting
          Should button focus be painted
protected  int orientation
          The toolbar orientation
protected  V parent
          A possible parent builder
protected  T toolbar
          The toolbar component
protected  Class<? extends ButtonUI> uiclass
          UI Class used for buttons
 
Fields inherited from interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
Constructor Summary
  AbstractToolbarBuilder()
          Create a new Builder that create a JToolBar
  AbstractToolbarBuilder(JComponent toolbar)
          Crate a builder that fills the given component.
  AbstractToolbarBuilder(JComponent toolbar, int orientation)
          Create a new builder that fills the given component with given direction.
  AbstractToolbarBuilder(JComponent toolbar, int orientation, Class<? extends ButtonUI> uiclass)
          Create a new builder with given component, orientation and a default button ui class.
  AbstractToolbarBuilder(JToolBar toolbar)
          Crate a builder that fills a given JToolBar
protected AbstractToolbarBuilder(V parent, JComponent container)
          Craete groups using this constructor
 
Method Summary
 V add(AbstractButton[] buttons)
          Add all buttons to a new group.
 V add(Action action)
          Craetes a Button for the given action using the component factory and add the button to the toolbar.
 V add(Action[] actions)
          Create and adds buttons for all actions.
 V add(ButtonGroup group)
          Create a group for all the buttons in the given ButtonGroup.
 V add(JComponent component)
          Adds the given component to the toolbar
protected  void addImpl(JComponent component)
          Internal method that adds component to the toolbar container
 V addSeperator()
          Adds a default separator to the toolbar
 V addSeperator(Dimension dimension)
          Adds a seperator with given size to the toolbar
 V close()
          Close the builder.
 V createGroup()
          Create a new group
protected abstract  V createGroup(JComponent container)
          Create a new buidler group
 V createGroupContainer()
          Create a new group that is based in a seperate container.
 V createGroupContainer(JComponent container)
          Create a group that uses the given container
protected  V getBuilder()
          Returns this builder.
 T getToolbar()
           
protected  void initBuilder()
          Initialize the builder
protected  JComponent installUI(AbstractButton button, Class<? extends ButtonUI> ui)
          Install the button UI for all AbstractButtons except JCheckBox and JRadioButton.
protected  void prepareComponent(JComponent component)
          Called for each compoennt before added.
 V withComponentFactory(AbstractToolbarBuilder.ComponentFactory factory)
          Sets the component factory used to create toolbar components.
 V withFocusPainting(boolean focusPainting)
          Enable/Disable focus painting for buttons.
 V withProperty(String key, Object value)
          Add a property key value pair that is applied to each component added to the toolbar.
 V withUI(Class<? extends ButtonUI> uiclass)
          Switch the button UI class used for buttons added.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

uiclass

protected Class<? extends ButtonUI> uiclass
UI Class used for buttons


toolbar

protected T extends JComponent toolbar
The toolbar component


orientation

protected int orientation
The toolbar orientation


componentFactory

protected AbstractToolbarBuilder.ComponentFactory componentFactory
The current component factory


componentProperties

protected Map<String,Object> componentProperties
Store component properties that are applied to all components


focusPainting

protected boolean focusPainting
Should button focus be painted


builder

protected V extends AbstractToolbarBuilder builder
Reference to the builder


parent

protected V extends AbstractToolbarBuilder parent
A possible parent builder

Constructor Detail

AbstractToolbarBuilder

public AbstractToolbarBuilder()
Create a new Builder that create a JToolBar


AbstractToolbarBuilder

public AbstractToolbarBuilder(JToolBar toolbar)
Crate a builder that fills a given JToolBar

Parameters:
toolbar - the toolbar

AbstractToolbarBuilder

public AbstractToolbarBuilder(JComponent toolbar)
Crate a builder that fills the given component. The default orientation is HORIZONTAL.

Parameters:
toolbar - the toolabr component

AbstractToolbarBuilder

public AbstractToolbarBuilder(JComponent toolbar,
                              int orientation)
Create a new builder that fills the given component with given direction.

Parameters:
toolbar - the component
orientation - the direction (HORIZONTAL or VERTICAL)

AbstractToolbarBuilder

public AbstractToolbarBuilder(JComponent toolbar,
                              int orientation,
                              Class<? extends ButtonUI> uiclass)
Create a new builder with given component, orientation and a default button ui class.

Parameters:
toolbar - the toolbar
orientation - the orientation
uiclass - the ui class (null permited)

AbstractToolbarBuilder

protected AbstractToolbarBuilder(V parent,
                                 JComponent container)
Craete groups using this constructor

Parameters:
parent - the parent builder
container - the container
Method Detail

initBuilder

protected void initBuilder()
Initialize the builder


withProperty

public V withProperty(String key,
                      Object value)
Add a property key value pair that is applied to each component added to the toolbar.

Parameters:
key - propety key
value - property value
Returns:
builder the builder

withUI

public V withUI(Class<? extends ButtonUI> uiclass)
Switch the button UI class used for buttons added. If set to null, no specific ui is set for buttons.

Parameters:
uiclass - the ui class or null
Returns:
builder the builder

withComponentFactory

public V withComponentFactory(AbstractToolbarBuilder.ComponentFactory factory)
Sets the component factory used to create toolbar components. Null is not permitted

Parameters:
factory - the factory (null not permitted)
Returns:
builder the builder

withFocusPainting

public V withFocusPainting(boolean focusPainting)
Enable/Disable focus painting for buttons. Is disabled by default.

Parameters:
focusPainting - enable/disable focus painting
Returns:
builder the builder

add

public V add(JComponent component)
Adds the given component to the toolbar

Parameters:
component - the component
Returns:
builder the builder

addSeperator

public V addSeperator()
Adds a default separator to the toolbar

Returns:
builder the builder

addSeperator

public V addSeperator(Dimension dimension)
Adds a seperator with given size to the toolbar

Parameters:
dimension - the prefered size of the toolbar
Returns:
builder the builder

add

public V add(Action action)
Craetes a Button for the given action using the component factory and add the button to the toolbar.

Parameters:
action - the action
Returns:
builder the builder

add

public V add(ButtonGroup group)
Create a group for all the buttons in the given ButtonGroup.

Parameters:
group - the button group
Returns:
builder the builder

add

public V add(Action[] actions)
Create and adds buttons for all actions.

Parameters:
actions - the actions
Returns:
builder the builder

add

public V add(AbstractButton[] buttons)
Add all buttons to a new group.

Parameters:
buttons - the buttons
Returns:
builder the builder

createGroup

public V createGroup()
Create a new group

Returns:
group the created group

createGroupContainer

public V createGroupContainer()
Create a new group that is based in a seperate container.

Returns:
group the created group

createGroupContainer

public V createGroupContainer(JComponent container)
Create a group that uses the given container

Parameters:
container - the container
Returns:
group the group

getToolbar

public T getToolbar()
Returns:
toolbar the final toolbar component

getBuilder

protected V getBuilder()
Returns this builder. We need this to avoid manual casting to the proper builder type.

Returns:
builder the current builder

addImpl

protected void addImpl(JComponent component)
Internal method that adds component to the toolbar container

Parameters:
component -

createGroup

protected abstract V createGroup(JComponent container)
Create a new buidler group

Parameters:
container - the container
Returns:
group the group

close

public V close()
Close the builder. This is used top close sub builder ad groups

Returns:

prepareComponent

protected void prepareComponent(JComponent component)
Called for each compoennt before added.

Parameters:
component - the componet

installUI

protected JComponent installUI(AbstractButton button,
                               Class<? extends ButtonUI> ui)
Install the button UI for all AbstractButtons except JCheckBox and JRadioButton. This creates an instance of the given ButtonUI using either a default constructor without a parameter or it looks for a constructor that takes the button as a single parameter.

Parameters:
button - the button
ui - the ui class
Returns:
button the button