commit | author | age
|
f08aab
|
1 |
/*
|
JM |
2 |
* Copyright 2011 gitblit.com.
|
|
3 |
*
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
6 |
* You may obtain a copy of the License at
|
|
7 |
*
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9 |
*
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13 |
* See the License for the specific language governing permissions and
|
|
14 |
* limitations under the License.
|
|
15 |
*/
|
|
16 |
package com.gitblit.client;
|
|
17 |
|
|
18 |
import java.awt.BorderLayout;
|
|
19 |
import java.awt.Dimension;
|
|
20 |
import java.awt.FlowLayout;
|
|
21 |
import java.awt.Font;
|
|
22 |
import java.awt.GridLayout;
|
|
23 |
import java.awt.Insets;
|
|
24 |
import java.awt.event.ActionEvent;
|
|
25 |
import java.awt.event.ActionListener;
|
|
26 |
import java.awt.event.KeyEvent;
|
|
27 |
import java.text.MessageFormat;
|
|
28 |
import java.util.ArrayList;
|
|
29 |
import java.util.Collections;
|
|
30 |
import java.util.HashSet;
|
|
31 |
import java.util.List;
|
|
32 |
import java.util.Set;
|
|
33 |
|
|
34 |
import javax.swing.ImageIcon;
|
|
35 |
import javax.swing.JButton;
|
|
36 |
import javax.swing.JComponent;
|
|
37 |
import javax.swing.JDialog;
|
|
38 |
import javax.swing.JLabel;
|
|
39 |
import javax.swing.JOptionPane;
|
|
40 |
import javax.swing.JPanel;
|
|
41 |
import javax.swing.JRootPane;
|
|
42 |
import javax.swing.JTabbedPane;
|
|
43 |
import javax.swing.JTextField;
|
|
44 |
import javax.swing.KeyStroke;
|
|
45 |
|
|
46 |
import com.gitblit.Constants.AccessRestrictionType;
|
|
47 |
import com.gitblit.models.RepositoryModel;
|
|
48 |
import com.gitblit.models.ServerSettings;
|
|
49 |
import com.gitblit.models.TeamModel;
|
|
50 |
import com.gitblit.utils.StringUtils;
|
|
51 |
|
|
52 |
public class EditTeamDialog extends JDialog {
|
|
53 |
|
|
54 |
private static final long serialVersionUID = 1L;
|
|
55 |
|
|
56 |
private final String teamname;
|
|
57 |
|
|
58 |
private final TeamModel team;
|
|
59 |
|
|
60 |
private final ServerSettings settings;
|
|
61 |
|
|
62 |
private boolean isCreate;
|
|
63 |
|
|
64 |
private boolean canceled = true;
|
|
65 |
|
|
66 |
private JTextField teamnameField;
|
|
67 |
|
0b9119
|
68 |
private JTextField mailingListsField;
|
JM |
69 |
|
f08aab
|
70 |
private JPalette<String> repositoryPalette;
|
JM |
71 |
|
|
72 |
private JPalette<String> userPalette;
|
|
73 |
|
97d3af
|
74 |
private JPalette<String> preReceivePalette;
|
JM |
75 |
|
f2dff4
|
76 |
private JLabel preReceiveInherited;
|
JM |
77 |
|
97d3af
|
78 |
private JPalette<String> postReceivePalette;
|
f2dff4
|
79 |
|
JM |
80 |
private JLabel postReceiveInherited;
|
97d3af
|
81 |
|
f08aab
|
82 |
private Set<String> teamnames;
|
JM |
83 |
|
|
84 |
public EditTeamDialog(int protocolVersion, ServerSettings settings) {
|
|
85 |
this(protocolVersion, new TeamModel(""), settings);
|
|
86 |
this.isCreate = true;
|
|
87 |
setTitle(Translation.get("gb.newTeam"));
|
|
88 |
}
|
|
89 |
|
|
90 |
public EditTeamDialog(int protocolVersion, TeamModel aTeam, ServerSettings settings) {
|
|
91 |
super();
|
|
92 |
this.teamname = aTeam.name;
|
|
93 |
this.team = new TeamModel("");
|
|
94 |
this.settings = settings;
|
|
95 |
this.teamnames = new HashSet<String>();
|
|
96 |
this.isCreate = false;
|
|
97 |
initialize(protocolVersion, aTeam);
|
|
98 |
setModal(true);
|
|
99 |
setTitle(Translation.get("gb.edit") + ": " + aTeam.name);
|
|
100 |
setIconImage(new ImageIcon(getClass().getResource("/gitblt-favicon.png")).getImage());
|
|
101 |
}
|
|
102 |
|
|
103 |
@Override
|
|
104 |
protected JRootPane createRootPane() {
|
|
105 |
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
|
|
106 |
JRootPane rootPane = new JRootPane();
|
|
107 |
rootPane.registerKeyboardAction(new ActionListener() {
|
|
108 |
public void actionPerformed(ActionEvent actionEvent) {
|
|
109 |
setVisible(false);
|
|
110 |
}
|
|
111 |
}, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
|
|
112 |
return rootPane;
|
|
113 |
}
|
|
114 |
|
|
115 |
private void initialize(int protocolVersion, TeamModel aTeam) {
|
|
116 |
teamnameField = new JTextField(aTeam.name == null ? "" : aTeam.name, 25);
|
|
117 |
|
0b9119
|
118 |
mailingListsField = new JTextField(aTeam.mailingLists == null ? ""
|
JM |
119 |
: StringUtils.flattenStrings(aTeam.mailingLists, " "), 50);
|
|
120 |
|
f08aab
|
121 |
JPanel fieldsPanel = new JPanel(new GridLayout(0, 1));
|
JM |
122 |
fieldsPanel.add(newFieldPanel(Translation.get("gb.teamName"), teamnameField));
|
0b9119
|
123 |
fieldsPanel.add(newFieldPanel(Translation.get("gb.mailingLists"), mailingListsField));
|
f08aab
|
124 |
|
JM |
125 |
final Insets _insets = new Insets(5, 5, 5, 5);
|
|
126 |
repositoryPalette = new JPalette<String>();
|
|
127 |
userPalette = new JPalette<String>();
|
0b9119
|
128 |
|
f08aab
|
129 |
JPanel fieldsPanelTop = new JPanel(new BorderLayout());
|
JM |
130 |
fieldsPanelTop.add(fieldsPanel, BorderLayout.NORTH);
|
0b9119
|
131 |
|
f08aab
|
132 |
JPanel repositoriesPanel = new JPanel(new BorderLayout()) {
|
JM |
133 |
|
|
134 |
private static final long serialVersionUID = 1L;
|
|
135 |
|
|
136 |
public Insets getInsets() {
|
|
137 |
return _insets;
|
|
138 |
}
|
|
139 |
};
|
|
140 |
repositoriesPanel.add(repositoryPalette, BorderLayout.CENTER);
|
|
141 |
|
|
142 |
JPanel usersPanel = new JPanel(new BorderLayout()) {
|
|
143 |
|
|
144 |
private static final long serialVersionUID = 1L;
|
|
145 |
|
|
146 |
public Insets getInsets() {
|
|
147 |
return _insets;
|
|
148 |
}
|
|
149 |
};
|
|
150 |
usersPanel.add(userPalette, BorderLayout.CENTER);
|
|
151 |
|
97d3af
|
152 |
preReceivePalette = new JPalette<String>(true);
|
f2dff4
|
153 |
preReceiveInherited = new JLabel();
|
97d3af
|
154 |
JPanel preReceivePanel = new JPanel(new BorderLayout(5, 5));
|
f2dff4
|
155 |
preReceivePanel.add(preReceivePalette, BorderLayout.CENTER);
|
JM |
156 |
preReceivePanel.add(preReceiveInherited, BorderLayout.WEST);
|
|
157 |
|
97d3af
|
158 |
postReceivePalette = new JPalette<String>(true);
|
f2dff4
|
159 |
postReceiveInherited = new JLabel();
|
97d3af
|
160 |
JPanel postReceivePanel = new JPanel(new BorderLayout(5, 5));
|
f2dff4
|
161 |
postReceivePanel.add(postReceivePalette, BorderLayout.CENTER);
|
JM |
162 |
postReceivePanel.add(postReceiveInherited, BorderLayout.WEST);
|
97d3af
|
163 |
|
f08aab
|
164 |
JTabbedPane panel = new JTabbedPane(JTabbedPane.TOP);
|
JM |
165 |
panel.addTab(Translation.get("gb.general"), fieldsPanelTop);
|
|
166 |
panel.addTab(Translation.get("gb.teamMembers"), usersPanel);
|
|
167 |
panel.addTab(Translation.get("gb.restrictedRepositories"), repositoriesPanel);
|
97d3af
|
168 |
panel.addTab(Translation.get("gb.preReceiveScripts"), preReceivePanel);
|
JM |
169 |
panel.addTab(Translation.get("gb.postReceiveScripts"), postReceivePanel);
|
f08aab
|
170 |
|
JM |
171 |
JButton createButton = new JButton(Translation.get("gb.save"));
|
|
172 |
createButton.addActionListener(new ActionListener() {
|
|
173 |
public void actionPerformed(ActionEvent event) {
|
|
174 |
if (validateFields()) {
|
|
175 |
canceled = false;
|
|
176 |
setVisible(false);
|
|
177 |
}
|
|
178 |
}
|
|
179 |
});
|
|
180 |
|
|
181 |
JButton cancelButton = new JButton(Translation.get("gb.cancel"));
|
|
182 |
cancelButton.addActionListener(new ActionListener() {
|
|
183 |
public void actionPerformed(ActionEvent event) {
|
|
184 |
canceled = true;
|
|
185 |
setVisible(false);
|
|
186 |
}
|
|
187 |
});
|
|
188 |
|
|
189 |
JPanel controls = new JPanel();
|
|
190 |
controls.add(cancelButton);
|
|
191 |
controls.add(createButton);
|
0b9119
|
192 |
|
f08aab
|
193 |
JPanel centerPanel = new JPanel(new BorderLayout(5, 5)) {
|
JM |
194 |
|
|
195 |
private static final long serialVersionUID = 1L;
|
|
196 |
|
|
197 |
@Override
|
|
198 |
public Insets getInsets() {
|
|
199 |
return _insets;
|
|
200 |
}
|
|
201 |
};
|
|
202 |
centerPanel.add(panel, BorderLayout.CENTER);
|
|
203 |
centerPanel.add(controls, BorderLayout.SOUTH);
|
|
204 |
|
|
205 |
getContentPane().setLayout(new BorderLayout(5, 5));
|
|
206 |
getContentPane().add(centerPanel, BorderLayout.CENTER);
|
|
207 |
pack();
|
|
208 |
}
|
|
209 |
|
|
210 |
private JPanel newFieldPanel(String label, JComponent comp) {
|
|
211 |
JLabel fieldLabel = new JLabel(label);
|
|
212 |
fieldLabel.setFont(fieldLabel.getFont().deriveFont(Font.BOLD));
|
|
213 |
fieldLabel.setPreferredSize(new Dimension(150, 20));
|
|
214 |
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
|
|
215 |
panel.add(fieldLabel);
|
|
216 |
panel.add(comp);
|
|
217 |
return panel;
|
|
218 |
}
|
|
219 |
|
|
220 |
private boolean validateFields() {
|
|
221 |
String tname = teamnameField.getText();
|
|
222 |
if (StringUtils.isEmpty(tname)) {
|
|
223 |
error("Please enter a team name!");
|
|
224 |
return false;
|
|
225 |
}
|
|
226 |
|
|
227 |
boolean rename = false;
|
|
228 |
// verify teamname uniqueness on create
|
|
229 |
if (isCreate) {
|
|
230 |
if (teamnames.contains(tname.toLowerCase())) {
|
|
231 |
error(MessageFormat.format("Team name ''{0}'' is unavailable.", tname));
|
|
232 |
return false;
|
|
233 |
}
|
|
234 |
} else {
|
|
235 |
// check rename collision
|
|
236 |
rename = !StringUtils.isEmpty(teamname) && !teamname.equalsIgnoreCase(tname);
|
|
237 |
if (rename) {
|
|
238 |
if (teamnames.contains(tname.toLowerCase())) {
|
|
239 |
error(MessageFormat.format(
|
|
240 |
"Failed to rename ''{0}'' because ''{1}'' already exists.", teamname,
|
|
241 |
tname));
|
|
242 |
return false;
|
|
243 |
}
|
|
244 |
}
|
|
245 |
}
|
|
246 |
team.name = tname;
|
|
247 |
|
0b9119
|
248 |
String ml = mailingListsField.getText();
|
JM |
249 |
if (!StringUtils.isEmpty(ml)) {
|
|
250 |
Set<String> list = new HashSet<String>();
|
|
251 |
for (String address : ml.split("(,|\\s)")) {
|
|
252 |
if (StringUtils.isEmpty(address)) {
|
|
253 |
continue;
|
|
254 |
}
|
|
255 |
list.add(address.toLowerCase());
|
|
256 |
}
|
|
257 |
team.mailingLists.clear();
|
|
258 |
team.mailingLists.addAll(list);
|
|
259 |
}
|
|
260 |
|
f08aab
|
261 |
team.repositories.clear();
|
JM |
262 |
team.repositories.addAll(repositoryPalette.getSelections());
|
0b9119
|
263 |
|
f08aab
|
264 |
team.users.clear();
|
JM |
265 |
team.users.addAll(userPalette.getSelections());
|
97d3af
|
266 |
|
JM |
267 |
team.preReceiveScripts.clear();
|
|
268 |
team.preReceiveScripts.addAll(preReceivePalette.getSelections());
|
|
269 |
|
|
270 |
team.postReceiveScripts.clear();
|
|
271 |
team.postReceiveScripts.addAll(postReceivePalette.getSelections());
|
|
272 |
|
f08aab
|
273 |
return true;
|
JM |
274 |
}
|
|
275 |
|
|
276 |
private void error(String message) {
|
|
277 |
JOptionPane.showMessageDialog(EditTeamDialog.this, message, Translation.get("gb.error"),
|
|
278 |
JOptionPane.ERROR_MESSAGE);
|
|
279 |
}
|
|
280 |
|
|
281 |
public void setTeams(List<TeamModel> teams) {
|
|
282 |
teamnames.clear();
|
|
283 |
for (TeamModel team : teams) {
|
|
284 |
teamnames.add(team.name.toLowerCase());
|
|
285 |
}
|
|
286 |
}
|
|
287 |
|
|
288 |
public void setRepositories(List<RepositoryModel> repositories, List<String> selected) {
|
|
289 |
List<String> restricted = new ArrayList<String>();
|
|
290 |
for (RepositoryModel repo : repositories) {
|
|
291 |
if (repo.accessRestriction.exceeds(AccessRestrictionType.NONE)) {
|
|
292 |
restricted.add(repo.name);
|
|
293 |
}
|
|
294 |
}
|
|
295 |
StringUtils.sortRepositorynames(restricted);
|
|
296 |
if (selected != null) {
|
|
297 |
StringUtils.sortRepositorynames(selected);
|
|
298 |
}
|
|
299 |
repositoryPalette.setObjects(restricted, selected);
|
|
300 |
}
|
0b9119
|
301 |
|
f08aab
|
302 |
public void setUsers(List<String> users, List<String> selected) {
|
JM |
303 |
Collections.sort(users);
|
|
304 |
if (selected != null) {
|
|
305 |
Collections.sort(selected);
|
|
306 |
}
|
|
307 |
userPalette.setObjects(users, selected);
|
|
308 |
}
|
|
309 |
|
f2dff4
|
310 |
public void setPreReceiveScripts(List<String> unused, List<String> inherited,
|
JM |
311 |
List<String> selected) {
|
97d3af
|
312 |
Collections.sort(unused);
|
JM |
313 |
if (selected != null) {
|
|
314 |
Collections.sort(selected);
|
|
315 |
}
|
|
316 |
preReceivePalette.setObjects(unused, selected);
|
f2dff4
|
317 |
showInherited(inherited, preReceiveInherited);
|
97d3af
|
318 |
}
|
JM |
319 |
|
f2dff4
|
320 |
public void setPostReceiveScripts(List<String> unused, List<String> inherited,
|
JM |
321 |
List<String> selected) {
|
97d3af
|
322 |
Collections.sort(unused);
|
JM |
323 |
if (selected != null) {
|
|
324 |
Collections.sort(selected);
|
|
325 |
}
|
|
326 |
postReceivePalette.setObjects(unused, selected);
|
f2dff4
|
327 |
showInherited(inherited, postReceiveInherited);
|
JM |
328 |
}
|
|
329 |
|
|
330 |
private void showInherited(List<String> list, JLabel label) {
|
|
331 |
StringBuilder sb = new StringBuilder();
|
|
332 |
if (list != null && list.size() > 0) {
|
|
333 |
sb.append("<html><body><b>INHERITED</b><ul style=\"margin-left:5px;list-style-type: none;\">");
|
|
334 |
for (String script : list) {
|
|
335 |
sb.append("<li>").append(script).append("</li>");
|
|
336 |
}
|
|
337 |
sb.append("</ul></body></html>");
|
|
338 |
}
|
|
339 |
label.setText(sb.toString());
|
97d3af
|
340 |
}
|
JM |
341 |
|
f08aab
|
342 |
public TeamModel getTeam() {
|
JM |
343 |
if (canceled) {
|
|
344 |
return null;
|
|
345 |
}
|
|
346 |
return team;
|
|
347 |
}
|
|
348 |
}
|