Mise à jour de 'src/JsonInspector/GraphicFile.java'

This commit is contained in:
Romain BESSON 2023-01-11 13:52:46 +01:00
parent 0ccdd70536
commit 2db0e6e3f2

View File

@ -1,142 +1,144 @@
import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.util.ArrayList;
public class GraphicFile extends JPanel {
private Node firstNode;
private ArrayList<Line> lines = new ArrayList<>();
private int index = 0;
public GraphicFile(Tree tree) {
super();
//firstNode = tree.getFirstNode();
this.setBackground(Parameters.BACKGROUND_COLOR);
firstNode = createTree();
createFileRecursive(firstNode, 0, false);
displayLines();
}
private void createFileRecursive(Node node, int depth, boolean inList) {
String indentation = "";
for (int i = 0; i < depth; i++) {
indentation += Parameters.INDENTATION;
}
if (node.getType() == Type.OBJECT) {
createObject(node, depth, indentation);
} else if (node.getType() == Type.ARRAY) {
createArray(node, depth, indentation);
} else if (node.getType() == Type.PAIR) {
createPair(node, indentation, inList);
}
}
private void createPair(Node node, String indentation, boolean virgule) {
Line line = new Line(node);
line.add(indentation);
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
line.add(": ");
line.add("\"" + node.getValues().get(0) + "\"", Parameters.VALUE_COLOR);
if (virgule) {
line.add(",");
}
lines.add(line);
}
private void createObject(Node node, int depth, String indentation) {
boolean virgule;
Line line = new Line(node);
line.add(indentation);
if (node.getName().equals("")) {
line.add("{");
} else {
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
line.add(": {");
}
lines.add(line);
callNextNodes(node, depth);
lines.add(new Line(node, indentation + "}"));
}
private void createArray(Node node, int depth, String indentation) {
Line line = new Line(node);
line.add(indentation);
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
line.add(": [");
lines.add(line);
callNextNodes(node, depth);
lines.add(new Line(node, indentation + "]"));
}
private void callNextNodes(Node node, int depth) {
boolean virgule;
for (int i = 0; i < node.getNodes().size(); 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.getNodes().size() - 1);
createFileRecursive(node.getNodes().get(i), depth + 1, virgule);
}
}
private void displayLines() {
this.setLayout(new GridLayout(30, 1));
MyJPanel tempPanel;
MyJLabel label;
for (int i = 0; i < lines.size(); i++) {
tempPanel = new MyJPanel();
label = new MyJLabel("" + (i + 1));
label.setBackground(Parameters.IHM_COLOR);
tempPanel.add(label);
tempPanel.add(lines.get(i));
this.add(tempPanel);
//System.out.println(lines.get(i).index);
}
System.out.println();
System.out.println(lines.size() + " ligne(s)");
}
private Node createTree() {
Node beginning = new Node("", Type.OBJECT);
Node d = new Node("d", Type.OBJECT);
Node result = new Node("result", Type.ARRAY);
Node inter = new Node("", Type.OBJECT);
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);
String value1 = "EmployeeDetails.Employe", value2 = "E12012", value3 = "35";
beginning.addNode(d);
d.addNode(result);
result.addNode(inter);
inter.addNode(metadata);
inter.addNode(userID);
inter.addNode(roleCode);
metadata.addNode(type);
type.addValue(value1);
userID.addValue(value2);
roleCode.addValue(value3);
System.out.println(Tree.printTree(beginning, 0));
return beginning;
}
}
package JsonInspector;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.util.ArrayList;
public class GraphicFile extends JPanel {
private Node firstNode;
private ArrayList<Line> lines = new ArrayList<>();
private int index = 0;
public GraphicFile(Tree tree) {
super();
//firstNode = tree.getFirstNode();
this.setBackground(Parameters.BACKGROUND_COLOR);
firstNode = createTree();
createFileRecursive(firstNode, 0, false);
displayLines();
}
private void createFileRecursive(Node node, int depth, boolean inList) {
String indentation = "";
for (int i = 0; i < depth; i++) {
indentation += Parameters.INDENTATION;
}
if (node.getType() == Type.OBJECT) {
createObject(node, depth, indentation);
} else if (node.getType() == Type.ARRAY) {
createArray(node, depth, indentation);
} else if (node.getType() == Type.PAIR) {
createPair(node, indentation, inList);
}
}
private void createPair(Node node, String indentation, boolean virgule) {
Line line = new Line(node);
line.add(indentation);
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
line.add(": ");
line.add("\"" + node.getValues().get(0) + "\"", Parameters.VALUE_COLOR);
if (virgule) {
line.add(",");
}
lines.add(line);
}
private void createObject(Node node, int depth, String indentation) {
boolean virgule;
Line line = new Line(node);
line.add(indentation);
if (node.getName().equals("")) {
line.add("{");
} else {
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
line.add(": {");
}
lines.add(line);
callNextNodes(node, depth);
lines.add(new Line(node, indentation + "}"));
}
private void createArray(Node node, int depth, String indentation) {
Line line = new Line(node);
line.add(indentation);
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
line.add(": [");
lines.add(line);
callNextNodes(node, depth);
lines.add(new Line(node, indentation + "]"));
}
private void callNextNodes(Node node, int depth) {
boolean virgule;
for (int i = 0; i < node.getNodes().size(); 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.getNodes().size() - 1);
createFileRecursive(node.getNodes().get(i), depth + 1, virgule);
}
}
private void displayLines() {
this.setLayout(new GridLayout(30, 1));
MyJPanel tempPanel;
MyJLabel label;
for (int i = 0; i < lines.size(); i++) {
tempPanel = new MyJPanel();
label = new MyJLabel("" + (i + 1));
label.setBackground(Parameters.IHM_COLOR);
tempPanel.add(label);
tempPanel.add(lines.get(i));
this.add(tempPanel);
//System.out.println(lines.get(i).index);
}
System.out.println();
System.out.println(lines.size() + " ligne(s)");
}
private Node createTree() {
Node beginning = new Node("", Type.OBJECT);
Node d = new Node("d", Type.OBJECT);
Node result = new Node("result", Type.ARRAY);
Node inter = new Node("", Type.OBJECT);
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);
String value1 = "EmployeeDetails.Employe", value2 = "E12012", value3 = "35";
beginning.addNode(d);
d.addNode(result);
result.addNode(inter);
inter.addNode(metadata);
inter.addNode(userID);
inter.addNode(roleCode);
metadata.addNode(type);
type.addValue(value1);
userID.addValue(value2);
roleCode.addValue(value3);
System.out.println(Tree.printTree(beginning, 0));
return beginning;
}
}