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; package JsonInspector;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
public class Frame extends JFrame { public class Frame extends JFrame {
private static final Dimension DEFAULT_FRAME_SIZE = new Dimension(800, 600); 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 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 Node node; private boolean showTab = true;
private GraphicFile file;
public Frame() { private Node node;
super("Inspecteur JSON"); private Tree tree;
init();
this.setVisible(true); public Frame() {
} super("Inspecteur JSON");
tree = new Tree("");
public Frame(Node node) { init();
super("Inspecteur JSON"); this.setVisible(true);
this.node = node; }
init();
this.add(secondCard()); public Frame(Node node) {
cards.last(this.getContentPane()); super("Inspecteur JSON");
this.setVisible(true); this.node = node;
} init();
this.add(secondCard());
private void init() { cards.last(this.getContentPane());
this.setSize(DEFAULT_FRAME_SIZE); this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE); }
this.setMinimumSize(MINIMUM_FRAME_SIZE);
this.setLayout(cards); private void init() {
this.add(firstCard()); this.setSize(DEFAULT_FRAME_SIZE);
//this.add(secondCard()); this.setDefaultCloseOperation(EXIT_ON_CLOSE);
cards.first(this.getContentPane()); this.setMinimumSize(MINIMUM_FRAME_SIZE);
} this.setLayout(cards);
this.add(firstCard());
private JPanel firstCard() { //this.add(secondCard());
GridBagLayout layout = new GridBagLayout(); cards.first(this.getContentPane());
GridBagConstraints gbc = new GridBagConstraints(); }
JTextField textField = new JTextField(DEFAULT_LINK, 30);
JButton button = new JButton("Valider"); private JPanel firstCard() {
button.addActionListener((event) -> validationAction(textField)); GridBagLayout layout = new GridBagLayout();
JPanel panel = new JPanel(); GridBagConstraints gbc = new GridBagConstraints();
panel.setLayout(layout); JTextField textField = new JTextField(DEFAULT_LINK, 30);
JButton button = new JButton("Valider");
gbc.insets = new Insets(10, 10, 10, 10); button.addActionListener((event) -> validationAction(textField));
gbc.gridx = 0; JPanel panel = new JPanel();
gbc.gridy = 0; panel.setLayout(layout);
JLabel label = new JLabel("URL :");
label.setForeground(Parameters.DEFAULT_TEXT_COLOR); gbc.insets = new Insets(10, 10, 10, 10);
panel.add(label, gbc); gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridx = 1; JLabel label = new JLabel("URL :");
panel.add(textField, gbc); label.setForeground(Parameters.DEFAULT_TEXT_COLOR);
panel.add(label, gbc);
gbc.gridy = 1;
panel.add(button, gbc); gbc.gridx = 1;
panel.add(textField, gbc);
panel.setBackground(Parameters.IHM_COLOR);
return panel; gbc.gridy = 1;
} panel.add(button, gbc);
private JPanel secondCard() { panel.setBackground(Parameters.IHM_COLOR);
JPanel mainPanel = new JPanel(), southPanel = new JPanel(); return panel;
JButton backButton = new JButton("Retour"); }
backButton.addActionListener((event) -> backAction(mainPanel));
GraphicFile file = new GraphicFile(new Tree("")); private JPanel secondCard() {
JScrollPane scroll = new JScrollPane(); file = new GraphicFile(tree, showTab);
mainPanel.setLayout(new BorderLayout()); JButton showButton = new JButton();
JButton backButton = new JButton("Retour");
southPanel.setBackground(Parameters.IHM_COLOR); JPanel mainPanel = new JPanel(), southPanel = new JPanel();
southPanel.add(backButton); JScrollPane scroll = new JScrollPane();
southPanel.add(new JButton("Tout déplier")); mainPanel.setLayout(new BorderLayout());
southPanel.add(new JButton("convertir en PHP"));
if (showTab) {
mainPanel.add(file, BorderLayout.CENTER); showButton.setText("Tout déplier");
mainPanel.add(southPanel, BorderLayout.SOUTH); } else {
mainPanel.add(scroll); showButton.setText("Tout replier");
}
scroll.setViewportView(file);
return mainPanel; southPanel.setBackground(Parameters.IHM_COLOR);
} southPanel.add(backButton);
backButton.addActionListener((event) -> backAction(mainPanel));
private void validationAction(JTextField field) { southPanel.add(showButton);
try { showButton.addActionListener((event) -> showAction(mainPanel));
URL url = new URL(field.getText()); southPanel.add(new JButton("convertir en PHP"));
this.add(secondCard());
cards.last(this.getContentPane()); mainPanel.add(file);
} catch (MalformedURLException e) { mainPanel.add(southPanel, BorderLayout.SOUTH);
JOptionPane.showMessageDialog(this, "Invalid URL", "Error", JOptionPane.ERROR_MESSAGE); mainPanel.add(scroll);
}
} scroll.setViewportView(file);
return mainPanel;
private void backAction(JPanel panel) { }
this.remove(panel);
cards.first(this.getContentPane());
} 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; 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 final GridBagConstraints gbc = new GridBagConstraints();
private Node firstNode; private final ArrayList<Line> lines = new ArrayList<>();
private ArrayList<Line> lines = new ArrayList<>(); private final JPanel alignementPanel = new JPanel();
private int index = 0; private final Node firstNode;
public GraphicFile(Tree tree) {
super(); public GraphicFile(Tree tree, boolean hided) {
//firstNode = tree.getFirstNode(); super();
this.setBackground(Parameters.BACKGROUND_COLOR); //firstNode = tree.getFirstNode();
firstNode = createTree(); this.setBackground(Parameters.BACKGROUND_COLOR);
createFileRecursive(firstNode, 0, false); this.setLayout(new FlowLayout(FlowLayout.LEFT));
displayLines(); firstNode = createTree();
} createFileRecursive(firstNode, 0, false);
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.WEST;
private void createFileRecursive(Node node, int depth, boolean inList) { alignementPanel.setLayout(new GridBagLayout());
String indentation = ""; this.add(alignementPanel);
for (int i = 0; i < depth; i++) { if (hided) {
indentation += Parameters.INDENTATION; displayHidedLines();
} } else {
displayAllLines();
if (node.getType() == Type.OBJECT) { }
createObject(node, depth, indentation); }
} else if (node.getType() == Type.ARRAY) {
createArray(node, depth, indentation); private void createFileRecursive(Node node, int depth, boolean inList) {
String indentation = "";
} else if (node.getType() == Type.PAIR) {
createPair(node, indentation, inList); for (int i = 0; i < depth; i++) {
} indentation += Parameters.IHM_INDENTATION;
} }
if (node.getType() == Type.OBJECT) {
private void createPair(Node node, String indentation, boolean virgule) { createObject(node, depth, indentation);
Line line = new Line(node);
line.add(indentation); } else if (node.getType() == Type.ARRAY) {
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR); createArray(node, depth, indentation);
line.add(": ");
line.add("\"" + node.getValues().get(0) + "\"", Parameters.VALUE_COLOR); } else if (node.getType() == Type.PAIR) {
if (virgule) { createPair(node, depth, indentation, inList);
line.add(","); }
} }
lines.add(line);
} private void createPair(Node node, int depth, String indentation, boolean virgule) {
Line line = new Line(node, depth);
line.add(indentation);
private void createObject(Node node, int depth, String indentation) { line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
boolean virgule; line.add(": ");
Line line = new Line(node);
line.add(indentation); if (node.getSize() != 0) {
line.add("\"" + node.get(0) + "\"", Parameters.STRING_COLOR);
if (node.getName().equals("")) { } else {
line.add("{"); line.add("null" , Parameters.OTHER_COLOR);
} else { }
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
line.add(": {"); if (virgule) {
} line.add(",");
}
lines.add(line);
callNextNodes(node, depth); lines.add(line);
lines.add(new Line(node, indentation + "}")); }
}
private void createObject(Node node, int depth, String indentation) {
private void createArray(Node node, int depth, String indentation) { Line line = new Line(node, depth);
Line line = new Line(node); line.add(indentation);
line.add(indentation);
if (node.getName().equals("")) {
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR); line.add("{");
line.add(": ["); } else {
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
lines.add(line); line.add(": {");
callNextNodes(node, depth); }
lines.add(new Line(node, indentation + "]"));
} lines.add(line);
callNextNodes(node, depth);
lines.add(new Line(node, indentation + "}", depth));
private void callNextNodes(Node node, int depth) { }
boolean virgule;
for (int i = 0; i < node.getNodes().size(); i++) { private void createArray(Node node, int depth, String indentation) {
// si l'élément afficher est le dernier à son niveau "virgule" est faux Line line = new Line(node, depth);
// donc il n'y aura pas de virgule en fin ligne line.add(indentation);
virgule = !(i == node.getNodes().size() - 1);
createFileRecursive(node.getNodes().get(i), depth + 1, virgule); line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
} line.add(": [");
}
lines.add(line);
private void displayLines() { for (int i = 0; i < node.getSize(); i++) {
this.setLayout(new GridLayout(30, 1)); Line valueLine = new Line(node, depth + 1);
MyJPanel tempPanel;
MyJLabel label; if (node.get(i).isObjectOrArray()) {
callNextNodes(node, depth);
for (int i = 0; i < lines.size(); i++) { } else {
tempPanel = new MyJPanel(); valueLine.add(
label = new MyJLabel("" + (i + 1)); indentation + Parameters.IHM_INDENTATION + "\"" + node.get(i).getValue() + "\"",
label.setBackground(Parameters.IHM_COLOR); Parameters.STRING_COLOR
tempPanel.add(label); );
tempPanel.add(lines.get(i)); }
this.add(tempPanel);
//System.out.println(lines.get(i).index); if (i != node.getSize() - 1) {
} valueLine.add(",");
System.out.println(); }
System.out.println(lines.size() + " ligne(s)");
} lines.add(valueLine);
}
private Node createTree() { lines.add(new Line(node, indentation + "]", depth));
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); private void callNextNodes(Node node, int depth) {
Node metadata = new Node("metadata", Type.OBJECT); boolean virgule;
Node userID = new Node("UserId", Type.PAIR);
Node roleCode = new Node("RoleCode", Type.PAIR); for (int i = 0; i < node.getSize(); i++) {
Node type = new Node("type", Type.PAIR); // si l'élément afficher est le dernier à son niveau "virgule" est faux
String value1 = "EmployeeDetails.Employe", value2 = "E12012", value3 = "35"; // donc il n'y aura pas de virgule en fin ligne
virgule = i != node.getSize() - 1;
beginning.addNode(d);
d.addNode(result); if (node.get(i).isNode()) {
result.addNode(inter); createFileRecursive((Node) node.get(i).getValue(), depth + 1, virgule);
inter.addNode(metadata); }
inter.addNode(userID); }
inter.addNode(roleCode); }
metadata.addNode(type);
type.addValue(value1);
userID.addValue(value2); private void displayAllLines() {
roleCode.addValue(value3); for (int i = 0; i < lines.size(); i++) {
displayOneLine(i);
System.out.println(Tree.printTree(beginning, 0)); }
}
return beginning;
}
} 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; 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
private Node node; public boolean show = true;
private int depth;
public Line(Node node) { private Node node;
super();
this.node = node;
} public Line(Node node, int depth) {
super();
public Line(Node node, String str) { this.node = node;
super(); this.depth = depth;
this.add(new MyJLabel(str)); }
this.node = node;
}
public Line(Node node, String str, int depth) {
public Line(Node node, String str, Color color) { super();
super(); this.add(new MyJLabel(str));
this.add(new MyJLabel(str, color)); this.node = node;
this.node = node; this.depth = depth;
} }
public void add(String string) {
this.add(new MyJLabel(string)); public Line(Node node, String str, Color color, int depth) {
} super();
this.add(new MyJLabel(str, color));
public void add(String string, Color color) { this.node = node;
this.add(new MyJLabel(string, color)); 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; package JsonInspector;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
public class MyJLabel extends JLabel { public class MyJLabel extends JLabel {
public MyJLabel(String text, Color color) { public MyJLabel(String text, Color color) {
super(text); super(text);
this.setFont(Parameters.FILE_FONT); this.setFont(Parameters.FILE_FONT);
this.setForeground(color); this.setForeground(color);
} }
public MyJLabel(String text) { public MyJLabel(String text) {
super(text); super(text);
this.setFont(Parameters.FILE_FONT); this.setFont(Parameters.FILE_FONT);
this.setForeground(Parameters.DEFAULT_TEXT_COLOR); this.setForeground(Parameters.DEFAULT_TEXT_COLOR);
} }
} }

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);
} }
} }