From a29f8f5ec345bda2e28e78c5170ea254fe963f48 Mon Sep 17 00:00:00 2001 From: Arthur <78031901+Arthur7770@users.noreply.github.com> Date: Wed, 17 Feb 2021 14:59:02 +0100 Subject: [PATCH] Listener's Factorisation --- src/lsystem/Main.java | 2 +- src/lsystem/screen/MainFrame.java | 7 +- src/lsystem/screen/Tab.java | 12 ++- .../screen/listener/ClearListener.java | 26 ------ .../screen/listener/FieldListener.java | 45 ----------- .../screen/listener/GenerateListener.java | 34 -------- src/lsystem/screen/listener/HelpListener.java | 21 ----- src/lsystem/screen/listener/Listener.java | 79 +++++++++++++++++++ .../screen/listener/NewGenListener.java | 18 ----- 9 files changed, 88 insertions(+), 156 deletions(-) delete mode 100644 src/lsystem/screen/listener/ClearListener.java delete mode 100644 src/lsystem/screen/listener/FieldListener.java delete mode 100644 src/lsystem/screen/listener/GenerateListener.java delete mode 100644 src/lsystem/screen/listener/HelpListener.java create mode 100644 src/lsystem/screen/listener/Listener.java delete mode 100644 src/lsystem/screen/listener/NewGenListener.java diff --git a/src/lsystem/Main.java b/src/lsystem/Main.java index 59b298d..582586e 100644 --- a/src/lsystem/Main.java +++ b/src/lsystem/Main.java @@ -15,7 +15,7 @@ public class Main { MainFrame frame = new MainFrame(); frame.setVisible(true); - SwingGLCanvas canvas = new SwingGLCanvas(); + //SwingGLCanvas canvas = new SwingGLCanvas(); } diff --git a/src/lsystem/screen/MainFrame.java b/src/lsystem/screen/MainFrame.java index 72ee8bd..631934e 100644 --- a/src/lsystem/screen/MainFrame.java +++ b/src/lsystem/screen/MainFrame.java @@ -1,8 +1,7 @@ package lsystem.screen; -import lsystem.screen.listener.HelpListener; -import lsystem.screen.listener.NewGenListener; +import lsystem.screen.listener.Listener; import java.awt.*; import java.util.ArrayList; @@ -36,10 +35,10 @@ public class MainFrame extends JFrame { JToolBar toolBar = new JToolBar(); newGen = new JButton("Nouvelle génération"); - newGen.addActionListener(new NewGenListener(this)); + newGen.addActionListener(new Listener(this,null,"Tab")); toolBar.add(newGen); help = new JButton("Aide"); - help.addActionListener(new HelpListener(this)); + help.addActionListener(new Listener(this,null,"Help")); toolBar.add(help); this.setTitle("L-system interface"); diff --git a/src/lsystem/screen/Tab.java b/src/lsystem/screen/Tab.java index 1323be1..a7fb8c7 100644 --- a/src/lsystem/screen/Tab.java +++ b/src/lsystem/screen/Tab.java @@ -1,8 +1,6 @@ package lsystem.screen; -import lsystem.screen.listener.ClearListener; -import lsystem.screen.listener.FieldListener; -import lsystem.screen.listener.GenerateListener; +import lsystem.screen.listener.Listener; import javax.swing.*; import java.awt.*; @@ -30,19 +28,19 @@ public class Tab { JLabel rules = new JLabel("Règle "+ nbRules+" :"); JTextField axiomeField = new JTextField(); - axiomeField.addKeyListener(new FieldListener(frame,nbTabs,(byte)0)); + axiomeField.addKeyListener(new Listener(frame,nbTabs,"Axiom")); axiomeField.setPreferredSize(new Dimension(120,20)); frame.addToTextFieldList(axiomeField,nbTabs); JTextField rulesField = new JTextField(); - rulesField.addKeyListener(new FieldListener(frame,nbTabs+10,(byte)1)); + rulesField.addKeyListener(new Listener(frame,nbTabs+10,"Rules")); 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 ClearListener(frame,nbTabs)); - submitButton.addActionListener(new GenerateListener(frame,nbTabs)); + clearButton.addActionListener(new Listener(frame,nbTabs,"Clear")); + submitButton.addActionListener(new Listener(frame,nbTabs,"Generate")); JPanel southComponents = subPanel(clearButton,submitButton,null); GridBagConstraints gc = new GridBagConstraints(); diff --git a/src/lsystem/screen/listener/ClearListener.java b/src/lsystem/screen/listener/ClearListener.java deleted file mode 100644 index 562c008..0000000 --- a/src/lsystem/screen/listener/ClearListener.java +++ /dev/null @@ -1,26 +0,0 @@ -package lsystem.screen.listener; - -import lsystem.screen.MainFrame; - -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -public class ClearListener implements ActionListener { - MainFrame frame; - int nbTabs; - - public ClearListener(MainFrame frame, int nbTabs){ - this.frame =frame; - this.nbTabs = nbTabs; - } - public void forceAction(){ - actionPerformed(null); - } - @Override - public void actionPerformed(ActionEvent e) { - frame.textAreaList.get(nbTabs).setText("Axiome : \n"); - frame.textAreaList.get(nbTabs+10).setText("Règles : \n"); - frame.textFieldList.get(nbTabs).setText(""); - frame.textFieldList.get(nbTabs+10).setText(""); - } -} diff --git a/src/lsystem/screen/listener/FieldListener.java b/src/lsystem/screen/listener/FieldListener.java deleted file mode 100644 index 98cdde0..0000000 --- a/src/lsystem/screen/listener/FieldListener.java +++ /dev/null @@ -1,45 +0,0 @@ -package lsystem.screen.listener; - -import lsystem.screen.MainFrame; - -import javax.swing.*; -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; -import java.lang.reflect.Field; - -public class FieldListener implements KeyListener { - - MainFrame frame; - int index; - int nbAxioms; - byte type; - - public FieldListener(MainFrame frame, int index,byte type){ - this.frame = frame; - this.index = index; - nbAxioms = 0; - this.type = type; - } - @Override - public void keyTyped(KeyEvent ke) { - - if(ke.getKeyCode() != KeyEvent.VK_ENTER && nbAxioms==0) - frame.changeList(String.valueOf(ke.getKeyChar()), (JTextArea) frame.textAreaList.get(index),nbAxioms); - } - - @Override - public void keyPressed(KeyEvent ke) { - if(ke.getKeyCode() == KeyEvent.VK_ENTER) { - frame.textFieldList.get(index).setText(null); - String str = ";"; - frame.changeList(str, (JTextArea) frame.textAreaList.get(index),nbAxioms); - if(type == 0) - nbAxioms ++; - } - } - - @Override - public void keyReleased(KeyEvent e) { - - } -} diff --git a/src/lsystem/screen/listener/GenerateListener.java b/src/lsystem/screen/listener/GenerateListener.java deleted file mode 100644 index 2156c55..0000000 --- a/src/lsystem/screen/listener/GenerateListener.java +++ /dev/null @@ -1,34 +0,0 @@ -package lsystem.screen.listener; - -import lsystem.engine.Parser; -import lsystem.engine.Rewrite; -import lsystem.screen.MainFrame; - -import javax.swing.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.util.List; - -public class GenerateListener implements ActionListener { - MainFrame frame; - int nbTabs; - public GenerateListener(MainFrame frame,int nbTabs) { - this.frame = frame; - this.nbTabs = nbTabs; - } - @Override - public void actionPerformed(ActionEvent e) { - String axiom = frame.getAxiom(nbTabs); - List rules = frame.getRules(nbTabs+10); - Parser parser = new Parser(axiom,rules,12); - if(!parser.isCorrect()) { - JOptionPane.showMessageDialog(null, "Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer"); - new ClearListener(frame,nbTabs).forceAction(); - } - else{ - Rewrite rewriter = new Rewrite(axiom, parser.parseRules(), 12); - final String word = rewriter.rewrite(); - System.out.println(word); - } - } -} diff --git a/src/lsystem/screen/listener/HelpListener.java b/src/lsystem/screen/listener/HelpListener.java deleted file mode 100644 index 918bec9..0000000 --- a/src/lsystem/screen/listener/HelpListener.java +++ /dev/null @@ -1,21 +0,0 @@ -package lsystem.screen.listener; - -import lsystem.screen.MainFrame; - -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -public class HelpListener implements ActionListener { - - MainFrame frame; - public HelpListener(MainFrame frame) { - - this.frame = frame; - } - - @Override - public void actionPerformed(ActionEvent e) { - - frame.newHelp(); - } -} diff --git a/src/lsystem/screen/listener/Listener.java b/src/lsystem/screen/listener/Listener.java new file mode 100644 index 0000000..35cee60 --- /dev/null +++ b/src/lsystem/screen/listener/Listener.java @@ -0,0 +1,79 @@ +package lsystem.screen.listener; + +import lsystem.engine.Parser; +import lsystem.engine.Rewrite; +import lsystem.screen.MainFrame; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.util.List; + +public class Listener implements ActionListener, KeyListener { + + MainFrame frame; + Integer index; + String type; + Integer nbAxioms; + + public Listener(MainFrame frame, Integer index, String type){ + this.frame = frame; + this.index = index; + this.type = type; + nbAxioms = 0; + } + @Override + public void actionPerformed(ActionEvent e) { + switch (type){ + case "Help": + frame.newHelp(); + break; + case "Tab": + frame.newTab(); + break; + 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(""); + break; + case "Generate": + String axiom = frame.getAxiom(index); + List rules = frame.getRules(index+10); + Parser parser = new Parser(axiom,rules,12); + if(!parser.isCorrect()) { + JOptionPane.showMessageDialog(null, "Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer"); + new Listener(frame,index,"Clear"); + } + else{ + Rewrite rewriter = new Rewrite(axiom, parser.parseRules(), 12); + final String word = rewriter.rewrite(); + System.out.println(word); + } + } + + } + @Override + public void keyTyped(KeyEvent ke) { + if(ke.getKeyCode() != KeyEvent.VK_ENTER && nbAxioms==0) + frame.changeList(String.valueOf(ke.getKeyChar()), (JTextArea) frame.textAreaList.get(index),nbAxioms); + } + + @Override + public void keyPressed(KeyEvent ke) { + if(ke.getKeyCode() == KeyEvent.VK_ENTER) { + frame.textFieldList.get(index).setText(null); + String str = ";"; + frame.changeList(str, (JTextArea) frame.textAreaList.get(index),nbAxioms); + if(type == "Axiom") + nbAxioms ++; + } + } + + @Override + public void keyReleased(KeyEvent ke) { + + } +} diff --git a/src/lsystem/screen/listener/NewGenListener.java b/src/lsystem/screen/listener/NewGenListener.java deleted file mode 100644 index 389fdb9..0000000 --- a/src/lsystem/screen/listener/NewGenListener.java +++ /dev/null @@ -1,18 +0,0 @@ -package lsystem.screen.listener; - -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import lsystem.screen.MainFrame; - -public class NewGenListener implements ActionListener { - MainFrame frame; - public NewGenListener(MainFrame frame) { - this.frame = frame; - } - - @Override - public void actionPerformed(ActionEvent e) { - - frame.newTab(); - } -}