Before suggesting an implementation of the service Car, the needed UNO interfaces for controlling the speed of the car have to be specified. As mentioned in the section above, UNO interfaces must be derived from a mandatory base interface com.sun.star.uno.XInterface. Like every interface, the interface XAccelerationControl provides no implementation at all. As stated, interfaces are purely declarative. For example, extending an implementation A with an interface means extending the type of A. The interface allows you to create one or more methods in a class that have no definitions. You provide part of the interface without providing a corresponding implementation, which is created by inheritors like the component Car. It can be put into practice as follows:
public interface XAccelerationControl extends com.sun.star.uno.XInterface
{
public void speedUp( /*IN*/double doubleKilometer );
public void slowDown( /*IN*/double doubleKilometer );
public static final com.sun.star.lib.uno.typeinfo.TypeInfo UNOTYPEINFO[] = { new com.sun.star.lib.uno.typeinfo.MethodTypeInfo( "speedUp", 0, 0 ), new com.sun.star.lib.uno.typeinfo.MethodTypeInfo( "slowDown", 1, 0 ) }; }
The interface includes the public abstract methods speedUp() to increase car
and slowDown() to decrease the velocity of the car. Further on, the constant
array UNOTYPEINFO enables the interface description of the programming language
Java to be as expressive as the UNO IDL and should not be described in detail
in this tutorial.
The interface XDrivingDirection can be described as follows:
public interface XDrivingDirection extends com.sun.star.uno.XInterface
{
public void turnLeft( /*IN*/double doubleAngle );
public void turnRight( /*IN*/double doubleAngle );
public static final com.sun.star.lib.uno.typeinfo.TypeInfo UNOTYPEINFO[] = { new com.sun.star.lib.uno.typeinfo.MethodTypeInfo( "turnLeft", 0, 0 ), new com.sun.star.lib.uno.typeinfo.MethodTypeInfo( "turnRight", 1, 0 ) }; }
Based on the interfaces XAccelerationControl and XDrivingDirection a possible implementation of the Car example could be realized. The implementation could be composed of two classes, the framework class Car and the inner class Car$_Car. The classes will be nested in order to integrate the parts of the component and for clarity. A simplified framework for the classes is specified in the following. The complete, commented source code is available from the following address:
http://api.openoffice.org/unbranded-source/browse/~checkout~/api/odk/examples/examples.html
public class Car {
static public class _Car implements XAccelerationControl, XDrivingDirection,
XPropertySet {
static private final String __serviceName = "org.OOo.Car";
public Short Seats = new Short( ( short ) 4 );
public Long Color = new Long( 0 );
public void slowDown(double doubleKilometer) {}
public void speedUp(double doubleKilometer) {}
public void turnRight(double doubleAngle) {}
public void turnLeft(double doubleAngle) {}
public void setPropertyValue( String stringPropertyName, Object obj)
throws UnknownPropertyException, PropertyVetoException,
IllegalArgumentException, WrappedTargetException {}
public java.lang.Object getPropertyValue( String stringPropertyName )
throws UnknownPropertyException, WrappedTargetException {}
}
public static XSingleServiceFactory __getServiceFactory(String implName,
XMultiServiceFactory multiFactory,
XRegistryKey regKey) {}
public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {}
}
irst, the class Car will be described. This class provides the factory method and the service info method. The factory method __getServiceFactory() instantiates the component on demand and is used to acquire the service Car. The service info method __writeRegistryServiceInfo() writes the class name and service name into the given registry key. To all components it applies that both methods must be implemented in the framework class, because they will be called by the Java Component Loader.
The inner class Car$_Car represents the component as a concrete implementation of the service description. In order to provide the functional specification all required interfaces have to be implemented. Therefore, the class implements the methods speedUp() and slowDown() of the interface XAccelerationControl to allow accelerating and braking the car. Furthermore, the inner class provides the methods turnLeft() and turnRight() of the interface XDrivingDirection to steer the car. In order to be able to determine the color and the number of seats of the car, the interface XPropertySet must be implemented. In the above program code the methods setPropertyValue() and getPropertyValue() are represented, which enable reading and writing of the properties of the car. A direct assignment of a value to a service property is not possible. Other methods of the interface XPropertySet are omitted here. Apart from the above mentioned methods, the inner class also contains the two public properties Seats and Color, which should store the number of seats and the color of the car. Also, the inner class has a private attribute, __serviceName. The service Car should be addressable by the specified name, __serviceName.