[github] Rework github share dialog to common UI framework

This commit is contained in:
Ivan Semenov
2018-04-17 15:14:41 +03:00
parent 7f97bc7174
commit c6b50d0aa9
3 changed files with 79 additions and 246 deletions
@@ -1,105 +1,94 @@
package org.jetbrains.plugins.github.ui;
package org.jetbrains.plugins.github.ui
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.TestOnly;
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.openapi.ui.ValidationInfo
import com.intellij.ui.components.JBCheckBox
import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.components.JBTextField
import com.intellij.util.ui.JBDimension
import com.intellij.util.ui.JBUI.Panels.simplePanel
import com.intellij.util.ui.UI.PanelFactory.grid
import com.intellij.util.ui.UI.PanelFactory.panel
import com.intellij.util.ui.UIUtil
import org.jetbrains.annotations.TestOnly
import java.util.regex.Pattern
import javax.swing.BorderFactory
import javax.swing.JComponent
import javax.swing.JTextArea
import javax.swing.*;
import java.util.Set;
import java.util.regex.Pattern;
class GithubShareDialog(project: Project,
private val existingRepos: Set<String>,
private val existingRemotes: Set<String>,
privateRepoAllowed: Boolean) : DialogWrapper(project) {
private val GITHUB_REPO_PATTERN = Pattern.compile("[a-zA-Z0-9_.-]+")
/**
* @author oleg
* @date 10/22/10
*/
public class GithubShareDialog extends DialogWrapper {
private static final Pattern GITHUB_REPO_PATTERN = Pattern.compile("[a-zA-Z0-9_.-]+");
private final GithubSharePanel myGithubSharePanel;
private final Set<String> myAvailableNames;
private final Set<String> myAvailableRemotes;
private val repositoryTextField = JBTextField(project.name)
private val privateCheckBox = JBCheckBox("Private", privateRepoAllowed).apply {
toolTipText = "Your account doesn't support private repositories"
}
private val remoteTextField = JBTextField(if (existingRemotes.isEmpty()) "origin" else "github")
private val descriptionTextArea = JTextArea()
public GithubShareDialog(Project project, Set<String> availableNames, Set<String> availableRemotes, boolean privateRepoAllowed) {
super(project);
myAvailableNames = availableNames;
myAvailableRemotes = availableRemotes;
myGithubSharePanel = new GithubSharePanel(this);
init();
setTitle("Share Project On GitHub");
setOKButtonText("Share");
myGithubSharePanel.setRepositoryName(project.getName());
myGithubSharePanel.setRemoteName(availableRemotes.isEmpty() ? "origin" : "github");
myGithubSharePanel.setPrivateRepoAvailable(privateRepoAllowed);
init();
updateOkButton();
init {
title = "Share Project On GitHub"
setOKButtonText("Share")
init()
}
@Override
protected String getHelpId() {
return "github.share";
override fun createCenterPanel(): JComponent? {
val descriptionPane = JBScrollPane(descriptionTextArea)
descriptionPane.minimumSize = JBDimension(150, 50)
descriptionPane.preferredSize = JBDimension(150, 50)
descriptionPane.border = BorderFactory.createEtchedBorder()
return grid()
.add(panel(simplePanel(UIUtil.DEFAULT_HGAP, 0).addToCenter(repositoryTextField).addToRight(privateCheckBox))
.withLabel("Repository name:"))
.add(panel(remoteTextField).withLabel("Remote:"))
.add(panel(descriptionPane).withLabel("Description:"))
.createPanel()
}
@Override
protected String getDimensionServiceKey() {
return "Github.ShareDialog";
override fun doValidateAll(): List<ValidationInfo> {
val repositoryName = repositoryTextField.text
return listOf(
{
if (repositoryName.isNullOrBlank()) ValidationInfo("No repository name selected",
repositoryTextField)
else null
},
{
if (existingRepos.contains(repositoryName)) ValidationInfo("Repository with selected name already exists",
repositoryTextField)
else null
},
{
if (existingRemotes.contains(getRemoteName())) ValidationInfo("Remote with selected name already exists",
remoteTextField)
else null
},
{
if (!GITHUB_REPO_PATTERN.matcher(repositoryName).matches()) ValidationInfo(
"Invalid repository name. Name should consist of letters, numbers, dashes, dots and underscores",
repositoryTextField)
else null
}
).mapNotNull { it() }
}
@Override
protected JComponent createCenterPanel() {
return myGithubSharePanel.getPanel();
}
override fun getHelpId() = "github.share"
override fun getDimensionServiceKey() = "Github.ShareDialog"
override fun getPreferredFocusedComponent() = repositoryTextField
@Override
public JComponent getPreferredFocusedComponent() {
return myGithubSharePanel.getPreferredFocusComponent();
}
public void updateOkButton() {
String repositoryName = getRepositoryName();
String remoteName = getRemoteName();
if (StringUtil.isEmpty(repositoryName)){
setErrorText("No repository name selected", myGithubSharePanel.getPanel());
setOKActionEnabled(false);
return;
}
if (myAvailableNames.contains(repositoryName)){
setErrorText("Repository with selected name already exists", myGithubSharePanel.getPanel());
setOKActionEnabled(false);
return;
}
if (myAvailableRemotes.contains(remoteName)) {
setErrorText("Remote with selected name already exists", myGithubSharePanel.getPanel());
setOKActionEnabled(false);
return;
}
if (!GITHUB_REPO_PATTERN.matcher(repositoryName).matches()){
setErrorText("Invalid repository name. Name should consist of letters, numbers, dashes, dots and underscores", myGithubSharePanel.getPanel());
setOKActionEnabled(false);
return;
}
setErrorText(null);
setOKActionEnabled(true);
}
public String getRepositoryName() {
return myGithubSharePanel.getRepositoryName();
}
public boolean isPrivate() {
return myGithubSharePanel.isPrivate();
}
public String getDescription() {
return myGithubSharePanel.getDescription();
}
public String getRemoteName() {
return myGithubSharePanel.getRemoteName();
}
fun getRepositoryName(): String = repositoryTextField.text
fun getRemoteName(): String = remoteTextField.text
fun isPrivate(): Boolean = privateCheckBox.isSelected
fun getDescription(): String = descriptionTextArea.text
@TestOnly
public void testSetRepositoryName(@NotNull String name) {
myGithubSharePanel.setRepositoryName(name);
fun testSetRepositoryName(name: String) {
repositoryTextField.text = name
}
}
@@ -1,84 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.jetbrains.plugins.github.ui.GithubSharePanel">
<grid id="27dc6" binding="myPanel" layout-manager="GridLayoutManager" row-count="4" column-count="3" 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="383" height="102"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="199a4" 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="942c9"/>
<text value="New repository name:"/>
</properties>
</component>
<component id="942c9" class="javax.swing.JTextField" binding="myRepositoryTextField">
<constraints>
<grid row="0" 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/>
</component>
<component id="f3966" class="javax.swing.JCheckBox" binding="myPrivateCheckBox" default-binding="true">
<constraints>
<grid row="0" column="2" 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 value="Private"/>
</properties>
</component>
<component id="539e4" 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>
<labelFor value="7cb3e"/>
<text value="Remote name:"/>
</properties>
</component>
<component id="7cb3e" class="javax.swing.JTextField" binding="myRemoteTextField">
<constraints>
<grid row="1" column="1" row-span="1" col-span="2" 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/>
</component>
<component id="d1ca7" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="9" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="26029"/>
<text value="Description"/>
</properties>
</component>
<scrollpane id="83208" class="com.intellij.ui.components.JBScrollPane">
<constraints>
<grid row="2" column="1" row-span="1" col-span="2" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="250" height="100"/>
</grid>
</constraints>
<properties/>
<border type="empty"/>
<children>
<component id="26029" class="javax.swing.JTextArea" binding="myDescriptionTextArea">
<constraints/>
<properties/>
</component>
</children>
</scrollpane>
<vspacer id="18a1f">
<constraints>
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid>
</form>
@@ -1,72 +0,0 @@
package org.jetbrains.plugins.github.ui;
import com.intellij.ui.DocumentAdapter;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
/**
* @author oleg
* @date 10/22/10
*/
public class GithubSharePanel {
private JPanel myPanel;
private JTextField myRepositoryTextField;
private JCheckBox myPrivateCheckBox;
private JTextArea myDescriptionTextArea;
private JTextField myRemoteTextField;
private final GithubShareDialog myGithubShareDialog;
public GithubSharePanel(final GithubShareDialog githubShareDialog) {
myGithubShareDialog = githubShareDialog;
myPrivateCheckBox.setSelected(false);
DocumentAdapter changeListener = new DocumentAdapter() {
@Override
protected void textChanged(DocumentEvent e) {
myGithubShareDialog.updateOkButton();
}
};
myRepositoryTextField.getDocument().addDocumentListener(changeListener);
myRemoteTextField.getDocument().addDocumentListener(changeListener);
}
public JPanel getPanel() {
return myPanel;
}
public JComponent getPreferredFocusComponent() {
return myRepositoryTextField;
}
public String getRepositoryName() {
return myRepositoryTextField.getText().trim();
}
public void setRepositoryName(final String name) {
myRepositoryTextField.setText(name);
}
public String getRemoteName() {
return myRemoteTextField.getText().trim();
}
public void setRemoteName(final String name) {
myRemoteTextField.setText(name);
}
public boolean isPrivate() {
return myPrivateCheckBox.isSelected();
}
public String getDescription() {
return myDescriptionTextArea.getText().trim();
}
public void setPrivateRepoAvailable(final boolean privateRepoAllowed) {
if (!privateRepoAllowed) {
myPrivateCheckBox.setEnabled(false);
myPrivateCheckBox.setToolTipText("Your account doesn't support private repositories");
}
}
}