package br.com.mastersys.tools.form; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import br.com.mastersys.persistence.annotations.validators.Root; import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; import javafx.scene.control.Tab; import javafx.scene.control.TabPane; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.stage.Stage; public class Gerador extends Application { TabPane root; CheckBox bt_only_root; ComboBox cb_root; TextField tf_abas; @Override public void start(Stage stage) throws Exception { root = new TabPane(); buildRoot(); Scene scene = new Scene(root, 900, 600); stage.setScene(scene); stage.setTitle("Gerador de Formulários"); stage.show(); } private void buildRoot() throws Exception { Tab tab_main = new Tab(); tab_main.setClosable(false); tab_main.setText("PRINCIPAL"); root.getTabs().add(tab_main); GridPane pn_main = new GridPane(); pn_main.setHgap(10D); pn_main.setVgap(10D); pn_main.setPadding(new Insets(10)); tab_main.setContent(pn_main); bt_only_root = new CheckBox(); bt_only_root.setText("Exibir Somente Roots:"); bt_only_root.setSelected(true); bt_only_root.selectedProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observableValue, Boolean oldValue, Boolean newValue) { cb_root.getItems().setAll(getRoots(bt_only_root.isSelected())); } }); pn_main.add(bt_only_root, 0, 0, 2, 1); Label lb_root = new Label(); lb_root.setText("Root:"); pn_main.add(lb_root, 0, 1); cb_root = new ComboBox<>(); cb_root.getItems().setAll(getRoots(bt_only_root.isSelected())); cb_root.setPrefWidth(500); pn_main.add(cb_root, 1, 1); Label lb_abas = new Label(); lb_abas.setText("Abas:"); pn_main.add(lb_abas, 0, 2); tf_abas = new TextField(); tf_abas.setMaxWidth(50); pn_main.add(tf_abas, 1, 2); Button bt_gerar_abas = new Button(); bt_gerar_abas.setText("GERAR ABAS"); bt_gerar_abas.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { gerarAbas(); } }); pn_main.add(bt_gerar_abas, 0, 3, 2, 1); } private void gerarAbas() { } private List getRoots(boolean onlyRoots) { try { List roots = new ArrayList<>(); getRoots(roots,"br.com.mastersys.persistence.model", onlyRoots); return roots; } catch (Exception e) { e.printStackTrace(); return new ArrayList<>(); } } @SuppressWarnings({ "rawtypes", "unchecked" }) private void getRoots(List roots, String rootPackage, boolean onlyRoots) throws Exception { Class[] classes = getClasses(rootPackage); if(classes != null) { for (Class clazz : classes) { if(onlyRoots) { if(clazz.isAnnotationPresent(Root.class)) { roots.add(clazz.getName()); } } else { roots.add(clazz.getName()); } } } } /** * Scans all classes accessible from the context class loader which belong to the given package and subpackages. * * @param packageName The base package * @return The classes * @throws ClassNotFoundException * @throws IOException */ @SuppressWarnings("rawtypes") private static Class[] getClasses(String packageName) throws ClassNotFoundException, IOException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; String path = packageName.replace('.', '/'); Enumeration resources = classLoader.getResources(path); List dirs = new ArrayList(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); dirs.add(new File(resource.getFile())); } ArrayList classes = new ArrayList(); for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } return classes.toArray(new Class[classes.size()]); } /** * Recursive method used to find all classes in a given directory and subdirs. * * @param directory The base directory * @param packageName The package name for classes found inside the base directory * @return The classes * @throws ClassNotFoundException */ @SuppressWarnings("rawtypes") private static List findClasses(File directory, String packageName) throws ClassNotFoundException { List classes = new ArrayList(); if (!directory.exists()) { return classes; } File[] files = directory.listFiles(); for (File file : files) { if (file.isDirectory()) { assert !file.getName().contains("."); classes.addAll(findClasses(file, packageName + "." + file.getName())); } else if (file.getName().endsWith(".class")) { classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6))); } } return classes; } /** * @param args */ public static void main(String[] args) { Gerador.launch(args); } }