Transférer les fichiers vers 'src/JsonInspector'

This commit is contained in:
Romain BESSON 2023-01-12 21:57:42 +01:00
parent 338501d52b
commit 6e6a6bceda
5 changed files with 421 additions and 300 deletions

View File

@ -10,10 +10,14 @@ public class Frame extends JFrame {
private static final Dimension MINIMUM_FRAME_SIZE = new Dimension(600, 500); 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 static final String DEFAULT_LINK = "https://gbfs.citibikenyc.com/gbfs/en/station_information.json";
private final CardLayout cards = new CardLayout(); private final CardLayout cards = new CardLayout();
private boolean showTab = true;
private GraphicFile file;
private Node node; private Node node;
private Tree tree;
public Frame() { public Frame() {
super("Inspecteur JSON"); super("Inspecteur JSON");
tree = new Tree("");
init(); init();
this.setVisible(true); this.setVisible(true);
} }
@ -64,19 +68,27 @@ public class Frame extends JFrame {
} }
private JPanel secondCard() { private JPanel secondCard() {
JPanel mainPanel = new JPanel(), southPanel = new JPanel(); file = new GraphicFile(tree, showTab);
JButton showButton = new JButton();
JButton backButton = new JButton("Retour"); JButton backButton = new JButton("Retour");
backButton.addActionListener((event) -> backAction(mainPanel)); JPanel mainPanel = new JPanel(), southPanel = new JPanel();
GraphicFile file = new GraphicFile(new Tree(""));
JScrollPane scroll = new JScrollPane(); JScrollPane scroll = new JScrollPane();
mainPanel.setLayout(new BorderLayout()); mainPanel.setLayout(new BorderLayout());
if (showTab) {
showButton.setText("Tout déplier");
} else {
showButton.setText("Tout replier");
}
southPanel.setBackground(Parameters.IHM_COLOR); southPanel.setBackground(Parameters.IHM_COLOR);
southPanel.add(backButton); southPanel.add(backButton);
southPanel.add(new JButton("Tout déplier")); backButton.addActionListener((event) -> backAction(mainPanel));
southPanel.add(showButton);
showButton.addActionListener((event) -> showAction(mainPanel));
southPanel.add(new JButton("convertir en PHP")); southPanel.add(new JButton("convertir en PHP"));
mainPanel.add(file, BorderLayout.CENTER); mainPanel.add(file);
mainPanel.add(southPanel, BorderLayout.SOUTH); mainPanel.add(southPanel, BorderLayout.SOUTH);
mainPanel.add(scroll); mainPanel.add(scroll);
@ -84,6 +96,25 @@ public class Frame extends JFrame {
return mainPanel; return mainPanel;
} }
private void showAction(JPanel panel) {
if (showTab) {
showTab = false;
repaintFile(panel);
} else {
showTab = true;
repaintFile(panel);
}
}
private void repaintFile(JPanel panel) {
this.remove(panel);
this.add(secondCard());
cards.last(this.getContentPane());
}
private void validationAction(JTextField field) { private void validationAction(JTextField field) {
try { try {
URL url = new URL(field.getText()); URL url = new URL(field.getText());

View File

@ -2,21 +2,32 @@ package JsonInspector;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
public class GraphicFile extends JPanel { public class GraphicFile extends JPanel {
private Node firstNode; private final GridBagConstraints gbc = new GridBagConstraints();
private ArrayList<Line> lines = new ArrayList<>(); private final ArrayList<Line> lines = new ArrayList<>();
private int index = 0; private final JPanel alignementPanel = new JPanel();
private final Node firstNode;
public GraphicFile(Tree tree) {
public GraphicFile(Tree tree, boolean hided) {
super(); super();
//firstNode = tree.getFirstNode(); //firstNode = tree.getFirstNode();
this.setBackground(Parameters.BACKGROUND_COLOR); this.setBackground(Parameters.BACKGROUND_COLOR);
this.setLayout(new FlowLayout(FlowLayout.LEFT));
firstNode = createTree(); firstNode = createTree();
createFileRecursive(firstNode, 0, false); createFileRecursive(firstNode, 0, false);
displayLines(); gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.WEST;
alignementPanel.setLayout(new GridBagLayout());
this.add(alignementPanel);
if (hided) {
displayHidedLines();
} else {
displayAllLines();
}
} }
@ -24,7 +35,7 @@ public class GraphicFile extends JPanel {
String indentation = ""; String indentation = "";
for (int i = 0; i < depth; i++) { for (int i = 0; i < depth; i++) {
indentation += Parameters.INDENTATION; indentation += Parameters.IHM_INDENTATION;
} }
if (node.getType() == Type.OBJECT) { if (node.getType() == Type.OBJECT) {
@ -34,17 +45,23 @@ public class GraphicFile extends JPanel {
createArray(node, depth, indentation); createArray(node, depth, indentation);
} else if (node.getType() == Type.PAIR) { } else if (node.getType() == Type.PAIR) {
createPair(node, indentation, inList); createPair(node, depth, indentation, inList);
} }
} }
private void createPair(Node node, String indentation, boolean virgule) { private void createPair(Node node, int depth, String indentation, boolean virgule) {
Line line = new Line(node); Line line = new Line(node, depth);
line.add(indentation); line.add(indentation);
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR); line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
line.add(": "); line.add(": ");
line.add("\"" + node.getValues().get(0) + "\"", Parameters.VALUE_COLOR);
if (node.getSize() != 0) {
line.add("\"" + node.get(0) + "\"", Parameters.STRING_COLOR);
} else {
line.add("null" , Parameters.OTHER_COLOR);
}
if (virgule) { if (virgule) {
line.add(","); line.add(",");
} }
@ -54,8 +71,7 @@ public class GraphicFile extends JPanel {
private void createObject(Node node, int depth, String indentation) { private void createObject(Node node, int depth, String indentation) {
boolean virgule; Line line = new Line(node, depth);
Line line = new Line(node);
line.add(indentation); line.add(indentation);
if (node.getName().equals("")) { if (node.getName().equals("")) {
@ -67,51 +83,110 @@ public class GraphicFile extends JPanel {
lines.add(line); lines.add(line);
callNextNodes(node, depth); callNextNodes(node, depth);
lines.add(new Line(node, indentation + "}")); lines.add(new Line(node, indentation + "}", depth));
} }
private void createArray(Node node, int depth, String indentation) { private void createArray(Node node, int depth, String indentation) {
Line line = new Line(node); Line line = new Line(node, depth);
line.add(indentation); line.add(indentation);
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR); line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
line.add(": ["); line.add(": [");
lines.add(line); lines.add(line);
for (int i = 0; i < node.getSize(); i++) {
Line valueLine = new Line(node, depth + 1);
if (node.get(i).isObjectOrArray()) {
callNextNodes(node, depth); callNextNodes(node, depth);
lines.add(new Line(node, indentation + "]")); } else {
valueLine.add(
indentation + Parameters.IHM_INDENTATION + "\"" + node.get(i).getValue() + "\"",
Parameters.STRING_COLOR
);
}
if (i != node.getSize() - 1) {
valueLine.add(",");
}
lines.add(valueLine);
}
lines.add(new Line(node, indentation + "]", depth));
} }
private void callNextNodes(Node node, int depth) { private void callNextNodes(Node node, int depth) {
boolean virgule; boolean virgule;
for (int i = 0; i < node.getNodes().size(); i++) { for (int i = 0; i < node.getSize(); i++) {
// si l'élément afficher est le dernier à son niveau "virgule" est faux // si l'élément afficher est le dernier à son niveau "virgule" est faux
// donc il n'y aura pas de virgule en fin ligne // donc il n'y aura pas de virgule en fin ligne
virgule = !(i == node.getNodes().size() - 1); virgule = i != node.getSize() - 1;
createFileRecursive(node.getNodes().get(i), depth + 1, virgule);
if (node.get(i).isNode()) {
createFileRecursive((Node) node.get(i).getValue(), depth + 1, virgule);
}
} }
} }
private void displayLines() { private void displayAllLines() {
this.setLayout(new GridLayout(30, 1)); for (int i = 0; i < lines.size(); i++) {
MyJPanel tempPanel; displayOneLine(i);
MyJLabel label; }
}
private void displayHidedLines() {
boolean inArray = false, array, object;
Line openedArrayObject = lines.get(0);
for (int i = 0; i < lines.size(); i++) { for (int i = 0; i < lines.size(); i++) {
tempPanel = new MyJPanel(); if (!inArray) {
label = new MyJLabel("" + (i + 1)); array = lines.get(i).getNode().getType() == Type.ARRAY;
label.setBackground(Parameters.IHM_COLOR); object = lines.get(i).getNode().getType() == Type.OBJECT;
tempPanel.add(label);
tempPanel.add(lines.get(i)); // Vérifie si le noeud est du type ARRAY ou du type OBJECT
this.add(tempPanel); if ((array || object) && 0 < lines.get(i).getDepth()) {
//System.out.println(lines.get(i).index); inArray = true;
openedArrayObject = lines.get(i);
if (openedArrayObject.getNode().getType() == Type.ARRAY) {
displayOneHidedLine(i, "... ]");
} else {
displayOneHidedLine(i, "... }");
} }
System.out.println();
System.out.println(lines.size() + " ligne(s)"); //Sinon affiche la ligne normalement
} else {
displayOneLine(i);
}
} else if (lines.get(i).getNode().equals(openedArrayObject.getNode())) {
inArray = 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);
} }
@ -119,23 +194,19 @@ public class GraphicFile extends JPanel {
Node beginning = new Node("", Type.OBJECT); Node beginning = new Node("", Type.OBJECT);
Node d = new Node("d", Type.OBJECT); Node d = new Node("d", Type.OBJECT);
Node result = new Node("result", Type.ARRAY); Node result = new Node("result", Type.ARRAY);
Node inter = new Node("", Type.OBJECT); Node metadata = new Node("", Type.ELEMENT);
Node metadata = new Node("metadata", Type.OBJECT);
Node userID = new Node("UserId", Type.PAIR);
Node roleCode = new Node("RoleCode", Type.PAIR);
Node type = new Node("type", Type.PAIR); Node type = new Node("type", Type.PAIR);
Node e = new Node("e", Type.ARRAY);
String value1 = "EmployeeDetails.Employe", value2 = "E12012", value3 = "35"; String value1 = "EmployeeDetails.Employe", value2 = "E12012", value3 = "35";
beginning.addNode(d); beginning.add(d);
d.addNode(result); beginning.add(e);
result.addNode(inter); d.add(result);
inter.addNode(metadata); result.add(metadata);
inter.addNode(userID); metadata.add(type);
inter.addNode(roleCode); result.add(value1);
metadata.addNode(type); result.add(value2);
type.addValue(value1); result.add(value3);
userID.addValue(value2);
roleCode.addValue(value3);
System.out.println(Tree.printTree(beginning, 0)); System.out.println(Tree.printTree(beginning, 0));

View File

@ -3,31 +3,51 @@ package JsonInspector;
import java.awt.*; import java.awt.*;
public class Line extends MyJPanel { public class Line extends MyJPanel {
//Le nœud qui est représenté par cette objet Line //Le nœud qui est représenté par cet objet Line
public boolean show = true;
private int depth;
private Node node; private Node node;
public Line(Node node) {
public Line(Node node, int depth) {
super(); super();
this.node = node; this.node = node;
this.depth = depth;
} }
public Line(Node node, String str) {
public Line(Node node, String str, int depth) {
super(); super();
this.add(new MyJLabel(str)); this.add(new MyJLabel(str));
this.node = node; this.node = node;
this.depth = depth;
} }
public Line(Node node, String str, Color color) {
public Line(Node node, String str, Color color, int depth) {
super(); super();
this.add(new MyJLabel(str, color)); this.add(new MyJLabel(str, color));
this.node = node; this.node = node;
this.depth = depth;
} }
public void add(String string) { public void add(String string) {
this.add(new MyJLabel(string)); this.add(new MyJLabel(string));
} }
public void add(String string, Color color) { public void add(String string, Color color) {
this.add(new MyJLabel(string, color)); this.add(new MyJLabel(string, color));
} }
public Node getNode() {
return node;
}
public int getDepth() {
return depth;
}
} }

View File

@ -6,20 +6,19 @@ import java.awt.*;
public class MyJPanel extends JPanel { public class MyJPanel extends JPanel {
public MyJPanel(Color color) { public MyJPanel(Color color) {
super(); super();
this.setLayout(new FlowLayout(FlowLayout.LEFT)); this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
this.setBackground(color); this.setBackground(color);
} }
public MyJPanel() { public MyJPanel() {
super(); super();
this.setLayout(new FlowLayout(FlowLayout.LEFT)); this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
//this.setBackground(new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));
this.setBackground(Parameters.BACKGROUND_COLOR); this.setBackground(Parameters.BACKGROUND_COLOR);
} }
public MyJPanel(boolean isTransparent) { public MyJPanel(boolean isTransparent) {
super(); super();
this.setLayout(new FlowLayout(FlowLayout.LEFT)); this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
this.setOpaque(isTransparent); this.setOpaque(isTransparent);
} }
} }