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..e98f723 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,"Axiome")); 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,"Règles")); 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..fe10cc1 --- /dev/null +++ b/src/lsystem/screen/listener/Listener.java @@ -0,0 +1,84 @@ +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(); + 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); + 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(nbAxioms==0 && ke.getKeyChar() !='\b') + frame.changeList(String.valueOf(ke.getKeyChar()), frame.textAreaList.get(index),nbAxioms); + if(ke.getKeyChar() == '\b'){ + String str = frame.textAreaList.get(index).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); + } + } + } + } + + @Override + public void keyPressed(KeyEvent ke) { + if(ke.getKeyCode() == KeyEvent.VK_ENTER) { + frame.textFieldList.get(index).setText(null); + String str = ";"; + frame.changeList(str, frame.textAreaList.get(index),nbAxioms); + if(type.equals("Axiome")) + 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(); - } -}