package ihm.agenda; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.MouseInfo; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.List; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.border.LineBorder; import exception.DroitsInsuffisantsException; import exception.SuppressionRdvImpossibleException; import agenda.AgendaImpl; import agenda.AgendaItf; import agenda.RendezVous; import agenda.Semaine; public class AgendaIHMGraphique extends JPanel implements AgendaIHM { private static final long serialVersionUID = 1L; private int droits; private int semaineCourante; private AgendaImpl agendaDistant; private JPanel panelAgenda,panelTopMenu; private JButton left,right; private JLabel semaine; private JTextField numSemaine; private JPopupMenu menu; public AgendaIHMGraphique(int droits,AgendaItf agendaDistant) { this.agendaDistant = (AgendaImpl) agendaDistant; this.droits = droits; this.semaineCourante=1; setMinimumSize(new Dimension(900,560));//largeur*hauteur this.panelAgenda = new JPanel(new GridLayout(7,10)); this.panelTopMenu = new JPanel(/*new GridLayout(1,3)*/); this.left = new JButton("Semaine précédente"); this.right = new JButton("Semaine suivante"); this.semaine = new JLabel("Semaine"); this.numSemaine =new JTextField(""+this.semaineCourante,2); this.panelTopMenu.add(left); this.panelTopMenu.add(semaine); this.panelTopMenu.add(numSemaine); this.panelTopMenu.add(right); this.left.addActionListener(new ButtonListener()); this.right.addActionListener(new ButtonListener()); this.numSemaine.addActionListener(new ButtonListener()); this.add(this.panelTopMenu, BorderLayout.NORTH); this.add(panelAgenda, BorderLayout.CENTER); } public void affiche() { // retire les anciens elements du panel agenda this.panelAgenda.removeAll(); this.panelAgenda.setBorder(new LineBorder(Color.BLACK)); Semaine semaine = this.agendaDistant.getLesSemaines()[this.semaineCourante-1]; for (int i = 0; i < 7; i++) { // crée le pnale de la journée JPanel day = new JPanel(); day.setBorder(new LineBorder(Color.BLACK)); day.setLayout(new GridLayout(1,9)); this.panelAgenda.add(day); // crée le nom de journée JLabel dayName = new JLabel(); dayName.setHorizontalAlignment(SwingConstants.CENTER); dayName.setBorder(new LineBorder(Color.BLACK)); dayName.setText(semaine.getJour(i+1).getNom() + " " + semaine.getJour(i+1).getNum()); day.add(dayName); List rdvs = semaine.getRendezVous(i+1); Color color; String c; // crée chaque plage horaire int k=0; for (int j = 0; j < 9; j++) { if(k%2 == 1) c="90"; else c="AA"; if(i%2 == 1) color=Color.decode("0xFF"+c+c); else color=Color.decode("0x"+c+"FF"+c); // crée le rdv s'il en existe un if((k0) { PlageIHM rdv = new PlageIHM(rdvs.get(k),"",SwingConstants.CENTER); rdv.setBorder(new LineBorder(color)); rdv.setBackground(color); rdv.setOpaque(true); if(first) rdv.setText(rdvs.get(k).getIntitule()); rdv.setToolTipText(rdvs.get(k).getIntitule()); day.add(rdv); duree--; j++; first=false; } j--; k++; } // crée la pause de midi else if(j==4) { PlageIHM rdv = new PlageIHM(null,"Pause Midi",SwingConstants.CENTER); rdv.setBorder(new LineBorder(Color.BLACK)); rdv.setBackground(Color.decode("0xAAAAFF")); rdv.setOpaque(true); rdv.setToolTipText("Aucun rendez-vous ne pourra être pris pendant la pause de midi"); day.add(rdv); } // crée une plage libre else { PlageIHM rdv = new PlageIHM(null,"",SwingConstants.CENTER); rdv.setBorder(new LineBorder(Color.BLACK)); day.add(rdv); } } } this.paintAll(this.getGraphics()); } public String attendreChoix() {return "";} private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource() instanceof JButton) { JButton b=(JButton) e.getSource(); if(b.equals(left)) semaineCourante--; else if(b.equals(right))semaineCourante++; } else if(e.getSource() instanceof JTextField) { String res=numSemaine.getText(); if(!res.equals("")) semaineCourante=Integer.parseInt(res); } if(semaineCourante<=0) semaineCourante=52; else if (semaineCourante>=53) semaineCourante=1; numSemaine.setText(""+ semaineCourante); affiche(); } } private class PlageIHM extends JLabel { private static final long serialVersionUID = 1L; private RendezVous rdv; public PlageIHM(RendezVous rdv,String string, int center) { super(string,center); this.rdv = rdv; this.setPreferredSize(new Dimension(100,70)); if(rdv != null) this.addMouseListener(new PlageMouseListener()); } } private class PlageMouseListener extends MouseAdapter { private int jour; private int sem; private int deb; public void mouseReleased(MouseEvent arg) { if(menu != null) { menu.setVisible(false); } RendezVous rdv = ((PlageIHM)arg.getComponent()).rdv; this.jour=rdv.getNumJour()%7; this.sem=(this.jour/7); if(this.jour>0) this.sem++; else this.jour=7; this.deb = rdv.getHeureDebut(); JMenuItem supprimer = new JMenuItem("supprimer"); supprimer.setText("Supprimer le rendez-vous"); supprimer.addActionListener(new SupprimerActionListener()); Point souris = MouseInfo.getPointerInfo().getLocation(); menu = new JPopupMenu(); menu.add(supprimer); menu.setLocation(souris); menu.setVisible(true); } private class SupprimerActionListener implements ActionListener { public void actionPerformed(ActionEvent event) { try { agendaDistant.supprimerRendezVous(droits, jour, sem, deb); affiche(); menu.setVisible(false); } catch (SuppressionRdvImpossibleException e) { e.printStackTrace(); } catch (DroitsInsuffisantsException e) { e.printStackTrace(); } } } } }