Listener's Factorisation
This commit is contained in:
parent
0bc81d70a5
commit
a29f8f5ec3
@ -15,7 +15,7 @@ public class Main {
|
||||
|
||||
MainFrame frame = new MainFrame();
|
||||
frame.setVisible(true);
|
||||
SwingGLCanvas canvas = new SwingGLCanvas();
|
||||
//SwingGLCanvas canvas = new SwingGLCanvas();
|
||||
|
||||
}
|
||||
|
||||
|
@ -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");
|
||||
|
@ -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();
|
||||
|
@ -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("");
|
||||
}
|
||||
}
|
@ -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) {
|
||||
|
||||
}
|
||||
}
|
@ -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<String> 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
79
src/lsystem/screen/listener/Listener.java
Normal file
79
src/lsystem/screen/listener/Listener.java
Normal file
@ -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<String> 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) {
|
||||
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user