restricted:mco-2:correction:tp4

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
restricted:mco-2:correction:tp4 [2016/03/30 10:51] fbruckerrestricted:mco-2:correction:tp4 [2016/03/30 12:58] (Version actuelle) fbrucker
Ligne 1: Ligne 1:
 +====== Correction TP4 ======
  
 +
 +===== MVC =====
 +
 +Seuls UserInterface et Controller ont changé. On a mis en place des getter dans UserInterface et ont les a utilisés dans le contrôleur.
 +
 +=== UserInterface.java ===
 +
 +<code java>
 +package com.mco;
 +
 +import javafx.scene.Group;
 +import javafx.scene.control.Button;
 +import javafx.scene.control.TextField;
 +import javafx.scene.layout.HBox;
 +import javafx.scene.text.Text;
 +
 +public class UserInterface {
 +    private Group root;
 +
 +    private TextField todoItemInput;
 +    private Button addButton;
 +
 +    public UserInterface() {
 +        root = generateUI();
 +    }
 +
 +    private Group generateUI() {
 +        Group uiRoot = new Group();
 +
 +        HBox hBox = new HBox(10);
 +        uiRoot.getChildren().add(hBox);
 +
 +        Text text = new Text("Input:");
 +        todoItemInput = new TextField();
 +        addButton = new Button("add");
 +
 +        hBox.getChildren().addAll(text, todoItemInput, addButton);
 +
 +        return uiRoot;
 +    }
 +
 +    public Group getRoot() {
 +        return root;
 +    }
 +
 +    public TextField getTodoItemInput() {
 +        return todoItemInput;
 +    }
 +
 +    public Button getAddButton() {
 +        return addButton;
 +    }
 +}
 +
 +</code> 
 +
 +
 +=== Controller.java === 
 +
 +<code java>
 +package com.mco;
 +
 +import javafx.event.ActionEvent;
 +import javafx.scene.control.Button;
 +
 +import java.util.ArrayList;
 +
 +public class Controller {
 +    private ArrayList<String> model;
 +    private UserInterface vue;
 +
 +    public Controller(ArrayList<String> model, UserInterface vue) {
 +        this.model = model;
 +        this.vue = vue;
 +
 +        Button button = vue.getAddButton();
 +
 +        button.setOnAction(this::handleButtonAdd);
 +    }
 +
 +    private void handleButtonAdd(ActionEvent actionEvent) {
 +        String todoItemText = vue.getTodoItemInput().getText();
 +        if (!todoItemText.equals("")) {
 +            model.add(todoItemText);
 +        }
 +        
 +        System.out.println(model);
 +    }
 +}
 +</code>
 +
 +
 +===== Amélioration de UI =====
 +
 +Javafx 8 permet de faire de nombreux agencements de composants graphique, lisez par exemple ce [[http://docs.oracle.com/javase/8/javafx/layout-tutorial/builtin_layouts.htm|tutorial sur les layouts]].
 +
 +==== Représentez le modèle ==== 
 +
 +On a ajouté un champ contenant notre liste dans la vue et on l'a utilisé dans le contrôleur.
 +
 +=== UserInterface.java ===
 +
 +<code java>
 +package com.mco;
 +
 +import javafx.scene.Group;
 +import javafx.scene.control.Button;
 +import javafx.scene.control.ScrollPane;
 +import javafx.scene.control.TextField;
 +import javafx.scene.layout.HBox;
 +import javafx.scene.layout.VBox;
 +import javafx.scene.text.Text;
 +
 +public class UserInterface {
 +    private Group root;
 +
 +    private TextField todoItemInput;
 +    private Button addButton;
 +    private VBox todoItemList;
 +
 +    public UserInterface() {
 +        root = generateUI();
 +    }
 +
 +    private Group generateUI() {
 +        Group uiRoot = new Group();
 +
 +        HBox hBox = new HBox(10);
 +
 +
 +        Text text = new Text("Input:");
 +        todoItemInput = new TextField();
 +        addButton = new Button("add");
 +
 +        hBox.getChildren().addAll(text, todoItemInput, addButton);
 +
 +        VBox vBox = new VBox(20);
 +
 +        todoItemList = new VBox(5);
 +        ScrollPane scrollPane = new ScrollPane();
 +        scrollPane.setContent(todoItemList);
 +        scrollPane.setPrefSize(200, 200);
 +
 +        vBox.getChildren().add(hBox);
 +        vBox.getChildren().add(scrollPane);
 +
 +        uiRoot.getChildren().add(vBox);
 +
 +        return uiRoot;
 +    }
 +
 +    public Group getRoot() {
 +        return root;
 +    }
 +
 +    public TextField getTodoItemInput() {
 +        return todoItemInput;
 +    }
 +
 +    public Button getAddButton() {
 +        return addButton;
 +    }
 +
 +    public VBox getTodoItemList() {
 +        return todoItemList;
 +    }
 +}
 +
 +</code>
 +
 +=== Controller.java ===
 +
 +<code java>
 +package com.mco;
 +
 +import javafx.event.ActionEvent;
 +import javafx.scene.control.Button;
 +import javafx.scene.text.Text;
 +
 +import java.util.ArrayList;
 +
 +public class Controller {
 +    private ArrayList<String> model;
 +    private UserInterface vue;
 +
 +    public Controller(ArrayList<String> model, UserInterface vue) {
 +        this.model = model;
 +        this.vue = vue;
 +
 +        Button button = vue.getAddButton();
 +
 +        button.setOnAction(this::handleButtonAdd);
 +    }
 +
 +    private void handleButtonAdd(ActionEvent actionEvent) {
 +        String todoItemText = vue.getTodoItemInput().getText();
 +        if (!todoItemText.equals("")) {
 +            model.add(todoItemText);
 +            vue.getTodoItemList().getChildren().add(new Text(todoItemText));
 +        }
 +
 +    }
 +}
 +</code>
 +
 +
 +==== Ajout du résumé ==== 
 +
 +=== UserInterface.java ===
 +
 +<code java>
 +package com.mco;
 +
 +import javafx.scene.Group;
 +import javafx.scene.control.Button;
 +import javafx.scene.control.ScrollPane;
 +import javafx.scene.control.TextField;
 +import javafx.scene.layout.HBox;
 +import javafx.scene.layout.VBox;
 +import javafx.scene.text.Text;
 +
 +public class UserInterface {
 +    private Group root;
 +
 +    private TextField todoItemInput;
 +    private Button addButton;
 +    private VBox todoItemList;
 +    private Text totalItem;
 +    private int numberTotalItem;
 +
 +    public UserInterface() {
 +        numberTotalItem = 0;
 +        root = generateUI();
 +    }
 +
 +    private Group generateUI() {
 +        Group uiRoot = new Group();
 +
 +        HBox hBox = new HBox(10);
 +
 +
 +        Text text = new Text("Input:");
 +        todoItemInput = new TextField();
 +        addButton = new Button("add");
 +
 +        hBox.getChildren().addAll(text, todoItemInput, addButton);
 +
 +        VBox vBox = new VBox(20);
 +
 +        todoItemList = new VBox(5);
 +        ScrollPane scrollPane = new ScrollPane();
 +        scrollPane.setContent(todoItemList);
 +        scrollPane.setPrefSize(200, 200);
 +
 +        vBox.getChildren().add(hBox);
 +        vBox.getChildren().add(scrollPane);
 +
 +        totalItem = new Text();
 +        text = new Text("Total item:");
 +        HBox hBoxNumberItem = new HBox(10);
 +        hBoxNumberItem.getChildren().addAll(text, totalItem);
 +
 +        totalItem.setText(String.valueOf(numberTotalItem));
 +        vBox.getChildren().add(hBoxNumberItem);
 +
 +        uiRoot.getChildren().add(vBox);
 +
 +        return uiRoot;
 +    }
 +
 +    public Group getRoot() {
 +        return root;
 +    }
 +
 +    public TextField getTodoItemInput() {
 +        return todoItemInput;
 +    }
 +
 +    public Button getAddButton() {
 +        return addButton;
 +    }
 +
 +    public VBox getTodoItemList() {
 +        return todoItemList;
 +    }
 +
 +    public void setNumberTotalItem(int numberTotalItem) {
 +        this.numberTotalItem = numberTotalItem;
 +        totalItem.setText(String.valueOf(numberTotalItem));
 +    }
 +}
 +
 +</code>
 +
 +=== Controller.java ===
 +
 +<code java>
 +package com.mco;
 +
 +import javafx.event.ActionEvent;
 +import javafx.scene.control.Button;
 +import javafx.scene.text.Text;
 +
 +import java.util.ArrayList;
 +
 +public class Controller {
 +    private ArrayList<String> model;
 +    private UserInterface vue;
 +
 +    public Controller(ArrayList<String> model, UserInterface vue) {
 +        this.model = model;
 +        this.vue = vue;
 +
 +        Button button = vue.getAddButton();
 +
 +        button.setOnAction(this::handleButtonAdd);
 +    }
 +
 +    private void handleButtonAdd(ActionEvent actionEvent) {
 +        String todoItemText = vue.getTodoItemInput().getText();
 +        if (!todoItemText.equals("")) {
 +            model.add(todoItemText);
 +            vue.getTodoItemList().getChildren().add(new Text(todoItemText));
 +            vue.setNumberTotalItem(model.size());
 +        }
 +
 +    }
 +}
 +
 +</code>
 +
 +
 +===== Mise à jour du modèle =====
 +
 +=== Main.java === 
 +
 +<code Java>
 +package com.mco;
 +
 +import javafx.application.Application;
 +import javafx.scene.Group;
 +import javafx.scene.Scene;
 +import javafx.stage.Stage;
 +
 +import java.util.ArrayList;
 +
 +public class Main extends Application {
 +
 +    public static void main(String[] args) {
 +        launch(args);
 +    }
 +
 +
 +    @Override
 +    public void start(Stage primaryStage) throws Exception {
 +        // on met en place la racine de la scène
 +        Group root = new Group();
 +        Scene theScene = new Scene(root);
 +        primaryStage.setScene(theScene);
 +
 +
 +        //Vue
 +        UserInterface userInterface = new UserInterface();
 +        root.getChildren().add(userInterface.getRoot());
 +
 +        //Modèle
 +        ArrayList<TodoItem> todoList = new ArrayList<>();
 +
 +        //Contrôleur
 +        Controller controller = new Controller(todoList, userInterface);
 +
 +        //Affichage de l'UI
 +        primaryStage.setTitle("Todo List");
 +        primaryStage.show();
 +    }
 +}
 +</code>
 +
 +
 +=== TodoItem.java === 
 +
 +<code Java>
 +package com.mco;
 +
 +import java.util.Date;
 +
 +
 +public class TodoItem {
 +    private String name;
 +    private Date creation;
 +
 +    public TodoItem(String name) {
 +        this.name = name;
 +        creation = new Date();
 +    }
 +
 +    public String getName() {
 +        return name;
 +    }
 +
 +    public Date getCreation() {
 +        return creation;
 +    }
 +}
 +
 +</code>
 +
 +=== UserIterface.java === 
 +
 +<code Java>
 +package com.mco;
 +
 +import javafx.scene.Group;
 +import javafx.scene.control.Button;
 +import javafx.scene.control.ScrollPane;
 +import javafx.scene.control.TextField;
 +import javafx.scene.layout.HBox;
 +import javafx.scene.layout.VBox;
 +import javafx.scene.text.Text;
 +
 +public class UserInterface {
 +    private Group root;
 +
 +    private TextField todoItemInput;
 +    private Button addButton;
 +    private VBox todoItemList;
 +    private Text totalItem;
 +    private int numberTotalItem;
 +
 +    public UserInterface() {
 +        numberTotalItem = 0;
 +        root = generateUI();
 +    }
 +
 +    private Group generateUI() {
 +        Group uiRoot = new Group();
 +
 +        HBox hBox = new HBox(10);
 +
 +
 +        Text text = new Text("Input:");
 +        todoItemInput = new TextField();
 +        addButton = new Button("add");
 +
 +        hBox.getChildren().addAll(text, todoItemInput, addButton);
 +
 +        VBox vBox = new VBox(20);
 +
 +        todoItemList = new VBox(5);
 +        ScrollPane scrollPane = new ScrollPane();
 +        scrollPane.setContent(todoItemList);
 +        scrollPane.setPrefSize(300, 200);
 +
 +        vBox.getChildren().add(hBox);
 +        vBox.getChildren().add(scrollPane);
 +
 +        totalItem = new Text();
 +        text = new Text("Total item:");
 +        HBox hBoxNumberItem = new HBox(10);
 +        hBoxNumberItem.getChildren().addAll(text, totalItem);
 +
 +        totalItem.setText(String.valueOf(numberTotalItem));
 +        vBox.getChildren().add(hBoxNumberItem);
 +
 +        uiRoot.getChildren().add(vBox);
 +
 +        return uiRoot;
 +    }
 +
 +    public void addTodoItem(TodoItem item) {
 +        HBox hBox = new HBox(20);
 +        hBox.getChildren().addAll(new Text(item.getName()),
 +                new Text(item.getCreation().toString()));
 +
 +        todoItemList.getChildren().add(hBox);
 +    }
 +
 +    public Group getRoot() {
 +        return root;
 +    }
 +
 +    public TextField getTodoItemInput() {
 +        return todoItemInput;
 +    }
 +
 +    public Button getAddButton() {
 +        return addButton;
 +    }
 +
 +    public VBox getTodoItemList() {
 +        return todoItemList;
 +    }
 +
 +    public void setNumberTotalItem(int numberTotalItem) {
 +        this.numberTotalItem = numberTotalItem;
 +        totalItem.setText(String.valueOf(numberTotalItem));
 +    }
 +}
 +
 +</code>
 +
 +=== Controller.java === 
 +
 +<code Java>
 +package com.mco;
 +
 +import javafx.event.ActionEvent;
 +import javafx.scene.control.Button;
 +
 +import java.util.ArrayList;
 +
 +public class Controller {
 +    private ArrayList<TodoItem> model;
 +    private UserInterface vue;
 +
 +    public Controller(ArrayList<TodoItem> model, UserInterface vue) {
 +        this.model = model;
 +        this.vue = vue;
 +
 +        Button button = vue.getAddButton();
 +
 +        button.setOnAction(this::handleButtonAdd);
 +    }
 +
 +    private void handleButtonAdd(ActionEvent actionEvent) {
 +        String todoItemText = vue.getTodoItemInput().getText();
 +        if (!todoItemText.equals("")) {
 +            TodoItem item = new TodoItem(todoItemText);
 +            model.add(item);
 +            vue.addTodoItem(item);
 +
 +            vue.setNumberTotalItem(model.size());
 +        }
 +
 +    }
 +}
 +
 +</code>
 +
 +===== Mise à jour de la vue =====
 +
 +Seul UserInterface a été impacté.
 +
 +=== UserInterface.java === 
 +
 +<code java>
 +package com.mco;
 +
 +import javafx.scene.Group;
 +import javafx.scene.control.Button;
 +import javafx.scene.control.TextField;
 +import javafx.scene.layout.VBox;
 +
 +public class UserInterface {
 +    private Group root;
 +
 +    private InputTodoItemUI inputTodoItemUI;
 +    private TodoListUI todoListUI;
 +    private SummaryUI summaryUI;
 +
 +    public UserInterface() {
 +        root = generateUI();
 +    }
 +
 +    private Group generateUI() {
 +        Group uiRoot = new Group();
 +        VBox vBox = new VBox(20);
 +
 +        inputTodoItemUI = new InputTodoItemUI();
 +        todoListUI = new TodoListUI();
 +        summaryUI = new SummaryUI();
 +
 +        vBox.getChildren().addAll(
 +                inputTodoItemUI.getRoot(),
 +                todoListUI.getRoot(),
 +                summaryUI.getRoot()
 +        );
 +
 +        uiRoot.getChildren().add(vBox);
 +
 +        return uiRoot;
 +    }
 +
 +    public void addTodoItem(TodoItem item) {
 +        todoListUI.addTodoItem(item);
 +    }
 +
 +    public Group getRoot() {
 +        return root;
 +    }
 +
 +    public TextField getTodoItemInput() {
 +        return inputTodoItemUI.getTodoItemInput();
 +    }
 +
 +    public Button getAddButton() {
 +        return inputTodoItemUI.getAddButton();
 +    }
 +
 +    public void setNumberTotalItem(int numberTotalItem) {
 +        summaryUI.setNumberTotalItem(numberTotalItem);
 +    }
 +}
 +
 +</code>
 +
 +=== InputTodoItemUI.java === 
 +
 +<code java>
 +package com.mco;
 +
 +import javafx.scene.Group;
 +import javafx.scene.control.Button;
 +import javafx.scene.control.TextField;
 +import javafx.scene.layout.HBox;
 +import javafx.scene.text.Text;
 +
 +
 +public class InputTodoItemUI {
 +
 +    private Group root;
 +    private TextField todoItemInput;
 +    private Button addButton;
 +
 +    public InputTodoItemUI() {
 +        root = new Group();
 +        HBox hBox = new HBox(10);
 +
 +
 +        Text text = new Text("Input:");
 +        todoItemInput = new TextField();
 +        addButton = new Button("add");
 +
 +        hBox.getChildren().addAll(text, todoItemInput, addButton);
 +
 +        root.getChildren().add(hBox);
 +    }
 +
 +    public Group getRoot() {
 +        return root;
 +    }
 +
 +    public TextField getTodoItemInput() {
 +        return todoItemInput;
 +    }
 +
 +    public Button getAddButton() {
 +        return addButton;
 +    }
 +}
 +
 +</code>
 +
 +=== TodoListUI.java === 
 +
 +<code java>
 +package com.mco;
 +
 +import javafx.scene.Group;
 +import javafx.scene.control.ScrollPane;
 +import javafx.scene.layout.VBox;
 +
 +public class TodoListUI {
 +    private Group root;
 +    private VBox todoItemList;
 +
 +    public TodoListUI() {
 +        root = new Group();
 +        todoItemList = new VBox(5);
 +        ScrollPane scrollPane = new ScrollPane();
 +        scrollPane.setContent(todoItemList);
 +        scrollPane.setPrefSize(300, 200);
 +
 +        root.getChildren().add(scrollPane);
 +    }
 +
 +    public void addTodoItem(TodoItem item) {
 +        TodoItemUI todoItemUI = new TodoItemUI(item);
 +        todoItemList.getChildren().add(todoItemUI.getRoot());
 +    }
 +
 +    public Group getRoot() {
 +        return root;
 +    }
 +
 +}
 +
 +</code>
 +
 +=== TodoItemUI.java === 
 +
 +<code java>
 +package com.mco;
 +
 +
 +import javafx.scene.Group;
 +import javafx.scene.layout.HBox;
 +import javafx.scene.text.Text;
 +
 +public class TodoItemUI {
 +    private Group root;
 +    public TodoItemUI(TodoItem item) {
 +        root = new Group();
 +
 +        HBox hBox = new HBox(20);
 +        hBox.getChildren().addAll(
 +                new Text(item.getName()),
 +                new Text(item.getCreation().toString())
 +        );
 +        root.getChildren().add(hBox);
 +    }
 +
 +    public Group getRoot() {
 +        return root;
 +    }
 +
 +}
 +
 +</code>
 +
 +=== SummaryUI.java === 
 +
 +<code java>
 +package com.mco;
 +
 +
 +import javafx.scene.Group;
 +import javafx.scene.layout.HBox;
 +import javafx.scene.text.Text;
 +
 +public class SummaryUI {
 +    private Group root;
 +    private Text totalItem;
 +    private int numberTotalItem;
 +
 +    public SummaryUI() {
 +        root = new Group();
 +
 +        numberTotalItem = 0;
 +        totalItem = new Text();
 +        Text text = new Text("Total item:");
 +        HBox hBoxNumberItem = new HBox(10);
 +        hBoxNumberItem.getChildren().addAll(text, totalItem);
 +
 +        totalItem.setText(String.valueOf(numberTotalItem));
 +
 +        root.getChildren().add(hBoxNumberItem);
 +
 +    }
 +
 +    public Group getRoot() {
 +        return root;
 +    }
 +    public void setNumberTotalItem(int numberTotalItem) {
 +        this.numberTotalItem = numberTotalItem;
 +        totalItem.setText(String.valueOf(numberTotalItem));
 +    }
 +
 +}
 +
 +</code>