public class FileObject implements IlpObject {
The
FileObject
class contains the definition of the corresponding
IlpClass. The corresponding code is not
repeated here.
This class contains a number of attribute members. It wraps a
java.io.File
and has a
file
attribute, which is also used as the class identifier. This class
also contains an
IlpClass
attribute which makes it possible to use it with
IlpClass
instances other than the one it defines (typically subclasses of
the one it defines). This class also contains an
AttributeValueChangeSupport
instance that handles notification of attribute changes.
protected File file;
protected IlpClass ilpClass;
protected IlpAttributeValueChangeSupport support =
new IlpAttributeValueChangeSupport(this);
The static method
GetIlpClass
allows you to register the
IlpClass
with the class manager automatically. Note that
ILPCLASS
is a static member.
public static IlpClass GetIlpClass() {
return ILPCLASS;
}
The class has two constructors. The first one is the easiest to
use, as it takes only the file to be wrapped as parameter. The
second one lets you initialize the
ilpClass
attribute. It corresponds to the method
newInstance(IlpClass ilpClass, Object
identifier, boolean initializeAttributeValues)
of the
IlpClass
.
public FileObject(File file) {
this.file = file;
this.ilpClass = GetIlpClass();
}
public FileObject(File file, IlpClass ilpClass) {
this.file = file;
this.ilpClass = ilpClass;
}
The following methods add or remove listeners to attribute value
changes and fire events. They all delegate to the
IlpAttributeValueChangeSupport instance.
public void addAttributeValueListener (AttributeValueListener l) {
support.addAttributeValueListener(l);
}
public void removeAttributeValueListener (AttributeValueListener l) {
support.removeAttributeValueListener(l);
}
public void fireEvent (AttributeValueEvent ev) {
support.fireEvent(ev);
}
The method
getAttributeValue
retrieves attribute values from the
java.io.File
object.
public Object getAttributeValue (IlpAttribute attribute) {
if (attribute == EXISTS) {
return file.exists()?Boolean.TRUE:Boolean.FALSE;
}
if (attribute == NAME) {
String name = file.getName();
// for the roots the name may be null, so return the path in this case
return name != null && name.length() != 0 ?name:file.getPath();
}
if (attribute == PARENT) {
return file.getParent();
}
if (attribute == PATH) {
return file.getPath();
}
if (attribute == DIRECTORY) {
return file.isDirectory()?Boolean.TRUE:Boolean.FALSE;
}
if (attribute == HIDDEN) {
return file.isHidden()?Boolean.TRUE:Boolean.FALSE;
}
if (attribute == LASTMODIFIED) {
return new Long(file.lastModified());
}
if (attribute == LASTMODIFIEDDATE) {
Date date = new Date();
date.setTime(file.lastModified());
return date;
}
if (attribute == LENGTH) {
return new Long(file.length());
}
if (attribute == ROOT) {
return file.getParent() == null ?Boolean.TRUE:Boolean.FALSE;
}
return null;
}
Here the method
setAttributeValue
sets the value for one attribute only and sets off notification
about value change.
public void setAttributeValue (IlpAttribute attribute, Object value) {
if (attribute == LASTMODIFIED) {
if (value instanceof Number) {
file.setLastModified(((Number)value).longValue());
fireEvent(new AttributeValueEvent(this, LASTMODIFIED));
} else
throw new IllegalArgumentException(value+" is not acceptable as value
of attribute "+attribute);
}
}
The
getIdentifier
method simply returns the wrapped file. The
getIlpClass
method returns the corresponding attribute. The
getIlpAttributeGroup
method also returns the
IlpClass
. The
hasAttributeValue
method is delegated to the attribute group.
The
initializeDefaultValues
method does nothing here. It is invoked to initialize the default
values of the attributes and corresponds to the XML attribute
initializeDefaultValue
. See
Elements in an XML data fileĀ for details.
public Object getIdentifier() {
return file;
}
public IlpClass getIlpClass() {
return ilpClass;
}
public IlpAttributeGroup getAttributeGroup () {
return getIlpClass();
}
public boolean hasAttributeValue (IlpAttribute a) {
return getAttributeGroup().hasAttribute(a);
}
public void initializeDefaultValues() {
// nothing to do here
}