1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
public class activeIncidentMainFrame extends JFrame implements ActionListener{
JLabel incidentTextLabel = null;
JTextField incidentTextField = null;
activeIncidentMainFrame(String title) {
super(title);
//on centre la fenetre
this.setLocationRelativeTo(this.getParent());
//on applique un gridLayout sur cette fenetre afin d'afficher le JLabel suivi du champ de saisie sur la meme ligne
this.setLayout(new GridLayout(1,2));
//ajout d'un champ texte de saisie dans la fenetre et placement dans la fenetre
incidentTextLabel = new JLabel("numéro d\'incident à activer : ");
this.getContentPane().add(incidentTextLabel);
incidentTextField = new JTextField ();
this.getContentPane().add(incidentTextField);
//ajout d'un listener sur ce composant, this est ce listener
incidentTextField.addActionListener(this);
this.pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {
//on recupere le nom de la classe générant l'evenement
String className = arg0.getSource().getClass().getName();
//ici on traitera les evenements levés par le JTextField
if(className == "javax.swing.JTextField") {
String incidentNumber = arg0.getActionCommand();
//on recupere la ref du JTextField qui a appelé afin d'y effacer le texte renseigné
JTextField jtfWhocall = (JTextField)arg0.getSource();
jtfWhocall.setText(null);
//on recupere une instance du controlleur d'incident
activeIncidentController controller = activeIncidentController.getInstance();
//on envoie le numéro d'incident au controlleur et affiche un message d'erreur si
//on considère le champ comme mal formaté sinon on redessine la fenetre
if(!controller.addIncidentToActivate(incidentNumber))
printErrorMessage();
else this.redraw();
}
//ici on traitera les evenements levés par les JButtons
else if(className == "javax.swing.JButton"){
String buttonName = arg0.getActionCommand();
//pas encore fait
}
}
private void printErrorMessage() {
JOptionPane.showMessageDialog(this, "le texte saisi ne semble pas être un numero d'incident","ugh",JOptionPane.WARNING_MESSAGE);
}
//affiche le champ de saisie dans la JFrame
private void printChampSaisie() {
incidentTextLabel = new JLabel("numéro d\'incident à activer : ");
incidentTextField = new JTextField ();
this.getContentPane().add(incidentTextLabel);
this.getContentPane().add(incidentTextField);
}
//afficher une ligne pour chaque numéro d'incident precedemment //saisi avec a coté un bouton supprimer afin de pouvoir l'enlever de la //liste
public void drawIncidentLine(Object o, int index){
//on cast l'objet en string puisque qu'actuellement,il ne tient qu'a ca
String incidentNumber = (String)o;
System.out.println("on est dans drawIncidentLine, le numero d incident est le : "+incidentNumber);
//label permettant d'afficher le numéro d'incident
JLabel incidentNumberLabel = new JLabel(incidentNumber+" ");
JButton removeIncidentFromList = new JButton("remove");
//action command renvoyé par le bouton sera son index
removeIncidentFromList.setActionCommand(Integer.toString(index));
this.getContentPane().add(incidentNumberLabel);
this.getContentPane().add(removeIncidentFromList);
}
//redessiner la fenetre apres une action
private void redraw()
//on recupere le nombre d'incidents a afficher
activeIncidentController controller = activeIncidentController.getInstance();
int incidentListSize = controller.getIncidentToActivateListSize();
//on enleve tous les composants de la Frame avant de les redessiner
this.removeAll();
//on fait un nouveau layout avec le nombre de lignese qu'il faut, + 1 pour le champ de saisie qu'on met au debut
this.setLayout(new GridLayout(incidentListSize+1,2));
printChampSaisie();
Object[] incidentList = controller.returnIncidentArray();
for(int i=0;i<incidentList.length;i++)
drawIncidentLine(incidentList[i], i);
this.pack();
}
} |
Partager