PageRenderTime 66ms CodeModel.GetById 20ms app.highlight 12ms RepoModel.GetById 18ms app.codeStats 0ms

/src/org/bitbucket/connectors/jetbrains/tasks/BitbucketIssueRepositoryEditor.java

https://bitbucket.org/atlassian/jetbrains-bitbucket-connector/
Java | 129 lines | 106 code | 23 blank | 0 comment | 17 complexity | 5fdb499b9d7c0988379d68e67873b796 MD5 | raw file
  1package org.bitbucket.connectors.jetbrains.tasks;
  2
  3import com.intellij.ui.ListCellRendererWrapper;
  4import com.intellij.openapi.diagnostic.Logger;
  5import com.intellij.openapi.project.Project;
  6import com.intellij.tasks.config.BaseRepositoryEditor;
  7import com.intellij.util.ArrayUtil;
  8import com.intellij.util.Consumer;
  9import org.bitbucket.connectors.jetbrains.BitbucketSettings;
 10import org.bitbucket.connectors.jetbrains.BitbucketUtil;
 11import org.bitbucket.connectors.jetbrains.RepositoryInfo;
 12import org.bitbucket.connectors.jetbrains.ui.BitbucketBundle;
 13
 14import javax.swing.*;
 15import java.awt.*;
 16import java.awt.event.ItemEvent;
 17import java.awt.event.ItemListener;
 18import java.lang.reflect.InvocationTargetException;
 19import java.util.Collections;
 20
 21public class BitbucketIssueRepositoryEditor extends BaseRepositoryEditor<BitbucketIssueRepository> {
 22    private static final Logger log =
 23            Logger.getInstance("#org.bitbucket.connectors.jetbrains.tasks.BitbucketIssueRepository");
 24
 25    private JComboBox mySelectRepositoryComboBox;
 26
 27    private JPanel bitbucketCustomPanel;
 28
 29    public BitbucketIssueRepositoryEditor(final Project project, final BitbucketIssueRepository repository,
 30                                          Consumer<BitbucketIssueRepository> changeListener) {
 31        super(project, repository, changeListener);
 32
 33        myUrlLabel.setVisible(false);
 34        myURLText.setVisible(false);
 35
 36        myPasswordLabel.setVisible(false);
 37        myPasswordText.setVisible(false);
 38
 39        myUserNameText.setEditable(false);
 40        myUserNameText.setText(BitbucketSettings.getInstance().getLogin());
 41
 42        myPasswordText.setText(BitbucketSettings.getInstance().getPassword());
 43
 44        if (!SwingUtilities.isEventDispatchThread()) try {
 45            SwingUtilities.invokeAndWait(new Runnable() {
 46                public void run() {
 47                    setupUI(project, repository);
 48                }
 49            });
 50        } catch (InterruptedException e) {
 51            log.info(e);
 52        } catch (InvocationTargetException e) {
 53            log.info(e);
 54        }
 55        else setupUI(project, repository);
 56
 57    }
 58
 59    protected JComponent createCustomPanel() {
 60        bitbucketCustomPanel = new JPanel();
 61        return bitbucketCustomPanel;
 62    }
 63
 64    private boolean setupUI(Project project, BitbucketIssueRepository repository) {
 65        java.util.List<RepositoryInfo> repos = BitbucketUtil.getRepositories(project, false);
 66
 67
 68        if (repos == null || repos.size() == 0) {
 69            bitbucketCustomPanel.add(new JLabel(BitbucketBundle.message("no-repos-available")), BorderLayout.CENTER);
 70            mySelectRepositoryComboBox = null;
 71            return true;
 72        }
 73
 74        Collections.sort(repos);
 75        mySelectRepositoryComboBox = new JComboBox(ArrayUtil.toObjectArray(repos));
 76
 77        mySelectRepositoryComboBox
 78                .setRenderer(new ListCellRendererWrapper<RepositoryInfo>() {
 79                    public void customize(JList list, RepositoryInfo value, int index, boolean selected,
 80                                          boolean cellHasFocus) {
 81                        if (value == null) return;
 82                        setText(value.getOwner() + "/" + value.getName());
 83                    }
 84                });
 85
 86
 87        for (int i = 0; i < mySelectRepositoryComboBox.getItemCount(); i++) {
 88            RepositoryInfo r = (RepositoryInfo) mySelectRepositoryComboBox.getItemAt(i);
 89            if (r == null) continue;
 90            log.debug(r.toString());
 91            if (r.getOwner().equals(repository.getRepositoryOwner()) && r.getSlug().equals(repository.getRepositoryName())) {
 92                mySelectRepositoryComboBox.setSelectedIndex(i);
 93                break;
 94            }
 95        }
 96        if (mySelectRepositoryComboBox.getSelectedIndex() == -1) {
 97            mySelectRepositoryComboBox.setSelectedIndex(0);
 98            apply();
 99        }
100        mySelectRepositoryComboBox.addItemListener(new ItemListener() {
101            public void itemStateChanged(ItemEvent e) {
102                apply();
103            }
104        });
105
106        bitbucketCustomPanel.add(mySelectRepositoryComboBox, BorderLayout.CENTER);
107        bitbucketCustomPanel.add(new JLabel(BitbucketBundle.message("select-task-repository"), SwingConstants.RIGHT) {
108            @Override
109            public Dimension getPreferredSize() {
110                final Dimension oldSize = super.getPreferredSize();
111                final Dimension size = mySelectRepositoryComboBox.getPreferredSize();
112                return new Dimension(oldSize.width, size.height);
113            }
114        }, BorderLayout.CENTER);
115
116        bitbucketCustomPanel.doLayout();
117        return false;
118    }
119
120    @Override
121    public void apply() {
122        if (mySelectRepositoryComboBox != null) {
123            final RepositoryInfo info = ((RepositoryInfo) mySelectRepositoryComboBox.getSelectedItem());
124            myRepository.setRepositoryName(info.getSlug());
125            myRepository.setRepositoryOwner(info.getOwner());
126        }
127        super.apply();
128    }
129}