package net.xoetrope.optional.data.pojo;
import java.awt.Component;
import net.xoetrope.optional.data.pojo.XPersistentPojoModel;
import net.xoetrope.xui.PageSupport;
import net.xoetrope.xui.XListHolder;
import net.xoetrope.xui.data.XDataBinding;
import net.xoetrope.xui.data.XListBinding;
import net.xoetrope.xui.data.XModel;
/**
* <p>Helps to handle a XList selection while working with POJO model.
* </p>
* <p>Copyright (c) Xoetrope Ltd., 2001-2007<br>
* License: see license.txt
*/
public class XPersistentListHelper
{
private XPersistentPojoModel selectionModel;
private XPersistentPojoModel sourceModel;
private int oldIdx;
private XListHolder listHolder;
private PageSupport page;
/**
* Creates a new instance of XPersistentListHelper
* @param lh the XListHolder object whose selection is to be handled.
* @param ps the owning page of the specified list holder object.
* @param sm the model node intended to store the selected POJO.
*/
public XPersistentListHelper( XListHolder lh, PageSupport ps, XPersistentPojoModel sm )
{
listHolder = lh;
page = ps;
oldIdx = listHolder.getSelectedIndex();
sourceModel = (XPersistentPojoModel)page.getBinding( listHolder ).getSource();
selectionModel = sm;
}
/**
* Creates a new instance of XPersistentListHelper
* @param lh the XListHolder object whose selection is to be handled.
* @param ps the owning page of the specified list holder object.
*/
public XPersistentListHelper( XListHolder lh, PageSupport ps )
{
listHolder = lh;
page = ps;
oldIdx = -1;
sourceModel = (XPersistentPojoModel)page.getBinding( listHolder ).getSource();
selectionModel = null;
}
/**
* Creates a new instance of XPersisentListHelper
* @param lh the XListHolder object whose selection is to be handled
* @param ps the owning page of the specified list
* @param outputPath the binding path to the selection model node which will
* store the selected POJOs.
*/
public XPersistentListHelper( XListHolder lh, PageSupport ps, String outputPath )
{
listHolder = lh;
page = ps;
oldIdx = -1;
sourceModel = (XPersistentPojoModel)page.getBinding( listHolder ).getSource();
selectionModel = (XPersistentPojoModel)page.getProject().
getModel().get( outputPath );
}
/**
* Gets the selected element on the list
* @return selected model node
*/
public XPersistentPojoModel getListSelectedModel()
{
int idx = listHolder.getSelectedIndex();
return ( idx >= 0 ? (XPersistentPojoModel)sourceModel.get( idx ) : null );
}
/**
* Gets the selection model
* @return selected XPersistentPojoModel object
* or null if nothing is selected
*/
public XPersistentPojoModel getSelectionModel()
{
return selectionModel;
}
/**
* Persists the selected POJO
*/
public void persistSelectedPojo()
{
XPersistentPojoModel pm = getListSelectedModel();
// persist the POJO stored in the underlying list
if ( pm != null ) {
pm.persist();
// merge the POJO stored in the selection model node
if ( selectionModel != null )
selectionModel.merge();
}
}
/**
* Merges the selected POJO, changes its state to persistent
*/
public void mergeSelectedPojo()
{
// merge the selected POJO stored in the underlying list
XPersistentPojoModel pm = getListSelectedModel();
if ( pm != null )
pm.merge();
// merge the POJO stored in the selection model node
if ( selectionModel != null )
selectionModel.merge();
}
/**
* Removes the POJO being stored in the selection model from
* the persistent context.
*/
public void deleteSelectedPojo()
{
// removes the POJO being stored in the element of the
// underlying list holder object.
XPersistentPojoModel pm = getListSelectedModel();
if ( pm != null )
pm.delete();
// remove the reference to the POJO from the selection model
if ( selectionModel != null )
selectionModel.set( null );
}
/**
* Removes the reference to the underlying POJO from
* the selection model.
*/
public void clearSelectionModel()
{
if ( selectionModel != null )
selectionModel.set( null );
}
/**
* Copies the currently selected element on the list
* into the given model node.
* This method should be invoked whenever the selection event
* of the given list occurs.
* @return true if the selected index has actualy changed, false
* otherwise
*/
public boolean selectionChanged()
{
int idx = listHolder.getSelectedIndex();
if ( idx == oldIdx )
return false; // the selection hasn't changed
oldIdx = idx;
// put the selected POJO to the selectionModel node
if ( selectionModel != null ) {
XModel m = getListSelectedModel();
selectionModel.set( m != null ? m.get() : null );
}
return true;
}
/**
* Indicates whether the list selection is empty
* @return true if nothing is selected, false otherwise
*/
public boolean selectionEmpty()
{
return ( listHolder.getSelectedIndex() == -1 );
}
/**
* Gets the POJO of the selected model node, if
* the selection model has been set the its underlying POJO
* is returned, otherwise the POJO on the underlying list is returned.
* @return the selected POJO on the list
*/
public Object getSelectedPojo()
{
Object pojo = null;
if ( selectionModel != null ) {
pojo = selectionModel.get();
}
else {
XModel lsm = getListSelectedModel();
if ( lsm != null )
pojo = lsm.get();
}
return pojo;
}
/**
* Removes the selection from the underlying list holder
*/
public void clearSelection()
{
listHolder.select( -1 );
oldIdx = -1;
if ( selectionModel != null )
selectionModel.set( null );
if ( listHolder instanceof Component )
((Component)listHolder).repaint(); //@todo ???
}
/**
* Updates the binding of the underlying XListHolder object
*/
public void updateBinding()
{
XDataBinding binding = page.getBinding( listHolder );
// in case of XList object, mark the binding as dirty
if ( binding instanceof XListBinding )
((XListBinding)binding).setDirty( true );
page.updateBinding( binding );
}
/**
* Marks the source model node as dirty and updates the
* binding of the underlying list holder object.
*/
public void updateListHolder()
{
sourceModel.setDirty( true );
XDataBinding binding = page.getBinding( listHolder );
// in case of XList object, mark the binding as dirty
if ( binding instanceof XListBinding )
((XListBinding)binding).setDirty( true );
page.updateBinding( binding );
}
}