1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
/*
* Copyright 2012 gitblit.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gitblit.client;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import com.gitblit.Constants.AccessPermission;
import com.gitblit.Constants.PermissionType;
import com.gitblit.Constants.RegistrantType;
import com.gitblit.models.RegistrantAccessPermission;
import com.gitblit.utils.StringUtils;
public class RegistrantPermissionsPanel extends JPanel {
private static final long serialVersionUID = 1L;
private JTable permissionsTable;
private RegistrantPermissionsTableModel tableModel;
private DefaultComboBoxModel registrantModel;
private JComboBox registrantSelector;
private JComboBox permissionSelector;
private JButton addButton;
private JPanel addPanel;
public RegistrantPermissionsPanel(final RegistrantType registrantType) {
super(new BorderLayout(5, 5));
tableModel = new RegistrantPermissionsTableModel();
permissionsTable = Utils.newTable(tableModel, Utils.DATE_FORMAT);
permissionsTable.setModel(tableModel);
permissionsTable.setPreferredScrollableViewportSize(new Dimension(400, 150));
JScrollPane jsp = new JScrollPane(permissionsTable);
add(jsp, BorderLayout.CENTER);
permissionsTable.getColumnModel().getColumn(RegistrantPermissionsTableModel.Columns.Registrant.ordinal())
.setCellRenderer(new NameRenderer());
permissionsTable.getColumnModel().getColumn(RegistrantPermissionsTableModel.Columns.Type.ordinal())
.setCellRenderer(new PermissionTypeRenderer());
permissionsTable.getColumnModel().getColumn(RegistrantPermissionsTableModel.Columns.Permission.ordinal())
.setCellEditor(new AccessPermissionEditor());
registrantModel = new DefaultComboBoxModel();
registrantSelector = new JComboBox(registrantModel);
permissionSelector = new JComboBox(AccessPermission.NEWPERMISSIONS);
addButton = new JButton(Translation.get("gb.add"));
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (registrantSelector.getSelectedIndex() < 0) {
return;
}
if (permissionSelector.getSelectedIndex() < 0) {
return;
}
RegistrantAccessPermission rp = new RegistrantAccessPermission(registrantType);
rp.registrant = registrantSelector.getSelectedItem().toString();
rp.permission = (AccessPermission) permissionSelector.getSelectedItem();
if (StringUtils.findInvalidCharacter(rp.registrant) != null) {
rp.permissionType = PermissionType.REGEX;
} else {
rp.permissionType = PermissionType.EXPLICIT;
}
tableModel.permissions.add(rp);
registrantModel.removeElement(rp.registrant);
registrantSelector.setSelectedIndex(-1);
registrantSelector.invalidate();
addPanel.setVisible(registrantModel.getSize() > 0);
tableModel.fireTableDataChanged();
}
});
addPanel = new JPanel();
addPanel.add(registrantSelector);
addPanel.add(permissionSelector);
addPanel.add(addButton);
add(addPanel, BorderLayout.SOUTH);
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
permissionsTable.setEnabled(enabled);
registrantSelector.setEnabled(enabled);
permissionSelector.setEnabled(enabled);
addButton.setEnabled(enabled);
}
public void setObjects(List<String> registrants, List<RegistrantAccessPermission> permissions) {
List<String> filtered;
if (registrants == null) {
filtered = new ArrayList<String>();
} else {
filtered = new ArrayList<String>(registrants);
}
if (permissions == null) {
permissions = new ArrayList<RegistrantAccessPermission>();
}
for (RegistrantAccessPermission rp : permissions) {
if (rp.mutable) {
// only remove editable duplicates
// this allows for specifying an explicit permission
filtered.remove(rp.registrant);
} else if (rp.isAdmin()) {
// administrators can not have their permission changed
filtered.remove(rp.registrant);
} else if (rp.isOwner()) {
// owners can not have their permission changed
filtered.remove(rp.registrant);
}
}
for (String registrant : filtered) {
registrantModel.addElement(registrant);
}
tableModel.setPermissions(permissions);
registrantSelector.setSelectedIndex(-1);
permissionSelector.setSelectedIndex(-1);
addPanel.setVisible(filtered.size() > 0);
}
public List<RegistrantAccessPermission> getPermissions() {
return tableModel.permissions;
}
private class AccessPermissionEditor extends DefaultCellEditor {
private static final long serialVersionUID = 1L;
public AccessPermissionEditor() {
super(new JComboBox(AccessPermission.values()));
}
}
private class PermissionTypeRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
public PermissionTypeRenderer() {
super();
setHorizontalAlignment(SwingConstants.CENTER);
}
@Override
protected void setValue(Object value) {
RegistrantAccessPermission ap = (RegistrantAccessPermission) value;
switch (ap.permissionType) {
case ADMINISTRATOR:
setText(ap.source == null ? Translation.get("gb.administrator") : ap.source);
setToolTipText(Translation.get("gb.administratorPermission"));
break;
case OWNER:
setText(Translation.get("gb.owner"));
setToolTipText(Translation.get("gb.ownerPermission"));
break;
case TEAM:
setText(ap.source == null ? Translation.get("gb.team") : ap.source);
setToolTipText(MessageFormat.format(Translation.get("gb.teamPermission"), ap.source));
break;
case REGEX:
setText("regex");
setToolTipText(MessageFormat.format(Translation.get("gb.regexPermission"), ap.source));
break;
default:
if (ap.isMissing()) {
setText(Translation.get("gb.missing"));
setToolTipText(Translation.get("gb.missingPermission"));
} else {
setText("");
setToolTipText(null);
}
break;
}
}
}
}
|