Moving MainFrame methods to Tab and remove useless HashMaps, Implements nbIterations modification
This commit is contained in:
parent
f4fe768cd8
commit
0d4d8c3169
@ -21,12 +21,10 @@ public class MainFrame extends JFrame {
|
||||
private JButton newGen;
|
||||
private JButton help;
|
||||
private int nbRules;
|
||||
public HashMap<Integer,JTextArea> textAreaList;
|
||||
public HashMap<Integer,JTextField> textFieldList;
|
||||
|
||||
|
||||
public MainFrame(){
|
||||
textAreaList = new HashMap<>();
|
||||
textFieldList = new HashMap<>();
|
||||
|
||||
nbRules = 1;
|
||||
nbTabs = 0;
|
||||
basePanel = new JPanel();
|
||||
@ -35,10 +33,10 @@ public class MainFrame extends JFrame {
|
||||
|
||||
JToolBar toolBar = new JToolBar();
|
||||
newGen = new JButton("Nouvelle génération");
|
||||
newGen.addActionListener(new Listener(this,null,"Tab"));
|
||||
newGen.addActionListener(new Listener(this,null,"Tab",null));
|
||||
toolBar.add(newGen);
|
||||
help = new JButton("Aide");
|
||||
help.addActionListener(new Listener(this,null,"Help"));
|
||||
help.addActionListener(new Listener(this,null,"Help",null));
|
||||
toolBar.add(help);
|
||||
|
||||
this.setTitle("L-system interface");
|
||||
@ -49,12 +47,6 @@ public class MainFrame extends JFrame {
|
||||
this.add(toolBar, BorderLayout.NORTH);
|
||||
this.setPreferredSize(new Dimension(640,600));
|
||||
}
|
||||
public void addToTextAreaList(JTextArea c ,Integer i){
|
||||
textAreaList.put(i,c);
|
||||
}
|
||||
public void addToTextFieldList(JTextField c ,Integer i){
|
||||
textFieldList.put(i,c);
|
||||
}
|
||||
|
||||
public void newHelp() {
|
||||
JFrame aide = new JFrame();
|
||||
@ -75,38 +67,15 @@ public class MainFrame extends JFrame {
|
||||
JOptionPane.showMessageDialog(null, "Nombre maximal de générations atteintes");
|
||||
else {
|
||||
nbTabs++;
|
||||
new Tab(this, nbTabs, nbRules, tabs);
|
||||
new Tab(nbTabs, nbRules, tabs);
|
||||
}
|
||||
|
||||
}
|
||||
public void closeTab() {
|
||||
//TODO : Pour fermer un onglet, nécessite l'implémentation d'un button fermer grâce à la méthode newTab().
|
||||
}
|
||||
public void changeList(String stringToAdd, JTextArea list,int nbAxioms) {
|
||||
if(nbAxioms>0)
|
||||
JOptionPane.showMessageDialog(null, "Nombre maximal d'axiomes créés");
|
||||
else {
|
||||
list.append(stringToAdd);
|
||||
if (stringToAdd == ";")
|
||||
nbAxioms++;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
public String getAxiom(int index){
|
||||
String str = textAreaList.get(index).getText();
|
||||
str = str.substring(10,str.length());
|
||||
return str;
|
||||
}
|
||||
public List<String> getRules(int index){
|
||||
List<String> list = new ArrayList<>();
|
||||
String str = textAreaList.get(index).getText();
|
||||
str = str.substring(10,str.length());
|
||||
String[] strsplit = str.split("\n");
|
||||
for(int y = 0;y<strsplit.length;y++){
|
||||
list.add(strsplit[y]);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -4,43 +4,52 @@ import lsystem.screen.listener.Listener;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.swing.JTabbedPane;
|
||||
|
||||
public class Tab {
|
||||
MainFrame frame;
|
||||
public class Tab extends JPanel{
|
||||
|
||||
int nbTabs;
|
||||
int nbRules;
|
||||
JSpinner nbIterations;
|
||||
JTextField axiomeField;
|
||||
JTextField rulesField;
|
||||
JTextArea axiomList;
|
||||
JTextArea rulesList;
|
||||
JSpinner itSpinner;
|
||||
|
||||
public Tab(MainFrame frame,int nbTabs,int nbRules,JTabbedPane tabs) {
|
||||
this.frame = frame;
|
||||
public Tab(int nbTabs,int nbRules,JTabbedPane tabs) {
|
||||
this.nbRules = nbRules;
|
||||
this.nbTabs = nbTabs;
|
||||
|
||||
JPanel tab = new JPanel();
|
||||
|
||||
JTextArea axiomList = textArea("Axiome : \n",nbTabs);
|
||||
JTextArea rulesList = textArea("Règles : \n",nbTabs+10);
|
||||
|
||||
axiomList = textArea("Axiome : \n",nbTabs);
|
||||
rulesList = textArea("Règles : \n",nbTabs+10);
|
||||
|
||||
nbIterations = new JSpinner();
|
||||
nbIterations.setModel(new SpinnerNumberModel(1, 1, 15, 1));
|
||||
|
||||
JLabel itLabel = new JLabel("Nombre d'itérations : ");
|
||||
itSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 15, 1));
|
||||
((JSpinner.DefaultEditor) itSpinner.getEditor()).getTextField().setEditable(false);
|
||||
|
||||
JLabel axiome = new JLabel("Axiome :");
|
||||
JLabel rules = new JLabel("Règle "+ nbRules+" :");
|
||||
|
||||
JTextField axiomeField = new JTextField();
|
||||
axiomeField.addKeyListener(new Listener(frame,nbTabs,"Axiome"));
|
||||
axiomeField = new JTextField();
|
||||
axiomeField.addKeyListener(new Listener(null,nbTabs,"Axiome",this));
|
||||
axiomeField.setPreferredSize(new Dimension(120,20));
|
||||
frame.addToTextFieldList(axiomeField,nbTabs);
|
||||
|
||||
JTextField rulesField = new JTextField();
|
||||
rulesField.addKeyListener(new Listener(frame,nbTabs+10,"Règles"));
|
||||
rulesField = new JTextField();
|
||||
rulesField.addKeyListener(new Listener(null,nbTabs+10,"Règles",this));
|
||||
rulesField.setPreferredSize(new Dimension(120,20));
|
||||
frame.addToTextFieldList(rulesField,nbTabs+10);
|
||||
|
||||
JButton submitButton = new JButton("Générer");
|
||||
JButton clearButton = new JButton("Clear");
|
||||
clearButton.addActionListener(new Listener(frame,nbTabs,"Clear"));
|
||||
submitButton.addActionListener(new Listener(frame,nbTabs,"Generate"));
|
||||
clearButton.addActionListener(new Listener(null,nbTabs,"Clear",this));
|
||||
submitButton.addActionListener(new Listener(null,nbTabs,"Generate",this));
|
||||
JPanel southComponents = subPanel(clearButton,submitButton,null);
|
||||
|
||||
GridBagConstraints gc = new GridBagConstraints();
|
||||
@ -49,17 +58,19 @@ public class Tab {
|
||||
gc.weightx = 0;
|
||||
gc.weighty = 0.5;
|
||||
|
||||
JPanel eastComponents = subPanel(axiomList,rulesList,gc);
|
||||
JPanel centerComponents = subPanel(axiomeField,rulesField,gc);
|
||||
JPanel westComponents = subPanel(axiome,rules,gc);
|
||||
JPanel lists = subPanel(axiomList,rulesList,gc);
|
||||
JPanel fields = subPanel(axiomeField,rulesField,gc);
|
||||
JPanel labels = subPanel(axiome,rules,gc);
|
||||
|
||||
JPanel Iterations = subPanel(itLabel,itSpinner,gc);
|
||||
|
||||
JPanel aboveComponents = new JPanel();
|
||||
aboveComponents.add(westComponents);
|
||||
aboveComponents.add(centerComponents);
|
||||
aboveComponents.add(eastComponents);
|
||||
aboveComponents.add(labels);
|
||||
aboveComponents.add(fields);
|
||||
aboveComponents.add(lists);
|
||||
aboveComponents.add(Iterations);
|
||||
|
||||
aboveComponents.setLayout(new GridLayout(1,3));
|
||||
aboveComponents.setLayout(new GridLayout(1,4));
|
||||
tab.add(aboveComponents);
|
||||
tab.add(southComponents);
|
||||
tab.setLayout(new BoxLayout(tab,1));
|
||||
@ -71,7 +82,7 @@ public class Tab {
|
||||
res.setText(texte);
|
||||
res.setEditable(false);
|
||||
res.setPreferredSize(new Dimension(120,100));
|
||||
frame.addToTextAreaList(res,nb);
|
||||
|
||||
return res;
|
||||
}
|
||||
public JPanel subPanel(Component a, Component b,GridBagConstraints gc){
|
||||
@ -88,4 +99,38 @@ public class Tab {
|
||||
gc.gridy = 0;
|
||||
return res;
|
||||
}
|
||||
public JTextArea getTextArea(byte i){
|
||||
return (i == 0) ? axiomList : rulesList;
|
||||
}
|
||||
public JTextField getTextField(byte i){
|
||||
return (i == 0) ? axiomeField : rulesField;
|
||||
}
|
||||
public void changeList(String stringToAdd, JTextArea list,int nbAxioms) {
|
||||
if(nbAxioms>0)
|
||||
JOptionPane.showMessageDialog(null, "Nombre maximal d'axiomes créés");
|
||||
else {
|
||||
list.append(stringToAdd);
|
||||
if (stringToAdd == ";")
|
||||
nbAxioms++;
|
||||
}
|
||||
|
||||
}
|
||||
public String getAxiom(){
|
||||
String str = axiomList.getText();
|
||||
str = str.substring(10,str.length());
|
||||
return str;
|
||||
}
|
||||
public java.util.List<String> getRules(){
|
||||
List<String> list = new ArrayList<>();
|
||||
String str = rulesList.getText();
|
||||
str = str.substring(10,str.length());
|
||||
String[] strsplit = str.split("\n");
|
||||
for(int y = 0;y<strsplit.length;y++){
|
||||
list.add(strsplit[y]);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
public int getNbIterations(){
|
||||
return (int) itSpinner.getValue();
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package lsystem.screen.listener;
|
||||
import lsystem.engine.Parser;
|
||||
import lsystem.engine.Rewrite;
|
||||
import lsystem.screen.MainFrame;
|
||||
import lsystem.screen.Tab;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
@ -12,13 +13,14 @@ import java.awt.event.KeyListener;
|
||||
import java.util.List;
|
||||
|
||||
public class Listener implements ActionListener, KeyListener {
|
||||
|
||||
Tab tab;
|
||||
MainFrame frame;
|
||||
Integer index;
|
||||
String type;
|
||||
Integer nbAxioms;
|
||||
|
||||
public Listener(MainFrame frame, Integer index, String type){
|
||||
public Listener(MainFrame frame, Integer index, String type, Tab tab){
|
||||
this.tab = tab;
|
||||
this.frame = frame;
|
||||
this.index = index;
|
||||
this.type = type;
|
||||
@ -27,40 +29,52 @@ public class Listener implements ActionListener, KeyListener {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
switch (type) {
|
||||
case "Help" -> frame.newHelp();
|
||||
case "Tab" -> frame.newTab();
|
||||
case "Clear" -> {
|
||||
frame.textAreaList.get(index).setText("Axiome : \n");
|
||||
frame.textAreaList.get(index + 10).setText("Règles : \n");
|
||||
frame.textFieldList.get(index).setText("");
|
||||
frame.textFieldList.get(index + 10).setText("");
|
||||
}
|
||||
case "Generate" -> {
|
||||
String axiom = frame.getAxiom(index);
|
||||
List<String> rules = frame.getRules(index + 10);
|
||||
Parser parser = new Parser(axiom, rules, 12);
|
||||
case "Help":
|
||||
frame.newHelp();
|
||||
break;
|
||||
case "Tab":
|
||||
frame.newTab();
|
||||
break;
|
||||
case "Clear":
|
||||
tab.getTextArea((byte) 0).setText("Axiome : \n");
|
||||
tab.getTextArea((byte) 1).setText("Règles : \n");
|
||||
tab.getTextField((byte) 0).setText("");
|
||||
tab.getTextField((byte) 1).setText("");
|
||||
Listener kl = (Listener) tab.getTextField((byte) 0).getKeyListeners()[0];
|
||||
kl.resetNbAxioms();
|
||||
|
||||
break;
|
||||
|
||||
case "Generate":
|
||||
String axiom = tab.getAxiom();
|
||||
List<String> rules = tab.getRules();
|
||||
Parser parser = new Parser(axiom, rules, tab.getNbIterations());
|
||||
if (!parser.isCorrect()) {
|
||||
JOptionPane.showMessageDialog(null, "Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer");
|
||||
new Listener(frame, index, "Clear");
|
||||
new Listener(null, index, "Clear",tab);
|
||||
} else {
|
||||
Rewrite rewriter = new Rewrite(axiom, parser.parseRules(), 12);
|
||||
Rewrite rewriter = new Rewrite(axiom, parser.parseRules(), tab.getNbIterations());
|
||||
final String word = rewriter.rewrite();
|
||||
System.out.println(word);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@Override
|
||||
public void keyTyped(KeyEvent ke) {
|
||||
|
||||
byte i = (byte) ((type.equals("Axiome")) ? 0 : 1);
|
||||
|
||||
if(nbAxioms==0 && ke.getKeyChar() !='\b')
|
||||
frame.changeList(String.valueOf(ke.getKeyChar()), frame.textAreaList.get(index),nbAxioms);
|
||||
tab.changeList(String.valueOf(ke.getKeyChar()), tab.getTextArea(i),nbAxioms);
|
||||
if(ke.getKeyChar() == '\b'){
|
||||
String str = frame.textAreaList.get(index).getText();
|
||||
String str = tab.getTextArea(i).getText();
|
||||
if(str.length()>10) {
|
||||
if (!(str.endsWith(";\n") || str.endsWith(";"))){
|
||||
str = str.substring(10, str.length() - 1);
|
||||
frame.textAreaList.get(index).setText(type + " : \n" + str);
|
||||
tab.getTextArea(i).setText(type + " : \n" + str);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -68,11 +82,14 @@ public class Listener implements ActionListener, KeyListener {
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent ke) {
|
||||
|
||||
byte i = (byte) ((type.equals("Axiome")) ? 0 : 1);
|
||||
|
||||
if(ke.getKeyCode() == KeyEvent.VK_ENTER) {
|
||||
frame.textFieldList.get(index).setText(null);
|
||||
tab.getTextField(i).setText(null);
|
||||
String str = ";";
|
||||
frame.changeList(str, frame.textAreaList.get(index),nbAxioms);
|
||||
if(type.equals("Axiome"))
|
||||
tab.changeList(str, tab.getTextArea(i),nbAxioms);
|
||||
if(i == 0)
|
||||
nbAxioms ++;
|
||||
}
|
||||
}
|
||||
@ -81,4 +98,7 @@ public class Listener implements ActionListener, KeyListener {
|
||||
public void keyReleased(KeyEvent ke) {
|
||||
|
||||
}
|
||||
public void resetNbAxioms(){
|
||||
nbAxioms = 0;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user