Igor Vaynberg wrote:
yeah, thats pretty interesting, what does that panel look like? are you
using the latest 1.3 trunk?
Yes i have just tried latest trunk - the same.
Here goes the full code:
public class ThermometersPanel extends Panel {
@SpringBean
protected UnitConfigService unitConfigService;
private class ThermometerSensorPanel extends Panel {
private class ThermometerSensorActionHandler implements
ActionHandler {
public ThermometerSensorActionHandler() {
}
@Override
public void delete( Action action ) {
TemperatureSensor sensor = (TemperatureSensor)
getModelObject();
sensor.removeAction( action );
// TODO czemu to nie jest kaskada?
unitConfigService.makePersistent( sensor );
unitConfigService.makeTransient( action );
}
@Override
public void save( Action action ) {
TemperatureSensor sensor = (TemperatureSensor)
getModelObject();
if ( action.getId() == null ) {
sensor.addAction( action );
unitConfigService.makePersistent(
sensor );
} else
unitConfigService.makePersistent(
action );
}
}
public ThermometerSensorPanel( String id, IModel model, IModel
captionModel ) {
super( id, model );
add( new ActionsPanel( "actions", new PropertyModel( getModel(),
"actions" ), captionModel,
new
ThermometerSensorActionHandler() ) );
}
@Override
public boolean isVisible() {
this throws:
return ( (TemperatureSensor) getModelObject()
).isLevelEnabled();
}
}
public ThermometersPanel( String id, IModel model ) {
super( id, model );
add( new Link( "add", getModel() ) {
@Override
public void onClick() {
setResponsePage( new ThermometerPage( new
ThermometerModel( (Long) null ), getPage() ) );
}
@Override
public boolean isVisible() {
return (
unitConfigService.getAvailableThermometerSlots( (UnitConfig) getModelObject()
).size() > 0 );
}
} );
add( new RefreshingView( "list", new PropertyModel( getModel(),
"thermometers" ) ) {
@SuppressWarnings("unchecked")
@Override
protected Iterator getItemModels() {
System.out.println( "get item models called" );
Map<Integer, Thermometer> thermometers =
(Map<Integer, Thermometer>) getModelObject();
return new ModelIteratorAdapter(
thermometers.values().iterator() ) {
@Override
protected IModel model( Object obj ) {
return new ThermometerModel(
(Thermometer) obj );
}
};
}
@Override
protected void populateItem( final Item item ) {
item.add( new Link( "edit", item.getModel() ) {
@Override
public void onClick() {
setResponsePage( new
ThermometerPage( getModel(), getPage() ) );
}
}.add( new Label( "name", new PropertyModel(
item.getModel(), "name" ) ) ) );
item.add( new Link( "delete", item.getModel() )
{
@Override
public void onClick() {
UnitConfig unitConfig =
unitConfigService.getCurrentUnitConfig( UserUtils.getCurrentUnit() );
unitConfigService.removeThermometer( unitConfig,
(Thermometer) getModelObject()
);
}
} );
item.add( new ThermometerSensorPanel(
"lowLevelSensorPanel", new PropertyModel( item.getModel(),
"lowLevelSensor" ), new
ResourceModel( "lowLevelActions" ) ) );
item.add( new ThermometerSensorPanel(
"highLevelSensorPanel", new PropertyModel( item.getModel(),
"highLevelSensor" ), new
ResourceModel( "highLevelActions" ) ) );
}
} );
}
}
public class ActionsPanel extends Panel {
public ActionsPanel( String id, IModel model, IModel captionModel,
final ActionHandler handler ) {
super( id, model );
add( new Label( "caption", captionModel ) );
add( new Link( "add", getModel() ) {
@Override
public void onClick() {
setResponsePage( new ActionPage( new
ActionModel( (Long) null ), handler, getPage() ) );
}
@SuppressWarnings("unchecked")
@Override
public boolean isVisible() {
Set<Action> actions = (Set<Action>)
getModelObject();
return actions.size() <
ActionListSerializer.MAX_PER_PAGE_ACTION_COUNT;
}
} );
add( new RefreshingView( "list", getModel() ) {
@SuppressWarnings("unchecked")
@Override
protected Iterator getItemModels() {
Set<Action> actions = (Set<Action>)
getModelObject();
return new ModelIteratorAdapter(
actions.iterator() ) {
@Override
protected IModel model( Object object )
{
return new ActionModel(
(Action) object );
}
};
}
@Override
protected void populateItem( Item item ) {
item.add( new Label( "name", new PropertyModel(
item.getModel(), "actionDefinition.name" ) ) );
item.add( new Link( "delete", item.getModel() )
{
@Override
public void onClick() {
handler.delete( (Action)
getModelObject() );
}
} );
}
} );
}
}
The model looks like this:
unitconfig -> thermometers
each thermometer is being wrapped with loadable detachable thermometer
model which holds its id
thermometer -> lowlevelsensor
lowlevelsensor -> actions
each action wrapped with loadable detachable action model
everything is based on property models apart from building refreshing
list elements which do not need parent model.
--
Leszek Gawron
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]