This commit is contained in:
pro.boooooo
2023-01-15 20:37:28 +01:00
parent 068f0f3b50
commit 3175d93380
16 changed files with 168 additions and 179 deletions

View File

@@ -0,0 +1,40 @@
package fr.sae.JSonInspector.Graphics;
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) {
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) {
}
}

View File

@@ -0,0 +1,149 @@
package fr.sae.JSonInspector.Graphics;
import fr.sae.JSonInspector.Exception.JsonSyntaxException;
import fr.sae.JSonInspector.Main;
import fr.sae.JSonInspector.Settings.Parameters;
import fr.sae.JSonInspector.Storage.Tree;
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 final CardLayout cards = new CardLayout();
private boolean showTab = true;
private JPanel secondCard;
private GraphicFile file;
private Tree tree;
public Frame() {
super("Inspecteur JSON");
init();
this.setVisible(true);
}
private void init() {
this.setSize(DEFAULT_FRAME_SIZE);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setMinimumSize(MINIMUM_FRAME_SIZE);
this.setLayout(cards);
this.add(firstCard());
// this.add(secondCard());
cards.first(this.getContentPane());
}
private JPanel firstCard() {
GridBagLayout layout = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
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();
panel.setLayout(layout);
gbc.insets = new Insets(10, 10, 10, 10);
gbc.gridx = 0;
gbc.gridy = 0;
JLabel label = new JLabel("URL :");
label.setForeground(Parameters.DEFAULT_TEXT_COLOR);
panel.add(label, gbc);
gbc.gridx = 1;
panel.add(textField, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 2;
panel.add(button, gbc);
panel.setBackground(Parameters.IHM_COLOR);
return panel;
}
private JPanel secondCard() {
file = new GraphicFile(this, tree);
return initSecondCard(file);
}
private JPanel secondCard(GraphicFile file) {
this.file = file;
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());
southPanel.setBackground(Parameters.IHM_COLOR);
southPanel.add(backButton);
backButton.addActionListener((event) -> backAction(mainPanel));
southPanel.add(unfoldButton);
unfoldButton.addActionListener((event) -> unfoldAction());
southPanel.add(retreatButton);
retreatButton.addActionListener((event) -> retreatAction());
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 unfoldAction() {
file.showAll();
repaintFile();
}
private void retreatAction() {
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());
}
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, "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);
}
}
private void backAction(JPanel panel) {
this.remove(panel);
cards.first(this.getContentPane());
}
}

View File

@@ -0,0 +1,275 @@
package fr.sae.JSonInspector.Graphics;
import fr.sae.JSonInspector.Settings.Parameters;
import fr.sae.JSonInspector.Storage.Node;
import fr.sae.JSonInspector.Storage.Tree;
import fr.sae.JSonInspector.Storage.Type;
import fr.sae.JSonInspector.Storage.Value;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class GraphicFile extends JPanel {
private final GridBagConstraints gbc = new GridBagConstraints();
private final JPanel alignementPanel = new JPanel();
private final Frame frame;
private ArrayList<Line> lines;
private Node firstNode;
public GraphicFile(Frame frame, Tree tree) {
super();
firstNode = tree.getFirstNode();
init();
lines = new ArrayList<>();
this.frame = frame;
createFileRecursive(firstNode, 0, false);
// displayAllLines();
displayLines();
}
public GraphicFile(Frame frame, ArrayList<Line> lines) {
super();
init();
this.frame = frame;
this.lines = lines;
// displayAllLines();
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);
}
private void createFileRecursive(Node node, int depth, boolean virgule) {
String indentation = "";
for (int i = 0; i < depth; i++) {
indentation += Parameters.IHM_INDENTATION;
}
if (node.isObject() || node.isElement()) {
createObjectElement(node, depth, indentation, virgule);
} else if (node.isArray()) {
createArray(node, depth, indentation, virgule);
} else if (node.isPair()) {
createPair(node, depth, indentation, virgule);
}
}
private void createPair(Node node, int depth, String indentation, boolean virgule) {
Line line = new Line(node, depth);
line.add(indentation);
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
line.add(": ");
if (node.getSize() != 0) {
createValue(line, node.get(0));
} else {
line.add("null", Parameters.OTHER_COLOR);
}
if (virgule) {
line.add(",");
}
lines.add(line);
}
private void createObjectElement(Node node, int depth, String indentation, boolean virgule) {
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 {
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
line.add(": {");
}
if (node.getSize() == 0) {
if (virgule) {
line.add(" },");
} else {
line.add(" }");
}
lines.add(line);
} else {
line.addMouseListener(new ArrayObjectListener(line, frame));
lines.add(line);
callNextNodes(node, depth);
Line endLine = new Line(node, indentation + "}", depth);
endLine.setClosingElement();
if (virgule) {
endLine.add(",");
}
lines.add(endLine);
}
}
private void createArray(Node node, int depth, String indentation, boolean virgule) {
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(": [");
if (node.getSize() == 0) {
if (virgule) {
line.add(" ],");
} else {
line.add(" ]");
}
lines.add(line);
} else {
line.addMouseListener(new ArrayObjectListener(line, frame));
lines.add(line);
for (int i = 0; i < node.getSize(); i++) {
Line valueLine = new Line(new Node("", Type.NULL), depth + 1);
if (node.get(i).isNode()) {
callNextNodes(node, depth);
} else {
String valueString = indentation + Parameters.IHM_INDENTATION;
valueLine.add(valueString, Parameters.STRING_COLOR);
createValue(valueLine, node.get(i));
if (i != node.getSize() - 1) {
valueLine.add(",");
}
}
lines.add(valueLine);
}
Line endLine = new Line(node, indentation + "]", depth);
endLine.setClosingElement();
if (virgule) {
endLine.add(",");
}
lines.add(endLine);
}
}
private void createValue(Line line, Value value) {
if (value.isNumber()) {
line.add("" + value.getValue(), Parameters.NUMBER_COLOR);
} else if (value.isString()) {
line.add("\"" + value.getValue() + "\"", Parameters.STRING_COLOR);
} else {
line.add("" + value.getValue(), Parameters.OTHER_COLOR);
}
}
private void callNextNodes(Node node, int depth) {
boolean virgule;
for (int i = 0; i < node.getSize(); i++) {
// si l'élément afficher est le dernier à son niveau "virgule" est faux
// donc il n'y aura pas de virgule en fin ligne
virgule = i != node.getSize() - 1;
if (node.get(i).isNode()) {
createFileRecursive((Node) node.get(i).getValue(), depth + 1, virgule);
}
}
}
private void displayLines() {
boolean inArrayObject = false, array, object;
Node openedArrayObject = lines.get(0).getNode();
removeAllClosingLabel();
for (int i = 0; i < lines.size(); i++) {
if (!inArrayObject) {
array = lines.get(i).getNode().isArray();
object = lines.get(i).getNode().isObject();
// 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).getNode();
if (openedArrayObject.isArray()) {
displayOneHidedLine(i, Parameters.ARRAY_CLOSING);
} else {
displayOneHidedLine(i, Parameters.OBJECT_ELEMENT_CLOSING);
}
// Sinon affiche la ligne normalement
} else {
displayOneLine(i);
}
} else if (lines.get(i).getNode().equals(openedArrayObject)) {
inArrayObject = false;
}
}
}
private void displayOneLine(int index) {
gbc.gridy = index;
gbc.gridx = 0;
alignementPanel.add(lines.get(index), gbc);
}
private void displayOneHidedLine(int index, String endOfLine) {
gbc.gridy = index;
gbc.gridx = 0;
lines.get(index).add(endOfLine);
alignementPanel.add(lines.get(index), gbc);
}
public void showAll() {
for (Line line : lines) {
if (!line.isShow()) {
line.removeClosingLabel();
}
line.unfold();
}
}
public void retreatAll() {
for (Line line : lines) {
if (line.getNode().isArrayObjectElement()) {
if (0 < line.getDepth() && 0 < line.getNode().getSize() && !line.isClosingElement()) {
line.retreat();
}
}
}
}
private void removeAllClosingLabel() {
for (Line line : lines) {
line.removeClosingLabel();
}
}
public ArrayList<Line> getLines() {
return lines;
}
}

View File

@@ -0,0 +1,83 @@
package fr.sae.JSonInspector.Graphics;
import fr.sae.JSonInspector.Graphics.MyJLabel;
import fr.sae.JSonInspector.Graphics.MyJPanel;
import fr.sae.JSonInspector.Storage.Node;
import java.awt.*;
public class Line extends MyJPanel {
private boolean show = true;
private final int depth;
private final Node node;
private MyJLabel lastElement;
private boolean closingElement = false;
public Line(Node node, int depth) {
super();
this.node = node;
this.depth = depth;
}
public Line(Node node, String str, int depth) {
super();
this.add(new MyJLabel(str));
this.node = node;
this.depth = depth;
}
public Line(Node node, String str, Color color, int depth) {
super();
this.add(new MyJLabel(str, color));
this.node = node;
this.depth = depth;
}
public void add(String string) {
lastElement = new MyJLabel(string);
this.add(lastElement);
}
public void add(String string, Color color) {
lastElement = new MyJLabel(string, color);
this.add(lastElement);
}
public Node getNode() {
return node;
}
public int getDepth() {
return depth;
}
public boolean isShow() {
return show;
}
public void unfold() {
show = true;
}
public void retreat() {
show = false;
}
public void setClosingElement() {
closingElement = true;
}
public boolean isClosingElement() {
return closingElement;
}
public void removeClosingLabel() {
try {
if (lastElement.getText().equals("...]") || lastElement.getText().equals("...}")) {
this.remove(lastElement);
}
} catch (NullPointerException e) {
// System.out.println("La ligne est vide");
}
}
}

View File

@@ -0,0 +1,20 @@
package fr.sae.JSonInspector.Graphics;
import fr.sae.JSonInspector.Settings.Parameters;
import javax.swing.*;
import java.awt.*;
public class MyJLabel extends JLabel {
public MyJLabel(String text, Color color) {
super(text);
// this.setFont(Parameters.FILE_FONT);
this.setForeground(color);
}
public MyJLabel(String text) {
super(text);
// this.setFont(Parameters.FILE_FONT);
this.setForeground(Parameters.DEFAULT_TEXT_COLOR);
}
}

View File

@@ -0,0 +1,26 @@
package fr.sae.JSonInspector.Graphics;
import fr.sae.JSonInspector.Settings.Parameters;
import javax.swing.*;
import java.awt.*;
public class MyJPanel extends JPanel {
public MyJPanel(Color color) {
super();
this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
this.setBackground(color);
}
public MyJPanel() {
super();
this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
this.setBackground(Parameters.BACKGROUND_COLOR);
}
public MyJPanel(boolean isTransparent) {
super();
this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
this.setOpaque(isTransparent);
}
}