PageRenderTime 231ms CodeModel.GetById 100ms app.highlight 13ms RepoModel.GetById 98ms app.codeStats 0ms

/src/org/bitbucket/connectors/jetbrains/ui/BitbucketCloneProjectPanel.java

https://bitbucket.org/atlassian/jetbrains-bitbucket-connector/
Java | 155 lines | 129 code | 26 blank | 0 comment | 5 complexity | 12a04a5cb2789650c776106208380a12 MD5 | raw file
  1package org.bitbucket.connectors.jetbrains.ui;
  2
  3import com.intellij.openapi.fileChooser.FileChooser;
  4import com.intellij.openapi.fileChooser.FileChooserDescriptor;
  5import com.intellij.openapi.ui.TextFieldWithBrowseButton;
  6import com.intellij.openapi.vfs.LocalFileSystem;
  7import com.intellij.openapi.vfs.VirtualFile;
  8import com.intellij.ui.DocumentAdapter;
  9import com.intellij.ui.ListCellRendererWrapper;
 10import com.intellij.util.ArrayUtil;
 11import org.bitbucket.connectors.jetbrains.RepositoryInfo;
 12import org.jetbrains.annotations.Nullable;
 13
 14import javax.swing.*;
 15import javax.swing.event.DocumentEvent;
 16import javax.swing.event.DocumentListener;
 17import java.awt.event.ActionEvent;
 18import java.awt.event.ActionListener;
 19import java.awt.event.ItemEvent;
 20import java.awt.event.ItemListener;
 21import java.util.Collections;
 22import java.util.List;
 23
 24public class BitbucketCloneProjectPanel {
 25
 26    private JPanel myPanel;
 27
 28    private JComboBox mySelectRepositoryComboBox;
 29    private JTextField myRepositoryUrlField;
 30    private TextFieldWithBrowseButton myTextFieldWithBrowseButton;
 31    private JTextField myProjectNameText;
 32    private JRadioButton myRepositoryListRadioButton;
 33    private JRadioButton myRepositoryUrlRadioButton;
 34
 35    private BitbucketCloneProjectDialog myDialog;
 36
 37    public BitbucketCloneProjectPanel(BitbucketCloneProjectDialog dialog) {
 38        myDialog = dialog;
 39        mySelectRepositoryComboBox.setRenderer(new ListCellRendererWrapper<RepositoryInfo>() {
 40            public void customize(JList list, RepositoryInfo value, int index, boolean selected, boolean cellHasFocus) {
 41                setText(value.getOwner() + "/" + value.getName());
 42            }
 43        });
 44        mySelectRepositoryComboBox.addItemListener(new ItemListener() {
 45            public void itemStateChanged(ItemEvent e) {
 46                RepositoryInfo repositoryInfo = (RepositoryInfo) e.getItem();
 47                if (repositoryInfo != null) {
 48                    myProjectNameText.setText(repositoryInfo.getName());
 49                    myDialog.updateOkButton();
 50                }
 51            }
 52        });
 53
 54        DocumentListener listener = new DocumentAdapter() {
 55            protected void textChanged(DocumentEvent documentEvent) {
 56                myDialog.updateOkButton();
 57            }
 58        };
 59        myProjectNameText.getDocument().addDocumentListener(listener);
 60        myRepositoryUrlField.getDocument().addDocumentListener(listener);
 61        myTextFieldWithBrowseButton.getChildComponent().getDocument().addDocumentListener(listener);
 62        myTextFieldWithBrowseButton.setTextFieldPreferredWidth(50);
 63
 64        myTextFieldWithBrowseButton.addActionListener(new ActionListener() {
 65            public void actionPerformed(ActionEvent e) {
 66                chooseFolder();
 67            }
 68        });
 69
 70        myRepositoryListRadioButton.addActionListener(new ActionListener() {
 71            public void actionPerformed(ActionEvent e) {
 72                repositoryTypeChanged(true);
 73                myDialog.updateOkButton();
 74            }
 75        });
 76        myRepositoryUrlRadioButton.addActionListener(new ActionListener() {
 77            public void actionPerformed(ActionEvent e) {
 78                repositoryTypeChanged(false);
 79                myDialog.updateOkButton();
 80            }
 81        });
 82
 83        myRepositoryListRadioButton.setSelected(true);
 84        repositoryTypeChanged(true);
 85    }
 86
 87    private void repositoryTypeChanged(boolean selection) {
 88        mySelectRepositoryComboBox.setEnabled(selection);
 89        myRepositoryUrlField.setEnabled(!selection);
 90    }
 91
 92    private void chooseFolder() {
 93        FileChooserDescriptor fileChooserDescriptor = new FileChooserDescriptor(false, true, false, false, false, false) {
 94            public String getName(VirtualFile virtualFile) {
 95                return virtualFile.getName();
 96            }
 97
 98            @Nullable
 99            public String getComment(VirtualFile virtualFile) {
100                return virtualFile.getPresentableUrl();
101            }
102        };
103        fileChooserDescriptor.setTitle("Select project destination folder");
104
105        String preselectedFolderPath = myTextFieldWithBrowseButton.getText();
106        VirtualFile preselectedFolder = LocalFileSystem.getInstance().findFileByPath(preselectedFolderPath);
107
108        VirtualFile[] files = FileChooser.chooseFiles(fileChooserDescriptor, myPanel, null, preselectedFolder);
109        if (files.length > 0) {
110            myTextFieldWithBrowseButton.setText(files[0].getPath());
111        }
112    }
113
114    public JComponent getPanel() {
115        return myPanel;
116    }
117
118    public JComponent getPreferrableFocusComponent() {
119        return mySelectRepositoryComboBox;
120    }
121
122    public boolean isRepositoryUrl() {
123        return myRepositoryUrlRadioButton.isSelected();
124    }
125
126    public RepositoryInfo getSelectedRepository() {
127        return myRepositoryListRadioButton.isSelected() ? (RepositoryInfo) mySelectRepositoryComboBox.getModel().getSelectedItem() : null;
128    }
129
130    public String getSelectedPath() {
131        return myTextFieldWithBrowseButton.getText();
132    }
133
134    public String getProjectName() {
135        return myProjectNameText.getText();
136    }
137
138    public void setAvailableRepos(List<RepositoryInfo> repos) {
139        Collections.sort(repos);
140
141        mySelectRepositoryComboBox.setModel(new DefaultComboBoxModel(ArrayUtil.toObjectArray(repos)));
142        RepositoryInfo preselectedRepository = (RepositoryInfo) mySelectRepositoryComboBox.getSelectedItem();
143        if (preselectedRepository != null) {
144            myProjectNameText.setText(preselectedRepository.getName());
145        }
146    }
147
148    public void setSelectedPath(String path) {
149        myTextFieldWithBrowseButton.setText(path);
150    }
151
152    public String getRepositoryUrl() {
153        return myRepositoryUrlRadioButton.isSelected() ? myRepositoryUrlField.getText() : null;
154    }
155}