From 0d4d8c3169549daa8a21f87d77170607815f8eb5 Mon Sep 17 00:00:00 2001 From: Arthur <78031901+Arthur7770@users.noreply.github.com> Date: Thu, 18 Feb 2021 15:28:38 +0100 Subject: [PATCH] Moving MainFrame methods to Tab and remove useless HashMaps, Implements nbIterations modification --- src/lsystem/screen/MainFrame.java | 43 ++--------- src/lsystem/screen/Tab.java | 91 +++++++++++++++++------ src/lsystem/screen/listener/Listener.java | 68 +++++++++++------ 3 files changed, 118 insertions(+), 84 deletions(-) diff --git a/src/lsystem/screen/MainFrame.java b/src/lsystem/screen/MainFrame.java index 631934e..5a2e3ec 100644 --- a/src/lsystem/screen/MainFrame.java +++ b/src/lsystem/screen/MainFrame.java @@ -21,12 +21,10 @@ public class MainFrame extends JFrame { private JButton newGen; private JButton help; private int nbRules; - public HashMap textAreaList; - public HashMap 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 getRules(int index){ - List list = new ArrayList<>(); - String str = textAreaList.get(index).getText(); - str = str.substring(10,str.length()); - String[] strsplit = str.split("\n"); - for(int y = 0;y0) + 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 getRules(){ + List list = new ArrayList<>(); + String str = rulesList.getText(); + str = str.substring(10,str.length()); + String[] strsplit = str.split("\n"); + for(int y = 0;y 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 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 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; + } }