Transférer les fichiers vers 'src/fr/sae/JSonInspector/Graphics'

This commit is contained in:
Romain BESSON 2023-01-16 00:10:49 +01:00
parent d38211b431
commit 77cb91dc32
5 changed files with 796 additions and 553 deletions

View File

@ -1,149 +1,211 @@
package fr.sae.JSonInspector.Graphics; package JsonInspector.Graphics;
import fr.sae.JSonInspector.Exception.JsonSyntaxException; import JsonInspector.Exception.JsonSyntaxException;
import fr.sae.JSonInspector.Main; import JsonInspector.Main;
import fr.sae.JSonInspector.Settings.Parameters; import JsonInspector.Settings.Parameters;
import fr.sae.JSonInspector.Storage.Tree; import JsonInspector.Storage.Tree;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
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 final CardLayout cards = new CardLayout(); private final CardLayout cards = new CardLayout();
private boolean showTab = true; private boolean php = false;
private JPanel secondCard; private JPanel secondCard;
private GraphicFile file; private GraphicFile file;
private Tree tree; private Tree tree;
public Frame() {
super("Inspecteur JSON"); public Frame() {
init(); super("Inspecteur JSON");
this.setVisible(true); init();
} this.setVisible(true);
}
private void init() {
this.setSize(DEFAULT_FRAME_SIZE);
this.setDefaultCloseOperation(EXIT_ON_CLOSE); private void init() {
this.setMinimumSize(MINIMUM_FRAME_SIZE); this.setSize(DEFAULT_FRAME_SIZE);
this.setLayout(cards); this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.add(firstCard()); this.setMinimumSize(MINIMUM_FRAME_SIZE);
// this.add(secondCard()); this.setLayout(cards);
cards.first(this.getContentPane()); 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("file:C:\\Users\\Elève\\Desktop\\temp\\jason.json", 30); /**
JButton button = new JButton("Valider"); * Créer le premier panel l'on entre l'URL du fichier
button.addActionListener((event) -> validationAction(textField)); * @return le panel créé
JPanel panel = new JPanel(); */
panel.setLayout(layout); private JPanel firstCard() {
GridBagLayout layout = new GridBagLayout();
gbc.insets = new Insets(10, 10, 10, 10); GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0; JTextField textField = new JTextField(30);
gbc.gridy = 0; JButton button = new JButton("Valider");
JLabel label = new JLabel("URL :"); button.addActionListener((event) -> validationAction(textField));
label.setForeground(Parameters.DEFAULT_TEXT_COLOR); JPanel panel = new JPanel();
panel.add(label, gbc); panel.setLayout(layout);
gbc.gridx = 1; gbc.insets = new Insets(10, 10, 10, 10);
panel.add(textField, gbc); gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridx = 0; JLabel label = new JLabel("URL :");
gbc.gridy = 1; label.setForeground(Parameters.DEFAULT_TEXT_COLOR);
gbc.gridwidth = 2; panel.add(label, gbc);
panel.add(button, gbc);
gbc.gridx = 1;
panel.setBackground(Parameters.IHM_COLOR); panel.add(textField, gbc);
return panel;
} gbc.gridx = 0;
gbc.gridy = 1;
private JPanel secondCard() { gbc.gridwidth = 2;
file = new GraphicFile(this, tree); panel.add(button, gbc);
return initSecondCard(file);
} panel.setBackground(Parameters.IHM_COLOR);
return panel;
private JPanel secondCard(GraphicFile file) { }
this.file = file;
return initSecondCard(file);
} /**
* Créer le deuxième panel l'on voit le fichier
private JPanel initSecondCard(GraphicFile file) { * @return le panel créé
JButton unfoldButton = new JButton("Tout déplier"); */
JButton retreatButton = new JButton("Tout replier"); private JPanel secondCard() {
JButton backButton = new JButton("Retour"); file = new GraphicFile(this, tree, php);
JPanel mainPanel = new JPanel(), southPanel = new JPanel(); return initSecondCard(file);
JScrollPane scroll = new JScrollPane(); }
mainPanel.setLayout(new BorderLayout());
southPanel.setBackground(Parameters.IHM_COLOR); /**
southPanel.add(backButton); * Créer le deuxième panel l'on voit le fichier
backButton.addActionListener((event) -> backAction(mainPanel)); * @return le panel créé
southPanel.add(unfoldButton); */
unfoldButton.addActionListener((event) -> unfoldAction()); private JPanel secondCard(GraphicFile file) {
southPanel.add(retreatButton); this.file = file;
retreatButton.addActionListener((event) -> retreatAction()); return initSecondCard(file);
southPanel.add(new JButton("convertir en PHP")); }
mainPanel.add(file);
mainPanel.add(southPanel, BorderLayout.SOUTH); private JPanel initSecondCard(GraphicFile file) {
mainPanel.add(scroll); JButton unfoldButton = new JButton("Tout déplier");
JButton retreatButton = new JButton("Tout replier");
scroll.setViewportView(file); JButton convertButton = new JButton();
return mainPanel; JButton backButton = new JButton("Retour");
} JPanel mainPanel = new JPanel(), southPanel = new JPanel();
JScrollPane scroll = new JScrollPane();
private void unfoldAction() { mainPanel.setLayout(new BorderLayout());
file.showAll();
repaintFile(); if (php) {
} convertButton.setText("Convertir en JSON");
} else {
private void retreatAction() { convertButton.setText("Convertir en PHP");
file.retreatAll(); }
repaintFile();
} southPanel.setBackground(Parameters.IHM_COLOR);
southPanel.add(backButton);
public void repaintFile() { backButton.addActionListener((event) -> backAction(mainPanel));
file = new GraphicFile(this, file.getLines()); southPanel.add(unfoldButton);
this.remove(secondCard); unfoldButton.addActionListener((event) -> unfoldAction());
secondCard = secondCard(file); southPanel.add(retreatButton);
this.add(secondCard); retreatButton.addActionListener((event) -> retreatAction());
cards.last(this.getContentPane()); southPanel.add(convertButton);
} convertButton.addActionListener((event) -> convertAction());
private void validationAction(JTextField field) { mainPanel.add(file);
try { mainPanel.add(southPanel, BorderLayout.SOUTH);
URL url = new URL(field.getText()); mainPanel.add(scroll);
String file = Main.getJsonInOneLine(url);
scroll.setViewportView(file);
if (file.length() <= 2) { return mainPanel;
throw new FileNotFoundException(); }
}
tree = new Tree(file); /**
secondCard = secondCard(); * Passe de la version PHP du fichier à la version JSON et vice-versa
this.add(secondCard); */
cards.last(this.getContentPane()); private void convertAction() {
if (!php) {
} catch (MalformedURLException e) { php = true;
JOptionPane.showMessageDialog(this, "URL invalide", "Error", JOptionPane.ERROR_MESSAGE); } else {
php = false;
} catch (JsonSyntaxException j) { }
JOptionPane.showMessageDialog(this, "Erreur de syntax dans le fichier", "Error", JOptionPane.ERROR_MESSAGE); file = new GraphicFile(this, tree, php);
repaintFile();
} catch (FileNotFoundException f) { }
JOptionPane.showMessageDialog(this, "Impossible trouver le fichier", "Error", JOptionPane.ERROR_MESSAGE);
}
} /**
* Déplit l'élément cliqué
private void backAction(JPanel panel) { */
this.remove(panel); private void unfoldAction() {
cards.first(this.getContentPane()); file.showAll();
} repaintFile();
} }
/**
* Replit l'élément cliqué
*/
private void retreatAction() {
file.retreatAll();
repaintFile();
}
/**
* Met à jour le fichier
*/
public void repaintFile() {
file = new GraphicFile(this, file.getLines());
this.remove(secondCard);
secondCard = secondCard(file);
this.add(secondCard);
cards.last(this.getContentPane());
}
/**
* Passe du premier panel au second
* @param field le champ contenant l'URL
*/
private void validationAction(JTextField field) {
try {
URL url = new URL(field.getText());
String file = Main.getJsonInOneLine(url);
if (file.length() <= 2) {
throw new FileNotFoundException();
}
tree = new Tree(file);
secondCard = secondCard();
this.add(secondCard);
cards.last(this.getContentPane());
} catch (MalformedURLException e) {
JOptionPane.showMessageDialog(this, "URL invalide", "Error", JOptionPane.ERROR_MESSAGE);
} catch (JsonSyntaxException j) {
JOptionPane.showMessageDialog(this, "Erreur de syntax dans le fichier", "Error", JOptionPane.ERROR_MESSAGE);
} catch (FileNotFoundException f) {
JOptionPane.showMessageDialog(this, "Impossible trouver le fichier", "Error", JOptionPane.ERROR_MESSAGE);
}
}
/**
* Passe du second panel au premier
* @param panel
*/
private void backAction(JPanel panel) {
this.remove(panel);
cards.first(this.getContentPane());
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,83 +1,125 @@
package fr.sae.JSonInspector.Graphics; package JsonInspector.Graphics;
import fr.sae.JSonInspector.Graphics.MyJLabel; import JsonInspector.Storage.Node;
import fr.sae.JSonInspector.Graphics.MyJPanel;
import fr.sae.JSonInspector.Storage.Node; import java.awt.*;
import java.awt.*; /**
* Représente une ligne dans l'IHM
public class Line extends MyJPanel { */
private boolean show = true; public class Line extends MyJPanel {
private final int depth; private boolean show = true;
private final Node node; private final int depth;
private MyJLabel lastElement; private final Node node;
private boolean closingElement = false; private MyJLabel lastElement;
private boolean closingElement = false;
public Line(Node node, int depth) {
super();
this.node = node; /**
this.depth = depth; *
} * @param node le noeud représenté par cette ligne
* @param depth la profondeur dans l'arbre
public Line(Node node, String str, int depth) { */
super(); public Line(Node node, int depth) {
this.add(new MyJLabel(str)); super();
this.node = node; this.node = node;
this.depth = depth; this.depth = depth;
} }
public Line(Node node, String str, Color color, int depth) {
super(); /**
this.add(new MyJLabel(str, color)); *
this.node = node; * @param node le noeud représenté par cette ligne
this.depth = depth; * @param str le texte à afficher sur la ligne
} * @param depth profondeur dans l'arbre
*/
public void add(String string) { public Line(Node node, String str, int depth) {
lastElement = new MyJLabel(string); super();
this.add(lastElement); this.add(new MyJLabel(str));
} this.node = node;
this.depth = depth;
public void add(String string, Color color) { }
lastElement = new MyJLabel(string, color);
this.add(lastElement);
} /**
* Ajoute du texte sur la ligne
public Node getNode() { * @param string le texte à ajouter
return node; */
} public void add(String string) {
lastElement = new MyJLabel(string);
public int getDepth() { this.add(lastElement);
return depth; }
}
public boolean isShow() { /**
return show; * Ajoute du texte sur la ligne
} * @param string le texte à ajouter
* @param color la couleur du texte
public void unfold() { */
show = true; public void add(String string, Color color) {
} lastElement = new MyJLabel(string, color);
this.add(lastElement);
public void retreat() { }
show = false;
}
public Node getNode() {
public void setClosingElement() { return node;
closingElement = true; }
}
public boolean isClosingElement() { public int getDepth() {
return closingElement; return depth;
} }
public void removeClosingLabel() {
try { public boolean isShow() {
if (lastElement.getText().equals("...]") || lastElement.getText().equals("...}")) { return show;
this.remove(lastElement); }
}
} catch (NullPointerException e) {
// System.out.println("La ligne est vide"); /**
} * Change le statut de visibilité de la ligne à 'vrai'
} */
} public void unfold() {
show = true;
}
/**
* Change le statut de visibilité de la ligne à 'faux'
*/
public void retreat() {
show = false;
}
/**
* Définit si la ligne représente la fin d'un noeud
*/
public void setClosingElement() {
closingElement = true;
}
/**
* Test si la ligne représente la fin d'un noeud
* @return
*/
public boolean isClosingElement() {
return closingElement;
}
/**
* Enlève le dernier élément de la ligne
*/
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

@ -1,20 +1,27 @@
package fr.sae.JSonInspector.Graphics; package JsonInspector.Graphics;
import fr.sae.JSonInspector.Settings.Parameters; import JsonInspector.Settings.Parameters;
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) { /**
super(text); *
// this.setFont(Parameters.FILE_FONT); * @param text le texte à afficher
this.setForeground(color); * @param color la couleur du texte
} */
public MyJLabel(String text, Color color) {
public MyJLabel(String text) { super(text);
super(text); this.setForeground(color);
// this.setFont(Parameters.FILE_FONT); }
this.setForeground(Parameters.DEFAULT_TEXT_COLOR);
} /**
} *
* @param text le texte à afficher
*/
public MyJLabel(String text) {
super(text);
this.setForeground(Parameters.DEFAULT_TEXT_COLOR);
}
}

View File

@ -1,26 +1,27 @@
package fr.sae.JSonInspector.Graphics; package JsonInspector.Graphics;
import fr.sae.JSonInspector.Settings.Parameters; import JsonInspector.Settings.Parameters;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
public class MyJPanel extends JPanel { public class MyJPanel extends JPanel {
public MyJPanel(Color color) { /**
super(); * Construit un JPanel qui aligne tous ses éléments sur la gauche
this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); * @param color la couleur de fond du panel
this.setBackground(color); */
} public MyJPanel(Color color) {
super();
public MyJPanel() { this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
super(); this.setBackground(color);
this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); }
this.setBackground(Parameters.BACKGROUND_COLOR);
} /**
* Construit un JPanel qui aligne tous ses éléments sur la gauche
public MyJPanel(boolean isTransparent) { */
super(); public MyJPanel() {
this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); super();
this.setOpaque(isTransparent); this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
} this.setBackground(Parameters.BACKGROUND_COLOR);
} }
}