From 2847e957fc745678ea75395fb1ff913e00fe76e3 Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Sat, 10 Aug 2019 11:57:05 +0300 Subject: [PATCH] git: cleanup: convert GitUpdateOptionsPanel to Kotlin DSL GitOrigin-RevId: bc7384352459d27caa1ef5e7ab4075dd5501df65 --- .../src/git4idea/config/GitVcsPanel.kt | 4 +- .../src/git4idea/config/UpdateMethod.java | 19 +++- .../update/GitUpdateConfigurable.java | 8 +- .../update/GitUpdateOptionsPanel.form | 66 ------------ .../git4idea/update/GitUpdateOptionsPanel.kt | 100 +++++++----------- 5 files changed, 58 insertions(+), 139 deletions(-) delete mode 100644 plugins/git4idea/src/git4idea/update/GitUpdateOptionsPanel.form diff --git a/plugins/git4idea/src/git4idea/config/GitVcsPanel.kt b/plugins/git4idea/src/git4idea/config/GitVcsPanel.kt index c09362956964..0bcfcfda31fc 100644 --- a/plugins/git4idea/src/git4idea/config/GitVcsPanel.kt +++ b/plugins/git4idea/src/git4idea/config/GitVcsPanel.kt @@ -216,9 +216,7 @@ internal class GitVcsPanel(private val project: Project, EnumComboBoxModel(UpdateMethod::class.java), { projectSettings.updateMethod }, { projectSettings.updateMethod = it!! }, - renderer = SimpleListCellRenderer.create("") { value -> - value.name.replace('_', ' ').toLowerCase().capitalize() - } + renderer = SimpleListCellRenderer.create("", UpdateMethod::asString) ) } } diff --git a/plugins/git4idea/src/git4idea/config/UpdateMethod.java b/plugins/git4idea/src/git4idea/config/UpdateMethod.java index 4091a573e9b3..16b6b70d0e32 100644 --- a/plugins/git4idea/src/git4idea/config/UpdateMethod.java +++ b/plugins/git4idea/src/git4idea/config/UpdateMethod.java @@ -15,6 +15,8 @@ */ package git4idea.config; +import org.jetbrains.annotations.NotNull; + /** * The type of update to perform */ @@ -22,13 +24,24 @@ public enum UpdateMethod { /** * Use default specified in the config file for the branch */ - BRANCH_DEFAULT, + BRANCH_DEFAULT("Branch Default"), /** * Merge fetched commits with local branch */ - MERGE, + MERGE("Merge"), /** * Rebase local commits upon the fetched branch */ - REBASE + REBASE("Rebase"); + + @NotNull private final String myPresentation; + + UpdateMethod(@NotNull String presentation) { + myPresentation = presentation; + } + + @NotNull + public String asString() { + return myPresentation; + } } diff --git a/plugins/git4idea/src/git4idea/update/GitUpdateConfigurable.java b/plugins/git4idea/src/git4idea/update/GitUpdateConfigurable.java index 821aca21a03b..d095e9be5f59 100644 --- a/plugins/git4idea/src/git4idea/update/GitUpdateConfigurable.java +++ b/plugins/git4idea/src/git4idea/update/GitUpdateConfigurable.java @@ -47,7 +47,7 @@ public class GitUpdateConfigurable implements Configurable { */ @Override public JComponent createComponent() { - myPanel = new GitUpdateOptionsPanel(); + myPanel = new GitUpdateOptionsPanel(mySettings); return myPanel.getPanel(); } @@ -56,7 +56,7 @@ public class GitUpdateConfigurable implements Configurable { */ @Override public boolean isModified() { - return myPanel.isModified(mySettings); + return myPanel.isModified(); } /** @@ -64,7 +64,7 @@ public class GitUpdateConfigurable implements Configurable { */ @Override public void apply() { - myPanel.applyTo(mySettings); + myPanel.applyTo(); } /** @@ -72,7 +72,7 @@ public class GitUpdateConfigurable implements Configurable { */ @Override public void reset() { - myPanel.updateFrom(mySettings); + myPanel.updateFrom(); } /** diff --git a/plugins/git4idea/src/git4idea/update/GitUpdateOptionsPanel.form b/plugins/git4idea/src/git4idea/update/GitUpdateOptionsPanel.form deleted file mode 100644 index cebe2419eb47..000000000000 --- a/plugins/git4idea/src/git4idea/update/GitUpdateOptionsPanel.form +++ /dev/null @@ -1,66 +0,0 @@ - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/plugins/git4idea/src/git4idea/update/GitUpdateOptionsPanel.kt b/plugins/git4idea/src/git4idea/update/GitUpdateOptionsPanel.kt index d78af2d5aab0..acbb56e02914 100644 --- a/plugins/git4idea/src/git4idea/update/GitUpdateOptionsPanel.kt +++ b/plugins/git4idea/src/git4idea/update/GitUpdateOptionsPanel.kt @@ -13,72 +13,46 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package git4idea.update; +package git4idea.update -import git4idea.config.GitVcsSettings; -import git4idea.config.UpdateMethod; +import com.intellij.openapi.ui.DialogPanel +import com.intellij.ui.layout.* +import git4idea.config.GitVcsSettings +import git4idea.config.UpdateMethod +import git4idea.config.UpdateMethod.BRANCH_DEFAULT -import javax.swing.*; +class GitUpdateOptionsPanel(private val settings: GitVcsSettings) { + val panel = createPanel() -/** - * Update options panel - */ -public class GitUpdateOptionsPanel { - private JPanel myPanel; - private JRadioButton myBranchDefaultRadioButton; - private JRadioButton myForceRebaseRadioButton; - private JRadioButton myForceMergeRadioButton; - - public JComponent getPanel() { - return myPanel; - } - - public boolean isModified(GitVcsSettings settings) { - UpdateMethod type = getUpdateType(); - return type != settings.getUpdateMethod(); - } - - /** - * @return get the currently selected update type - */ - private UpdateMethod getUpdateType() { - UpdateMethod type = null; - if (myForceRebaseRadioButton.isSelected()) { - type = UpdateMethod.REBASE; + fun createPanel(): DialogPanel = + panel { + titledRow("Update Type") { + buttonGroup { + getUpdateMethods().forEach { method -> + row { + radioButton(method.asString()).withSelectedBinding(PropertyBinding( + get = { settings.updateMethod == method }, + set = { selected -> if (selected) settings.updateMethod = method } + )) + } + } + } + } } - else if (myForceMergeRadioButton.isSelected()) { - type = UpdateMethod.MERGE; - } - else if (myBranchDefaultRadioButton.isSelected()) { - type = UpdateMethod.BRANCH_DEFAULT; - } - assert type != null; - return type; - } - /** - * Save configuration to settings object - */ - public void applyTo(GitVcsSettings settings) { - settings.setUpdateMethod(getUpdateType()); - } + fun isModified(): Boolean = panel.isModified() - /** - * Update panel according to settings - */ - public void updateFrom(GitVcsSettings settings) { - switch (settings.getUpdateMethod()) { - case REBASE: - myForceRebaseRadioButton.setSelected(true); - break; - case MERGE: - myForceMergeRadioButton.setSelected(true); - break; - case BRANCH_DEFAULT: - myBranchDefaultRadioButton.setSelected(true); - break; - default: - assert false : "Unknown value of update type: " + settings.getUpdateMethod(); - } - } -} + fun applyTo() = panel.apply() + + fun updateFrom() = panel.reset() + + private fun getUpdateMethods(): List = + UpdateMethod.values().sortedWith(Comparator { o1, o2 -> + when { + o1 == o2 -> 0 + o1 == BRANCH_DEFAULT -> 1 + o2 == BRANCH_DEFAULT -> -1 + else -> o1.ordinal - o2.ordinal + } + }) +} \ No newline at end of file