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

@ -1,101 +1,132 @@
package JsonInspector;
import javax.swing.*;
import java.awt.*;
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 Node node;
public Frame() {
super("Inspecteur JSON");
init();
this.setVisible(true);
}
public Frame(Node node) {
super("Inspecteur JSON");
this.node = node;
init();
this.add(secondCard());
cards.last(this.getContentPane());
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(DEFAULT_LINK, 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.gridy = 1;
panel.add(button, gbc);
panel.setBackground(Parameters.IHM_COLOR);
return panel;
}
private JPanel secondCard() {
JPanel mainPanel = new JPanel(), southPanel = new JPanel();
JButton backButton = new JButton("Retour");
backButton.addActionListener((event) -> backAction(mainPanel));
GraphicFile file = new GraphicFile(new Tree(""));
JScrollPane scroll = new JScrollPane();
mainPanel.setLayout(new BorderLayout());
southPanel.setBackground(Parameters.IHM_COLOR);
southPanel.add(backButton);
southPanel.add(new JButton("Tout déplier"));
southPanel.add(new JButton("convertir en PHP"));
mainPanel.add(file, BorderLayout.CENTER);
mainPanel.add(southPanel, BorderLayout.SOUTH);
mainPanel.add(scroll);
scroll.setViewportView(file);
return mainPanel;
}
private void validationAction(JTextField field) {
try {
URL url = new URL(field.getText());
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());
}
}
package JsonInspector;
import javax.swing.*;
import java.awt.*;
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 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();
this.add(secondCard());
cards.last(this.getContentPane());
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(DEFAULT_LINK, 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.gridy = 1;
panel.add(button, gbc);
panel.setBackground(Parameters.IHM_COLOR);
return panel;
}
private JPanel secondCard() {
file = new GraphicFile(tree, showTab);
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(mainPanel));
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 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) {
try {
URL url = new URL(field.getText());
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());
}
}

View File

@ -1,144 +1,215 @@
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;
}
}
package JsonInspector;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class GraphicFile extends JPanel {
private final GridBagConstraints gbc = new GridBagConstraints();
private final ArrayList<Line> lines = new ArrayList<>();
private final JPanel alignementPanel = new JPanel();
private final Node firstNode;
public GraphicFile(Tree tree, boolean hided) {
super();
//firstNode = tree.getFirstNode();
this.setBackground(Parameters.BACKGROUND_COLOR);
this.setLayout(new FlowLayout(FlowLayout.LEFT));
firstNode = createTree();
createFileRecursive(firstNode, 0, false);
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.WEST;
alignementPanel.setLayout(new GridBagLayout());
this.add(alignementPanel);
if (hided) {
displayHidedLines();
} else {
displayAllLines();
}
}
private void createFileRecursive(Node node, int depth, boolean inList) {
String indentation = "";
for (int i = 0; i < depth; i++) {
indentation += Parameters.IHM_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, depth, indentation, inList);
}
}
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) {
line.add("\"" + node.get(0) + "\"", Parameters.STRING_COLOR);
} else {
line.add("null" , Parameters.OTHER_COLOR);
}
if (virgule) {
line.add(",");
}
lines.add(line);
}
private void createObject(Node node, int depth, String indentation) {
Line line = new Line(node, depth);
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 + "}", depth));
}
private void createArray(Node node, int depth, String indentation) {
Line line = new Line(node, depth);
line.add(indentation);
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
line.add(": [");
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);
} 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) {
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 displayAllLines() {
for (int i = 0; i < lines.size(); i++) {
displayOneLine(i);
}
}
private void displayHidedLines() {
boolean inArray = false, array, object;
Line openedArrayObject = lines.get(0);
for (int i = 0; i < lines.size(); i++) {
if (!inArray) {
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;
openedArrayObject = lines.get(i);
if (openedArrayObject.getNode().getType() == Type.ARRAY) {
displayOneHidedLine(i, "... ]");
} else {
displayOneHidedLine(i, "... }");
}
//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);
}
private Node createTree() {
Node beginning = new Node("", Type.OBJECT);
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);
String value1 = "EmployeeDetails.Employe", value2 = "E12012", value3 = "35";
beginning.add(d);
beginning.add(e);
d.add(result);
result.add(metadata);
metadata.add(type);
result.add(value1);
result.add(value2);
result.add(value3);
System.out.println(Tree.printTree(beginning, 0));
return beginning;
}
}

View File

@ -1,33 +1,53 @@
package JsonInspector;
import java.awt.*;
public class Line extends MyJPanel {
//Le nœud qui est représenté par cette objet Line
private Node node;
public Line(Node node) {
super();
this.node = node;
}
public Line(Node node, String str) {
super();
this.add(new MyJLabel(str));
this.node = node;
}
public Line(Node node, String str, Color color) {
super();
this.add(new MyJLabel(str, color));
this.node = node;
}
public void add(String string) {
this.add(new MyJLabel(string));
}
public void add(String string, Color color) {
this.add(new MyJLabel(string, color));
}
}
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;
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) {
this.add(new MyJLabel(string));
}
public void add(String string, Color color) {
this.add(new MyJLabel(string, color));
}
public Node getNode() {
return node;
}
public int getDepth() {
return depth;
}
}

View File

@ -1,18 +1,18 @@
package JsonInspector;
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);
}
}
package JsonInspector;
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

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