summaryrefslogtreecommitdiff
path: root/plugins/github/src/org/jetbrains/plugins/github/ui/GithubSettingsPanel.java
blob: 229170e29ead6f1532e34799c32ab49bde0790ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * Copyright 2000-2010 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.plugins.github.ui;

import com.intellij.ide.BrowserUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.DocumentAdapter;
import com.intellij.ui.HyperlinkAdapter;
import com.intellij.ui.components.JBLabel;
import com.intellij.util.ThrowableConvertor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.github.api.GithubApiUtil;
import org.jetbrains.plugins.github.api.GithubUser;
import org.jetbrains.plugins.github.exceptions.GithubAuthenticationException;
import org.jetbrains.plugins.github.exceptions.GithubOperationCanceledException;
import org.jetbrains.plugins.github.util.*;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.HyperlinkEvent;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

/**
 * @author oleg
 * @date 10/20/10
 */
public class GithubSettingsPanel {
  private static final String DEFAULT_PASSWORD_TEXT = "************";
  private final static String AUTH_PASSWORD = "Password";
  private final static String AUTH_TOKEN = "Token";

  private static final Logger LOG = GithubUtil.LOG;

  private final GithubSettings mySettings;

  private JTextField myLoginTextField;
  private JPasswordField myPasswordField;
  private JPasswordField myTokenField; // look at createUIComponents() to understand
  private JTextPane mySignupTextField;
  private JPanel myPane;
  private JButton myTestButton;
  private JTextField myHostTextField;
  private ComboBox myAuthTypeComboBox;
  private JPanel myCardPanel;
  private JBLabel myAuthTypeLabel;
  private JSpinner myTimeoutSpinner;
  private JButton myCreateTokenButton;

  private boolean myCredentialsModified;

  public GithubSettingsPanel() {
    mySettings = GithubSettings.getInstance();
    mySignupTextField.addHyperlinkListener(new HyperlinkAdapter() {
      @Override
      protected void hyperlinkActivated(final HyperlinkEvent e) {
        BrowserUtil.browse(e.getURL());
      }
    });
    mySignupTextField.setText("<html>Do not have an account at github.com? <a href=\"https://github.com\">" + "Sign up" + "</a></html>");
    mySignupTextField.setBackground(myPane.getBackground());
    mySignupTextField.setCursor(new Cursor(Cursor.HAND_CURSOR));
    myAuthTypeLabel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
    myAuthTypeComboBox.addItem(AUTH_PASSWORD);
    myAuthTypeComboBox.addItem(AUTH_TOKEN);

    final Project project = ProjectManager.getInstance().getDefaultProject();

    myTestButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        try {
          final GithubAuthData auth = getAuthData();
          GithubUser user = GithubUtil
            .computeValueInModal(project, "Access to GitHub", new ThrowableConvertor<ProgressIndicator, GithubUser, IOException>() {
              @NotNull
              @Override
              public GithubUser convert(ProgressIndicator indicator) throws IOException {
                return GithubUtil.checkAuthData(project, new GithubAuthDataHolder(auth), indicator);
              }
            });

          if (GithubAuthData.AuthType.TOKEN.equals(getAuthType())) {
            GithubNotifications.showInfoDialog(myPane, "Success", "Connection successful for user " + user.getLogin());
          }
          else {
            GithubNotifications.showInfoDialog(myPane, "Success", "Connection successful");
          }
        }
        catch (GithubAuthenticationException ex) {
          GithubNotifications.showErrorDialog(myPane, "Login Failure", "Can't login using given credentials: " + ex.getMessage());
        }
        catch (IOException ex) {
          LOG.info(ex);
          GithubNotifications.showErrorDialog(myPane, "Login Failure", "Can't login: " + GithubUtil.getErrorTextFromException(ex));
        }
      }
    });

    myCreateTokenButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        try {
          myPasswordField.setText(
            GithubUtil.computeValueInModal(project, "Access to GitHub", new ThrowableConvertor<ProgressIndicator, String, IOException>() {
              @NotNull
              @Override
              public String convert(ProgressIndicator indicator) throws IOException {
                return GithubUtil.runTaskWithBasicAuthForHost(project, GithubAuthDataHolder.createFromSettings(indicator.getModalityState()),
                                                              indicator, getHost(),
                                                              new ThrowableConvertor<GithubAuthData, String, IOException>() {
                                                                @NotNull
                                                                @Override
                                                                public String convert(@NotNull GithubAuthData auth) throws IOException {
                                                                  return GithubApiUtil.getMasterToken(auth, "IntelliJ plugin");
                                                                }
                                                              }
                );
              }
            })
          );
        }
        catch (IOException ex) {
          GithubNotifications.showErrorDialog(myPane, "Can't create API token", ex);
        }
      }
    });

    myPasswordField.getDocument().addDocumentListener(new DocumentAdapter() {
      @Override
      protected void textChanged(DocumentEvent e) {
        myCredentialsModified = true;
      }
    });

    DocumentListener passwordEraser = new DocumentAdapter() {
      @Override
      protected void textChanged(DocumentEvent e) {
        if (!myCredentialsModified) {
          erasePassword();
        }
      }
    };
    myHostTextField.getDocument().addDocumentListener(passwordEraser);
    myLoginTextField.getDocument().addDocumentListener(passwordEraser);

    myPasswordField.addFocusListener(new FocusListener() {
      @Override
      public void focusGained(FocusEvent e) {
        if (!myCredentialsModified && !getPassword().isEmpty()) {
          erasePassword();
        }
      }

      @Override
      public void focusLost(FocusEvent e) {
      }
    });

    myAuthTypeComboBox.addItemListener(new ItemListener() {
      @Override
      public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
          String item = e.getItem().toString();
          if (AUTH_PASSWORD.equals(item)) {
            ((CardLayout)myCardPanel.getLayout()).show(myCardPanel, AUTH_PASSWORD);
          }
          else if (AUTH_TOKEN.equals(item)) {
            ((CardLayout)myCardPanel.getLayout()).show(myCardPanel, AUTH_TOKEN);
          }
          erasePassword();
        }
      }
    });

    reset();
  }

  private void erasePassword() {
    setPassword("");
    myCredentialsModified = true;
  }

  public JComponent getPanel() {
    return myPane;
  }

  @NotNull
  public String getHost() {
    return myHostTextField.getText().trim();
  }

  @NotNull
  public String getLogin() {
    return myLoginTextField.getText().trim();
  }

  public void setHost(@NotNull final String host) {
    myHostTextField.setText(host);
  }

  public void setLogin(@Nullable final String login) {
    myLoginTextField.setText(login);
  }

  @NotNull
  private String getPassword() {
    return String.valueOf(myPasswordField.getPassword());
  }

  private void setPassword(@NotNull final String password) {
    // Show password as blank if password is empty
    myPasswordField.setText(StringUtil.isEmpty(password) ? null : password);
  }

  @NotNull
  public GithubAuthData.AuthType getAuthType() {
    Object selected = myAuthTypeComboBox.getSelectedItem();
    if (AUTH_PASSWORD.equals(selected)) return GithubAuthData.AuthType.BASIC;
    if (AUTH_TOKEN.equals(selected)) return GithubAuthData.AuthType.TOKEN;
    LOG.error("GithubSettingsPanel: illegal selection: basic AuthType returned", selected.toString());
    return GithubAuthData.AuthType.BASIC;
  }

  public void setAuthType(@NotNull final GithubAuthData.AuthType type) {
    switch (type) {
      case BASIC:
        myAuthTypeComboBox.setSelectedItem(AUTH_PASSWORD);
        break;
      case TOKEN:
        myAuthTypeComboBox.setSelectedItem(AUTH_TOKEN);
        break;
      case ANONYMOUS:
      default:
        myAuthTypeComboBox.setSelectedItem(AUTH_PASSWORD);
    }
  }

  @NotNull
  public GithubAuthData getAuthData() {
    if (!myCredentialsModified) {
      return mySettings.getAuthData(null);
    }
    Object selected = myAuthTypeComboBox.getSelectedItem();
    if (AUTH_PASSWORD.equals(selected)) return GithubAuthData.createBasicAuth(getHost(), getLogin(), getPassword());
    if (AUTH_TOKEN.equals(selected)) return GithubAuthData.createTokenAuth(getHost(), getPassword());
    LOG.error("GithubSettingsPanel: illegal selection: anonymous AuthData created", selected.toString());
    return GithubAuthData.createAnonymous(getHost());
  }

  public void setConnectionTimeout(int timeout) {
    myTimeoutSpinner.setValue(Integer.valueOf(timeout));
  }

  public int getConnectionTimeout() {
    return ((SpinnerNumberModel)myTimeoutSpinner.getModel()).getNumber().intValue();
  }

  public void reset() {
    setHost(mySettings.getHost());
    setLogin(mySettings.getLogin());
    setPassword(mySettings.isAuthConfigured() ? DEFAULT_PASSWORD_TEXT : "");
    setAuthType(mySettings.getAuthType());
    setConnectionTimeout(mySettings.getConnectionTimeout());
    resetCredentialsModification();
  }

  public void apply() {
    if (myCredentialsModified) {
      mySettings.setAuthData(getAuthData(), true, null);
    }
    mySettings.setConnectionTimeout(getConnectionTimeout());
    resetCredentialsModification();
  }

  public boolean isModified() {
    return myCredentialsModified || !Comparing.equal(mySettings.getHost(), getHost()) ||
           !Comparing.equal(mySettings.getConnectionTimeout(), getConnectionTimeout());
  }

  public void resetCredentialsModification() {
    myCredentialsModified = false;
  }

  private void createUIComponents() {
    Document doc = new PlainDocument();
    myPasswordField = new JPasswordField(doc, null, 0);
    myTokenField = new JPasswordField(doc, null, 0);
    myTimeoutSpinner =
      new JSpinner(new SpinnerNumberModel(Integer.valueOf(5000), Integer.valueOf(0), Integer.valueOf(60000), Integer.valueOf(500)));
  }
}