vcs-impl
[idea/community.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / changes / ui / ChangeListChooser.java
1 package com.intellij.openapi.vcs.changes.ui;
2
3 import com.intellij.openapi.project.Project;
4 import com.intellij.openapi.ui.DialogWrapper;
5 import com.intellij.openapi.vcs.changes.ChangeList;
6 import com.intellij.openapi.vcs.changes.LocalChangeList;
7 import com.intellij.openapi.vcs.changes.LocalChangeListImpl;
8 import com.intellij.openapi.vcs.changes.ChangeListEditHandler;
9 import com.intellij.util.Consumer;
10 import org.jetbrains.annotations.NotNull;
11 import org.jetbrains.annotations.Nullable;
12
13 import javax.swing.*;
14 import java.util.Collection;
15
16 /**
17  * @author max
18  */
19 public class ChangeListChooser extends DialogWrapper {
20   private final Project myProject;
21   private LocalChangeList mySelectedList;
22   private final ChangeListChooserPanel myPanel;
23
24   public ChangeListChooser(@NotNull Project project,
25                            @NotNull Collection<? extends ChangeList> changelists,
26                            @Nullable ChangeList defaultSelection,
27                            final String title,
28                            @Nullable final String defaultName) {
29     super(project, false);
30     myProject = project;
31
32     ChangeListEditHandler handler = null;
33     for (ChangeList changelist : changelists) {
34       handler = ((LocalChangeListImpl)changelist).getEditHandler();
35       if (handler != null) {
36         break;
37       }
38     }
39
40     myPanel = new ChangeListChooserPanel(null, new Consumer<Boolean>() {
41       public void consume(final Boolean aBoolean) {
42         setOKActionEnabled(aBoolean);
43       }
44     });
45
46     myPanel.init(project);
47     myPanel.setChangeLists(changelists);
48     myPanel.setDefaultSelection(defaultSelection);
49
50     setTitle(title);
51     if (defaultName != null) {
52       myPanel.setDefaultName(defaultName);
53     }
54
55     init();
56   }
57
58   public JComponent getPreferredFocusedComponent() {
59     return myPanel.getPreferredFocusedComponent();
60   }
61
62   protected String getDimensionServiceKey() {
63     return "VCS.ChangelistChooser";
64   }
65
66   protected void doOKAction() {
67     mySelectedList = myPanel.getSelectedList(myProject);
68     if (mySelectedList != null) {
69       super.doOKAction();
70     }
71   }
72
73   public LocalChangeList getSelectedList() {
74     return mySelectedList;
75   }
76
77   protected JComponent createCenterPanel() {
78     return myPanel;
79   }
80 }