From 700a9511b4cff43c49e39e1aa02d7d0f11ed2078 Mon Sep 17 00:00:00 2001 From: besson Date: Sat, 14 Jan 2023 16:38:37 +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/ArrayObjectListener.java | 38 +++++++++ src/JsonInspector/Frame.java | 66 ++++++++++++--- src/JsonInspector/GraphicFile.java | 93 +++++++++++++++------- src/JsonInspector/Line.java | 40 ++++++++-- src/JsonInspector/MyJLabel.java | 4 +- 5 files changed, 193 insertions(+), 48 deletions(-) create mode 100644 src/JsonInspector/ArrayObjectListener.java diff --git a/src/JsonInspector/ArrayObjectListener.java b/src/JsonInspector/ArrayObjectListener.java new file mode 100644 index 0000000..57b485d --- /dev/null +++ b/src/JsonInspector/ArrayObjectListener.java @@ -0,0 +1,38 @@ +package JsonInspector; + +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; + +public class ArrayObjectListener implements MouseListener { + private final Line line; + private final Frame frame; + + + public ArrayObjectListener(Line line, Frame frame) { + this.line = line; + this.frame = frame; + } + + @Override + public void mouseClicked(MouseEvent e) { + System.out.println("Clique"); + if (line.isShow()) { + line.retreat(); + } else { + line.unfold(); + } + frame.repaintFile(); + } + + @Override + public void mousePressed(MouseEvent e) {} + + @Override + public void mouseReleased(MouseEvent e) {} + + @Override + public void mouseEntered(MouseEvent e) {} + + @Override + public void mouseExited(MouseEvent e) {} +} diff --git a/src/JsonInspector/Frame.java b/src/JsonInspector/Frame.java index ec32d2b..137ba92 100644 --- a/src/JsonInspector/Frame.java +++ b/src/JsonInspector/Frame.java @@ -11,6 +11,7 @@ public class Frame extends JFrame { 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; @@ -26,7 +27,8 @@ public class Frame extends JFrame { super("Inspecteur JSON"); this.node = node; init(); - this.add(secondCard()); + secondCard = secondCard(); + this.add(secondCard); cards.last(this.getContentPane()); this.setVisible(true); } @@ -60,7 +62,9 @@ public class Frame extends JFrame { gbc.gridx = 1; panel.add(textField, gbc); - gbc.gridy = 1; + gbc.gridx = 0; + gbc.gridy = 1; + gbc.gridwidth = 2; panel.add(button, gbc); panel.setBackground(Parameters.IHM_COLOR); @@ -68,7 +72,7 @@ public class Frame extends JFrame { } private JPanel secondCard() { - file = new GraphicFile(tree, showTab); + file = new GraphicFile(this, tree); JButton showButton = new JButton(); JButton backButton = new JButton("Retour"); JPanel mainPanel = new JPanel(), southPanel = new JPanel(); @@ -85,7 +89,7 @@ public class Frame extends JFrame { southPanel.add(backButton); backButton.addActionListener((event) -> backAction(mainPanel)); southPanel.add(showButton); - showButton.addActionListener((event) -> showAction(mainPanel)); + showButton.addActionListener((event) -> showAction()); southPanel.add(new JButton("convertir en PHP")); mainPanel.add(file); @@ -97,20 +101,54 @@ public class Frame extends JFrame { } - private void showAction(JPanel panel) { + private JPanel secondCard(GraphicFile file) { + this.file = file; + 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) { - showTab = false; - repaintFile(panel); + showButton.setText("Tout déplier"); } else { - showTab = true; - repaintFile(panel); + 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; } - private void repaintFile(JPanel panel) { - this.remove(panel); - this.add(secondCard()); + private void showAction() { + if (showTab) { + showTab = false; + file.showAll(); + } else { + showTab = true; + file.retreatAll(); + } + + repaintFile(); + } + + + public void repaintFile() { + file = new GraphicFile(this, file.getLines()); + this.remove(secondCard); + secondCard = secondCard(file); + this.add(secondCard); cards.last(this.getContentPane()); } @@ -118,13 +156,15 @@ public class Frame extends JFrame { private void validationAction(JTextField field) { try { URL url = new URL(field.getText()); - this.add(secondCard()); + secondCard = secondCard(); + this.add(secondCard); cards.last(this.getContentPane()); } catch (MalformedURLException e) { JOptionPane.showMessageDialog(this, "Invalid URL", "Error", JOptionPane.ERROR_MESSAGE); } } + private void backAction(JPanel panel) { this.remove(panel); cards.first(this.getContentPane()); diff --git a/src/JsonInspector/GraphicFile.java b/src/JsonInspector/GraphicFile.java index 6437633..4e5a7f4 100644 --- a/src/JsonInspector/GraphicFile.java +++ b/src/JsonInspector/GraphicFile.java @@ -6,28 +6,39 @@ import java.util.ArrayList; public class GraphicFile extends JPanel { private final GridBagConstraints gbc = new GridBagConstraints(); - private final ArrayList lines = new ArrayList<>(); private final JPanel alignementPanel = new JPanel(); - private final Node firstNode; + private final Frame frame; + private ArrayList lines = new ArrayList<>(); + private Node firstNode; - public GraphicFile(Tree tree, boolean hided) { + public GraphicFile(Frame frame, Tree tree) { super(); //firstNode = tree.getFirstNode(); - this.setBackground(Parameters.BACKGROUND_COLOR); - this.setLayout(new FlowLayout(FlowLayout.LEFT)); + init(); + this.frame = frame; firstNode = createTree(); createFileRecursive(firstNode, 0, false); + displayLines(); + } + + + public GraphicFile(Frame frame, ArrayList lines) { + super(); + init(); + this.frame = frame; + this.lines = lines; + displayLines(); + } + + + private void init() { + this.setBackground(Parameters.BACKGROUND_COLOR); + this.setLayout(new FlowLayout(FlowLayout.LEFT)); gbc.fill = GridBagConstraints.BOTH; gbc.anchor = GridBagConstraints.WEST; alignementPanel.setLayout(new GridBagLayout()); this.add(alignementPanel); - - if (hided) { - displayHidedLines(); - } else { - displayAllLines(); - } } @@ -74,6 +85,10 @@ public class GraphicFile extends JPanel { Line line = new Line(node, depth); line.add(indentation); + if (0 < depth && 0 < node.getSize()) { + line.retreat(); + } + if (node.getType() == Type.ELEMENT) { line.add("{"); } else { @@ -90,6 +105,7 @@ public class GraphicFile extends JPanel { lines.add(line); } else { + line.addMouseListener(new ArrayObjectListener(line, frame)); lines.add(line); callNextNodes(node, depth); Line endLine = new Line(node, indentation + "}", depth); @@ -107,6 +123,10 @@ public class GraphicFile extends JPanel { Line line = new Line(node, depth); line.add(indentation); + if (0 < depth && 0 < node.getSize()) { + line.retreat(); + } + line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR); line.add(": ["); @@ -118,6 +138,7 @@ public class GraphicFile extends JPanel { } lines.add(line); } else { + line.addMouseListener(new ArrayObjectListener(line, frame)); lines.add(line); for (int i = 0; i < node.getSize(); i++) { @@ -175,31 +196,24 @@ public class GraphicFile extends JPanel { } - private void displayAllLines() { - for (int i = 0; i < lines.size(); i++) { - displayOneLine(i); - } - } - - - private void displayHidedLines() { - boolean inArray = false, array, object; + private void displayLines() { + boolean inArrayObject = false, array, object; Line openedArrayObject = lines.get(0); for (int i = 0; i < lines.size(); i++) { - if (!inArray) { + if (!inArrayObject) { array = lines.get(i).getNode().getType() == Type.ARRAY; object = lines.get(i).getNode().getType() == Type.OBJECT; - // Vérifie si le noeud est du type ARRAY ou du type OBJECT - if ((array || object) && 0 < lines.get(i).getDepth()) { - inArray = true; + // Vérifie si le noeud est du type ARRAY ou du type OBJECT et s'il doit être affiché + if ((array || object) && !lines.get(i).isShow()) { + inArrayObject = true; openedArrayObject = lines.get(i); if (openedArrayObject.getNode().getType() == Type.ARRAY) { - displayOneHidedLine(i, "... ]"); + displayOneHidedLine(i, "...]"); } else { - displayOneHidedLine(i, "... }"); + displayOneHidedLine(i, "...}"); } //Sinon affiche la ligne normalement @@ -208,7 +222,7 @@ public class GraphicFile extends JPanel { } } else if (lines.get(i).getNode().equals(openedArrayObject.getNode())) { - inArray = false; + inArrayObject = false; } } } @@ -231,6 +245,30 @@ public class GraphicFile extends JPanel { } + public void showAll() { + for (Line line : lines) { + if (!line.isShow()) { + line.removeClosingLabel(); + } + line.unfold(); + } + } + + + public void retreatAll() { + for (Line line : lines) { + if (0 < line.getDepth() && 0 < line.getNode().getSize()) { + line.retreat(); + } + } + } + + + public ArrayList getLines() { + return lines; + } + + private Node createTree() { Node beginning = new Node("", Type.ELEMENT); Node d = new Node("d", Type.OBJECT); @@ -249,6 +287,7 @@ public class GraphicFile extends JPanel { d.add(result); result.add(metadata); metadata.add(type); + result.add(true); result.add(value1); result.add(value2); result.add(value3); diff --git a/src/JsonInspector/Line.java b/src/JsonInspector/Line.java index 3b640ce..8f2f308 100644 --- a/src/JsonInspector/Line.java +++ b/src/JsonInspector/Line.java @@ -3,10 +3,10 @@ package JsonInspector; import java.awt.*; public class Line extends MyJPanel { - //Le nœud qui est représenté par cet objet Line - public boolean show = true; - private int depth; - private Node node; + private boolean show = true; + private final int depth; + private final Node node; + private MyJLabel lastElement; public Line(Node node, int depth) { @@ -33,12 +33,14 @@ public class Line extends MyJPanel { public void add(String string) { - this.add(new MyJLabel(string)); + lastElement = new MyJLabel(string); + this.add(lastElement); } public void add(String string, Color color) { - this.add(new MyJLabel(string, color)); + lastElement = new MyJLabel(string, color); + this.add(lastElement); } @@ -50,4 +52,30 @@ public class Line extends MyJPanel { public int getDepth() { return depth; } + + + public boolean isShow() { + return show; + } + + + public void unfold() { + show = true; + } + + + public void retreat() { + show = false; + } + + + public void removeClosingLabel() { + try { + if (lastElement.getText().equals("...]") || lastElement.getText().equals("...}")) { + this.remove(lastElement); + } + } catch (NullPointerException e) { + //System.out.println("La ligne est vide"); + } + } } diff --git a/src/JsonInspector/MyJLabel.java b/src/JsonInspector/MyJLabel.java index ab265f0..9618d3a 100644 --- a/src/JsonInspector/MyJLabel.java +++ b/src/JsonInspector/MyJLabel.java @@ -6,13 +6,13 @@ import java.awt.*; public class MyJLabel extends JLabel { public MyJLabel(String text, Color color) { super(text); - this.setFont(Parameters.FILE_FONT); + //this.setFont(Parameters.FILE_FONT); this.setForeground(color); } public MyJLabel(String text) { super(text); - this.setFont(Parameters.FILE_FONT); + //this.setFont(Parameters.FILE_FONT); this.setForeground(Parameters.DEFAULT_TEXT_COLOR); } }