<installdir>/samples/datasource/explorer
.
FileDataSource
from that class allows you to benefit from this functionality.
public class FileDataSource extends IltDefaultDataSource {
public FileDataSource() { super(); // Retrieve the File Java class as an IlpClass IlpMutableClassManager classManager = getContext().getClassManager(); fileClass = (IlpMutableClass)classManager.getClass(File.class.getName()); // Create a subclass of the java.io.File IlpClass for root objects, // so as to apply different styles to them. classOfRoots = new IlpDefaultClass("DirectoryRoot", fileClass, Collections.EMPTY_LIST); fileClass.addSubClass(classOfRoots); classManager.addClass(classOfRoots); // loads the roots File[] rootdirs = File.listRoots(); addFiles(rootdirs, classOfRoots); }
java.io.File
. In this sample code,
java.io.File
objects are defined by the IlpClass automatically generated by the
class manager. Here, the class
java.io.File
is considered as a JavaBeans™ class (which is acceptable, as
most of its methods comply with JavaBeans conventions). As an
alternative, you could create a dedicated
IlpClass
implementation and the corresponding
IlpObject
implementation. See Defining the business model with dynamic
classes and Adding dynamic business objects.
getName
method of the class
java.io.File
does not display labels correctly. This is why it is necessary
here to subclass the
IlpClass
defining
java.io.File
to represent the roots of the file system.
IlpClass
objects are known, the data source is initialized with the root
objects returned by the
listRoots
method of the class
java.io.File
.
addFiles
method, detailed below, iterates over an array of
Files
, creates the corresponding
IlpObject
instances if they are not yet present in the data source, and
inserts these objects into the data source.
protected synchronized void addFiles(File[] files, IlpClass ilpClass) { List filesAsIlpObjects = new ArrayList(files.length); for (int i = 0; i < files.length; i++) { // use the file itself as the object identifier Object id = files[i]; if (getObject(id) == null) { IlpBeansObject ilpObject = new IlpBeansObject(files[i], ilpClass,files[i]); filesAsIlpObjects.add(ilpObject); } } if (filesAsIlpObjects.size() != 0) addObjects(filesAsIlpObjects); }
getObject(Object id)
method
getContainerInterface
method
IlpObject
.
getContainerInterface
of the data source (see Implementing the getContainerInterface
method). Since we do not use the access to structural
information provided by the default data source in order not to
duplicate it from the
File
class where it is already present, we also provide a specific
implementation of the
getChildInterface
method (see Implementing the getChildInterface method
and the IlpChild interface).
IlpObject
instances dynamically by reimplementing the method
getObject(Object id)
.
getContainerInterface
returns an IlpContainer. If the object is a container,
the method should return a non-null value, whether this container
has children or not, as shown in the following example.
public synchronized IlpContainer getContainerInterface(Object idOrIlpObject) { IlpObject object = idOrIlpObject instanceof IlpObject ? (IlpObject)idOrIlpObject : getObject(idOrIlpObject); if (object != null && fileClass.isAssignableFrom(object.getIlpClass())) { File file = (File)((IlpBeansObject)object).getBean(); if (file.isDirectory()) return DIRECTORY_LOADER; else return null; } return super.getContainerInterface(idOrIlpObject); }
getContainerInterface
method takes an
IlpObject
or an identifier as parameter. It retrieves the
IlpObject
instance from its parameter and checks whether the returned
object is an instance of the
IlpClass
corresponding to the
File
class and whether
File
is a directory. If it is not a directory, it returns
null
or the default implementation of the data source, if the object
is not of the
File IlpClass
. If it is a directory, the method returns a specific
implementation of the
IlpContainer
interface (see Implementing the IlpContainer interface).
IlpObject
is downcast to an instance of IlpBeansObject and that the Bean it
contains is in turn downcast to a
File
instance. We could have used the directory attribute of the
IlpClass
directly as follows:
Boolean isDirectory = Boolean)object.getAttributeValue(fileClass.getAttribute("directory")) ; if (isDirectory.booleanValue()) return DIRECTORY_LOADER; else return null;
File
object and load them. Following is an implementation of
IlpContainer
for the class
java.io.File
. It uses a static instance of the class since the entire context
of the
getChildren
method is contained in its parameter, which is the
IlpObject
instance itself.
IlpContainer DIRECTORY_LOADER = new IlpContainer(){ public Collection getChildren(IlpObject object) { // the file is supposed to be a directory File directory = (File)((IlpBeansObject)object).getBean(); File[] files = directory.listFiles(); List filesIds = new ArrayList(files.length); for (int i = 0; i < files.length; i++) { // 1st get all the identifiers filesIds.add(files[i]); } // then add the files addFiles(files, fileClass); return filesIds; } };
IlpObject
instance twice, no record is kept as to whether a directory has
already been loaded. The test in the
addFiles
method (described in Implementing the data source) is used to
check whether an
IlpObject
instance exists before creating it and adding it to the data
source.
getChildInterface
method and of the IlpChild interface. This is quite easy,
since the
java.io.File
class has a
getParentFile
method.
public synchronized IlpChild getChildInterface(Object idOrIlpObject) { IlpObject object = idOrIlpObject instanceof IlpObject ? (IlpObject)idOrIlpObject : getObject(idOrIlpObject); if (object != null && fileClass.isAssignableFrom(object.getIlpClass())) { File file = (File)((IlpBeansObject)object).getBean(); File parentFile = file.getParentFile(); if (parentFile != null) { return FILE_PARENT; } else { return null; } } return super.getChildInterface(idOrIlpObject); }
IlpObject
and checks whether that object is a file. It then checks
whether it has a parent file and returns the specific
implementation of the
IlpChild
interface, described below.
IlpChild
interface returns an identifier. Here, since the identifiers of
the objects are the files themselves, it returns the parent
file.
IlpChild FILE_PARENT = new IlpChild() { public Object getParent(IlpObject object) { File file = (File)((IlpBeansObject)object).getBean(); File parentFile = file.getParentFile(); return parentFile; }