swing - Java Generic programming: Determining if class extends another -


i've few object (terminal, runway, gate, airline, workers etc...) need show in jtable. i'm trying build generictablemodel (to use jtable) work every collection. thing of objects have data structures in , don't want show structures. i'm getting column names getting first object collection , asking declared fields aren't assignable abstractmap or collection classes. seems work me, fields need far. problem start when try show workers. have w_worker abstract class of fields worker, , i've different workers such w_manager, w_fuelworker etc... each of sub.classes have 1 or 2 fields, , when put them in table using generictablemodel fields specific worker without fields w_worker.

how can determine in beginning of method, if class extends class , making fields first?

i'll provide code if won't enough understand. 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 = 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 can see i'm using instanceof, isn't generic enough work w_worker.

i consider visitor pattern this. visitor know how deal tablemodel.

somewhat 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 treat model } 

then can make every node deal visitor , ask populating model:

public class worker {    public void accept(tablemodelvisitor tablemodelvisitor) {       //and here know worker , fields       //and have access visitor table model configure it.       tablemodelvistor.addfield(this.getmyfield1());       tablemodelvistor.addfield(this.getmyfield2());    } } 

at end, have create tablemodelvisitor , ask populate model want.

tablemodelvisitor visitor = new tablemodelvisitor(mytablemodel); visitor.visit(anyofmyobjects); 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -