From e92c6d230b3a350749fdb9fa2150bb1773260b8c Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Sun, 28 Oct 2012 13:27:26 -0400
Subject: [PATCH] Experimental JGit-based GC Executor

---
 src/com/gitblit/client/EditTeamDialog.java |  152 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 139 insertions(+), 13 deletions(-)

diff --git a/src/com/gitblit/client/EditTeamDialog.java b/src/com/gitblit/client/EditTeamDialog.java
index 4297599..4350310 100644
--- a/src/com/gitblit/client/EditTeamDialog.java
+++ b/src/com/gitblit/client/EditTeamDialog.java
@@ -33,6 +33,7 @@
 
 import javax.swing.ImageIcon;
 import javax.swing.JButton;
+import javax.swing.JCheckBox;
 import javax.swing.JComponent;
 import javax.swing.JDialog;
 import javax.swing.JLabel;
@@ -44,9 +45,11 @@
 import javax.swing.KeyStroke;
 
 import com.gitblit.Constants.AccessRestrictionType;
+import com.gitblit.models.RegistrantAccessPermission;
 import com.gitblit.models.RepositoryModel;
 import com.gitblit.models.ServerSettings;
 import com.gitblit.models.TeamModel;
+import com.gitblit.utils.ArrayUtils;
 import com.gitblit.utils.StringUtils;
 
 public class EditTeamDialog extends JDialog {
@@ -64,10 +67,26 @@
 	private boolean canceled = true;
 
 	private JTextField teamnameField;
+	
+	private JCheckBox canAdminCheckbox;
+	
+	private JCheckBox canForkCheckbox;
+	
+	private JCheckBox canCreateCheckbox;
 
-	private JPalette<String> repositoryPalette;
+	private JTextField mailingListsField;
+
+	private RegistrantPermissionsPanel repositoryPalette;
 
 	private JPalette<String> userPalette;
+
+	private JPalette<String> preReceivePalette;
+
+	private JLabel preReceiveInherited;
+
+	private JPalette<String> postReceivePalette;
+
+	private JLabel postReceiveInherited;
 
 	private Set<String> teamnames;
 
@@ -105,16 +124,29 @@
 	private void initialize(int protocolVersion, TeamModel aTeam) {
 		teamnameField = new JTextField(aTeam.name == null ? "" : aTeam.name, 25);
 
+		canAdminCheckbox = new JCheckBox(Translation.get("gb.canAdminDescription"), aTeam.canAdmin);		
+		canForkCheckbox = new JCheckBox(Translation.get("gb.canForkDescription"), aTeam.canFork);
+		canCreateCheckbox = new JCheckBox(Translation.get("gb.canCreateDescription"), aTeam.canCreate);
+
+		mailingListsField = new JTextField(aTeam.mailingLists == null ? ""
+				: StringUtils.flattenStrings(aTeam.mailingLists, " "), 50);
+
 		JPanel fieldsPanel = new JPanel(new GridLayout(0, 1));
 		fieldsPanel.add(newFieldPanel(Translation.get("gb.teamName"), teamnameField));
+		fieldsPanel.add(newFieldPanel(Translation.get("gb.canAdmin"), canAdminCheckbox));
+		fieldsPanel.add(newFieldPanel(Translation.get("gb.canFork"), canForkCheckbox));
+		fieldsPanel.add(newFieldPanel(Translation.get("gb.canCreate"), canCreateCheckbox));
+
+		fieldsPanel.add(newFieldPanel(Translation.get("gb.mailingLists"), mailingListsField));
 
 		final Insets _insets = new Insets(5, 5, 5, 5);
-		repositoryPalette = new JPalette<String>();
+		repositoryPalette = new RegistrantPermissionsPanel();
 		userPalette = new JPalette<String>();
+		userPalette.setEnabled(settings.supportsTeamMembershipChanges);
 		
 		JPanel fieldsPanelTop = new JPanel(new BorderLayout());
 		fieldsPanelTop.add(fieldsPanel, BorderLayout.NORTH);
-		
+
 		JPanel repositoriesPanel = new JPanel(new BorderLayout()) {
 
 			private static final long serialVersionUID = 1L;
@@ -135,11 +167,24 @@
 		};
 		usersPanel.add(userPalette, BorderLayout.CENTER);
 
+		preReceivePalette = new JPalette<String>(true);
+		preReceiveInherited = new JLabel();
+		JPanel preReceivePanel = new JPanel(new BorderLayout(5, 5));
+		preReceivePanel.add(preReceivePalette, BorderLayout.CENTER);
+		preReceivePanel.add(preReceiveInherited, BorderLayout.WEST);
+		
+		postReceivePalette = new JPalette<String>(true);
+		postReceiveInherited = new JLabel();
+		JPanel postReceivePanel = new JPanel(new BorderLayout(5, 5));
+		postReceivePanel.add(postReceivePalette, BorderLayout.CENTER);		
+		postReceivePanel.add(postReceiveInherited, BorderLayout.WEST);
+
 		JTabbedPane panel = new JTabbedPane(JTabbedPane.TOP);
 		panel.addTab(Translation.get("gb.general"), fieldsPanelTop);
 		panel.addTab(Translation.get("gb.teamMembers"), usersPanel);
 		panel.addTab(Translation.get("gb.restrictedRepositories"), repositoriesPanel);
-
+		panel.addTab(Translation.get("gb.preReceiveScripts"), preReceivePanel);
+		panel.addTab(Translation.get("gb.postReceiveScripts"), postReceivePanel);
 
 		JButton createButton = new JButton(Translation.get("gb.save"));
 		createButton.addActionListener(new ActionListener() {
@@ -162,7 +207,7 @@
 		JPanel controls = new JPanel();
 		controls.add(cancelButton);
 		controls.add(createButton);
-		
+
 		JPanel centerPanel = new JPanel(new BorderLayout(5, 5)) {
 
 			private static final long serialVersionUID = 1L;
@@ -218,11 +263,36 @@
 		}
 		team.name = tname;
 
-		team.repositories.clear();
-		team.repositories.addAll(repositoryPalette.getSelections());
-		
+		team.canAdmin = canAdminCheckbox.isSelected();
+		team.canFork = canForkCheckbox.isSelected();
+		team.canCreate = canCreateCheckbox.isSelected();
+
+		String ml = mailingListsField.getText();
+		if (!StringUtils.isEmpty(ml)) {
+			Set<String> list = new HashSet<String>();
+			for (String address : ml.split("(,|\\s)")) {
+				if (StringUtils.isEmpty(address)) {
+					continue;
+				}
+				list.add(address.toLowerCase());
+			}
+			team.mailingLists.clear();
+			team.mailingLists.addAll(list);
+		}
+
+		for (RegistrantAccessPermission rp : repositoryPalette.getPermissions()) {
+			team.setRepositoryPermission(rp.registrant, rp.permission);
+		}
+
 		team.users.clear();
 		team.users.addAll(userPalette.getSelections());
+
+		team.preReceiveScripts.clear();
+		team.preReceiveScripts.addAll(preReceivePalette.getSelections());
+
+		team.postReceiveScripts.clear();
+		team.postReceiveScripts.addAll(postReceivePalette.getSelections());
+
 		return true;
 	}
 
@@ -238,7 +308,7 @@
 		}
 	}
 
-	public void setRepositories(List<RepositoryModel> repositories, List<String> selected) {
+	public void setRepositories(List<RepositoryModel> repositories, List<RegistrantAccessPermission> permissions) {
 		List<String> restricted = new ArrayList<String>();
 		for (RepositoryModel repo : repositories) {
 			if (repo.accessRestriction.exceeds(AccessRestrictionType.NONE)) {
@@ -246,12 +316,36 @@
 			}
 		}
 		StringUtils.sortRepositorynames(restricted);
-		if (selected != null) {
-			StringUtils.sortRepositorynames(selected);
+		
+		List<String> list = new ArrayList<String>();
+		// repositories
+		list.add(".*");
+		// all repositories excluding personal repositories
+		list.add("[^~].*");
+		String lastProject = null;
+		for (String repo : restricted) {
+			String projectPath = StringUtils.getFirstPathElement(repo);
+			if (lastProject == null || !lastProject.equalsIgnoreCase(projectPath)) {
+				lastProject = projectPath;
+				if (!StringUtils.isEmpty(projectPath)) {
+					// regex for all repositories within a project
+					list.add(projectPath + "/.*");
+				}
+				list.add(repo);
+			}
 		}
-		repositoryPalette.setObjects(restricted, selected);
+
+		// remove repositories for which user already has a permission
+		if (permissions == null) {
+			permissions = new ArrayList<RegistrantAccessPermission>();
+		} else {
+			for (RegistrantAccessPermission rp : permissions) {
+				list.remove(rp.registrant);
+			}
+		}
+		repositoryPalette.setObjects(list, permissions);
 	}
-	
+
 	public void setUsers(List<String> users, List<String> selected) {
 		Collections.sort(users);
 		if (selected != null) {
@@ -260,6 +354,38 @@
 		userPalette.setObjects(users, selected);
 	}
 
+	public void setPreReceiveScripts(List<String> unused, List<String> inherited,
+			List<String> selected) {
+		Collections.sort(unused);
+		if (selected != null) {
+			Collections.sort(selected);
+		}
+		preReceivePalette.setObjects(unused, selected);
+		showInherited(inherited, preReceiveInherited);
+	}
+
+	public void setPostReceiveScripts(List<String> unused, List<String> inherited,
+			List<String> selected) {
+		Collections.sort(unused);
+		if (selected != null) {
+			Collections.sort(selected);
+		}
+		postReceivePalette.setObjects(unused, selected);
+		showInherited(inherited, postReceiveInherited);
+	}
+
+	private void showInherited(List<String> list, JLabel label) {
+		StringBuilder sb = new StringBuilder();
+		if (list != null && list.size() > 0) {
+			sb.append("<html><body><b>INHERITED</b><ul style=\"margin-left:5px;list-style-type: none;\">");
+			for (String script : list) {
+				sb.append("<li>").append(script).append("</li>");
+			}
+			sb.append("</ul></body></html>");
+		}
+		label.setText(sb.toString());
+	}
+
 	public TeamModel getTeam() {
 		if (canceled) {
 			return null;

--
Gitblit v1.9.1