]> source.dussan.org Git - gitblit.git/commitdiff
Extract re-usable conditional choice option panel
authorJames Moger <james.moger@gitblit.com>
Tue, 3 Jun 2014 13:56:50 +0000 (09:56 -0400)
committerJames Moger <james.moger@gitblit.com>
Tue, 3 Jun 2014 14:34:51 +0000 (10:34 -0400)
src/main/java/com/gitblit/wicket/pages/NewRepositoryPage.html
src/main/java/com/gitblit/wicket/pages/NewRepositoryPage.java
src/main/java/com/gitblit/wicket/panels/ConditionalChoiceOption.html [new file with mode: 0644]
src/main/java/com/gitblit/wicket/panels/ConditionalChoiceOption.java [new file with mode: 0644]

index df55381a43de5b9bc484689ca8112725f19df5d2..115b8c108e740e6359432742e8d746d2b8d61110 100644 (file)
                <hr/>\r
                \r
                <h4><wicket:message key="gb.initialCommit"></wicket:message></h4>\r
-               <div>\r
-                       <p><wicket:message key="gb.initialCommitDescription"></wicket:message></p>\r
-               </div>\r
+               <p><wicket:message key="gb.initialCommitDescription"></wicket:message></p>\r
                \r
-               <div style="clear:both;padding-top:10px;">\r
-                       <div style="display: inline-block;vertical-align: top;">\r
-                               <input type="checkbox" wicket:id="addReadme" />\r
-                       </div>\r
-                       <div style="display: inline-block;">\r
-                               <b><wicket:message key="gb.initWithReadme"></wicket:message></b><br/>\r
-                               <p><wicket:message key="gb.initWithReadmeDescription"></wicket:message></p>\r
-                       </div>\r
-               </div>\r
+               <div wicket:id="addReadme"></div>\r
                \r
-               <div style="clear:both;padding-top:10px;display:none;">\r
-                       <!-- future GitFlow -->\r
-                       <div style="display: inline-block;vertical-align: top;">\r
-                               <input type="checkbox" wicket:id="addGitflow" />\r
-                       </div>\r
-                       <div style="display: inline-block;">\r
-                               <b>Include a .gitflow file</b>\r
-                               <p>This will generate a config file which guides Git clients in setting up Gitflow branches.</p>\r
-                       </div>\r
-               </div>\r
+               <div wicket:id="addGitIgnore"></div>\r
                \r
-               <div style="clear:both;padding-top:10px;">\r
-                       <div style="display: inline-block;vertical-align: top;">\r
-                               <input type="checkbox" wicket:id="addGitignore" />\r
-                       </div>\r
-                       <div style="display:inline-block;">\r
-                               <b><wicket:message key="gb.initWithGitignore"></wicket:message></b><br/>\r
-                               <p><wicket:message key="gb.initWithGitignoreDescription"></wicket:message></p>\r
-                               <p style="padding-top:5px;"><select class="span2" wicket:id="gitignore" /></p>\r
-                       </div>\r
-               </div>          \r
+               <div wicket:id="addGitFlow"></div>\r
        </div>\r
 </div>\r
 \r
index d5ae3440e39625d9d903f04ec3de55a76b28e9e3..f0aa2241749003cd55b061a507107c85c4446718 100644 (file)
@@ -22,12 +22,8 @@ import java.util.ArrayList;
 import java.util.Collections;\r
 import java.util.List;\r
 \r
-import org.apache.wicket.ajax.AjaxRequestTarget;\r
-import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;\r
 import org.apache.wicket.behavior.SimpleAttributeModifier;\r
 import org.apache.wicket.markup.html.form.Button;\r
-import org.apache.wicket.markup.html.form.CheckBox;\r
-import org.apache.wicket.markup.html.form.DropDownChoice;\r
 import org.apache.wicket.markup.html.form.Form;\r
 import org.apache.wicket.model.CompoundPropertyModel;\r
 import org.apache.wicket.model.IModel;\r
@@ -60,6 +56,8 @@ import com.gitblit.utils.StringUtils;
 import com.gitblit.wicket.GitBlitWebSession;\r
 import com.gitblit.wicket.WicketUtils;\r
 import com.gitblit.wicket.panels.AccessPolicyPanel;\r
+import com.gitblit.wicket.panels.CheckboxOption;\r
+import com.gitblit.wicket.panels.ConditionalChoiceOption;\r
 import com.gitblit.wicket.panels.RepositoryNamePanel;\r
 \r
 public class NewRepositoryPage extends RootSubPage {\r
@@ -183,7 +181,10 @@ public class NewRepositoryPage extends RootSubPage {
 \r
                // add README\r
                addReadmeModel = Model.of(false);\r
-               form.add(new CheckBox("addReadme", addReadmeModel));\r
+               form.add(new CheckboxOption("addReadme",\r
+                               getString("gb.initWithReadme"),\r
+                               getString("gb.initWithReadmeDescription"),\r
+                               addReadmeModel));\r
 \r
                // add .gitignore\r
                File gitignoreDir = app().runtime().getFileOrFolder(Keys.git.gitignoreFolder, "${baseFolder}/gitignore");\r
@@ -198,29 +199,22 @@ public class NewRepositoryPage extends RootSubPage {
                        }\r
                }\r
                Collections.sort(gitignores);\r
-               gitignoreModel = Model.of("");\r
-               final DropDownChoice<String> gitignoreChoice = new DropDownChoice<String>("gitignore", gitignoreModel, gitignores);\r
-               gitignoreChoice.setOutputMarkupId(true);\r
-               form.add(gitignoreChoice.setEnabled(false));\r
 \r
+               gitignoreModel = Model.of("");\r
                addGitignoreModel = Model.of(false);\r
-               final CheckBox gitignoreCheckbox = new CheckBox("addGitignore", addGitignoreModel);\r
-               form.add(gitignoreCheckbox);\r
-\r
-               gitignoreCheckbox.add(new AjaxFormComponentUpdatingBehavior("onchange") {\r
-\r
-                       private static final long serialVersionUID = 1L;\r
-\r
-                       @Override\r
-                       protected void onUpdate(AjaxRequestTarget target) {\r
-                               gitignoreChoice.setEnabled(addGitignoreModel.getObject());\r
-                               target.addComponent(gitignoreChoice);\r
-                       }\r
-               });\r
-\r
-               // TODO add .gitflow\r
+               form.add(new ConditionalChoiceOption<String>("addGitIgnore",\r
+                               getString("gb.initWithGitignore"),\r
+                               getString("gb.initWithGitignoreDescription"),\r
+                               addGitignoreModel,\r
+                               gitignoreModel,\r
+                               gitignores));\r
+\r
+               // TODO consider gitflow at creation (ticket-55)\r
                addGitflowModel = Model.of(false);\r
-               form.add(new CheckBox("addGitflow", addGitflowModel));\r
+               form.add(new CheckboxOption("addGitFlow",\r
+                               "Include a .gitflow file",\r
+                               "This will generate a config file which guides Git clients in setting up Gitflow branches.",\r
+                               addGitflowModel).setVisible(false));\r
 \r
                form.add(new Button("create"));\r
 \r
diff --git a/src/main/java/com/gitblit/wicket/panels/ConditionalChoiceOption.html b/src/main/java/com/gitblit/wicket/panels/ConditionalChoiceOption.html
new file mode 100644 (file)
index 0000000..fb360d1
--- /dev/null
@@ -0,0 +1,19 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\r
+<html xmlns="http://www.w3.org/1999/xhtml"  \r
+      xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.3-strict.dtd"  \r
+      xml:lang="en"  \r
+      lang="en"> \r
+\r
+<body>\r
+<wicket:panel>\r
+       <div style="padding-top:4px;">\r
+               <div>\r
+                       <label style="font-weight:bold;" class="checkbox"><input type="checkbox" wicket:id="checkbox" /> <span wicket:id="name"></span></label>\r
+               </div>\r
+               <label class="checkbox" style="color:#777;"> <span wicket:id="description"></span>\r
+               <p style="padding-top:5px;"><select class="span3" wicket:id="choice" /></p>\r
+               </label>                \r
+       </div>\r
+</wicket:panel>\r
+</body>\r
+</html>
\ No newline at end of file
diff --git a/src/main/java/com/gitblit/wicket/panels/ConditionalChoiceOption.java b/src/main/java/com/gitblit/wicket/panels/ConditionalChoiceOption.java
new file mode 100644 (file)
index 0000000..07aaee8
--- /dev/null
@@ -0,0 +1,78 @@
+/*\r
+ * Copyright 2014 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit.wicket.panels;\r
+\r
+import java.util.List;\r
+\r
+import org.apache.wicket.ajax.AjaxRequestTarget;\r
+import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;\r
+import org.apache.wicket.markup.html.basic.Label;\r
+import org.apache.wicket.markup.html.form.CheckBox;\r
+import org.apache.wicket.markup.html.form.DropDownChoice;\r
+import org.apache.wicket.model.IModel;\r
+import org.parboiled.common.StringUtils;\r
+\r
+/**\r
+ * A re-usable conditional choice option panel.\r
+ *\r
+ * [x] title\r
+ *     description\r
+ *     [choices]\r
+ *\r
+ * @author James Moger\r
+ *\r
+ */\r
+public class ConditionalChoiceOption<T> extends BasePanel {\r
+\r
+       private static final long serialVersionUID = 1L;\r
+\r
+       final CheckBox checkbox;\r
+       final DropDownChoice<T> choice;\r
+\r
+       public ConditionalChoiceOption(String wicketId, String title, String description, IModel<Boolean> checkboxModel, IModel<T> choiceModel, List<T> choices) {\r
+               super(wicketId);\r
+               add(new Label("name", title));\r
+               add(new Label("description", description).setVisible(!StringUtils.isEmpty(description)));\r
+\r
+               this.checkbox = new CheckBox("checkbox", checkboxModel);\r
+               checkbox.setOutputMarkupId(true);\r
+\r
+               this.choice = new DropDownChoice<T>("choice", choiceModel, choices);\r
+               choice.setOutputMarkupId(true);\r
+\r
+               setup();\r
+       }\r
+\r
+       private void setup() {\r
+               add(checkbox);\r
+               add(choice.setMarkupId("choice").setEnabled(choice.getChoices().size() > 0));\r
+               choice.setEnabled(checkbox.getModelObject());\r
+\r
+               checkbox.add(new AjaxFormComponentUpdatingBehavior("onchange") {\r
+\r
+                       private static final long serialVersionUID = 1L;\r
+\r
+                       @Override\r
+                       protected void onUpdate(AjaxRequestTarget target) {\r
+                               choice.setEnabled(checkbox.getModelObject());\r
+                               target.addComponent(choice);\r
+                               if (!choice.isEnabled()) {\r
+                                       choice.setModelObject(null);\r
+                               }\r
+                       }\r
+               });\r
+       }\r
+}\r