From f9e51c9454c89a8587bb122183c7658b94606d42 Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 9 Jun 2014 09:10:13 -0400 Subject: [PATCH] Use separate project and name field models in the RepositoryNamePanel --- .../wicket/pages/EditRepositoryPage.java | 3 +- .../wicket/pages/NewRepositoryPage.java | 3 +- .../wicket/panels/RepositoryNamePanel.java | 73 +++++++++---------- 3 files changed, 36 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.java b/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.java index a0e6e7ba..a1c37385 100644 --- a/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.java +++ b/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.java @@ -70,8 +70,8 @@ import com.gitblit.wicket.StringChoiceRenderer; import com.gitblit.wicket.WicketUtils; import com.gitblit.wicket.panels.AccessPolicyPanel; import com.gitblit.wicket.panels.BasePanel.JavascriptEventConfirmation; -import com.gitblit.wicket.panels.BulletListPanel; import com.gitblit.wicket.panels.BooleanOption; +import com.gitblit.wicket.panels.BulletListPanel; import com.gitblit.wicket.panels.ChoiceOption; import com.gitblit.wicket.panels.RegistrantPermissionsPanel; import com.gitblit.wicket.panels.RepositoryNamePanel; @@ -383,7 +383,6 @@ public class EditRepositoryPage extends RootSubPage { } } catch (GitBlitException e) { error(e.getMessage()); - namePanel.resetModel(repositoryModel); return; } setRedirect(false); diff --git a/src/main/java/com/gitblit/wicket/pages/NewRepositoryPage.java b/src/main/java/com/gitblit/wicket/pages/NewRepositoryPage.java index b8752e33..46877168 100644 --- a/src/main/java/com/gitblit/wicket/pages/NewRepositoryPage.java +++ b/src/main/java/com/gitblit/wicket/pages/NewRepositoryPage.java @@ -56,8 +56,8 @@ import com.gitblit.utils.StringUtils; import com.gitblit.wicket.GitBlitWebSession; import com.gitblit.wicket.WicketUtils; import com.gitblit.wicket.panels.AccessPolicyPanel; -import com.gitblit.wicket.panels.BooleanOption; import com.gitblit.wicket.panels.BooleanChoiceOption; +import com.gitblit.wicket.panels.BooleanOption; import com.gitblit.wicket.panels.RepositoryNamePanel; public class NewRepositoryPage extends RootSubPage { @@ -142,7 +142,6 @@ public class NewRepositoryPage extends RootSubPage { } catch (GitBlitException e) { error(e.getMessage()); - namePanel.resetModel(repositoryModel); return; } setRedirect(true); diff --git a/src/main/java/com/gitblit/wicket/panels/RepositoryNamePanel.java b/src/main/java/com/gitblit/wicket/panels/RepositoryNamePanel.java index 358ff59b..d3e493ed 100644 --- a/src/main/java/com/gitblit/wicket/panels/RepositoryNamePanel.java +++ b/src/main/java/com/gitblit/wicket/panels/RepositoryNamePanel.java @@ -22,6 +22,8 @@ import java.util.TreeSet; import org.apache.wicket.markup.html.form.DropDownChoice; import org.apache.wicket.markup.html.form.TextField; +import org.apache.wicket.model.IModel; +import org.apache.wicket.model.Model; import com.gitblit.models.ProjectModel; import com.gitblit.models.RepositoryModel; @@ -41,7 +43,11 @@ public class RepositoryNamePanel extends BasePanel { private String fullName; - private DropDownChoice projectChoice; + private final IModel projectPath; + + private DropDownChoice pathChoice; + + private final IModel repoName; private TextField nameField; @@ -52,84 +58,82 @@ public class RepositoryNamePanel extends BasePanel { UserModel user = session.getUser(); // build project set for repository destination - String defaultProject = null; - Set projectNames = new TreeSet(); + String defaultPath = null; + String defaultName = null; + Set pathNames = new TreeSet(); // add the registered/known projects for (ProjectModel project : app().projects().getProjectModels(user, false)) { // TODO issue-351: user.canAdmin(project) if (user.canAdmin()) { if (project.isRoot) { - projectNames.add("/"); + pathNames.add("/"); } else { - projectNames.add(project.name + "/"); + pathNames.add(project.name + "/"); } } } // add the user's personal project namespace if (user.canAdmin() || user.canCreate()) { - projectNames.add(user.getPersonalPath() + "/"); + pathNames.add(user.getPersonalPath() + "/"); } if (!StringUtils.isEmpty(repository.name)) { // editing a repository name // set the defaultProject to the current repository project - defaultProject = repository.projectPath; - if (StringUtils.isEmpty(defaultProject)) { - defaultProject = "/"; + if (StringUtils.isEmpty(repository.projectPath)) { + defaultPath = "/"; } else { - defaultProject += "/"; + defaultPath = repository.projectPath + "/"; } - - projectNames.add(defaultProject); + defaultName = repository.name.substring(defaultPath.length()); + pathNames.add(defaultPath); } // if default project is not already set, set preference based on the user permissions - if (defaultProject == null) { + if (defaultPath == null) { if (user.canAdmin()) { - defaultProject = "/"; + defaultPath = "/"; } else if (user.canCreate()) { - defaultProject = user.getPersonalPath() + "/"; + defaultPath = user.getPersonalPath() + "/"; } } - // update the model which is reflectively mapped to the Wicket fields by name - repository.projectPath = defaultProject; - if (repository.projectPath.length() > 1 && !StringUtils.isEmpty(repository.name)) { - repository.name = repository.name.substring(repository.projectPath.length()); - } - projectChoice = new DropDownChoice("projectPath", new ArrayList(projectNames)); - nameField = new TextField("name"); + projectPath = Model.of(defaultPath); + pathChoice = new DropDownChoice("projectPath", projectPath, new ArrayList(pathNames)); + repoName = Model.of(defaultName); + nameField = new TextField("name", repoName); // only enable project selection if we actually have multiple choices - add(projectChoice.setEnabled(projectNames.size() > 1)); + add(pathChoice.setEnabled(pathNames.size() > 1)); add(nameField); add(new TextField("description")); } public void setEditable(boolean editable) { // only enable project selection if we actually have multiple choices - projectChoice.setEnabled(projectChoice.getChoices().size() > 1 && editable); + pathChoice.setEnabled(pathChoice.getChoices().size() > 1 && editable); nameField.setEnabled(editable); } public boolean updateModel(RepositoryModel repositoryModel) { - // confirm a project was selected - if (StringUtils.isEmpty(repositoryModel.projectPath)) { + // confirm a project path was selected + if (StringUtils.isEmpty(projectPath.getObject())) { error(getString("gb.pleaseSelectProject")); return false; } // confirm a repository name was entered - if (StringUtils.isEmpty(repositoryModel.name)) { + if (StringUtils.isEmpty(repoName.getObject())) { error(getString("gb.pleaseSetRepositoryName")); return false; } - String project = repositoryModel.projectPath; + String project = projectPath.getObject(); + String name = repoName.getObject(); - fullName = (project + repositoryModel.name).trim(); + fullName = (project + name).trim(); fullName = fullName.replace('\\', '/'); fullName = fullName.replace("//", "/"); if (fullName.charAt(0) == '/') { @@ -156,21 +160,12 @@ public class RepositoryNamePanel extends BasePanel { } repositoryModel.name = fullName; - repositoryModel.projectPath = null; return true; } - public void resetModel(RepositoryModel repositoryModel) { - // restore project and name fields on error condition - repositoryModel.projectPath = StringUtils.getFirstPathElement(fullName) + "/"; - if (repositoryModel.projectPath.length() > 1) { - repositoryModel.name = fullName.substring(repositoryModel.projectPath.length()); - } - } - @Override protected boolean getStatelessHint() { return false; } -} +} \ No newline at end of file -- 2.39.5