Commented MainFrame and Tab

This commit is contained in:
Arthur 2021-04-13 18:03:04 +02:00
parent 6e83f8c0d9
commit cce32f543b
3 changed files with 84 additions and 37 deletions

View File

@ -15,7 +15,7 @@ public class Listener implements ActionListener, KeyListener, MouseWheelListener
MainFrame frame; MainFrame frame;
Integer index; Integer index;
String type; String type;
Integer nbAxioms = 0; Integer nbAxioms= 0;
Thread parserThread = null; Thread parserThread = null;
ImageIcon staticIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(getClass().getClassLoader().getResource("./resources/loading-gif.gif"))); ImageIcon staticIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(getClass().getClassLoader().getResource("./resources/loading-gif.gif")));
@ -37,14 +37,16 @@ public class Listener implements ActionListener, KeyListener, MouseWheelListener
} }
frame.decreaseTab(); frame.decreaseTab();
frame.renameTabs(); frame.renameTabs();
break; break;
case "Help": case "Help":
frame.newHelp(); frame.newHelp();
break; break;
case "Tab": case "Tab":
frame.newTab(); frame.newTab();
break; break;
case "Clear": case "Clear":
tab.getTextArea((byte) 0).setText("Axiome : \n"); tab.getTextArea((byte) 0).setText("Axiome : \n");
tab.getTextArea((byte) 1).setText("Règles : \n"); tab.getTextArea((byte) 1).setText("Règles : \n");
@ -53,6 +55,7 @@ public class Listener implements ActionListener, KeyListener, MouseWheelListener
Listener kl = (Listener) tab.getTextField((byte) 0).getKeyListeners()[0]; Listener kl = (Listener) tab.getTextField((byte) 0).getKeyListeners()[0];
kl.resetNbAxioms(); kl.resetNbAxioms();
break; break;
case "Generate 3D": case "Generate 3D":
String axiom3D = tab.getAxiom(); String axiom3D = tab.getAxiom();
List<String> rules3D = tab.getRules(); List<String> rules3D = tab.getRules();
@ -92,7 +95,6 @@ public class Listener implements ActionListener, KeyListener, MouseWheelListener
parserThread.start(); parserThread.start();
} }
break; break;
} }
} }

View File

@ -10,9 +10,6 @@ import java.awt.event.WindowEvent;
public class MainFrame extends JFrame { public class MainFrame extends JFrame {
private static final long serialVersionUID = -7898079642230075807L;
private int nbTabs; private int nbTabs;
boolean helpWindow = false; boolean helpWindow = false;
private final JPanel basePanel; private final JPanel basePanel;
@ -21,8 +18,10 @@ public class MainFrame extends JFrame {
private final JButton help; private final JButton help;
private final int nbRules; private final int nbRules;
/**
* Create a new JFrame on which will be displayed all the GUI elements.
*/
public MainFrame(){ public MainFrame(){
nbRules = 1; nbRules = 1;
nbTabs = 0; nbTabs = 0;
basePanel = new JPanel(); basePanel = new JPanel();
@ -45,18 +44,20 @@ public class MainFrame extends JFrame {
this.add(tabs); this.add(tabs);
this.add(toolBar, BorderLayout.NORTH); this.add(toolBar, BorderLayout.NORTH);
this.setPreferredSize(windowDimension); this.setPreferredSize(windowDimension);
newTab();
nbTabs++;
new Tab(nbTabs, nbRules, tabs,this);
renameTabs(); renameTabs();
} }
public JTabbedPane getTab(){
return tabs; /**
} * Decrease the nbTabs variable by one.
*/
public void decreaseTab(){ public void decreaseTab(){
nbTabs -=1; nbTabs -=1;
} }
/**
* Create and display a new frame (and throws a message if one is already open) which contains a helping text (Uses the Constants class).
*/
public void newHelp() { public void newHelp() {
if(helpWindow){ if(helpWindow){
JOptionPane.showMessageDialog(null, "Une fenêtre d'aide est déjà ouverte."); JOptionPane.showMessageDialog(null, "Une fenêtre d'aide est déjà ouverte.");
@ -89,15 +90,23 @@ public class MainFrame extends JFrame {
}); });
} }
} }
/**
* Call the Tab class to create a new JPanel that will be added to the tabs variable of this class (JTabbedPane).
*/
public void newTab() { public void newTab() {
if(nbTabs>2) if(nbTabs>2)
JOptionPane.showMessageDialog(null, "Nombre maximal de générations atteintes"); JOptionPane.showMessageDialog(null, "Nombre maximal de générations atteintes");
else { else {
nbTabs++; nbTabs++;
new Tab(nbTabs, nbRules, tabs,this); tabs.addTab("Génération" + nbTabs,new Tab(nbTabs,this));
} }
} }
/**
* Checks the name of each tab and change it considering their order.
*/
public void renameTabs(){ public void renameTabs(){
for(int i =0;i<nbTabs;i++){ for(int i =0;i<nbTabs;i++){
tabs.setTitleAt(i,("Génération"+(i+1))); tabs.setTitleAt(i,("Génération"+(i+1)));

View File

@ -11,16 +11,20 @@ public class Tab extends JPanel{
public JSpinner itSpinner; public JSpinner itSpinner;
JTextField axiomeField,rulesField; JTextField axiomeField,rulesField;
JTextArea axiomList,rulesList; JTextArea axiomList,rulesList;
JButton submitButton3D, close; JButton submitButton3D;
/**
* A method which create a new instance of the class JPanel ready to be added to the MainFrame's tabs variable (JTabbedPane).
* @param nbTabs -> the number of this tab, useful for the listener.
* @param frame -> the MainFrame instance that is useful to access all the components.
*/
public Tab(int nbTabs,MainFrame frame) {
public Tab(int nbTabs,int nbRules,JTabbedPane tabs,MainFrame frame) {
this.nbRules = nbRules; this.nbRules = nbRules;
this.nbTabs = nbTabs; this.nbTabs = nbTabs;
this.add(new JButton("Close")); axiomList = textArea("Axiome : \n");
rulesList = textArea("Règles : \n");
axiomList = textArea("Axiome : \n",nbTabs);
rulesList = textArea("Règles : \n",nbTabs+10);
JLabel itLabel = new JLabel("Nombre d'itérations : "); JLabel itLabel = new JLabel("Nombre d'itérations : ");
itSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 30, 1)); itSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 30, 1));
@ -28,7 +32,7 @@ public class Tab extends JPanel{
itSpinner.addMouseWheelListener(new Listener(null,null,"Spinner",this)); itSpinner.addMouseWheelListener(new Listener(null,null,"Spinner",this));
JLabel axiome = new JLabel("Axiome :"); JLabel axiome = new JLabel("Axiome :");
JLabel rules = new JLabel("Règle "+ nbRules+" :"); JLabel rules = new JLabel("Règles :");
axiomeField = new JTextField(); axiomeField = new JTextField();
axiomeField.addKeyListener(new Listener(null,nbTabs,"Axiome",this)); axiomeField.addKeyListener(new Listener(null,nbTabs,"Axiome",this));
@ -40,8 +44,14 @@ public class Tab extends JPanel{
submitButton3D = new JButton("Générer en 3D"); submitButton3D = new JButton("Générer en 3D");
JButton clearButton = new JButton("Clear"); JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new Listener(null,nbTabs,"Clear",this)); clearButton.addActionListener(new Listener(null,nbTabs,"Clear",this));
clearButton.setBackground(Color.GREEN);
submitButton3D.addActionListener(new Listener(null,nbTabs,"Generate 3D",this)); submitButton3D.addActionListener(new Listener(null,nbTabs,"Generate 3D",this));
JPanel southComponents2 = subPanel(clearButton, submitButton3D, null); submitButton3D.setBackground(Color.CYAN);
JButton close = new JButton("Close");
close.addActionListener(new Listener(frame,null,"Close",null));
close.setBackground(Color.RED);
JPanel southComponents = subPanel(submitButton3D,close,null);
southComponents = subPanel(clearButton, southComponents, null);
GridBagConstraints gc = new GridBagConstraints(); GridBagConstraints gc = new GridBagConstraints();
gc.gridx = 0; gc.gridx = 0;
@ -64,13 +74,16 @@ public class Tab extends JPanel{
aboveComponents.setLayout(new GridLayout(1,4)); aboveComponents.setLayout(new GridLayout(1,4));
this.add(aboveComponents); this.add(aboveComponents);
this.add(southComponents2); this.add(southComponents);
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
tabs.addTab("Génération" + nbTabs,this);
} }
public JTextArea textArea(String texte, int nb){ /**
* Create and return a newly created TextArea component.
* @param texte -> the text that will be added in the returned component.
* @return A JTextArea component.
*/
public JTextArea textArea(String texte){
JTextArea res = new JTextArea(); JTextArea res = new JTextArea();
res.setText(texte); res.setText(texte);
res.setEditable(false); res.setEditable(false);
@ -79,6 +92,13 @@ public class Tab extends JPanel{
return res; return res;
} }
/**
* Fuse two components into a JPanel component organized with given GridBagConstraints.
* @param a -> the first component to add in the JPanel.
* @param b -> the second component to add in the JPanel.
* @param gc -> the GridBagConstraints given to organise the two fused components.
* @return A JPanel component.
*/
public JPanel subPanel(Component a, Component b,GridBagConstraints gc){ public JPanel subPanel(Component a, Component b,GridBagConstraints gc){
JPanel res = new JPanel(); JPanel res = new JPanel();
if(gc == null){ if(gc == null){
@ -94,31 +114,51 @@ public class Tab extends JPanel{
return res; return res;
} }
/**
* An accessor to the axiomList and the rulesList
* @param i -> a byte which indicates which component to return (axiomList or rulesList).
* @return A JTextArea component.
*/
public JTextArea getTextArea(byte i){ public JTextArea getTextArea(byte i){
return (i == 0) ? axiomList : rulesList; return (i == 0) ? axiomList : rulesList;
} }
/**
* An accessor to the axiomField and the rulesField.
* @param i -> a byte which indicates which component to return (axiomField or rulesField).
* @return A JTextField component.
*/
public JTextField getTextField(byte i){ public JTextField getTextField(byte i){
return (i == 0) ? axiomeField : rulesField; return (i == 0) ? axiomeField : rulesField;
} }
public void changeList(String stringToAdd, JTextArea list, int nbAxioms) { /**
if(nbAxioms > 0) * Checks if the maximal axioms number has been reach, if not, add the given String into the axiomList or into the rulesList.
* @param stringToAdd -> the String to add into the JTextArea.
* @param list -> the JTextArea where to add the String (axiomList or rulesList).
* @param nb -> the number of Axioms that are already created (maximum 1).
*/
public void changeList(String stringToAdd, JTextArea list, int nb) {
if(nb > 0)
JOptionPane.showMessageDialog(null, "Nombre maximal d'axiomes créés"); JOptionPane.showMessageDialog(null, "Nombre maximal d'axiomes créés");
else { else {
list.append(stringToAdd); list.append(stringToAdd);
if (stringToAdd.equals(";"))
nbAxioms++;
} }
} }
/**
* @return A string which contains the axiom entered by the user.
*/
public String getAxiom(){ public String getAxiom(){
String str = axiomList.getText(); String str = axiomList.getText();
str = str.substring(10).replaceAll(";", ""); str = str.substring(10).replaceAll(";", "");
return str; return str;
} }
/**
* @return A list of Strings corresponding to the different rules the user has entered.
*/
public java.util.List<String> getRules(){ public java.util.List<String> getRules(){
String str = rulesList.getText(); String str = rulesList.getText();
str = str.substring(10).replaceAll(";", ""); str = str.substring(10).replaceAll(";", "");
@ -126,15 +166,11 @@ public class Tab extends JPanel{
return Arrays.asList(strsplit); return Arrays.asList(strsplit);
} }
/**
* @return The number of iterations selected by the user thanks to the JSpinner.
*/
public int getNbIterations(){ public int getNbIterations(){
return (int) itSpinner.getValue(); return (int) itSpinner.getValue();
} }
public JButton closeButton(MainFrame frame) {
close = new JButton("Close");
int size = 17;
close.setPreferredSize(new Dimension(size, size));
close.addActionListener(new Listener(frame,this.nbTabs,"Close",this));
return close;
}
} }