SVN auth
[fedora-idea.git] / plugins / svn4idea / src / org / jetbrains / idea / svn / dialogs / SimpleCredentialsDialog.java
blob2a0d7cc88900656bd71cc50a98afbc3a59a362e3
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.jetbrains.idea.svn.dialogs;
18 import com.intellij.openapi.project.Project;
19 import com.intellij.openapi.ui.DialogWrapper;
20 import com.intellij.openapi.help.HelpManager;
22 import javax.swing.*;
23 import javax.swing.event.DocumentEvent;
24 import javax.swing.event.DocumentListener;
25 import java.awt.*;
27 import org.jetbrains.annotations.NonNls;
28 import org.jetbrains.idea.svn.SvnBundle;
30 /**
31 * Created by IntelliJ IDEA.
32 * User: alex
33 * Date: 25.06.2005
34 * Time: 16:47:22
35 * To change this template use File | Settings | File Templates.
37 public class SimpleCredentialsDialog extends DialogWrapper implements DocumentListener {
38 private boolean myAllowSave;
39 private String myUserName;
41 private String myRealm;
42 private JTextField myUserNameText;
43 private JCheckBox myAllowSaveCheckBox;
44 private JPasswordField myPasswordText;
46 @NonNls private static final String HELP_ID = "vcs.subversion.authentication";
48 protected SimpleCredentialsDialog(Project project) {
49 super(project, true);
50 setResizable(false);
53 public void setup(String realm, String userName, boolean allowSave) {
54 myRealm = realm;
55 myUserName = userName;
56 myAllowSave = allowSave;
57 getHelpAction().setEnabled(true);
58 init();
60 protected void doHelpAction() {
61 HelpManager.getInstance().invokeHelp(HELP_ID);
64 protected Action[] createActions() {
65 return new Action[]{getOKAction(), getCancelAction(), getHelpAction()};
68 protected JComponent createCenterPanel() {
69 JPanel panel = new JPanel();
70 panel.setLayout(new GridBagLayout());
72 GridBagConstraints gb = new GridBagConstraints();
74 // top label.
75 gb.insets = new Insets(2, 2, 2, 2);
76 gb.weightx = 1;
77 gb.weighty = 0;
78 gb.gridwidth = 2;
79 gb.gridheight = 1;
80 gb.gridx = 0;
81 gb.gridy = 0;
82 gb.anchor = GridBagConstraints.WEST;
83 gb.fill = GridBagConstraints.HORIZONTAL;
85 JLabel label = new JLabel(SvnBundle.message("label.auth.authentication.realm", myRealm));
86 panel.add(label, gb);
88 // user name
89 gb.gridy += 1;
90 gb.gridwidth = 1;
91 gb.weightx = 0;
92 gb.fill = GridBagConstraints.NONE;
94 label = new JLabel(SvnBundle.message("label.auth.user.name"));
95 panel.add(label, gb);
97 // user name field
98 gb.gridx = 1;
99 gb.weightx = 1;
100 gb.fill = GridBagConstraints.HORIZONTAL;
102 myUserNameText = new JTextField();
103 panel.add(myUserNameText, gb);
104 label.setLabelFor(myUserNameText);
106 if (myUserName != null) {
107 myUserNameText.setText(myUserName);
109 myUserNameText.selectAll();
110 myUserNameText.getDocument().addDocumentListener(this);
112 gb.gridy += 1;
113 gb.weightx = 0;
114 gb.gridx = 0;
115 gb.fill = GridBagConstraints.NONE;
116 gb.gridwidth = 1;
118 label = new JLabel(SvnBundle.message("label.auth.password"));
119 panel.add(label, gb);
121 // passworde field
122 gb.gridx = 1;
123 gb.weightx = 1;
124 gb.fill = GridBagConstraints.HORIZONTAL;
126 myPasswordText = new JPasswordField();
127 panel.add(myPasswordText, gb);
128 label.setLabelFor(myPasswordText);
130 gb.gridy += 1;
131 gb.gridx = 0;
132 gb.gridwidth = 2;
133 gb.weightx = 1;
134 gb.fill = GridBagConstraints.HORIZONTAL;
135 myAllowSaveCheckBox = new JCheckBox(SvnBundle.message("checkbox.auth.keep.for.current.session"));
136 panel.add(myAllowSaveCheckBox, gb);
137 gb.gridy += 1;
138 panel.add(new JSeparator(), gb);
140 myAllowSaveCheckBox.setSelected(false);
141 myAllowSaveCheckBox.setEnabled(myAllowSave);
143 return panel;
146 protected String getDimensionServiceKey() {
147 return "svn.passwordDialog";
150 public JComponent getPreferredFocusedComponent() {
151 return myUserNameText;
154 public boolean shouldCloseOnCross() {
155 return true;
158 public boolean isOKActionEnabled() {
159 return myUserNameText != null && myUserNameText.getText().trim().length() > 0
160 && myPasswordText != null && myPasswordText.getPassword() != null;
163 public String getUserName() {
164 return isOK() && myUserNameText != null ? myUserNameText.getText() : null;
167 public String getPassword() {
168 if (isOK() && myPasswordText != null) {
169 char[] pwd = myPasswordText.getPassword();
170 if (pwd != null) {
171 return new String(pwd);
174 return null;
177 public boolean isSaveAllowed() {
178 return isOK() && myAllowSave && myAllowSaveCheckBox != null && myAllowSaveCheckBox.isSelected();
181 public void insertUpdate(DocumentEvent e) {
182 updateOKButton();
185 public void removeUpdate(DocumentEvent e) {
186 updateOKButton();
189 public void changedUpdate(DocumentEvent e) {
190 updateOKButton();
193 private void updateOKButton() {
194 getOKAction().setEnabled(isOKActionEnabled());