OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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