public abstract class ALcyDomainObjectConverter extends Object
Abstract class which groups methods to create adapter objects for domain objects, and to convert domain objects to new domain objects for the same or another model.
The adapt
method creates an adapter object for a domain object, which is linked
to this domain object. Changes made to either the adapter object or the original domain object
affects the other object. The convert
method on the other hand creates a new domain
object, which has no link with the original domain object. Changes made to the converted domain
object will not alter the original domain object.
An example use of the adapt
method is to change the radius of a circle, which is
contained in a domain object. The domain object can be seen as a black box, and therefore there
is no setRadius
method available, although it contains a circle. The adapt method,
which knows about the structure of the domain object, can return an instance of
ILcdCircle
which function as an adapter object. Calling the setRadius
method on the adapter object results in a change of the radius of the circle, contained in the
domain object.
The convert
method creates a new object for a certain target model. This target
model can be different from the model of the original domain object. It also allows to convert
the shape of the domain model. Changes to the converted object do not alter the original object
since the original object and the resulting converted object are two distinct objects.
Example usages of the convert
method are the conversion from a polyline to a
buffer, a circle to a sphere, ...
Note: currently there are only ALcyDomainObjectConverter
instances available for the
domain objects of drawing models.
Those domain object converters can be obtained through the TLcyDrawingFormat.getDomainObjectConverter()
method:
ILcyLucyEnv lucy = ...;
TLcyDrawingAddOn drawingAddOn = lucy.retrieveAddOnByClass(TLcyDrawingAddOn.class);
ALcyDomainObjectConverter converter = drawingAddOn.getDrawingFormat().getDomainObjectConverter();
ILcdShape
extension, and interact with that shape adapter.
This allows you to make changes to the geometry without actual knowledge of the domain object
implementation details.
//adapt a domain object to an ILcdShape to be able to modify the shape properties
//a write lock is required as the shape will be modified
try(TLcdLock.Lock lock = TLcdLockUtil.writeLock(aModel)){
if (converter.canAdapt(domainObject, aModel, ILcdPolyline.class)){
ILcdPolyline adapter = converter.adapt(domainObject, aModel, ILcdPolyline.class);
//change the polyline
adapter.move2DPoint(0, 10, 10);
aModel.elementChanged(domainObject, ILcdModel.FIRE_LATER);
}
} finally{
aModel.fireCollectedModelChanges();
}
//convert a domain object to an extruded version, suited for the same model
//a write lock is required as the extruded version will be added to the model
try(TLcdLockUtil.Lock lock = TLcdLockUtil.writeLock(aModel)){
if (converter.canConvert(domainObject, aModel, aModel, ILcdExtrudedShape.class)) {
//convert the domain object to an object which can be adapted to an ILcdExtrudedShape
Object convertedObject = converter.convert(domainObject, aModel, aModel, ILcdExtrudedShape.class);
//adapt the object to an ILcdExtrudedShape
ILcdExtrudedShape extrudedShape = converter.adapt(convertedObject, aModel, ILcdExtrudedShape.class);
//use the adapter to retrieve the extruded shape properties
if (extrudedShape.getMinimumZ() > 0){
//do something with the shape
}
//add the extruded version of the domain object to the model
if (aModel.canAddElement(convertedObject)) {
aModel.addElement(convertedObject, ILcdModel.FIRE_LATER);
}
}
} finally {
aModel.fireCollectedModelChanges();
}
//copy a domain object from one model with a geodetic reference to another model with a grid reference
// domainObject is an element of aGeodeticReferenceModel in this example
Object convertedObject = null;
try(TLcdLockUtil.Lock readLock = TLcdLockUtil.readLock(aGeodeticReferenceModel)){
if (converter.canConvert(domainObject, aGeodeticReferenceModel, aGridReferenceModel)){
convertedObject = converter.convert(domainObject, aGeodeticReferenceModel, aGridReferenceModel);
}
}
if (convertedObject != null){
//add the converted object to the target model
try(TLcdLockUtil.Lock lock = TLcdLockUtil.writeLock(aGridReferenceModel)){
if (aGridReferenceModel.canAddElement(convertedObject)){
aGridReferenceModel.addElement(convertedObject, ILcdModel.FIRE_LATER);
}
} finally{
aGridReferenceModel.fireCollectedModelChanges();
}
}
//convert a domain object containing a circle to a domain object containing a sphere
try(TLcdLockUtil.Lock lock = TLcdLockUtil.writeLock(aModel)){
if (converter.canConvert(circleDomainObject, aModel, aModel, TLcdLonLatHeightSphere.class)){
Object sphereDomainObject = converter.convert(circleDomainObject, aModel, aModel, TLcdLonLatHeightSphere.class);
//add the domain object to the model
if (aModel.canAddElement(sphereDomainObject)){
aModel.addElement(sphereDomainObject, ILcdModel.FIRE_LATER);
}
}
} finally{
aModel.fireCollectedModelChanges();
}
Modifier | Constructor and Description |
---|---|
protected |
ALcyDomainObjectConverter()
Creates a new
ALcyDomainObjectConverter . |
Modifier and Type | Method and Description |
---|---|
abstract <T> T |
adapt(Object aDomainObject,
ILcdModel aSourceModel,
Class<T> aDesiredClass)
Creates an adapter object for
aDomainObject contained in
aSourceModel , which will be an instance of aDesiredClass . |
abstract boolean |
canAdapt(Object aDomainObject,
ILcdModel aSourceModel,
Class aDesiredClass)
Indicates whether this converter can create an adapter object for
aDomainObject , which will be an instance of aDesiredClass . |
boolean |
canConvert(Object aDomainObject,
ILcdModel aSourceModel,
ILcdModel aTargetModel)
The default implementation of this method calls the
canConvert method where the
parameter aCanAdaptToClass is set to Object.class . |
abstract boolean |
canConvert(Object aDomainObject,
ILcdModel aSourceModel,
ILcdModel aTargetModel,
Class aCanAdaptToClass)
Indicates whether this converter is capable of converting a domain object
aDomainObject of aSourceModel into a domain object for the model
aTargetModel . |
Object |
convert(Object aDomainObject,
ILcdModel aSourceModel,
ILcdModel aTargetModel)
The default implementation of this method calls the
convert method where the
parameter aCanAdaptToClass is set to Object.class . |
abstract Object |
convert(Object aDomainObject,
ILcdModel aSourceModel,
ILcdModel aTargetModel,
Class aCanAdaptToClass)
Converts a domain object
aDomainObject of aSourceModel into a
domain object for the model aTargetModel . |
protected ALcyDomainObjectConverter()
ALcyDomainObjectConverter
.public abstract boolean canAdapt(Object aDomainObject, ILcdModel aSourceModel, Class aDesiredClass)
Indicates whether this converter can create an adapter object for
aDomainObject
, which will be an instance of aDesiredClass
. The object
aDomainObject
is contained in aSourceModel
.
The adapter object and the domain object are linked, which means changes made to the adapter object will be passed on to the original object and vice versa.
Since this method indicates whether an adapter object for aDomainObject
can be
created, it must return false
when the adapter object adds information to the
aDomainObject
. The adapter object may only contain less information than the
original object. For example a sphere can be adapted to a circle, since this simply removes the
height information. Adapting a circle to a sphere should not be possible, since the adapter
object (the sphere) contains information that is not available in the original object (the
height). For such cases the convert
method should be used instead.
aDomainObject
- the original objectaSourceModel
- the model containing aDomainObject
aDesiredClass
- the class of the adapter objecttrue
when this converter can perform the adapt operationpublic abstract <T> T adapt(Object aDomainObject, ILcdModel aSourceModel, Class<T> aDesiredClass) throws IllegalArgumentException
Creates an adapter object for aDomainObject
contained in
aSourceModel
, which will be an instance of aDesiredClass
.
The adapter object and the domain object are linked, which means changes made to the adapter object will be passed on to the original object and vice versa.
An IllegalArgumentException
must be thrown when the converter cannot handle
the domain object, or could not adapt the domain object to an instance of
aDesiredClass
. It is possible to check in advance if this converter is capable of
creating an adapter object for the domain object by calling the canAdapt
method
first.
aDomainObject
- the original objectaSourceModel
- the model containing aDomainObject
aDesiredClass
- the class of the adapter objectaDomainObject
, which is an instance of
aDesiredClass
IllegalArgumentException
- when the converter could not create an adapter object of
aDesiredClass
for aDomainObject
public abstract boolean canConvert(Object aDomainObject, ILcdModel aSourceModel, ILcdModel aTargetModel, Class aCanAdaptToClass)
Indicates whether this converter is capable of converting a domain object
aDomainObject
of aSourceModel
into a domain object for the model
aTargetModel
. The parameter aCanAdaptToClass
is passed to this method
to check if the creation of a new domain object is possible in such a way that the adapt
method called with that new domain
object will return an object of instance aCanAdaptToClass
.
Notice that the source and target model may be the same. When the
aCanAdaptToClass
is not important, Object.class
should be passed as
parameter or the canConvert(Object, com.luciad.model.ILcdModel,
com.luciad.model.ILcdModel)
method can be used instead.
aDomainObject
- the original domain objectaSourceModel
- the model containing aDomainObject
aTargetModel
- the model for which a new domain object should be created.aCanAdaptToClass
- adapting the resulting new domain object to this class should be
possibletrue
when the conversion can be done by this converterpublic boolean canConvert(Object aDomainObject, ILcdModel aSourceModel, ILcdModel aTargetModel)
The default implementation of this method calls the canConvert
method where the
parameter aCanAdaptToClass
is set to Object.class
. This method should
therefore be used when the resulting domain object does not need to be adapted to a specific
class afterwards.
aDomainObject
- the original domain objectaSourceModel
- the model containing aDomainObject
aTargetModel
- the model for which a new domain object should be created.true
when the conversion can be done by this converterpublic abstract Object convert(Object aDomainObject, ILcdModel aSourceModel, ILcdModel aTargetModel, Class aCanAdaptToClass) throws IllegalArgumentException, TLcdOutOfBoundsException
Converts a domain object aDomainObject
of aSourceModel
into a
domain object for the model aTargetModel
. The parameter
aCanAdaptToClass
will influence the creation of the new domain object in such a
way that the adapt
method called with
that new domain object will return an object of instance aCanAdaptToClass
.
Note that the source and target model may be the same. When the
aCanAdaptToClass
is not important, Object.class
should be passed as
parameter or the convert(Object, com.luciad.model.ILcdModel,
com.luciad.model.ILcdModel)
method can be used instead.
An IllegalArgumentException
must be thrown when the converter cannot handle
the domain object, or could not convert the domain object in such a way that the resulting
domain object is adaptable to an instance of aCanAdaptToClass
afterwards. It is
possible to check in advance if this converter is capable of converting the domain object by
calling the canConvert
method first.
aDomainObject
- the original domain objectaSourceModel
- the model containing aDomainObject
aTargetModel
- the model for which a new domain object should be created.aCanAdaptToClass
- adapting the resulting new domain object to this class should be
possibleaTargetModel
, which can be adapted to an
instance of aCanAdaptToClass
IllegalArgumentException
- when the converter could not handle the domain object, or
could not convert the domain object in such a way that the
resulting domain object is adaptable to an instance of
aCanAdaptToClass
afterwards.TLcdOutOfBoundsException
- when restoring the state for the target model failed because
the coordinates could not be properly converted from the
source ILcdModelReference
to the target
ILcdModelReference
.public Object convert(Object aDomainObject, ILcdModel aSourceModel, ILcdModel aTargetModel) throws IllegalArgumentException, TLcdOutOfBoundsException
The default implementation of this method calls the convert
method where the
parameter aCanAdaptToClass
is set to Object.class
. This method should
therefore be used when the resulting domain object does not need to be adapted to a specific
class afterwards.
aDomainObject
- the original domain objectaSourceModel
- the model containing aDomainObject
aTargetModel
- the model for which a new domain object should be created.aTargetModel
, which can be adapted to an
instance of aCanAdaptToClass
IllegalArgumentException
- when the converter could not handle the domain object, or
could not convert the domain object in such a way that the
resulting domain object is adaptable to an instance of
aCanAdaptToClass
afterwards.TLcdOutOfBoundsException
- when restoring the state for the target model failed because
the coordinates could not be properly converted from the
source ILcdModelReference
to the target
ILcdModelReference
.