ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / plugins / svn4idea / src / org / jetbrains / idea / svn / dialogs / SimpleCredentialsDialog.java
blobf899d7553026086755f4ee34b4205dc549cc2608
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.help.HelpManager;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.ui.DialogWrapper;
21 import org.jetbrains.annotations.NonNls;
22 import org.jetbrains.idea.svn.SvnBundle;
24 import javax.swing.*;
25 import javax.swing.event.DocumentEvent;
26 import javax.swing.event.DocumentListener;
27 import java.awt.*;
29 /**
30 * @author alex
32 public class SimpleCredentialsDialog extends DialogWrapper implements DocumentListener {
33 private boolean myAllowSave;
34 private String myUserName;
36 private String myRealm;
37 private JTextField myUserNameText;
38 private JCheckBox myAllowSaveCheckBox;
39 private JPasswordField myPasswordText;
41 @NonNls private static final String HELP_ID = "vcs.subversion.authentication";
43 protected SimpleCredentialsDialog(Project project) {
44 super(project, true);
45 setResizable(false);
48 public void setup(String realm, String userName, boolean allowSave) {
49 myRealm = realm;
50 myUserName = userName;
51 myAllowSave = allowSave;
52 getHelpAction().setEnabled(true);
53 init();
55 protected void doHelpAction() {
56 HelpManager.getInstance().invokeHelp(HELP_ID);
59 protected Action[] createActions() {
60 return new Action[]{getOKAction(), getCancelAction(), getHelpAction()};
63 protected JComponent createCenterPanel() {
64 JPanel panel = new JPanel();
65 panel.setLayout(new GridBagLayout());
67 GridBagConstraints gb = new GridBagConstraints();
69 // top label.
70 gb.insets = new Insets(2, 2, 2, 2);
71 gb.weightx = 1;
72 gb.weighty = 0;
73 gb.gridwidth = 2;
74 gb.gridheight = 1;
75 gb.gridx = 0;
76 gb.gridy = 0;
77 gb.anchor = GridBagConstraints.WEST;
78 gb.fill = GridBagConstraints.HORIZONTAL;
80 JLabel label = new JLabel(SvnBundle.message("label.auth.authentication.realm", myRealm));
81 panel.add(label, gb);
83 // user name
84 gb.gridy += 1;
85 gb.gridwidth = 1;
86 gb.weightx = 0;
87 gb.fill = GridBagConstraints.NONE;
89 label = new JLabel(SvnBundle.message("label.auth.user.name"));
90 panel.add(label, gb);
92 // user name field
93 gb.gridx = 1;
94 gb.weightx = 1;
95 gb.fill = GridBagConstraints.HORIZONTAL;
97 myUserNameText = new JTextField();
98 panel.add(myUserNameText, gb);
99 label.setLabelFor(myUserNameText);
101 if (myUserName != null) {
102 myUserNameText.setText(myUserName);
104 myUserNameText.selectAll();
105 myUserNameText.getDocument().addDocumentListener(this);
107 gb.gridy += 1;
108 gb.weightx = 0;
109 gb.gridx = 0;
110 gb.fill = GridBagConstraints.NONE;
111 gb.gridwidth = 1;
113 label = new JLabel(SvnBundle.message("label.auth.password"));
114 panel.add(label, gb);
116 // passworde field
117 gb.gridx = 1;
118 gb.weightx = 1;
119 gb.fill = GridBagConstraints.HORIZONTAL;
121 myPasswordText = new JPasswordField();
122 panel.add(myPasswordText, gb);
123 label.setLabelFor(myPasswordText);
125 gb.gridy += 1;
126 gb.gridx = 0;
127 gb.gridwidth = 2;
128 gb.weightx = 1;
129 gb.fill = GridBagConstraints.HORIZONTAL;
130 myAllowSaveCheckBox = new JCheckBox(SvnBundle.message("checkbox.auth.keep.for.current.session"));
131 panel.add(myAllowSaveCheckBox, gb);
132 gb.gridy += 1;
133 panel.add(new JSeparator(), gb);
135 myAllowSaveCheckBox.setSelected(false);
136 myAllowSaveCheckBox.setEnabled(myAllowSave);
138 return panel;
141 protected String getDimensionServiceKey() {
142 return "svn.passwordDialog";
145 public JComponent getPreferredFocusedComponent() {
146 return myUserNameText;
149 public boolean shouldCloseOnCross() {
150 return true;
153 public boolean isOKActionEnabled() {
154 return myUserNameText != null && myUserNameText.getText().trim().length() > 0
155 && myPasswordText != null && myPasswordText.getPassword() != null;
158 public String getUserName() {
159 return isOK() && myUserNameText != null ? myUserNameText.getText() : null;
162 public String getPassword() {
163 if (isOK() && myPasswordText != null) {
164 char[] pwd = myPasswordText.getPassword();
165 if (pwd != null) {
166 return new String(pwd);
169 return null;
172 public boolean isSaveAllowed() {
173 return isOK() && myAllowSave && myAllowSaveCheckBox != null && myAllowSaveCheckBox.isSelected();
176 public void insertUpdate(DocumentEvent e) {
177 updateOKButton();
180 public void removeUpdate(DocumentEvent e) {
181 updateOKButton();
184 public void changedUpdate(DocumentEvent e) {
185 updateOKButton();
188 private void updateOKButton() {
189 getOKAction().setEnabled(isOKActionEnabled());