Java Data Sources > Java Data Source Adapters > Using IlsTableDS2JTableAdapter > Using a User-Defined Type
 
Using a User-Defined Type
In the server model, a class Date is derived from IlsMvUserType. On the component side, we are also going to define a class Date (see the network7 demo for the complete code of the class). The most important methods are Decode and encode. They are used to convert a Date from/to a MvMessage.
public MvMessage encode(MvMessage msg) {
msg.encodeUInt(_hour);
msg.encodeUInt(_mn);
msg.encodeUInt(_sec);
return msg;
}
 
public static Date Decode(MvMessage m) {
int hour,mn,sec;
hour=m.decodeUInt();
mn=m.decodeUInt();
sec=m.decodeUInt();
Date d = new Date(hour,mn,sec);
return d;
}
Then, we must also provide a constructor from the String class and to register a converter from the String class to the Date class so as to complete the encoding/decoding process used in the Server/JavaTM data-source protocol.
public Date(String aDate) {
int start = 0;
int end = aDate.indexOf(':');
_hour = Integer.parseInt(aDate.substring(0, end));
start = end + 1;
end = aDate.indexOf(':', start);
_mn = Integer.parseInt(aDate.substring(start, end));
start = end + 1;
_sec = Integer.parseInt(aDate.substring(start));
}
 
static {
IlsClassConverter.Register(String.class, Date.class, new IlsConverter() {
public Object convert(Object anObject) {
return new Date((String) anObject);
}
});
}
Now, we define a table view using the class Date:
view DomainTableView (any containerIndex=0,
string containerClass="table.DomainTableFrame"):
represent IlsDSRepresentation repres:
any containerIndex = view.containerIndex;
string containerClass = view.containerClass;
string title = "A domain table view";
 
subscribe Node:
represent IlsRpRow row:
mandatory Ref<IlsRpTable> table = view.origin->table;
string column[0] = name;
Date column[1] = lockDate;
 
subscribe origin Domain:
represent IlsRpTable table:
string column[0] = "Name";
string column[1] = "Date";
 
propagate nodes;
You can see that the type of the column[1] attribute is Date and the registered IlsClassConverter is automatically used to create representation objects with this type of column. Note that when the JTable instance renders a row cell with a Date class, the standard Java method toString is used, so you should also define it.
See the network7 demo for the complete code sample.

Version 5.8
Copyright © 2014, Rogue Wave Software, Inc. All Rights Reserved.