OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | 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.sql.users.rights;
15
 
73 ilm 16
import org.openconcerto.sql.Configuration;
17
import org.openconcerto.sql.TM;
18
import org.openconcerto.sql.element.SQLElement;
19
import org.openconcerto.sql.element.SQLElementDirectory;
17 ilm 20
import org.openconcerto.sql.model.SQLTable;
21
import org.openconcerto.sql.model.Where;
22
import org.openconcerto.sql.request.ListSQLRequest;
23
import org.openconcerto.sql.sqlobject.IComboSelectionItem;
24
import org.openconcerto.sql.users.UserManager;
25
import org.openconcerto.sql.view.ListeModifyPanel;
26
import org.openconcerto.ui.DefaultGridBagConstraints;
27
import org.openconcerto.utils.cc.IClosure;
28
 
29
import java.awt.GridBagConstraints;
30
import java.awt.GridBagLayout;
93 ilm 31
import java.awt.event.ActionEvent;
17 ilm 32
 
93 ilm 33
import javax.swing.JButton;
17 ilm 34
import javax.swing.JLabel;
35
import javax.swing.JPanel;
36
import javax.swing.JScrollPane;
37
import javax.swing.JSplitPane;
38
import javax.swing.event.ListSelectionEvent;
39
import javax.swing.event.ListSelectionListener;
40
 
41
public class UserRightsPanel extends JPanel {
42
 
43
    // Liste des utilisateurs
44
    private final JListSQLTablePanel list;
45
    private final ListeModifyPanel modifPanel;
46
 
47
    public UserRightsPanel() {
73 ilm 48
        this(Configuration.getInstance().getDirectory());
49
    }
50
 
51
    public UserRightsPanel(final SQLElementDirectory dir) {
17 ilm 52
        super(new GridBagLayout());
53
 
54
        // init the list before adding it, otherwise we see the first refresh from all lines to just
55
        // these of undef
93 ilm 56
        this.modifPanel = new ListeModifyPanel(dir.getElement(UserRightSQLElement.class)) {
57
            @Override
58
            protected void handleAction(JButton source, ActionEvent evt) {
59
                super.handleAction(source, evt);
60
                // if the user click add and the create frame is then hidden (not closed) and he
61
                // later click add again the create frame will be brought to the front, its content
62
                // unmodified even if he has selected another principal. So call reset() so that the
63
                // principal in the create frame match the selected principal.
64
                if (source == this.buttonAjouter) {
65
                    this.getAddComp().resetValue();
66
                }
67
            }
68
        };
69
        // don't listen to the list, as the behaviour differs if there's some rights for a principal
70
        // or not. I.e. if the user clicks add the current principal is selected :
71
        // - if the user select another principal with rights, the principal is updated
72
        // - if the user select another principal without rights, no lines are available and the
73
        // principal is not updated
74
        this.modifPanel.setDeaf(true);
17 ilm 75
        this.modifPanel.setSearchFullMode(false);
80 ilm 76
        // order is important for rights
77
        this.modifPanel.getListe().setSortingEnabled(false);
17 ilm 78
        final SQLTable table = this.getTable().getForeignTable("ID_USER_COMMON");
65 ilm 79
 
73 ilm 80
        final SQLElement usersElem = dir.getElement(table);
81
        this.list = new JListSQLTablePanel(JListSQLTablePanel.createComboRequest(usersElem, true), TM.tr("rightsPanel.defaultRights"));
17 ilm 82
        // only superusers can see superusers (that's how we prevent the setting of superuser
83
        // rights)
84
        if (!UserRightsManager.getCurrentUserRights().isSuperUser())
85
            this.list.getModel().setWhere(new Where(table.getField("SUPERUSER"), "=", false));
86
        this.list.getModel().setItemCustomizer(new IClosure<IComboSelectionItem>() {
87
            @Override
88
            public void executeChecked(IComboSelectionItem input) {
89
                if (input.getId() == UserManager.getUserID())
90
                    input.setFlag(IComboSelectionItem.IMPORTANT_FLAG);
91
            }
92
        });
93
        this.list.addListSelectionListener(new ListSelectionListener() {
94
            @Override
95
            public void valueChanged(ListSelectionEvent e) {
96
                updateListFromSelection();
97
            }
98
        });
99
        this.updateListFromSelection();
100
 
101
        // Liste des utilisateurs
102
        JPanel listePanel = new JPanel(new GridBagLayout());
103
        GridBagConstraints c = new DefaultGridBagConstraints();
104
        c.weightx = 1;
105
        c.gridwidth = GridBagConstraints.REMAINDER;
73 ilm 106
        listePanel.add(new JLabel(TM.getInstance().trM("element.list", "element", usersElem.getName())), c);
17 ilm 107
 
108
        c.weightx = 1;
109
        c.weighty = 1;
110
        c.gridy++;
111
        c.fill = GridBagConstraints.BOTH;
112
        listePanel.add(this.list, c);
113
 
114
        // Droits
115
        JPanel panelDroits = new JPanel(new GridBagLayout());
116
        GridBagConstraints c2 = new DefaultGridBagConstraints();
117
        c2.gridwidth = GridBagConstraints.REMAINDER;
73 ilm 118
        panelDroits.add(new JLabel(TM.tr("rights")), c2);
17 ilm 119
        c2.gridy++;
120
        c2.weightx = 1;
121
        c2.weighty = 0.7;
122
        c2.fill = GridBagConstraints.BOTH;
123
 
124
        panelDroits.add(new JScrollPane(this.modifPanel), c2);
125
 
126
        // SplitPane
127
        JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listePanel, panelDroits);
128
        GridBagConstraints c3 = new GridBagConstraints();
129
        c3.weightx = 1;
130
        c3.weighty = 1;
131
        c3.fill = GridBagConstraints.BOTH;
132
        this.add(pane, c3);
133
    }
134
 
135
    private void updateListFromSelection() {
136
        final int selectedIndex = this.list.getSelectedIndex();
137
 
138
        final boolean b = selectedIndex >= 0;
139
 
140
        final ListSQLRequest req = this.modifPanel.getListe().getRequest();
141
        final int userID;
142
        if (b) {
143
            userID = this.list.getModel().getRowAt(selectedIndex).getID();
144
        } else {
145
            // since we don't display user in the list (to avoid undef)
146
            // we need to always display at most one user
147
            userID = this.list.getModel().getTable().getUndefinedID();
148
        }
149
        req.setWhere(new Where(req.getPrimaryTable().getField("ID_USER_COMMON"), "=", userID));
150
 
151
        // enforce the limitation
93 ilm 152
        ((UserRightSQLComponent) this.modifPanel.getModifComp()).setUserID(userID);
153
        ((UserRightSQLComponent) this.modifPanel.getAddComp()).setUserID(userID);
17 ilm 154
    }
155
 
156
    public final SQLTable getTable() {
157
        return this.modifPanel.getElement().getTable();
158
    }
159
}