Transférer les fichiers vers 'src/JsonInspector'

This commit is contained in:
Romain BESSON 2023-01-14 16:38:37 +01:00
parent 9828f30743
commit 700a9511b4
5 changed files with 193 additions and 48 deletions

View File

@ -0,0 +1,38 @@
package JsonInspector;
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) {
System.out.println("Clique");
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

@ -11,6 +11,7 @@ public class Frame extends JFrame {
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 JPanel secondCard;
private GraphicFile file;
private Node node;
private Tree tree;
@ -26,7 +27,8 @@ public class Frame extends JFrame {
super("Inspecteur JSON");
this.node = node;
init();
this.add(secondCard());
secondCard = secondCard();
this.add(secondCard);
cards.last(this.getContentPane());
this.setVisible(true);
}
@ -60,7 +62,9 @@ public class Frame extends JFrame {
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);
@ -68,7 +72,7 @@ public class Frame extends JFrame {
}
private JPanel secondCard() {
file = new GraphicFile(tree, showTab);
file = new GraphicFile(this, tree);
JButton showButton = new JButton();
JButton backButton = new JButton("Retour");
JPanel mainPanel = new JPanel(), southPanel = new JPanel();
@ -85,7 +89,7 @@ public class Frame extends JFrame {
southPanel.add(backButton);
backButton.addActionListener((event) -> backAction(mainPanel));
southPanel.add(showButton);
showButton.addActionListener((event) -> showAction(mainPanel));
showButton.addActionListener((event) -> showAction());
southPanel.add(new JButton("convertir en PHP"));
mainPanel.add(file);
@ -97,20 +101,54 @@ public class Frame extends JFrame {
}
private void showAction(JPanel panel) {
private JPanel secondCard(GraphicFile file) {
this.file = file;
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());
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() {
if (showTab) {
showTab = false;
repaintFile(panel);
file.showAll();
} else {
showTab = true;
repaintFile(panel);
file.retreatAll();
}
repaintFile();
}
private void repaintFile(JPanel panel) {
this.remove(panel);
this.add(secondCard());
public void repaintFile() {
file = new GraphicFile(this, file.getLines());
this.remove(secondCard);
secondCard = secondCard(file);
this.add(secondCard);
cards.last(this.getContentPane());
}
@ -118,13 +156,15 @@ public class Frame extends JFrame {
private void validationAction(JTextField field) {
try {
URL url = new URL(field.getText());
this.add(secondCard());
secondCard = secondCard();
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

@ -6,28 +6,39 @@ 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;
private final Frame frame;
private ArrayList<Line> lines = new ArrayList<>();
private Node firstNode;
public GraphicFile(Tree tree, boolean hided) {
public GraphicFile(Frame frame, Tree tree) {
super();
//firstNode = tree.getFirstNode();
this.setBackground(Parameters.BACKGROUND_COLOR);
this.setLayout(new FlowLayout(FlowLayout.LEFT));
init();
this.frame = frame;
firstNode = createTree();
createFileRecursive(firstNode, 0, false);
displayLines();
}
public GraphicFile(Frame frame, ArrayList<Line> lines) {
super();
init();
this.frame = frame;
this.lines = lines;
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);
if (hided) {
displayHidedLines();
} else {
displayAllLines();
}
}
@ -74,6 +85,10 @@ public class GraphicFile extends JPanel {
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 {
@ -90,6 +105,7 @@ public class GraphicFile extends JPanel {
lines.add(line);
} else {
line.addMouseListener(new ArrayObjectListener(line, frame));
lines.add(line);
callNextNodes(node, depth);
Line endLine = new Line(node, indentation + "}", depth);
@ -107,6 +123,10 @@ public class GraphicFile extends JPanel {
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(": [");
@ -118,6 +138,7 @@ public class GraphicFile extends JPanel {
}
lines.add(line);
} else {
line.addMouseListener(new ArrayObjectListener(line, frame));
lines.add(line);
for (int i = 0; i < node.getSize(); i++) {
@ -175,25 +196,18 @@ public class GraphicFile extends JPanel {
}
private void displayAllLines() {
for (int i = 0; i < lines.size(); i++) {
displayOneLine(i);
}
}
private void displayHidedLines() {
boolean inArray = false, array, object;
private void displayLines() {
boolean inArrayObject = false, array, object;
Line openedArrayObject = lines.get(0);
for (int i = 0; i < lines.size(); i++) {
if (!inArray) {
if (!inArrayObject) {
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;
// 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);
if (openedArrayObject.getNode().getType() == Type.ARRAY) {
@ -208,7 +222,7 @@ public class GraphicFile extends JPanel {
}
} else if (lines.get(i).getNode().equals(openedArrayObject.getNode())) {
inArray = false;
inArrayObject = false;
}
}
}
@ -231,6 +245,30 @@ public class GraphicFile extends JPanel {
}
public void showAll() {
for (Line line : lines) {
if (!line.isShow()) {
line.removeClosingLabel();
}
line.unfold();
}
}
public void retreatAll() {
for (Line line : lines) {
if (0 < line.getDepth() && 0 < line.getNode().getSize()) {
line.retreat();
}
}
}
public ArrayList<Line> getLines() {
return lines;
}
private Node createTree() {
Node beginning = new Node("", Type.ELEMENT);
Node d = new Node("d", Type.OBJECT);
@ -249,6 +287,7 @@ public class GraphicFile extends JPanel {
d.add(result);
result.add(metadata);
metadata.add(type);
result.add(true);
result.add(value1);
result.add(value2);
result.add(value3);

View File

@ -3,10 +3,10 @@ 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;
private boolean show = true;
private final int depth;
private final Node node;
private MyJLabel lastElement;
public Line(Node node, int depth) {
@ -33,12 +33,14 @@ public class Line extends MyJPanel {
public void add(String string) {
this.add(new MyJLabel(string));
lastElement = new MyJLabel(string);
this.add(lastElement);
}
public void add(String string, Color color) {
this.add(new MyJLabel(string, color));
lastElement = new MyJLabel(string, color);
this.add(lastElement);
}
@ -50,4 +52,30 @@ public class Line extends MyJPanel {
public int getDepth() {
return depth;
}
public boolean isShow() {
return show;
}
public void unfold() {
show = true;
}
public void retreat() {
show = false;
}
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

@ -6,13 +6,13 @@ import java.awt.*;
public class MyJLabel extends JLabel {
public MyJLabel(String text, Color color) {
super(text);
this.setFont(Parameters.FILE_FONT);
//this.setFont(Parameters.FILE_FONT);
this.setForeground(color);
}
public MyJLabel(String text) {
super(text);
this.setFont(Parameters.FILE_FONT);
//this.setFont(Parameters.FILE_FONT);
this.setForeground(Parameters.DEFAULT_TEXT_COLOR);
}
}