OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 73 | Rev 144 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.task.ui;
15
 
16
import org.openconcerto.sql.Configuration;
17
import org.openconcerto.sql.model.SQLBase;
18
import org.openconcerto.sql.model.SQLRow;
73 ilm 19
import org.openconcerto.sql.model.SQLRowListRSH;
17 ilm 20
import org.openconcerto.sql.model.SQLRowValues;
21
import org.openconcerto.sql.model.SQLSelect;
22
import org.openconcerto.sql.model.SQLTable;
23
import org.openconcerto.sql.model.Where;
24
import org.openconcerto.sql.users.User;
25
import org.openconcerto.task.UserTaskRight;
73 ilm 26
import org.openconcerto.ui.DisplayabilityListener;
27
import org.openconcerto.utils.ExceptionHandler;
17 ilm 28
 
73 ilm 29
import java.awt.Component;
17 ilm 30
import java.awt.Font;
31
import java.awt.GridBagConstraints;
32
import java.awt.GridBagLayout;
33
import java.awt.Insets;
34
import java.awt.event.MouseAdapter;
35
import java.awt.event.MouseEvent;
36
import java.sql.SQLException;
37
import java.util.List;
38
 
39
import javax.swing.ImageIcon;
40
import javax.swing.JLabel;
41
import javax.swing.JList;
42
import javax.swing.JPanel;
43
import javax.swing.JScrollPane;
44
import javax.swing.JSeparator;
45
import javax.swing.ListModel;
46
 
47
public class UserRightPanelDetail extends JPanel {
48
 
49
    JLabel labelDocumentation1 = new JLabel("");
50
    JLabel labelDocumentation2 = new JLabel("");
73 ilm 51
    UserListModel model = new UserListModel();
17 ilm 52
    UserTaskRightListCellRenderer cellRenderer1 = new UserTaskRightListCellRenderer(UserTaskRightListCellRenderer.READ);
53
    UserTaskRightListCellRenderer cellRenderer2 = new UserTaskRightListCellRenderer(UserTaskRightListCellRenderer.MODIFY);
54
    UserTaskRightListCellRenderer cellRenderer3 = new UserTaskRightListCellRenderer(UserTaskRightListCellRenderer.ADD);
55
    UserTaskRightListCellRenderer cellRenderer4 = new UserTaskRightListCellRenderer(UserTaskRightListCellRenderer.VALIDATE);
56
    private User selectedUser;
57
    private JList list1;
58
    private JList list2;
59
    private JList list3;
60
    private JList list4;
61
 
62
    UserRightPanelDetail() {
63
        this.setLayout(new GridBagLayout());
64
        GridBagConstraints c = new GridBagConstraints();
65
        c.insets = new Insets(4, 10, 2, 2);
66
        c.fill = GridBagConstraints.HORIZONTAL;
67
        c.gridx = 0;
68
        c.gridy = 0;
69
        c.gridwidth = 4;
70
        labelDocumentation1.setFont(labelDocumentation1.getFont().deriveFont(Font.BOLD));
71
        this.add(labelDocumentation1, c);
72
        c.gridy++;
73
        c.insets = new Insets(0, 10, 10, 2);
74
        this.add(labelDocumentation2, c);
75
        c.gridy++;
76
        c.insets = new Insets(0, 0, 0, 0);
77
        this.add(new JSeparator(JSeparator.HORIZONTAL), c);
78
 
79
        c.fill = GridBagConstraints.BOTH;
80
        c.insets = new Insets(2, 10, 2, 10);
81
        //
82
        c.gridwidth = 1;
83
        c.weightx = 1;
84
        int YTOP = c.gridy + 1;
85
 
86
        c.gridy = YTOP;
87
        c.weighty = 0;
88
        JLabel label1 = new JLabel("Voir les tâches assignées à:");
89
        this.add(label1, c);
90
        c.gridy++;
91
        list1 = new JList(model);
92
        list1.addMouseListener(new MouseAdapter() {
93
            public void mouseClicked(MouseEvent e) {
94
                swapOnDoubleClick(list1, e, "READ");
95
            }
96
        });
97
        list1.setCellRenderer(cellRenderer1);
98
        c.weighty = 1;
99
        this.add(new JScrollPane(list1), c);
100
        //
101
        c.gridx++;
102
        c.gridy = YTOP;
103
        c.weighty = 0;
104
        JLabel label2 = new JLabel("Modifier les tâches créées par:");
105
 
106
        this.add(label2, c);
107
        c.gridy++;
108
        list2 = new JList(model);
109
        list2.addMouseListener(new MouseAdapter() {
110
            public void mouseClicked(MouseEvent e) {
111
                swapOnDoubleClick(list2, e, "MODIFY");
112
            }
113
        });
114
        list2.setCellRenderer(cellRenderer2);
115
        c.weighty = 1;
116
        this.add(new JScrollPane(list2), c);
117
        //
118
        c.gridx++;
119
        c.gridy = YTOP;
120
        c.weighty = 0;
121
        JLabel label3 = new JLabel("Assigner des tâches à:");
122
        this.add(label3, c);
123
        c.gridy++;
124
        list3 = new JList(model);
125
        list3.addMouseListener(new MouseAdapter() {
126
            public void mouseClicked(MouseEvent e) {
127
                swapOnDoubleClick(list3, e, "ADD");
128
            }
129
        });
130
        list3.setCellRenderer(cellRenderer3);
131
        c.weighty = 1;
132
        this.add(new JScrollPane(list3), c);
133
        //
134
        c.gridx++;
135
        c.gridy = YTOP;
136
        c.weighty = 0;
137
        JLabel label4 = new JLabel("Valider les tâches de:");
138
        this.add(label4, c);
139
        c.gridy++;
140
        list4 = new JList(model);
141
        list4.addMouseListener(new MouseAdapter() {
142
            public void mouseClicked(MouseEvent e) {
143
                swapOnDoubleClick(list4, e, "VALIDATE");
144
            }
145
        });
146
        list4.setCellRenderer(cellRenderer4);
147
        c.weighty = 1;
148
        this.add(new JScrollPane(list4), c);
149
 
150
        c.weighty = 0;
151
        c.gridy++;
152
        c.fill = GridBagConstraints.HORIZONTAL;
153
        c.gridx = 0;
154
        c.gridwidth = 4;
155
        JLabel labelHelp = new JLabel("Une autorisation désactivée apparait en gris clair");
80 ilm 156
        labelHelp.setIcon(new ImageIcon(UserRightPanelDetail.class.getResource("toc_open.gif")));
17 ilm 157
        this.add(labelHelp, c);
158
 
73 ilm 159
        this.addHierarchyListener(new DisplayabilityListener() {
160
            @Override
161
            protected void displayabilityChanged(Component c) {
162
                if (c.isDisplayable())
163
                    model.start();
164
                else
165
                    model.stop();
166
            }
167
        });
168
 
17 ilm 169
        setUser(null);
170
    }
171
 
172
    /**
173
     * @param list
174
     * @param e
175
     */
176
    private void swapOnDoubleClick(final JList list, MouseEvent e, String field) {
177
        if (e.getClickCount() == 2) {
178
            int index = list.locationToIndex(e.getPoint());
179
            ListModel dlm = list.getModel();
180
            Object item = dlm.getElementAt(index);
181
            list.ensureIndexIsVisible(index);
182
            User toUser = (User) item;
183
            swapState(selectedUser, toUser, field);
184
 
185
        }
186
    }
187
 
188
    protected void swapState(User user, User toUser, String field) {
189
        final SQLBase defaultBase = Configuration.getInstance().getBase();
190
        final SQLTable tableTacheRights = defaultBase.getTable("TACHE_RIGHTS");
191
 
73 ilm 192
        final SQLSelect sel = new SQLSelect();
65 ilm 193
        sel.addSelectStar(tableTacheRights);
17 ilm 194
        Where where = new Where(tableTacheRights.getField("ID_USER_COMMON"), "=", user.getId());
195
        where = where.and(new Where(tableTacheRights.getField("ID_USER_COMMON_TO"), "=", toUser.getId()));
196
        sel.setWhere(where);
197
 
73 ilm 198
        final List<SQLRow> rows = SQLRowListRSH.execute(sel);
199
        final SQLRowValues rowV;
200
        if (rows.size() == 0) {
201
            rowV = new SQLRowValues(tableTacheRights);
202
            rowV.put("ID_USER_COMMON", user.getId());
203
            rowV.put("ID_USER_COMMON_TO", toUser.getId());
204
            rowV.put(field, Boolean.TRUE);
205
        } else if (rows.size() == 1) {
206
            final SQLRow row = rows.get(0);
207
            rowV = row.createEmptyUpdateRow();
208
            rowV.put(field, !row.getBoolean(field));
209
        } else {
210
            throw new IllegalStateException("More than one row : " + rows);
17 ilm 211
        }
212
 
213
        try {
214
            rowV.commit();
73 ilm 215
            setUser(this.selectedUser);
17 ilm 216
        } catch (SQLException e1) {
73 ilm 217
            ExceptionHandler.handle(this, "Impossible de changer la valeur", e1);
17 ilm 218
        }
219
    }
220
 
221
    public void setUser(User selectedUser) {
222
        if (selectedUser == null) {
223
            labelDocumentation1.setText(" Veuillez sélectionner un utilisateur");
224
            labelDocumentation2.setText(" Pour cela, cliquez sur un nom de la liste de gauche");
225
            list1.setEnabled(false);
226
            list2.setEnabled(false);
227
            list3.setEnabled(false);
228
            list4.setEnabled(false);
229
            return;
230
        }
231
 
232
        this.selectedUser = selectedUser;
233
 
63 ilm 234
        labelDocumentation1.setText(" Autorisations de l'utilisateur " + selectedUser.getFirstName() + " " + selectedUser.getName());
17 ilm 235
        labelDocumentation2.setText(" Double cliquez sur un nom d'une des colonnes suivantes pour activer/désactiver le droit correspondant.");
236
        List<UserTaskRight> l = UserTaskRight.getUserTaskRight(selectedUser);
237
        cellRenderer1.setUserTaskRight(l);
238
        cellRenderer2.setUserTaskRight(l);
239
        cellRenderer3.setUserTaskRight(l);
240
        cellRenderer4.setUserTaskRight(l);
241
        model.clearAndReload();
242
        list1.setEnabled(true);
243
        list2.setEnabled(true);
244
        list3.setEnabled(true);
245
        list4.setEnabled(true);
246
    }
247
 
248
}