package test;

import javafx.application.Application;
import javafx.beans.property.ReadOnlyProperty;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.lang.reflect.InvocationTargetException;

public class PropertyTableApp1 extends Application {
    @Override
    public void start(Stage stage) {
        var table = new TableView<PropertyValue>();
        TableColumn<PropertyValue, String> propColumn = new TableColumn<>("Name");
        propColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        TableColumn<PropertyValue, String> valueColumn = new TableColumn<>("Value");
        valueColumn.setCellValueFactory(new PropertyValueFactory<>("value"));
        table.getColumns().addAll(propColumn, valueColumn);
        var button = new Button("List properties");
        button.addEventHandler(ActionEvent.ANY, e -> {
            try {
                for(var m : Button.class.getMethods()) {
                    if(ReadOnlyProperty.class.isAssignableFrom(m.getReturnType()) && m.getParameterTypes().length == 0) {
                        var prop = (ReadOnlyProperty)m.invoke(button);
                        System.out.println(prop.getName() + "=" + prop.getValue());
                        table.getItems().add(new PropertyValue(prop));
                    }
                }
            } catch (IllegalAccessException | InvocationTargetException ex) {
                ex.printStackTrace();
            }
        });
        var content = new BorderPane();
        content.setBottom(button);
        content.setCenter(table);
        var scene = new MyScene(content);
        stage.setScene(scene);
        stage.show();
    }

    private class PropertyValue {
        private final String name;
        private final Object value;

        public PropertyValue(ReadOnlyProperty property) {
            this.name = property.getName();
            this.value = property.getValue();
        }

        public String getName() {
            return name;
        }

        public Object getValue() {
            return value;
        }
    }

    public static void main(String[] args) {
        launch();
    }
}
