com.jgoodies.binding.value

Class ComponentValueModel

Implemented Interfaces:
Observable, Serializable, ValueModel

public final class ComponentValueModel
extends AbstractValueModel

A ValueModel that provides relevant GUI state in presentation models. It provides bound properties for the frequently used JComponent state enabled/visible and JTextComponent state editable. ComponentValueModels can be used to set these properties at the presentation model layer; any ComponentValueModel property change will be reflected by components bound to that ComponentValueModel.

The ComponentValueModel is similar to the Swing Action class. If you disable an Action, all buttons and menu items bound to that Action will be disabled. If you disable a ComponentValueModel, all components bound to that ComponentValueModel will be disabled. If you set the ComponentValueModel to invisible, the component bound to it will become invisible. If you set a ComponentValueModel to non-editable, the JTextComponents bound to it will become non-editable.

Since version 1.1, PresentationModels can vend ComponentValueModels using #getComponentModel(String) and #getBufferedComponentModel(String). Multiple calls to these factory methods return the same ComponentValueModel.

The BasicComponentFactory and the Bindings class check if the ValueModel provided to create/bind a Swing component is a ComponentValueModel. If so, the ComponentValueModel properties will be synchronized with the associated Swing component properties.

It is recommended to use ComponentValueModels only for those models that are bound to view components that require GUI state changes.

Example Code:

 final class AlbumView {
  
  ...
     
     private void initComponents() {
         // No state modifications required for the name field.
         nameField = BasicComponentFactory.createTextField(
             presentationModel.getModel(Album.PROPERTYNAME_NAME));
         ...
         // Enablement shall change for the composer field
         composerField = BasicComponentFactory.createTextField(
             presentationModel.getComponentModel(Album.PROPERTYNAME_COMPOSER));
         ...
     }
     
  ...
  
 }
 
 
 public final class AlbumPresentationModel extends PresentationModel {
 
  ...
  
     private void updateComposerEnablement(boolean enabled) {
         getComponentModel(Album.PROPERTYNAME_COMPOSER).setEnabled(enabled);
     }
     
  ...
   
 }
 

As of the Binding version 1.1.0 the ComponentValueModel feature is implemented for text components, radio buttons, and check boxes. JLists, JTables, JComboBoxes, and JColorChoosers bound using the Bindings class will ignore ComponentValueModel state. See also Issue 86.

TODO: Add an automatic binding for lists, tables, combos.

Version:
$Revision: 1.8 $
Author:
Karsten Lentzsch
Since:
1.1
See Also:
PresentationModel.getComponentModel(String), PresentationModel.getBufferedComponentModel(String), BasicComponentFactory, Bindings

Field Summary

static String
PROPERTYNAME_EDITABLE
The name of the property used to synchronize this model with the editable property of JTextComponents.
static String
PROPERTYNAME_ENABLED
The name of the property used to synchronize this model with the enabled property of JComponents.
static String
PROPERTYNAME_VISIBLE
The name of the property used to synchronize this model with the visible property of JComponents.

Fields inherited from class com.jgoodies.binding.value.AbstractValueModel

PROPERTYNAME_VALUE

Constructor Summary

ComponentValueModel(ValueModel subject)
Constructs a ComponentValueModel for the given ValueModel.

Method Summary

Object
getValue()
Returns this model's current subject value.
boolean
isEditable()
Returns if this model represents the editable or non-editable text component state.
boolean
isEnabled()
Returns if this model represents an enabled or disabled component state.
boolean
isVisible()
Returns if this model represents the visible or invisible component state.
void
setEditable(boolean b)
Sets this model state to editable or non-editable, which in turn will make all text components bound to this model editable or non-editable.
void
setEnabled(boolean b)
Enables or disabled this model, which in turn will enable or disable all Swing components bound to this model.
void
setValue(Object newValue)
Sets the given value as new subject value.
void
setVisible(boolean b)
Sets this model state to visible or invisible, which in turn will make all Swing components bound to this model visible or invisible.

Methods inherited from class com.jgoodies.binding.value.AbstractValueModel

addValueChangeListener, booleanValue, doubleValue, fireValueChange, fireValueChange, fireValueChange, fireValueChange, fireValueChange, fireValueChange, fireValueChange, floatValue, getString, intValue, longValue, removeValueChangeListener, setValue, setValue, setValue, setValue, setValue, toString

Methods inherited from class com.jgoodies.binding.beans.Model

addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, addVetoableChangeListener, equals, fireMultiplePropertiesChanged, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, getPropertyChangeListeners, getPropertyChangeListeners, getVetoableChangeListeners, getVetoableChangeListeners, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, removeVetoableChangeListener

Field Details

PROPERTYNAME_EDITABLE

public static final String PROPERTYNAME_EDITABLE
The name of the property used to synchronize this model with the editable property of JTextComponents.

PROPERTYNAME_ENABLED

public static final String PROPERTYNAME_ENABLED
The name of the property used to synchronize this model with the enabled property of JComponents.

PROPERTYNAME_VISIBLE

public static final String PROPERTYNAME_VISIBLE
The name of the property used to synchronize this model with the visible property of JComponents.

Constructor Details

ComponentValueModel

public ComponentValueModel(ValueModel subject)
Constructs a ComponentValueModel for the given ValueModel.
Parameters:
subject - the underlying (or wrapped) ValueModel

Method Details

getValue

public Object getValue()
Returns this model's current subject value.
Specified by:
getValue in interface ValueModel
Returns:
this model's current subject value.

isEditable

public boolean isEditable()
Returns if this model represents the editable or non-editable text component state.
Returns:
true for editable, false for non-editable

isEnabled

public boolean isEnabled()
Returns if this model represents an enabled or disabled component state.
Returns:
true for enabled, false for disabled

isVisible

public boolean isVisible()
Returns if this model represents the visible or invisible component state.
Returns:
true for visible, false for invisible

setEditable

public void setEditable(boolean b)
Sets this model state to editable or non-editable, which in turn will make all text components bound to this model editable or non-editable.
Parameters:
b - true for editable, false for non-editable

setEnabled

public void setEnabled(boolean b)
Enables or disabled this model, which in turn will enable or disable all Swing components bound to this model.
Parameters:
b - true to enable, false to disable.

setValue

public void setValue(Object newValue)
Sets the given value as new subject value.
Specified by:
setValue in interface ValueModel
Parameters:
newValue - the value to set

setVisible

public void setVisible(boolean b)
Sets this model state to visible or invisible, which in turn will make all Swing components bound to this model visible or invisible.
Parameters:
b - true for visible, false for invisible

Copyright © 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.