From b5d3a3cad7c51d83b3c49a718482abbdddf612f5 Mon Sep 17 00:00:00 2001 From: FyloZ Date: Mon, 30 May 2022 10:23:53 -0400 Subject: [PATCH] Add squelette --- .idea/uiDesigner.xml | 124 ++++++++++++++++++ src/Simulation.java | 15 +++ src/TestMain.java | 12 -- ...Iterator.java => ElementListIterator.java} | 4 +- ...NodeList.java => IterableElementList.java} | 6 +- src/configuration/XmlConfigurationParser.java | 12 +- src/view/Environment.java | 19 +++ src/view/MainPanel.java | 23 ++++ src/view/MainWindow.java | 44 +++++++ src/view/StrategyPanel.java | 57 ++++++++ src/view/StrategyWindow.java | 24 ++++ src/view/WindowMenu.java | 99 ++++++++++++++ 12 files changed, 416 insertions(+), 23 deletions(-) create mode 100644 .idea/uiDesigner.xml create mode 100644 src/Simulation.java delete mode 100644 src/TestMain.java rename src/configuration/{NodeListElementIterator.java => ElementListIterator.java} (87%) rename src/configuration/{IterableNodeList.java => IterableElementList.java} (77%) create mode 100644 src/view/Environment.java create mode 100644 src/view/MainPanel.java create mode 100644 src/view/MainWindow.java create mode 100644 src/view/StrategyPanel.java create mode 100644 src/view/StrategyWindow.java create mode 100644 src/view/WindowMenu.java diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Simulation.java b/src/Simulation.java new file mode 100644 index 0000000..9e16cb8 --- /dev/null +++ b/src/Simulation.java @@ -0,0 +1,15 @@ +import view.Environment; +import view.MainWindow; + +public class Simulation { + /** + * Cette classe représente l'application dans son ensemble. + */ + public static void main(String[] args) { + Environment environment = new Environment(); + MainWindow window = new MainWindow(); + + environment.addPropertyChangeListener(window); + environment.execute(); + } +} diff --git a/src/TestMain.java b/src/TestMain.java deleted file mode 100644 index 8c0bb96..0000000 --- a/src/TestMain.java +++ /dev/null @@ -1,12 +0,0 @@ -import configuration.ConfigurationParser; -import configuration.ConfigurationParsingException; -import configuration.XmlConfigurationParser; - -public class TestMain { - public static void main(String[] args) throws ConfigurationParsingException { - String configurationFilePath = "/home/william/Dev/Projects/Uni/Log121/TP01/squelette/src/ressources/configuration.xml"; - - ConfigurationParser parser = new XmlConfigurationParser(); - parser.parseConfiguration(configurationFilePath); - } -} diff --git a/src/configuration/NodeListElementIterator.java b/src/configuration/ElementListIterator.java similarity index 87% rename from src/configuration/NodeListElementIterator.java rename to src/configuration/ElementListIterator.java index 9859829..bf61e06 100644 --- a/src/configuration/NodeListElementIterator.java +++ b/src/configuration/ElementListIterator.java @@ -6,11 +6,11 @@ import org.w3c.dom.NodeList; import java.util.Iterator; -public class NodeListElementIterator implements Iterator { +public class ElementListIterator implements Iterator { private final NodeList nodes; private int position = 0; - public NodeListElementIterator(NodeList nodes) { + public ElementListIterator(NodeList nodes) { this.nodes = nodes; } diff --git a/src/configuration/IterableNodeList.java b/src/configuration/IterableElementList.java similarity index 77% rename from src/configuration/IterableNodeList.java rename to src/configuration/IterableElementList.java index 033e56a..cbf0851 100644 --- a/src/configuration/IterableNodeList.java +++ b/src/configuration/IterableElementList.java @@ -7,16 +7,16 @@ import java.util.Iterator; import java.util.Spliterator; import java.util.function.Consumer; -public class IterableNodeList implements Iterable { +public class IterableElementList implements Iterable { private final NodeList nodes; - public IterableNodeList(NodeList nodes) { + public IterableElementList(NodeList nodes) { this.nodes = nodes; } @Override public Iterator iterator() { - return new NodeListElementIterator(nodes); + return new ElementListIterator(nodes); } @Override diff --git a/src/configuration/XmlConfigurationParser.java b/src/configuration/XmlConfigurationParser.java index 192f390..88f3da3 100644 --- a/src/configuration/XmlConfigurationParser.java +++ b/src/configuration/XmlConfigurationParser.java @@ -63,7 +63,7 @@ public class XmlConfigurationParser implements ConfigurationParser { Node metadataNode = document.getElementsByTagName(TAG_METADATA).item(0); Map metadata = new HashMap<>(); - for (Element elem : new IterableNodeList(metadataNode.getChildNodes())) { + for (Element elem : new IterableElementList(metadataNode.getChildNodes())) { String buildingType = elem.getAttribute(ATTRIBUTE_TYPE); metadata.put(buildingType, parseBuildingMetadata(elem)); @@ -86,7 +86,7 @@ public class XmlConfigurationParser implements ConfigurationParser { FactoryOutput output = null; int productionInterval = -1; - for (Element elem : new IterableNodeList(factoryElement.getChildNodes())) { + for (Element elem : new IterableElementList(factoryElement.getChildNodes())) { switch (elem.getNodeName()) { case TAG_ICONS -> iconsPaths = parseBuildingIcons(elem); case TAG_INPUT -> { @@ -105,7 +105,7 @@ public class XmlConfigurationParser implements ConfigurationParser { Map iconsPaths = null; WarehouseInput input = null; - for (Element elem : new IterableNodeList(warehouseElement.getChildNodes())) { + for (Element elem : new IterableElementList(warehouseElement.getChildNodes())) { switch (elem.getNodeName()) { case TAG_ICONS -> iconsPaths = parseBuildingIcons(elem); case TAG_INPUT -> input = parseWarehouseInput(elem); @@ -118,7 +118,7 @@ public class XmlConfigurationParser implements ConfigurationParser { private Map parseBuildingIcons(Element iconsElement) { Map iconsPaths = new HashMap<>(); - for (Element elem : new IterableNodeList(iconsElement.getChildNodes())) { + for (Element elem : new IterableElementList(iconsElement.getChildNodes())) { BuildingState state = BuildingState.getFromTypeName(elem.getAttribute(ATTRIBUTE_TYPE)); String path = elem.getAttribute(ATTRIBUTE_PATH); @@ -154,7 +154,7 @@ public class XmlConfigurationParser implements ConfigurationParser { Collection buildings = new ArrayList<>(); Collection routes = null; - for (Element elem : new IterableNodeList(dataNode.getChildNodes())) { + for (Element elem : new IterableElementList(dataNode.getChildNodes())) { switch (elem.getNodeName()) { case TAG_BUILDING -> buildings.add(parseBuilding(elem)); case TAG_ROUTES -> routes = parseRoutes(elem); @@ -178,7 +178,7 @@ public class XmlConfigurationParser implements ConfigurationParser { private Collection parseRoutes(Element routesElement) { Collection routes = new ArrayList<>(); - for (Element elem : new IterableNodeList(routesElement.getChildNodes())) { + for (Element elem : new IterableElementList(routesElement.getChildNodes())) { int from = Integer.parseInt(elem.getAttribute(ATTRIBUTE_TO)); int to = Integer.parseInt(elem.getAttribute(ATTRIBUTE_FROM)); diff --git a/src/view/Environment.java b/src/view/Environment.java new file mode 100644 index 0000000..6759e85 --- /dev/null +++ b/src/view/Environment.java @@ -0,0 +1,19 @@ +package view; + +import javax.swing.*; + +public class Environment extends SwingWorker { + private static final int DELAY = 100; + + private boolean active = true; + + @Override + protected Object doInBackground() throws Exception { + while (active) { + Thread.sleep(DELAY); + + firePropertyChange("TEST", null, "Ceci est un test"); + } + return null; + } +} diff --git a/src/view/MainPanel.java b/src/view/MainPanel.java new file mode 100644 index 0000000..2558700 --- /dev/null +++ b/src/view/MainPanel.java @@ -0,0 +1,23 @@ +package view; + +import javax.swing.*; +import java.awt.*; +import java.io.Serial; + +public class MainPanel extends JPanel { + @Serial + private static final long serialVersionUID = 1L; + + private Point position = new Point(0, 0); + private Point speed = new Point(1, 1); + private int size = 32; + + @Override + public void paint(Graphics g) { + super.paint(g); + + // On ajoute à la position le delta x et y de la vitesse + position.translate(speed.x, speed.y); + g.fillRect(position.x, position.y, size, size); + } +} diff --git a/src/view/MainWindow.java b/src/view/MainWindow.java new file mode 100644 index 0000000..c23a3f2 --- /dev/null +++ b/src/view/MainWindow.java @@ -0,0 +1,44 @@ +package view; + +import javax.swing.*; +import java.awt.*; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.io.Serial; + +public class MainWindow extends JFrame implements PropertyChangeListener { + @Serial + private static final long serialVersionUID = 1L; + private static final String WINDOW_TITLE = "Laboratoire 1 : LOG121 - Simulation"; + private static final Dimension DIMENSION = new Dimension(700, 700); + + public MainWindow() { + MainPanel mainPanel = new MainPanel(); + WindowMenu menu = new WindowMenu(); + + add(mainPanel); + add(menu, BorderLayout.NORTH); + + // Faire en sorte que le X de la fenêtre ferme la fenêtre + setDefaultCloseOperation(EXIT_ON_CLOSE); + setTitle(WINDOW_TITLE); + setSize(DIMENSION); + + // Rendre la fenêtre visible + setVisible(true); + + // Mettre la fenêtre au centre de l'écran + setLocationRelativeTo(null); + + // Empêcher la redimension de la fenêtre + setResizable(false); + } + + @Override + public void propertyChange(PropertyChangeEvent event) { + if (event.getPropertyName().equals("TEST")) { + repaint(); + System.out.println(event.getNewValue()); + } + } +} diff --git a/src/view/StrategyPanel.java b/src/view/StrategyPanel.java new file mode 100644 index 0000000..0bb2cc1 --- /dev/null +++ b/src/view/StrategyPanel.java @@ -0,0 +1,57 @@ +package view; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.io.Serial; +import java.util.Enumeration; + +public class StrategyPanel extends JPanel { + @Serial + private static final long serialVersionUID = 1L; + + public StrategyPanel() { + JRadioButton strategy1 = new JRadioButton("Stratégie 1"); + JRadioButton strategy2 = new JRadioButton("Stratégie 2"); + + ButtonGroup buttonGroup = new ButtonGroup(); + buttonGroup.add(strategy1); + buttonGroup.add(strategy2); + + JButton confirmButton = new JButton("Confirmer"); + confirmButton.addActionListener((ActionEvent e) -> { + // TODO - Appeler la bonne stratégie + System.out.println(getSelectedButtonText(buttonGroup)); + // Fermer la fenêtre du component + SwingUtilities.getWindowAncestor((Component) e.getSource()).dispose(); + }); + + JButton cancelButton = new JButton("Annuler"); + cancelButton.addActionListener((ActionEvent e) -> { + // Fermer la fenêtre du component + SwingUtilities.getWindowAncestor((Component) e.getSource()).dispose(); + }); + + add(strategy1); + add(strategy2); + add(confirmButton); + add(cancelButton); + } + + /** + * Retourne le bouton sélectionné dans un groupe de boutons. + * + * @param buttonGroup + * @return + */ + public String getSelectedButtonText(ButtonGroup buttonGroup) { + for (Enumeration buttons = buttonGroup.getElements(); buttons.hasMoreElements(); ) { + AbstractButton button = buttons.nextElement(); + if (button.isSelected()) { + return button.getText(); + } + } + + return null; + } +} diff --git a/src/view/StrategyWindow.java b/src/view/StrategyWindow.java new file mode 100644 index 0000000..3eedb99 --- /dev/null +++ b/src/view/StrategyWindow.java @@ -0,0 +1,24 @@ +package view; + +import javax.swing.*; +import java.awt.*; +import java.io.Serial; + +public class StrategyWindow extends JFrame { + @Serial + private static final long serialVersionUID = 1L; + private static final String WINDOW_TITLE = "Sélectionnez votre stratégie de vente"; + private static final Dimension DIMENSION = new Dimension(250, 100); + + private final StrategyPanel strategyPanel = new StrategyPanel(); + + public StrategyWindow() { + add(strategyPanel); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + setTitle(WINDOW_TITLE); + setSize(DIMENSION); + setVisible(true); + setLocationRelativeTo(null); + setResizable(false); + } +} diff --git a/src/view/WindowMenu.java b/src/view/WindowMenu.java new file mode 100644 index 0000000..9791372 --- /dev/null +++ b/src/view/WindowMenu.java @@ -0,0 +1,99 @@ +package view; + +import javax.swing.*; +import javax.swing.filechooser.FileNameExtensionFilter; +import javax.swing.filechooser.FileSystemView; +import java.awt.event.ActionEvent; +import java.io.File; +import java.io.Serial; + +public class WindowMenu extends JMenuBar { + @Serial + private static final long serialVersionUID = 1L; + private static final String MENU_FILE_TITLE = "Fichier"; + private static final String MENU_FILE_LOAD = "Charger"; + private static final String MENU_FILE_EXIT = "Quitter"; + private static final String MENU_SIMULATION_TITLE = "Simulation"; + private static final String MENU_SIMULATION_CHOOSE = "Choisir"; + private static final String MENU_HELP_TITLE = "Aide"; + private static final String MENU_HELP_ABOUT = "À propos de..."; + + public WindowMenu() { + addFileMenu(); + addSimulationMenu(); + addHelpMenu(); + } + + /** + * Créer le menu de fichier. + */ + private void addFileMenu() { + JMenu fileMenu = new JMenu(MENU_FILE_TITLE); + JMenuItem loadMenu = new JMenuItem(MENU_FILE_LOAD); + JMenuItem exitMenu = new JMenuItem(MENU_FILE_EXIT); + + loadMenu.addActionListener((ActionEvent e) -> { + JFileChooser fileChooser = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()); + fileChooser.setDialogTitle("Sélectionnez un fichier de configuration"); + fileChooser.setAcceptAllFileFilterUsed(false); + // Créer un filtre + FileNameExtensionFilter filter = new FileNameExtensionFilter(".xml", "xml"); + fileChooser.addChoosableFileFilter(filter); + + int returnValue = fileChooser.showOpenDialog(null); + if (returnValue == JFileChooser.APPROVE_OPTION) { + // TODO - Parser le fichier XML sélectionné + File selectedFile = fileChooser.getSelectedFile(); + System.out.println(selectedFile.getAbsoluteFile()); + } + }); + + exitMenu.addActionListener((ActionEvent e) -> { + System.exit(0); + }); + + fileMenu.add(loadMenu); + fileMenu.add(exitMenu); + + add(fileMenu); + } + + /** + * Créer le menu de simulation. + */ + private void addSimulationMenu() { + JMenu simulationMenu = new JMenu(MENU_SIMULATION_TITLE); + JMenuItem chooseMenu = new JMenuItem(MENU_SIMULATION_CHOOSE); + + chooseMenu.addActionListener((ActionEvent e) -> { + // Ouvrir la fenêtre de sélection + // TODO - Récupérer la bonne stratégie de vente + new StrategyWindow(); + }); + + simulationMenu.add(chooseMenu); + + add(simulationMenu); + } + + /** + * Créer le menu aide. + */ + private void addHelpMenu() { + JMenu helpMenu = new JMenu(MENU_HELP_TITLE); + JMenuItem aboutMenu = new JMenuItem(MENU_HELP_ABOUT); + + aboutMenu.addActionListener((ActionEvent e) -> { + JOptionPane.showMessageDialog(null, + "

Application simulation une chaine de production d'avions.


" + + "

©   2017   Ghizlane El Boussaidi

" + + "

©   2017   Dany Boisvert

" + + "

©   2017   Vincent Mattard


" + + "

École de technologie supérieure

"); + }); + + helpMenu.add(aboutMenu); + + add(helpMenu); + } +}