OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

package org.openconcerto.modules.operation;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jopencalendar.model.Flag;
import org.jopencalendar.model.JCalendarItem;
import org.jopencalendar.ui.CalendarWithToolBar;
import org.jopencalendar.ui.MultipleDayView;
import org.openconcerto.erp.config.ComptaPropsConfiguration;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLTableEvent;
import org.openconcerto.sql.model.SQLTableModifiedListener;
import org.openconcerto.sql.users.User;
import org.openconcerto.sql.view.EditFrame;
import org.openconcerto.sql.view.EditPanel.EditMode;
import org.openconcerto.ui.FrameUtil;

/**
 * Planning de la semaine
 */
public class OperationCalendarPanel extends AbstractCalendarPanel {

    private CalendarWithToolBar calendar;
    private OperationCalendarManager manager;

    public OperationCalendarPanel(final ComptaPropsConfiguration conf) {
        this.setLayout(new BorderLayout());
        JPanel toolbar = new JPanel();
        toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
        toolbar.setOpaque(false);
        final JButton bPrint = new JButton("Imprimer les plannings des interventions");
        bPrint.setOpaque(false);
        toolbar.add(bPrint);
        //
        final JButton bCreate = new JButton("Ajouter une intervention");
        bCreate.setOpaque(false);
        toolbar.add(bCreate);
        //
        final JButton bPlan = new JButton("Planifier des interventions");
        bPlan.setOpaque(false);
        toolbar.add(bPlan);
        //
        this.add(toolbar, BorderLayout.NORTH);

        manager = new OperationCalendarManager("Plannings");
        calendar = new CalendarWithToolBar(manager);
        final MultipleDayView w = calendar.getWeekView();

        final JPanel filters = createFilterPanel(conf, w);
        split.setLeftComponent(filters);
        w.setHourRange(HOUR_START, HOUR_STOP);

        split.setRightComponent(calendar);
        split.setDividerLocation(250);
        this.add(split, BorderLayout.CENTER);
        conf.getRootSociete().getTable("CALENDAR_ITEM").addTableModifiedListener(new SQLTableModifiedListener() {

            @Override
            public void tableModified(SQLTableEvent evt) {
                reload();
            }

        });
        bPrint.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int w = calendar.getWeek();
                int y = calendar.getYear();
                final List<User> selectedUsers = getSelectedUsers();
                final List<String> selectedStates = getSelectedStates();
                final JFrame f = new JFrame(bPrint.getText());
                f.setContentPane(new CalendarPrintPanel(manager, w, y, selectedUsers, selectedStates));
                f.pack();
                // Un peu d'espace en largeur pour le titre
                f.setSize(f.getWidth() + 20, f.getHeight());
                f.setResizable(false);
                f.setLocationRelativeTo(OperationCalendarPanel.this);
                f.setVisible(true);
            }
        });
        final SQLElement element = conf.getDirectory().getElement(ModuleOperation.TABLE_OPERATION);
        // Create a new operation
        bCreate.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                final EditFrame f = new EditFrame(new OperationSQLComponent(element), EditMode.CREATION);
                FrameUtil.show(f);
            }
        });
        // Plan multiple operations
        bPlan.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                final EditFrame f = new EditFrame(new MultiOperationSQLComponent(element), EditMode.CREATION);
                f.setTitle("Plannication d'interventions");
                FrameUtil.show(f);
            }
        });
        calendar.scrollTo(8);
        w.setPopupMenuProvider(new OperationMenuProvider());
        this.beginStateSaving(conf.getConfDir(), w);
    }

    public static Map<Integer, Long> getDurations(List<List<JCalendarItem>> list, final Flag requiredFlag, final Flag excludedFlag) {
        final Map<Integer, Long> res = new HashMap<>();
        for (List<JCalendarItem> items : list) {
            for (JCalendarItem item : items) {
                if (!item.hasFlag(excludedFlag) && (requiredFlag == null || item.hasFlag(requiredFlag)) && item.getCookie() instanceof SQLRowValues) {
                    final SQLRowValues user = (SQLRowValues) item.getCookie();
                    final long toAddMinutes = (item.getDtEnd().getTimeInMillis() - item.getDtStart().getTimeInMillis()) / (60 * 1000);
                    final Integer key = user.getID();
                    final Long currentVal = res.get(key);
                    res.put(key, (currentVal == null ? 0 : currentVal.longValue()) + toAddMinutes);
                }
            }
        }
        return res;
    }

    public void reload() {
        calendar.reload();
    }

    @Override
    protected void setFilter(final JCheckBox check1, final JCheckBox check2, List<User> users, List<String> states) {
        if (manager.setFilter(users, states, check1.isSelected(), check2.isSelected()))
            reload();
    }

    @Override
    protected Date getViewDate() {
        return this.calendar.getCurrentDate().getTime();
    }

    @Override
    protected void setViewDate(Date d) {
        this.calendar.setCurrentDate(d);
    }
}