[git-stash] rewrite "Stash" dialog using the ui dsl

GitOrigin-RevId: ec039b40de34d03d9bec8fe9bfd99174d49f53a2
This commit is contained in:
Julia Beliaeva
2024-05-13 23:02:53 +00:00
committed by intellij-monorepo-bot
parent 09445f8bee
commit 8e35eadea3
4 changed files with 64 additions and 165 deletions
@@ -6,7 +6,8 @@ import com.intellij.openapi.vcs.changes.ChangeListManager;
import com.intellij.openapi.vfs.VirtualFile;
import git4idea.i18n.GitBundle;
import git4idea.stash.GitStashOperations;
import git4idea.ui.GitStashDialog;
import git4idea.stash.GitStashUtils;
import git4idea.stash.ui.GitStashDialog;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
@@ -19,13 +20,16 @@ public class GitStash extends GitRepositoryAction {
if (ChangeListManager.getInstance(project).isFreezedWithNotification(GitBundle.message("stash.error.can.not.stash.changes.now"))) {
return;
}
GitStashDialog d = new GitStashDialog(project, gitRoots, defaultRoot);
if (!d.showAndGet()) {
return;
}
d.logUsage();
GitStashDialog dialog = new GitStashDialog(project, gitRoots, defaultRoot);
if (!dialog.showAndGet()) return;
GitStashOperations.runStashInBackground(project, Collections.singleton(d.getGitRoot()), root -> d.handler());
VirtualFile selectedRoot = dialog.getSelectedRoot();
String message = dialog.getMessage();
boolean keepIndex = dialog.getKeepIndex();
GitStashOperations.runStashInBackground(project, Collections.singleton(selectedRoot), root -> {
return GitStashUtils.createStashHandler(project, root, keepIndex, message);
});
}
@Override
@@ -0,0 +1,53 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package git4idea.stash.ui
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.ui.components.JBCheckBox
import com.intellij.ui.components.JBLabel
import com.intellij.ui.dsl.builder.Align
import com.intellij.ui.dsl.builder.panel
import git4idea.GitStashUsageCollector
import git4idea.i18n.GitBundle
import git4idea.util.GitUIUtil
import javax.swing.JComponent
import javax.swing.JTextField
internal class GitStashDialog(project: Project, roots: List<VirtualFile>, defaultRoot: VirtualFile) : DialogWrapper(project, true) {
private val rootComboBox = ComboBox<VirtualFile>().also { it.toolTipText = GitBundle.message("common.git.root.tooltip") }
private val currentBranchLabel = JBLabel().also { it.toolTipText = GitBundle.message("common.current.branch.tooltip") }
private val stashMessageEditor = JTextField().also { it.toolTipText = GitBundle.message("stash.message.tooltip") }
private val keepIndexCheckBox = JBCheckBox(GitBundle.message("stash.keep.index"))
val selectedRoot: VirtualFile get() = rootComboBox.item
val message: String get() = stashMessageEditor.text
val keepIndex: Boolean get() = keepIndexCheckBox.isSelected
init {
title = GitBundle.message("stash.title")
setOKButtonText(GitBundle.message("stash.button"))
GitUIUtil.setupRootChooser(project, roots, defaultRoot, rootComboBox, currentBranchLabel)
init()
}
override fun createCenterPanel(): JComponent {
return panel {
row(GitBundle.message("common.git.root")) { cell(rootComboBox) }
row(GitBundle.message("common.current.branch")) { cell(currentBranchLabel) }
row(GitBundle.message("stash.message")) { cell(stashMessageEditor).align(Align.FILL) }
row { cell(keepIndexCheckBox) }
}
}
override fun doOKAction() {
super.doOKAction()
GitStashUsageCollector.logStashPushDialog(message.isNotEmpty(), keepIndex)
}
override fun getPreferredFocusedComponent(): JComponent = stashMessageEditor
override fun getDimensionServiceKey(): String = javaClass.name
override fun getHelpId(): String = "reference.VersionControl.Git.Stash"
}
@@ -1,80 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="git4idea.ui.GitStashDialog">
<grid id="27dc6" binding="myRootPanel" layout-manager="GridLayoutManager" row-count="5" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="152"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="259de" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="227eb"/>
<text resource-bundle="messages/GitBundle" key="common.git.root"/>
</properties>
</component>
<vspacer id="c4933">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="227eb" class="javax.swing.JComboBox" binding="myGitRootComboBox" default-binding="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<toolTipText resource-bundle="messages/GitBundle" key="common.git.root.tooltip"/>
</properties>
</component>
<component id="29f64" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/GitBundle" key="common.current.branch"/>
</properties>
</component>
<component id="d22b6" class="javax.swing.JLabel" binding="myCurrentBranch">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
<toolTipText resource-bundle="messages/GitBundle" key="common.current.branch.tooltip"/>
</properties>
</component>
<component id="d11aa" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="4d538"/>
<text resource-bundle="messages/GitBundle" key="stash.message"/>
</properties>
</component>
<component id="4d538" class="javax.swing.JTextField" binding="myMessageTextField" default-binding="true">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<toolTipText resource-bundle="messages/GitBundle" key="stash.message.tooltip"/>
</properties>
</component>
<component id="be5c8" class="javax.swing.JCheckBox" binding="myKeepIndexCheckBox" default-binding="true">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/GitBundle" key="stash.keep.index"/>
<toolTipText resource-bundle="messages/GitBundle" key="stash.keep.index.tooltip"/>
</properties>
</component>
</children>
</grid>
</form>
@@ -1,78 +0,0 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package git4idea.ui;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import git4idea.GitStashUsageCollector;
import git4idea.commands.GitLineHandler;
import git4idea.i18n.GitBundle;
import git4idea.stash.GitStashUtils;
import git4idea.util.GitUIUtil;
import javax.swing.*;
import java.util.List;
/**
* The git stash dialog.
*/
public class GitStashDialog extends DialogWrapper {
private JComboBox myGitRootComboBox; // Git root selector
private JPanel myRootPanel;
private JLabel myCurrentBranch;
private JTextField myMessageTextField;
private JCheckBox myKeepIndexCheckBox; // --keep-index
private final Project myProject;
/**
* A constructor
*
* @param project the project
* @param roots the list of Git roots
* @param defaultRoot the default root to select
*/
public GitStashDialog(final Project project, final List<VirtualFile> roots, final VirtualFile defaultRoot) {
super(project, true);
myProject = project;
setTitle(GitBundle.message("stash.title"));
setOKButtonText(GitBundle.message("stash.button"));
GitUIUtil.setupRootChooser(project, roots, defaultRoot, myGitRootComboBox, myCurrentBranch);
init();
}
public GitLineHandler handler() {
return GitStashUtils.createStashHandler(myProject, getGitRoot(), myKeepIndexCheckBox.isSelected(), myMessageTextField.getText());
}
public void logUsage() {
GitStashUsageCollector.logStashPushDialog(!StringUtil.isEmpty(myMessageTextField.getText()), myKeepIndexCheckBox.isSelected());
}
/**
* @return the selected git root
*/
public VirtualFile getGitRoot() {
return (VirtualFile)myGitRootComboBox.getSelectedItem();
}
@Override
protected JComponent createCenterPanel() {
return myRootPanel;
}
@Override
protected String getDimensionServiceKey() {
return getClass().getName();
}
@Override
protected String getHelpId() {
return "reference.VersionControl.Git.Stash";
}
@Override
public JComponent getPreferredFocusedComponent() {
return myMessageTextField;
}
}