HgPullDialog access to UI components should be from AWT
[idea/community.git] / plugins / hg4idea / src / org / zmlx / hg4idea / ui / HgPullDialog.java
1 // Copyright 2008-2010 Victor Iacoban
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software distributed under
10 // the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11 // either express or implied. See the License for the specific language governing permissions and
12 // limitations under the License.
13 package org.zmlx.hg4idea.ui;
14
15 import com.intellij.openapi.application.ApplicationManager;
16 import com.intellij.openapi.project.Project;
17 import com.intellij.openapi.ui.DialogWrapper;
18 import com.intellij.openapi.vfs.VirtualFile;
19 import com.intellij.util.ui.UIUtil;
20 import org.apache.commons.lang.StringUtils;
21 import org.zmlx.hg4idea.command.HgShowConfigCommand;
22
23 import javax.swing.*;
24 import javax.swing.event.DocumentEvent;
25 import javax.swing.event.DocumentListener;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.util.Collection;
29
30 public class HgPullDialog extends DialogWrapper {
31
32   private final Project project;
33   private HgRepositorySelectorComponent hgRepositorySelector;
34   private JTextField sourceTxt;
35   private JPanel mainPanel;
36
37   public HgPullDialog(Project project) {
38     super(project, false);
39     this.project = project;
40     hgRepositorySelector.setTitle("Select repository to pull changesets for");
41     hgRepositorySelector.addActionListener(new ActionListener() {
42       public void actionPerformed(ActionEvent e) {
43         onChangeRepository();
44       }
45     });
46     DocumentListener documentListener = new DocumentListener() {
47       public void insertUpdate(DocumentEvent e) {
48         onChangePullSource();
49       }
50
51       public void removeUpdate(DocumentEvent e) {
52         onChangePullSource();
53       }
54
55       public void changedUpdate(DocumentEvent e) {
56         onChangePullSource();
57       }
58     };
59     sourceTxt.getDocument().addDocumentListener(documentListener);
60     setTitle("Pull");
61     init();
62   }
63
64   public VirtualFile getRepository() {
65     return hgRepositorySelector.getRepository();
66   }
67
68   public String getSource() {
69     return sourceTxt.getText();
70   }
71
72   public void setRoots(Collection<VirtualFile> repos) {
73     hgRepositorySelector.setRoots(repos);
74     onChangeRepository();
75   }
76
77   protected JComponent createCenterPanel() {
78     return mainPanel;
79   }
80
81   @Override
82   protected String getHelpId() {
83     return "reference.mercurial.pull.dialog";
84   }
85
86   private void onChangeRepository() {
87     ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
88       @Override
89       public void run() {
90         VirtualFile repo = hgRepositorySelector.getRepository();
91         HgShowConfigCommand configCommand = new HgShowConfigCommand(project);
92         final String defaultPath = configCommand.getDefaultPath(repo);
93         UIUtil.invokeAndWaitIfNeeded(new Runnable() {
94           @Override
95           public void run() {
96             sourceTxt.setText(defaultPath);
97           }
98         });
99
100         onChangePullSource();
101       }
102     });
103   }
104
105   private void onChangePullSource() {
106     setOKActionEnabled(StringUtils.isNotBlank(sourceTxt.getText()));
107   }
108
109 }