From d8e3440fb748cc316f2a4c040932c9a68fe0f20e Mon Sep 17 00:00:00 2001 From: besson Date: Sun, 15 Jan 2023 18:25:45 +0100 Subject: [PATCH] =?UTF-8?q?Transf=C3=A9rer=20les=20fichiers=20vers=20'src/?= =?UTF-8?q?JsonInspector'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/JsonInspector/Frame.java | 94 +++++++++------------- src/JsonInspector/GraphicFile.java | 61 +------------- src/JsonInspector/JsonSyntaxException.java | 9 +++ 3 files changed, 48 insertions(+), 116 deletions(-) create mode 100644 src/JsonInspector/JsonSyntaxException.java diff --git a/src/JsonInspector/Frame.java b/src/JsonInspector/Frame.java index ec9b4d3..cf203f0 100644 --- a/src/JsonInspector/Frame.java +++ b/src/JsonInspector/Frame.java @@ -2,40 +2,27 @@ package JsonInspector; import javax.swing.*; import java.awt.*; +import java.io.FileNotFoundException; import java.net.MalformedURLException; import java.net.URL; public class Frame extends JFrame { private static final Dimension DEFAULT_FRAME_SIZE = new Dimension(800, 600); private static final Dimension MINIMUM_FRAME_SIZE = new Dimension(600, 500); - private static final String DEFAULT_LINK = "https://gbfs.citibikenyc.com/gbfs/en/station_information.json"; private final CardLayout cards = new CardLayout(); private boolean showTab = true; private JPanel secondCard; private GraphicFile file; - private Node node; private Tree tree; public Frame() { super("Inspecteur JSON"); - tree = new Tree(""); init(); this.setVisible(true); } - public Frame(Node node) { - super("Inspecteur JSON"); - this.node = node; - init(); - secondCard = secondCard(); - this.add(secondCard); - cards.last(this.getContentPane()); - this.setVisible(true); - } - - private void init() { this.setSize(DEFAULT_FRAME_SIZE); this.setDefaultCloseOperation(EXIT_ON_CLOSE); @@ -50,7 +37,7 @@ public class Frame extends JFrame { private JPanel firstCard() { GridBagLayout layout = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); - JTextField textField = new JTextField(DEFAULT_LINK, 30); + JTextField textField = new JTextField("file:C:\\Users\\Elève\\Desktop\\temp\\jason.json", 30); JButton button = new JButton("Valider"); button.addActionListener((event) -> validationAction(textField)); JPanel panel = new JPanel(); @@ -78,53 +65,31 @@ public class Frame extends JFrame { private JPanel secondCard() { file = new GraphicFile(this, tree); - JButton showButton = new JButton(); - JButton backButton = new JButton("Retour"); - JPanel mainPanel = new JPanel(), southPanel = new JPanel(); - JScrollPane scroll = new JScrollPane(); - mainPanel.setLayout(new BorderLayout()); - - if (showTab) { - showButton.setText("Tout déplier"); - } else { - showButton.setText("Tout replier"); - } - - southPanel.setBackground(Parameters.IHM_COLOR); - southPanel.add(backButton); - backButton.addActionListener((event) -> backAction(mainPanel)); - southPanel.add(showButton); - showButton.addActionListener((event) -> showAction()); - southPanel.add(new JButton("convertir en PHP")); - - mainPanel.add(file); - mainPanel.add(southPanel, BorderLayout.SOUTH); - mainPanel.add(scroll); - - scroll.setViewportView(file); - return mainPanel; + return initSecondCard(file); } private JPanel secondCard(GraphicFile file) { this.file = file; - JButton showButton = new JButton(); + return initSecondCard(file); + } + + + private JPanel initSecondCard(GraphicFile file) { + JButton unfoldButton = new JButton("Tout déplier"); + JButton retreatButton = new JButton("Tout replier"); JButton backButton = new JButton("Retour"); JPanel mainPanel = new JPanel(), southPanel = new JPanel(); JScrollPane scroll = new JScrollPane(); mainPanel.setLayout(new BorderLayout()); - if (showTab) { - showButton.setText("Tout déplier"); - } else { - showButton.setText("Tout replier"); - } - southPanel.setBackground(Parameters.IHM_COLOR); southPanel.add(backButton); backButton.addActionListener((event) -> backAction(mainPanel)); - southPanel.add(showButton); - showButton.addActionListener((event) -> showAction()); + southPanel.add(unfoldButton); + unfoldButton.addActionListener((event) -> unfoldAction()); + southPanel.add(retreatButton); + retreatButton.addActionListener((event) -> retreatAction()); southPanel.add(new JButton("convertir en PHP")); mainPanel.add(file); @@ -136,15 +101,14 @@ public class Frame extends JFrame { } - private void showAction() { - if (showTab) { - showTab = false; - file.showAll(); - } else { - showTab = true; - file.retreatAll(); - } + private void unfoldAction() { + file.showAll(); + repaintFile(); + } + + private void retreatAction() { + file.retreatAll(); repaintFile(); } @@ -161,11 +125,25 @@ public class Frame extends JFrame { private void validationAction(JTextField field) { try { URL url = new URL(field.getText()); + String file = Main.getJsonInOneLine(url); + + if (file.length() <= 2) { + throw new FileNotFoundException(); + } + + tree = new Tree(file); secondCard = secondCard(); this.add(secondCard); cards.last(this.getContentPane()); + } catch (MalformedURLException e) { - JOptionPane.showMessageDialog(this, "Invalid URL", "Error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(this, "URL invalide", "Error", JOptionPane.ERROR_MESSAGE); + + } catch (JsonSyntaxException j) { + JOptionPane.showMessageDialog(this, "Erreur de syntax dans le fichier", "Error", JOptionPane.ERROR_MESSAGE); + + } catch (FileNotFoundException f) { + JOptionPane.showMessageDialog(this, "Impossible trouver le fichier", "Error", JOptionPane.ERROR_MESSAGE); } } diff --git a/src/JsonInspector/GraphicFile.java b/src/JsonInspector/GraphicFile.java index e00cabd..f6a84f0 100644 --- a/src/JsonInspector/GraphicFile.java +++ b/src/JsonInspector/GraphicFile.java @@ -14,11 +14,10 @@ public class GraphicFile extends JPanel { public GraphicFile(Frame frame, Tree tree) { super(); - //firstNode = tree.getFirstNode(); + firstNode = tree.getFirstNode(); init(); lines = new ArrayList<>(); this.frame = frame; - firstNode = createTree(); createFileRecursive(firstNode, 0, false); //displayAllLines(); displayLines(); @@ -201,13 +200,6 @@ public class GraphicFile extends JPanel { } - private void displayAllLines() { - for (int i = 0; i < lines.size(); i++) { - displayOneLine(i); - } - } - - private void displayLines() { boolean inArrayObject = false, array, object; Node openedArrayObject = lines.get(0).getNode(); @@ -224,9 +216,9 @@ public class GraphicFile extends JPanel { openedArrayObject = lines.get(i).getNode(); if (openedArrayObject.isArray()) { - displayOneHidedLine(i, "...]"); + displayOneHidedLine(i, Parameters.ARRAY_CLOSING); } else { - displayOneHidedLine(i, "...}"); + displayOneHidedLine(i, Parameters.OBJECT_ELEMENT_CLOSING); } //Sinon affiche la ligne normalement @@ -289,51 +281,4 @@ public class GraphicFile extends JPanel { public ArrayList getLines() { return lines; } - - - private Node createTree() { - Node beginning = new Node("", Type.ELEMENT); - Node d = new Node("d", Type.OBJECT); - Node result = new Node("result", Type.ARRAY); - Node metadata = new Node("", Type.ELEMENT); - Node type = new Node("type", Type.PAIR); - Node e = new Node("e", Type.ARRAY); - Node key = new Node("key", Type.PAIR); - Node key2 = new Node("key2", Type.PAIR); - Node key3 = new Node("key3", Type.PAIR); - Node key4 = new Node("key4", Type.PAIR); - Node object = new Node("object", Type.OBJECT); - Node object2 = new Node("object2", Type.OBJECT); - Node array = new Node("array", Type.ARRAY); - Integer number = 12; - String value1 = "EmployeeDetails.Employe", value2 = "E12012", value3 = "35", value4 = "string"; - - beginning.add(d); - beginning.add(e); - d.add(result); - result.add(metadata); - metadata.add(type); - result.add(true); - result.add(value1); - result.add(value2); - result.add(value3); - key.add(number); - beginning.add(key); - beginning.add(key2); - beginning.add(object); - beginning.add(object2); - object2.add(key3); - object2.add(key4); - beginning.add(array); - - key2.add(value4); - key3.add(0.8); - key4.add(false); - array.add("str"); - array.add(1790); - - System.out.println(Tree.printTree(beginning, 0)); - - return beginning; - } } diff --git a/src/JsonInspector/JsonSyntaxException.java b/src/JsonInspector/JsonSyntaxException.java new file mode 100644 index 0000000..46c274c --- /dev/null +++ b/src/JsonInspector/JsonSyntaxException.java @@ -0,0 +1,9 @@ +package JsonInspector; + +public class JsonSyntaxException extends Throwable { + private static final String MESSAGE = "Syntax error in JSON file"; + + public JsonSyntaxException() { + super(MESSAGE); + } +}