OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
112 ilm 1
package org.openconcerto.modules.operation;
2
 
3
import java.awt.BorderLayout;
4
import java.awt.FlowLayout;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
150 ilm 7
import java.util.Date;
8
import java.util.HashMap;
112 ilm 9
import java.util.List;
150 ilm 10
import java.util.Map;
112 ilm 11
 
12
import javax.swing.JButton;
13
import javax.swing.JCheckBox;
14
import javax.swing.JFrame;
15
import javax.swing.JPanel;
16
 
17
import org.jopencalendar.model.Flag;
18
import org.jopencalendar.model.JCalendarItem;
19
import org.jopencalendar.ui.CalendarWithToolBar;
20
import org.jopencalendar.ui.MultipleDayView;
21
import org.openconcerto.erp.config.ComptaPropsConfiguration;
22
import org.openconcerto.sql.element.SQLElement;
23
import org.openconcerto.sql.model.SQLRowValues;
24
import org.openconcerto.sql.model.SQLTableEvent;
25
import org.openconcerto.sql.model.SQLTableModifiedListener;
26
import org.openconcerto.sql.users.User;
27
import org.openconcerto.sql.view.EditFrame;
28
import org.openconcerto.sql.view.EditPanel.EditMode;
29
import org.openconcerto.ui.FrameUtil;
30
 
31
/**
32
 * Planning de la semaine
33
 */
34
public class OperationCalendarPanel extends AbstractCalendarPanel {
35
 
36
    private CalendarWithToolBar calendar;
37
    private OperationCalendarManager manager;
38
 
150 ilm 39
    public OperationCalendarPanel(final ComptaPropsConfiguration conf) {
112 ilm 40
        this.setLayout(new BorderLayout());
41
        JPanel toolbar = new JPanel();
42
        toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
43
        toolbar.setOpaque(false);
44
        final JButton bPrint = new JButton("Imprimer les plannings des interventions");
45
        bPrint.setOpaque(false);
46
        toolbar.add(bPrint);
47
        //
48
        final JButton bCreate = new JButton("Ajouter une intervention");
49
        bCreate.setOpaque(false);
50
        toolbar.add(bCreate);
51
        //
150 ilm 52
        final JButton bPlan = new JButton("Planifier des interventions");
112 ilm 53
        bPlan.setOpaque(false);
54
        toolbar.add(bPlan);
55
        //
56
        this.add(toolbar, BorderLayout.NORTH);
57
 
58
        manager = new OperationCalendarManager("Plannings");
59
        calendar = new CalendarWithToolBar(manager);
60
        final MultipleDayView w = calendar.getWeekView();
150 ilm 61
 
62
        final JPanel filters = createFilterPanel(conf, w);
63
        split.setLeftComponent(filters);
112 ilm 64
        w.setHourRange(HOUR_START, HOUR_STOP);
65
 
66
        split.setRightComponent(calendar);
67
        split.setDividerLocation(250);
68
        this.add(split, BorderLayout.CENTER);
150 ilm 69
        conf.getRootSociete().getTable("CALENDAR_ITEM").addTableModifiedListener(new SQLTableModifiedListener() {
112 ilm 70
 
71
            @Override
72
            public void tableModified(SQLTableEvent evt) {
73
                reload();
74
            }
75
 
76
        });
77
        bPrint.addActionListener(new ActionListener() {
78
 
79
            @Override
80
            public void actionPerformed(ActionEvent e) {
81
                int w = calendar.getWeek();
82
                int y = calendar.getYear();
83
                final List<User> selectedUsers = getSelectedUsers();
84
                final List<String> selectedStates = getSelectedStates();
85
                final JFrame f = new JFrame(bPrint.getText());
86
                f.setContentPane(new CalendarPrintPanel(manager, w, y, selectedUsers, selectedStates));
87
                f.pack();
88
                // Un peu d'espace en largeur pour le titre
89
                f.setSize(f.getWidth() + 20, f.getHeight());
90
                f.setResizable(false);
91
                f.setLocationRelativeTo(OperationCalendarPanel.this);
92
                f.setVisible(true);
93
            }
94
        });
150 ilm 95
        final SQLElement element = conf.getDirectory().getElement(ModuleOperation.TABLE_OPERATION);
112 ilm 96
        // Create a new operation
97
        bCreate.addActionListener(new ActionListener() {
98
 
99
            @Override
100
            public void actionPerformed(ActionEvent e) {
101
                final EditFrame f = new EditFrame(new OperationSQLComponent(element), EditMode.CREATION);
102
                FrameUtil.show(f);
103
            }
104
        });
105
        // Plan multiple operations
106
        bPlan.addActionListener(new ActionListener() {
107
 
108
            @Override
109
            public void actionPerformed(ActionEvent e) {
110
                final EditFrame f = new EditFrame(new MultiOperationSQLComponent(element), EditMode.CREATION);
111
                f.setTitle("Plannication d'interventions");
112
                FrameUtil.show(f);
113
            }
114
        });
115
        calendar.scrollTo(8);
150 ilm 116
        w.setPopupMenuProvider(new OperationMenuProvider());
117
        this.beginStateSaving(conf.getConfDir(), w);
112 ilm 118
    }
119
 
181 ilm 120
    public static Map<Integer, Long> getDurations(List<List<JCalendarItem>> list, final Flag requiredFlag, final Flag excludedFlag) {
150 ilm 121
        final Map<Integer, Long> res = new HashMap<>();
112 ilm 122
        for (List<JCalendarItem> items : list) {
123
            for (JCalendarItem item : items) {
181 ilm 124
                if (!item.hasFlag(excludedFlag) && (requiredFlag == null || item.hasFlag(requiredFlag)) && item.getCookie() instanceof SQLRowValues) {
150 ilm 125
                    final SQLRowValues user = (SQLRowValues) item.getCookie();
126
                    final long toAddMinutes = (item.getDtEnd().getTimeInMillis() - item.getDtStart().getTimeInMillis()) / (60 * 1000);
127
                    final Integer key = user.getID();
128
                    final Long currentVal = res.get(key);
129
                    res.put(key, (currentVal == null ? 0 : currentVal.longValue()) + toAddMinutes);
112 ilm 130
                }
131
            }
132
        }
150 ilm 133
        return res;
112 ilm 134
    }
135
 
136
    public void reload() {
137
        calendar.reload();
138
    }
139
 
150 ilm 140
    @Override
141
    protected void setFilter(final JCheckBox check1, final JCheckBox check2, List<User> users, List<String> states) {
142
        if (manager.setFilter(users, states, check1.isSelected(), check2.isSelected()))
143
            reload();
144
    }
112 ilm 145
 
150 ilm 146
    @Override
147
    protected Date getViewDate() {
148
        return this.calendar.getCurrentDate().getTime();
149
    }
112 ilm 150
 
150 ilm 151
    @Override
152
    protected void setViewDate(Date d) {
153
        this.calendar.setCurrentDate(d);
112 ilm 154
    }
155
}