OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 168 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.jopencalendar.ui;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.print.PageFormat;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Calendar;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class CalendarWithToolBar extends JPanel {
    private final WeekView weekView;
    private final JSpinner spinWeek;
    private final JSpinner spinYear;
    private final DatePicker picker;
    private String title;
    final JScrollPane contentPane = new JScrollPane();
    private int week;
    private int year;

    public CalendarWithToolBar(JCalendarItemProvider manager) {
        this(manager, false);
    }

    public CalendarWithToolBar(JCalendarItemProvider manager, boolean showPrintButton) {
        this.weekView = new WeekView(manager);
        Calendar currentDate = Calendar.getInstance();
        this.year = currentDate.get(Calendar.YEAR);
        this.week = currentDate.get(Calendar.WEEK_OF_YEAR);

        JPanel toolbar = new JPanel();
        toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));

        JButton bPrevious = new JButton(new ImageIcon(this.getClass().getResource("left.png")));
        JButton bNext = new JButton(new ImageIcon(this.getClass().getResource("right.png")));
        configureButton(bPrevious);
        configureButton(bNext);
        toolbar.add(bPrevious);
        toolbar.add(bNext);
        bNext.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                nextWeek();
            }
        });
        bPrevious.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                previousWeek();
            }
        });

        toolbar.add(new JLabel("Semaine"));
        this.spinWeek = new JSpinner(new SpinnerNumberModel(this.week, 1, 53, 1));
        toolbar.add(this.spinWeek);
        toolbar.add(new JLabel(" de "));
        this.spinYear = new JSpinner(new SpinnerNumberModel(this.year, 1000, this.year + 20, 1));
        toolbar.add(this.spinYear);
        //
        this.picker = new DatePicker(false);
        toolbar.add(this.picker);
        //

        final JSlider zoomSlider = createZoomSlider(this.weekView);
        toolbar.add(new JLabel(" Zoom"));
        toolbar.add(zoomSlider);
        if (showPrintButton) {
            final JButton jButton = new JButton("Imprimer");
            jButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    final PrintJComponentAction a = new PrintJComponentAction(CalendarWithToolBar.this.getContentPane(), PageFormat.LANDSCAPE, CalendarWithToolBar.this.title);
                    a.actionPerformed(arg0);
                }
            });
            toolbar.add(jButton);
        }
        JButton reloadButton = new JButton(new ImageIcon(this.getClass().getResource("auto.png")));
        configureButton(reloadButton);
        toolbar.add(reloadButton);
        reloadButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                reload();
            }
        });

        this.setLayout(new GridBagLayout());

        //
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        this.add(toolbar, c);
        c.gridy++;
        this.add(new JSeparator(JSeparator.HORIZONTAL), c);
        c.gridy++;
        c.fill = GridBagConstraints.BOTH;
        c.weighty = 1;

        this.contentPane.setBorder(BorderFactory.createEmptyBorder());
        this.contentPane.setOpaque(false);
        this.contentPane.setColumnHeaderView(new WeekViewHeader(this.weekView));
        this.contentPane.setViewportView(this.weekView);
        this.contentPane.getViewport().setBackground(Color.WHITE);
        final MouseWheelListener[] l = this.contentPane.getMouseWheelListeners();
        for (int i = 0; i < l.length; i++) {
            MouseWheelListener string = l[i];
            this.contentPane.removeMouseWheelListener(string);
        }
        this.contentPane.addMouseWheelListener(new MouseWheelListener() {

            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                CalendarWithToolBar.this.weekView.mouseWheelMoved(e, l);
            }
        });

        this.add(this.contentPane, c);
        final int value = 300;
        this.contentPane.getVerticalScrollBar().setValue(value);
        this.weekView.loadWeek(this.week, this.year, false);
        final ChangeListener listener = new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                setDate(((Number) CalendarWithToolBar.this.spinWeek.getValue()).intValue(), CalendarWithToolBar.this.year);

            }
        };
        this.spinWeek.addChangeListener(listener);

        final ChangeListener listenerYear = new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                setDate(CalendarWithToolBar.this.week, ((Number) CalendarWithToolBar.this.spinYear.getValue()).intValue());
            }
        };

        this.spinYear.addChangeListener(listenerYear);

        this.picker.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                final Date d = CalendarWithToolBar.this.picker.getDate();
                final Calendar c = Calendar.getInstance();
                c.setTime(d);
                final int y = c.get(Calendar.YEAR);
                final int w = c.get(Calendar.WEEK_OF_YEAR);
                setDate(w, y);
            }
        });

    }

    public static JSlider createZoomSlider(final MultipleDayView mdview) {
        final JSlider zoomSlider = new JSlider(1, 10, 1);
        zoomSlider.setSnapToTicks(true);
        zoomSlider.setMajorTickSpacing(1);
        zoomSlider.setPaintTicks(true);
        zoomSlider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                mdview.setZoom(zoomSlider.getValue());
            }
        });
        mdview.addPropertyChangeListener("zoom", new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                zoomSlider.setValue(((Number) evt.getNewValue()).intValue());
            }
        });
        return zoomSlider;
    }

    protected void nextWeek() {
        if (getWeek() >= 52) {
            setDate(1, this.year + 1);
        } else {
            setDate(this.week + 1, this.year);
        }
    }

    protected void previousWeek() {
        if (getWeek() == 1) {
            setDate(52, this.year - 1);
        } else {
            setDate(this.week - 1, this.year);
        }
    }

    public Calendar getCurrentDate() {
        Calendar c = Calendar.getInstance();
        c.clear();
        c.set(Calendar.YEAR, this.year);
        c.set(Calendar.WEEK_OF_YEAR, this.week);
        return c;
    }

    public final void setCurrentDate(final Calendar c) {
        this.setCurrentDate(c.getTime());
    }

    public final void setCurrentDate(final Date d) {
        Calendar c = Calendar.getInstance();
        c.clear();
        c.setTime(d);
        this.setDate(c.get(Calendar.WEEK_OF_YEAR), c.get(Calendar.YEAR));

    }

    private void setDate(int week, int year) {
        if (this.week != week || this.year != year) {
            int oldWeek = this.week;
            int oldYear = this.year;
            this.week = week;
            this.year = year;
            if (this.week != oldWeek) {
                this.spinWeek.setValue(week);
            }
            if (this.year != oldYear) {
                this.spinYear.setValue(year);
            }

            Calendar c = Calendar.getInstance();
            c.clear();
            c.set(Calendar.YEAR, this.year);
            c.set(Calendar.WEEK_OF_YEAR, this.week);
            this.picker.setDate(c.getTime());

            CalendarWithToolBar.this.weekView.loadWeek(getWeek(), getYear(), false);

        }
    }

    private void configureButton(JButton b) {
        b.setOpaque(false);
        b.setBorderPainted(false);
        b.setFocusPainted(false);
        b.setContentAreaFilled(false);
        b.setMargin(new Insets(1, 5, 1, 5));
    }

    public void reload() {
        this.weekView.reload();
    }

    public int getWeek() {
        return this.week;
    }

    public int getYear() {
        return this.year;
    }

    public WeekView getWeekView() {
        return this.weekView;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void scrollTo(int hour) {
        this.contentPane.getVerticalScrollBar().setValue(hour * this.weekView.getRowHeight());
    }

    public JScrollPane getContentPane() {
        return this.contentPane;
    }
}