OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 65 | Go to most recent revision | Details | 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.sql.users.rights;
15
 
16
import org.openconcerto.sql.model.SQLTable;
17
import org.openconcerto.sql.model.Where;
18
import org.openconcerto.sql.request.ListSQLRequest;
19
import org.openconcerto.sql.sqlobject.IComboSelectionItem;
20
import org.openconcerto.sql.users.UserManager;
21
import org.openconcerto.sql.users.rights.UserRightSQLElement.UserRightComp;
22
import org.openconcerto.sql.view.ListeModifyPanel;
23
import org.openconcerto.ui.DefaultGridBagConstraints;
24
import org.openconcerto.utils.cc.IClosure;
25
 
26
import java.awt.GridBagConstraints;
27
import java.awt.GridBagLayout;
28
import java.util.Arrays;
29
 
30
import javax.swing.JLabel;
31
import javax.swing.JPanel;
32
import javax.swing.JScrollPane;
33
import javax.swing.JSplitPane;
34
import javax.swing.event.ListSelectionEvent;
35
import javax.swing.event.ListSelectionListener;
36
 
37
public class UserRightsPanel extends JPanel {
38
 
39
    // Liste des utilisateurs
40
    private final JListSQLTablePanel list;
41
    private final ListeModifyPanel modifPanel;
42
 
43
    public UserRightsPanel() {
44
        super(new GridBagLayout());
45
 
46
        // init the list before adding it, otherwise we see the first refresh from all lines to just
47
        // these of undef
48
        // instantiate our own element to be safe
49
        this.modifPanel = new ListeModifyPanel(new UserRightSQLElement());
50
        this.modifPanel.setSearchFullMode(false);
51
        final SQLTable table = this.getTable().getForeignTable("ID_USER_COMMON");
52
        this.list = new JListSQLTablePanel(table, Arrays.asList("NOM", "PRENOM"), "Droits par défaut");
53
        // only superusers can see superusers (that's how we prevent the setting of superuser
54
        // rights)
55
        if (!UserRightsManager.getCurrentUserRights().isSuperUser())
56
            this.list.getModel().setWhere(new Where(table.getField("SUPERUSER"), "=", false));
57
        this.list.getModel().setItemCustomizer(new IClosure<IComboSelectionItem>() {
58
            @Override
59
            public void executeChecked(IComboSelectionItem input) {
60
                if (input.getId() == UserManager.getUserID())
61
                    input.setFlag(IComboSelectionItem.IMPORTANT_FLAG);
62
            }
63
        });
64
        this.list.addListSelectionListener(new ListSelectionListener() {
65
            @Override
66
            public void valueChanged(ListSelectionEvent e) {
67
                updateListFromSelection();
68
            }
69
        });
70
        this.updateListFromSelection();
71
 
72
        // Liste des utilisateurs
73
        JPanel listePanel = new JPanel(new GridBagLayout());
74
        GridBagConstraints c = new DefaultGridBagConstraints();
75
        c.weightx = 1;
76
        c.gridwidth = GridBagConstraints.REMAINDER;
77
        listePanel.add(new JLabel("Liste des utilisateurs"), c);
78
 
79
        c.weightx = 1;
80
        c.weighty = 1;
81
        c.gridy++;
82
        c.fill = GridBagConstraints.BOTH;
83
        listePanel.add(this.list, c);
84
 
85
        // Droits
86
        JPanel panelDroits = new JPanel(new GridBagLayout());
87
        GridBagConstraints c2 = new DefaultGridBagConstraints();
88
        c2.gridwidth = GridBagConstraints.REMAINDER;
89
        panelDroits.add(new JLabel("Droits"), c2);
90
        c2.gridy++;
91
        c2.weightx = 1;
92
        c2.weighty = 0.7;
93
        c2.fill = GridBagConstraints.BOTH;
94
 
95
        panelDroits.add(new JScrollPane(this.modifPanel), c2);
96
 
97
        // SplitPane
98
        JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listePanel, panelDroits);
99
        GridBagConstraints c3 = new GridBagConstraints();
100
        c3.weightx = 1;
101
        c3.weighty = 1;
102
        c3.fill = GridBagConstraints.BOTH;
103
        this.add(pane, c3);
104
    }
105
 
106
    private void updateListFromSelection() {
107
        final int selectedIndex = this.list.getSelectedIndex();
108
 
109
        final boolean b = selectedIndex >= 0;
110
 
111
        final ListSQLRequest req = this.modifPanel.getListe().getRequest();
112
        final int userID;
113
        if (b) {
114
            userID = this.list.getModel().getRowAt(selectedIndex).getID();
115
        } else {
116
            // since we don't display user in the list (to avoid undef)
117
            // we need to always display at most one user
118
            userID = this.list.getModel().getTable().getUndefinedID();
119
        }
120
        req.setWhere(new Where(req.getPrimaryTable().getField("ID_USER_COMMON"), "=", userID));
121
 
122
        // enforce the limitation
123
        ((UserRightComp) this.modifPanel.getModifComp()).setUserID(userID);
124
        ((UserRightComp) this.modifPanel.getAddComp()).setUserID(userID);
125
    }
126
 
127
    public final SQLTable getTable() {
128
        return this.modifPanel.getElement().getTable();
129
    }
130
}