1

Hello everybody,

I was looking for javafx implementation of graphs, here, but for the id, I would like to have any type. The problem is that when I want to show it, It depends of the type, so I can do this :

public class LabelCell<T> extends Cell<T> {

public LabelCell(T id) {
    super(id);
    Pane pane=new Pane();
    Label view;

    if (id instanceof SQLTableVertex) {

     String labelString="";

     labelString+=((SQLTableVertex) id).getName();
     labelString+="\n"+"________________________";
     for (Identifier i:((SQLTableVertex) id).getIdentifiers())
         labelString+= "\n" +i;

     view = new Label(labelString);

    }       

    else 
        view=new Label(id.toString());


    view.setStyle("-fx-background-color: lightgray;");
    pane.getChildren().add(view);
    setView(pane);

}

But I lose the fact that it's generic. I could also add a case in the switch of the model but it is the same problem

How can I deal with it ? Thank's by advance.

Community
  • 1
  • 1
houlouk
  • 11
  • 1
  • 3
  • Could you make a subclass, for example `SQLTableLabelCell extends LabelCell` to handle the special logic for `SQLTableVertex`? The drawback is you are giving the client the responsibility for not using the general class in this case. This in turn could be encapsulated in a factory, but I believe the factory would need to use `instanceof`, so I guess you wouldn’t be quite happy; – Ole V.V. Dec 18 '16 at 13:08
  • Thank you,I can do that but I have to add a case to the switch in the model in this case, is it better than an instanceof ? Finally I've proceeded differently, I create a graph of "String" which is a string representation of SQLTableVertex and so I have no tests to do in cells. – houlouk Dec 19 '16 at 19:31
  • The last sounds to me like a fairly good solution. Not knowing your full design, I cannot really tell for sure, though. There may be pros and cons, and you can best weigh them yourself. – Ole V.V. Dec 20 '16 at 07:17

0 Answers0