From eb594e029369d3e24e8fda2a3a0dac68da02e4e2 Mon Sep 17 00:00:00 2001 From: Notmoo Date: Tue, 15 Aug 2017 19:05:09 +0200 Subject: [PATCH] =?UTF-8?q?Module=20Client=20:=20ajout=20=C3=A9cran=20stoc?= =?UTF-8?q?k?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/pqt/client/Main.java | 4 +- .../gui/modules/stock_screen/StockScreen.java | 28 ++++ .../stock_screen/StockScreenController.java | 66 ++++++++ .../stock_screen/StockScreenModel.java | 56 +++++++ .../modules/stock_screen/StockScreenView.java | 157 ++++++++++++++++++ .../listeners/IStockItemEventListener.java | 7 + .../listeners/IStockScreenModelListener.java | 7 + .../gui/ressources/strings/GUIStringTool.java | 51 +++++- 8 files changed, 373 insertions(+), 3 deletions(-) create mode 100644 Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/StockScreen.java create mode 100644 Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/StockScreenController.java create mode 100644 Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/StockScreenModel.java create mode 100644 Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/StockScreenView.java create mode 100644 Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/listeners/IStockItemEventListener.java create mode 100644 Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/listeners/IStockScreenModelListener.java diff --git a/Workspace/client/src/main/java/com/pqt/client/Main.java b/Workspace/client/src/main/java/com/pqt/client/Main.java index a15ac623..f552abf4 100644 --- a/Workspace/client/src/main/java/com/pqt/client/Main.java +++ b/Workspace/client/src/main/java/com/pqt/client/Main.java @@ -2,6 +2,7 @@ package com.pqt.client; import com.pqt.client.gui.main_frame.MainFrame; import com.pqt.client.gui.modules.sale_screen.SaleScreen; +import com.pqt.client.gui.modules.stock_screen.StockScreen; import com.pqt.client.gui.ressources.components.generics.others.SideBar; import com.pqt.client.gui.ressources.components.generics.others.listeners.ISideBarListener; import com.pqt.client.gui.ressources.css.GUICssTool; @@ -11,8 +12,6 @@ import com.pqt.client.module.sale.SaleService; import com.pqt.client.module.stock.StockService; import javafx.application.Application; import javafx.scene.Scene; -import javafx.scene.control.Button; -import javafx.scene.layout.BorderPane; import javafx.stage.Stage; public class Main extends Application{ @@ -29,6 +28,7 @@ public class Main extends Application{ MainFrame mainFrame = new MainFrame(accountService); mainFrame.addModule(new SaleScreen(accountService, stockService, saleService)); + mainFrame.addModule(new StockScreen(stockService)); Scene scene = new Scene(mainFrame.getPane(), 800, 600); scene.getStylesheets().clear(); diff --git a/Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/StockScreen.java b/Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/StockScreen.java new file mode 100644 index 00000000..3d9d298c --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/StockScreen.java @@ -0,0 +1,28 @@ +package com.pqt.client.gui.modules.stock_screen; + +import com.pqt.client.gui.modules.IGuiModule; +import com.pqt.client.module.stock.StockService; +import javafx.scene.layout.Pane; + +public class StockScreen implements IGuiModule { + + private StockScreenView view; + + public StockScreen(StockService stockService) { + StockScreenModel model = new StockScreenModel(stockService); + StockScreenController ctrl = new StockScreenController(model); + view = new StockScreenView(ctrl); + + ctrl.setView(view); + } + + @Override + public String getModuleName() { + return "Stock"; + } + + @Override + public Pane getPane() { + return view.getPane(); + } +} diff --git a/Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/StockScreenController.java b/Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/StockScreenController.java new file mode 100644 index 00000000..a0847c7b --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/StockScreenController.java @@ -0,0 +1,66 @@ +package com.pqt.client.gui.modules.stock_screen; + + +import com.pqt.client.gui.modules.stock_screen.listeners.IStockItemEventListener; +import com.pqt.client.gui.modules.stock_screen.listeners.IStockScreenModelListener; +import com.pqt.client.gui.ressources.strings.GUIStringTool; +import com.pqt.core.entities.product.Product; + +class StockScreenController implements IStockScreenModelListener{ + + private StockScreenModel model; + private StockScreenView view; + + StockScreenController(StockScreenModel model) { + this.model = model; + } + + void setView(StockScreenView view) { + this.view = view; + } + + void onAddProductRequest() { + detailProduct(null); + } + + void onDetailProductRequest() { + detailProduct(view.getSelectedProduct()); + } + + private void detailProduct(Product product){ + //TODO à faire + } + + void onDeleteProductRequest() { + deleteProduct(view.getSelectedProduct()); + } + + private void addProduct(Product product){ + model.commitProductAddition(product); + } + + private void modifyProduct(Product oldProduct, Product newProduct){ + model.commitProductModification(oldProduct, newProduct); + } + + private void deleteProduct(Product product){ + model.commitProductDeletion(product);; + } + + void onRefreshProductsRequest() { + refreshView(); + } + + IStockItemEventListener getProductActivationListener() { + return this::detailProduct; + } + + private void refreshView(){ + view.display(model.getProductCollection()); + } + + @Override + public void onStockUpdatedEvent() { + refreshView(); + } +} diff --git a/Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/StockScreenModel.java b/Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/StockScreenModel.java new file mode 100644 index 00000000..429120eb --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/StockScreenModel.java @@ -0,0 +1,56 @@ +package com.pqt.client.gui.modules.stock_screen; + +import com.pqt.client.gui.modules.stock_screen.listeners.IStockScreenModelListener; +import com.pqt.client.module.stock.Listeners.StockListenerAdapter; +import com.pqt.client.module.stock.StockService; +import com.pqt.core.entities.product.Product; + +import javax.swing.event.EventListenerList; +import java.util.Arrays; +import java.util.Collection; + +class StockScreenModel { + + private StockService stockService; + private EventListenerList listenerList; + + StockScreenModel(StockService stockService) { + listenerList = new EventListenerList(); + this.stockService = stockService; + this.stockService.addListener(new StockListenerAdapter(){ + @Override + public void onProductListChangedEvent() { + StockScreenModel.this.fireProductCollectionChanged(); + } + }); + } + + private void fireProductCollectionChanged() { + Arrays.stream(listenerList.getListeners(IStockScreenModelListener.class)) + .forEach(IStockScreenModelListener::onStockUpdatedEvent); + } + + Collection getProductCollection() { + return stockService.getProducts(); + } + + void commitProductDeletion(Product product) { + + } + + void commitProductModification(Product oldProduct, Product newProduct) { + + } + + void commitProductAddition(Product product) { + + } + + void addListener(IStockScreenModelListener l){ + listenerList.add(IStockScreenModelListener.class, l); + } + + void removeListener(IStockScreenModelListener l){ + listenerList.remove(IStockScreenModelListener.class, l); + } +} diff --git a/Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/StockScreenView.java b/Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/StockScreenView.java new file mode 100644 index 00000000..bb167158 --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/StockScreenView.java @@ -0,0 +1,157 @@ +package com.pqt.client.gui.modules.stock_screen; + +import com.pqt.client.gui.ressources.components.generics.IFXComponent; +import com.pqt.client.gui.ressources.strings.GUIStringTool; +import com.pqt.client.gui.ressources.strings.IObjectStringRenderer; +import com.pqt.core.entities.product.Product; +import javafx.application.Platform; +import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.property.SimpleDoubleProperty; +import javafx.beans.property.SimpleIntegerProperty; +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.value.ObservableValue; +import javafx.scene.control.*; +import javafx.scene.input.KeyCode; +import javafx.scene.input.MouseButton; +import javafx.scene.layout.BorderPane; +import javafx.scene.layout.HBox; +import javafx.scene.layout.Pane; +import javafx.scene.layout.Priority; +import javafx.util.Callback; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +class StockScreenView implements IFXComponent { + + private StockScreenController ctrl; + private Pane mainPane; + private TableView stockTableView; + + StockScreenView(StockScreenController ctrl) { + this.ctrl = ctrl; + initGui(); + } + + private void initGui() { + mainPane = new Pane(); + mainPane.getStyleClass().add("main-module-pane"); + BorderPane mainPaneContent = new BorderPane(); + mainPane.getChildren().add(mainPaneContent); + mainPaneContent.prefWidthProperty().bind(mainPane.widthProperty()); + mainPaneContent.prefHeightProperty().bind(mainPane.heightProperty()); + + Button addProductButton = new Button(GUIStringTool.getAddButtonLabel()); + addProductButton.setOnMouseClicked(event -> ctrl.onAddProductRequest()); + Button detailProductButton = new Button(GUIStringTool.getDetailButtonLabel()); + detailProductButton.setOnMouseClicked(event -> ctrl.onDetailProductRequest()); + Button removeProductButton = new Button(GUIStringTool.getRemoveButtonLabel()); + removeProductButton.setOnMouseClicked(event -> ctrl.onDeleteProductRequest()); + Button refreshProductButton = new Button(GUIStringTool.getRefreshButtonLabel()); + refreshProductButton.setOnMouseClicked(event -> ctrl.onRefreshProductsRequest()); + + ToolBar actionToolbar = new ToolBar(); + actionToolbar.getItems().addAll(addProductButton, detailProductButton, removeProductButton, refreshProductButton); + + HBox mainPaneTopContent = new HBox(); + HBox separator = new HBox(); + mainPaneTopContent.getChildren().addAll(separator, actionToolbar); + HBox.setHgrow(separator, Priority.ALWAYS); + mainPaneContent.setTop(mainPaneTopContent); + + stockTableView = new TableView<>(); + stockTableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); + stockTableView.setRowFactory(tableView->{ + TableRow row = new TableRow<>(); + row.setOnMouseClicked(event -> { + if (event.getButton().equals(MouseButton.PRIMARY) && event.getClickCount() == 2) + ctrl.getProductActivationListener().onProductActivated(row.getItem()); + }); + row.setOnKeyTyped(event -> { + if (event.getCode().equals(KeyCode.ENTER)) + ctrl.getProductActivationListener().onProductActivated(row.getItem()); + }); + return row; + }); + stockTableView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); + List> columns = new ArrayList<>(); + + columns.add(createNewTableColumn(String.class, + GUIStringTool.getProductNameColumnHeader(), + param -> new SimpleStringProperty(param.getValue().getName()), + null + )); + columns.add(createNewTableColumn(String.class, + GUIStringTool.getProductCategoryColumnHeader(), + param -> new SimpleStringProperty(param.getValue().getCategory().getName()), + null + )); + columns.add(createNewTableColumn(Integer.class, + GUIStringTool.getProductAmountRemainingColumnHeader(), + param -> new SimpleIntegerProperty(param.getValue().getAmountRemaining()).asObject(), + null + )); + columns.add(createNewTableColumn(Integer.class, + GUIStringTool.getProductAmountSoldColumnHeader(), + param -> new SimpleIntegerProperty(param.getValue().getAmountSold()).asObject(), + null + )); + columns.add(createNewTableColumn(Double.class, + GUIStringTool.getProductPriceColumnHeader(), + param -> new SimpleDoubleProperty(param.getValue().getAmountSold()).asObject(), + GUIStringTool.getPriceRenderer() + )); + columns.add(createNewTableColumn(Boolean.class, + GUIStringTool.getProductIsSellableColumnHeader(), + param -> new SimpleBooleanProperty(param.getValue().isSellable()), + GUIStringTool.getBooleanRenderer() + )); + + stockTableView.getColumns().addAll(columns); + mainPaneContent.setCenter(stockTableView); + } + + private TableColumn createNewTableColumn(Class clazz, + String header, + Callback, ObservableValue> cellValueFactory, + IObjectStringRenderer renderer){ + TableColumn column = new TableColumn<>(); + if(header!=null) + column.setText(header); + if(cellValueFactory!=null) + column.setCellValueFactory(cellValueFactory); + if(renderer!=null) + column.setCellFactory(table -> new TableCell() { + @Override + protected void updateItem(T item, boolean empty) { + super.updateItem(item, empty); + if (item == null || empty) { + setText(null); + setStyle(""); + } else { + setText(renderer.render(item)); + } + } + } + ); + + return column; + } + + void display(Collection productCollection){ + Platform.runLater(()->{ + this.stockTableView.getItems().clear(); + this.stockTableView.getItems().addAll(productCollection); + }); + } + + @Override + public Pane getPane() { + return mainPane; + } + + Product getSelectedProduct() { + return stockTableView.getSelectionModel().getSelectedItem(); + } +} diff --git a/Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/listeners/IStockItemEventListener.java b/Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/listeners/IStockItemEventListener.java new file mode 100644 index 00000000..08e002eb --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/listeners/IStockItemEventListener.java @@ -0,0 +1,7 @@ +package com.pqt.client.gui.modules.stock_screen.listeners; + +import com.pqt.core.entities.product.Product; + +public interface IStockItemEventListener { + void onProductActivated(Product product); +} diff --git a/Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/listeners/IStockScreenModelListener.java b/Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/listeners/IStockScreenModelListener.java new file mode 100644 index 00000000..b4035570 --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/gui/modules/stock_screen/listeners/IStockScreenModelListener.java @@ -0,0 +1,7 @@ +package com.pqt.client.gui.modules.stock_screen.listeners; + +import java.util.EventListener; + +public interface IStockScreenModelListener extends EventListener { + void onStockUpdatedEvent(); +} diff --git a/Workspace/client/src/main/java/com/pqt/client/gui/ressources/strings/GUIStringTool.java b/Workspace/client/src/main/java/com/pqt/client/gui/ressources/strings/GUIStringTool.java index aa9574d9..c70207f8 100644 --- a/Workspace/client/src/main/java/com/pqt/client/gui/ressources/strings/GUIStringTool.java +++ b/Workspace/client/src/main/java/com/pqt/client/gui/ressources/strings/GUIStringTool.java @@ -34,7 +34,12 @@ public class GUIStringTool { } public static IObjectStringRenderer getProductStringRenderer(){ - return product->String.format("%s - %.2f€ (%s)", product.getName(), product.getPrice(), (product.getAmountRemaining()>=30?"30+": Integer.toString(product.getAmountRemaining()))); + return product->{ + if(product!=null) + return String.format("%s - %.2f€ (%s)", product.getName(), product.getPrice(), (product.getAmountRemaining()>=30?"30+": Integer.toString(product.getAmountRemaining()))); + else + return "null"; + }; } public static String getCommandComposerTitleTitle() { @@ -165,6 +170,50 @@ public class GUIStringTool { public static String getSaleGuiModuleName() { return "Vente"; } + + public static String getAddButtonLabel() { + return "Ajouter"; + } + + public static String getDetailButtonLabel() { + return "Détail"; + } + + public static String getRemoveButtonLabel() { + return "Supprimer"; + } + + public static String getRefreshButtonLabel() { + return "Rafraichir"; + } + + public static String getProductNameColumnHeader() { + return "Nom"; + } + + public static String getProductCategoryColumnHeader() { + return "Catégorie"; + } + + public static String getProductAmountRemainingColumnHeader() { + return "Stock"; + } + + public static String getProductAmountSoldColumnHeader() { + return "Vendu"; + } + + public static String getProductPriceColumnHeader() { + return "Prix"; + } + + public static String getProductIsSellableColumnHeader() { + return "Vendable"; + } + + public static IObjectStringRenderer getBooleanRenderer() { + return bool->bool?"Oui":"Non"; + } }