From 2b19f4a8bef2e0d72d9c70359d83cd2165e8c463 Mon Sep 17 00:00:00 2001 From: Arthur <78031901+Arthur7770@users.noreply.github.com> Date: Wed, 17 Feb 2021 11:04:12 +0100 Subject: [PATCH] Fields correctly works and maximum axiome number is set to 1 --- src/lsystem/screen/MainFrame.java | 33 +++++-------------- .../screen/listener/FieldListener.java | 27 +++++++-------- .../screen/listener/NewGenListener.java | 1 + 3 files changed, 23 insertions(+), 38 deletions(-) diff --git a/src/lsystem/screen/MainFrame.java b/src/lsystem/screen/MainFrame.java index daa18b3..8c7c725 100644 --- a/src/lsystem/screen/MainFrame.java +++ b/src/lsystem/screen/MainFrame.java @@ -82,19 +82,16 @@ public class MainFrame extends JFrame { 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, byte messageType, JTextArea list) { - if (stringToAdd == null) { - switch(messageType) { - case 0: - list.setText("Axiome : \n"); - case 1: - list.setText("Règles : \n"); - default: - throw new IllegalArgumentException("Wrong argument given to method changeList"); - } - }else{ + 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 = ""; @@ -106,19 +103,5 @@ public class MainFrame extends JFrame { //TODO return the rules return list; } - public void incorrect() { - JFrame alert = new JFrame(); - alert.setTitle("Erreur"); - alert.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - alert.setSize(60,40); - alert.setLocationRelativeTo(null); - JLabel text = new JLabel("Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer"); - alert.add(text); - alert.setVisible(true); - - changeList(null,(byte) 0,null); - changeList(null,(byte)1,null); - - } } diff --git a/src/lsystem/screen/listener/FieldListener.java b/src/lsystem/screen/listener/FieldListener.java index e5e032d..98cdde0 100644 --- a/src/lsystem/screen/listener/FieldListener.java +++ b/src/lsystem/screen/listener/FieldListener.java @@ -11,30 +11,31 @@ public class FieldListener implements KeyListener { MainFrame frame; int index; - byte i; + int nbAxioms; + byte type; - public FieldListener(MainFrame frame, int index, byte i){ + public FieldListener(MainFrame frame, int index,byte type){ this.frame = frame; this.index = index; - this.i = i; - - + nbAxioms = 0; + this.type = type; } @Override public void keyTyped(KeyEvent ke) { - if(ke.getKeyCode() != KeyEvent.VK_ENTER) - frame.changeList(String.valueOf(ke.getKeyChar()),i, (JTextArea) frame.textAreaList.get(index)); - else{ - String str = ";\r\n"; - frame.changeList(str,i, (JTextArea) frame.textAreaList.get(index)); - frame.textFieldList.get(index).setText(null); - } + + 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) + 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 diff --git a/src/lsystem/screen/listener/NewGenListener.java b/src/lsystem/screen/listener/NewGenListener.java index 21fbb56..389fdb9 100644 --- a/src/lsystem/screen/listener/NewGenListener.java +++ b/src/lsystem/screen/listener/NewGenListener.java @@ -12,6 +12,7 @@ public class NewGenListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { + frame.newTab(); } }