Reputation: 194
I've a few object (Terminal, Runway, Gate, Airline, workers etc...) that I need to show in a JTable. I'm trying to build a GenericTableModel (To use with the JTable) that will work for every collection. Thing is some of those objects have data structures in it and I don't want to show those structures. So I'm getting column names by getting the first object from the collection and asking for its declared fields which aren't assignable from AbstractMap or Collection classes. This seems to work for me, I get only the fields I need so far. Problem start when I try to show the workers. I have a W_Worker abstract class with most of the fields for the worker, and I've different workers such as W_Manager, W_FuelWorker etc... Each of the sub.classes have only 1 or 2 fields, and when I put them in a table using my GenericTableModel I only get the fields from the specific worker without the fields from W_Worker.
How can I determine in the beginning of the method, if the class extends another class and making it get those fields first?
I'll provide code if this won't be enough to understand. Thanks in advance, Tal
Edit: Code added.
public String[] getColumnName(Collection<T> collection){
Field fields[] = null;
String fieldsNames[];
ArrayList<String> desiredFields = new ArrayList<>();
for(Object o : collection){
fields = o.getClass().getDeclaredFields();
if (o instanceof W_Worker){
Field[] superFields = o.getClass().getSuperclass().getDeclaredFields();
Field[] result = new Field[superFields.length + fields.length];
System.arraycopy(superFields, 0, result, 0, superFields.length);
System.arraycopy(fields, 0, result, superFields.length, fields.length);
fields = result;
}
break;
}
fieldsNames = new String[fields.length];
for(int i = 0; i<fields.length ;i++){
if (!Collection.class.isAssignableFrom(fields[i].getType()) && !AbstractMap.class.isAssignableFrom(fields[i].getType())){
fieldsNames[i] = fields[i].getName();
}
}
for(String field : fieldsNames){
if (field != null){
desiredFields.add(field);
}
}
String newFields[] = new String[desiredFields.size()];
return desiredFields.toArray(newFields);
}
As you can see I'm currently using instanceof, but this isn't generic enough as it will only work with W_Worker.
Upvotes: 0
Views: 176
Reputation: 194
I ended up changing the GenericTableModel to receive a collection and an array of Strings which would include the fields I wanted for my object. This way I kept it Generic still and get the best result for my project.
Upvotes: 0
Reputation: 78649
I would consider a Visitor Pattern for this. The visitor would know how to deal with the TableModel.
Somewhat like this:
public class TableModelVisitor {
private TableModel model;
public void visit(Terminal terminal) {
terminal.accept(this);
}
public void visit(Runway runway) {
runway.accept(this);
}
public void visit(Worker worker) {
worker.accept(this);
}
//your methods to treat the model
}
Then you can make every node deal with the visitor and ask it to do something like populating the model:
public class Worker {
public void accept(TableModelVisitor tableModelVisitor) {
//and here you know everything about your worker and its fields
//and have access to your visitor table model to configure it.
tableModelVistor.addField(this.getMyField1());
tableModelVistor.addField(this.getMyField2());
}
}
At the end, all you have to do is to create a TableModelVisitor and ask it to populate any model you want.
TableModelVisitor visitor = new TableModelVisitor(myTableModel);
visitor.visit(anyOfMyObjects);
Upvotes: 1
Reputation: 470
For any class you can see what it extends and/or implements by using Reflection. Afterwards do a comparison vs types of different worker classes you have and call the method you find appropriate. A fully worked example is in the trail: http://docs.oracle.com/javase/tutorial/reflect/class/classModifiers.html
Upvotes: 0
Reputation: 8204
You're probably using the Class.getDeclaredFields method. You want Class.getFields. More information here: http://forgetfulprogrammer.wordpress.com/2011/06/13/java-reflection-class-getfields-and-class-getdeclaredfields/
Upvotes: 0