001: /*
002: * Gruntspud
003: *
004: * Copyright (C) 2002 Brett Smith.
005: *
006: * Written by: Brett Smith <t_magicthize@users.sourceforge.net>
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public License
010: * as published by the Free Software Foundation; either version 2 of
011: * the License, or (at your option) any later version.
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Library General Public License for more details.
016: *
017: * You should have received a copy of the GNU Library General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: */
021:
022: package gruntspud.ui.preferences;
023:
024: import gruntspud.GruntspudUtil;
025: import gruntspud.connection.ConnectionProfile;
026: import gruntspud.connection.pserver.CVSRootPServerConnection;
027: import gruntspud.ui.UIUtil;
028: import gruntspud.ui.XTextField;
029:
030: import java.awt.GridBagConstraints;
031: import java.awt.GridBagLayout;
032: import java.awt.Insets;
033: import java.awt.event.ActionEvent;
034: import java.awt.event.ActionListener;
035: import java.io.BufferedReader;
036: import java.io.File;
037: import java.io.FileInputStream;
038: import java.io.IOException;
039: import java.io.InputStream;
040: import java.io.InputStreamReader;
041:
042: import javax.swing.ButtonGroup;
043: import javax.swing.JButton;
044: import javax.swing.JCheckBox;
045: import javax.swing.JFileChooser;
046: import javax.swing.JPasswordField;
047: import javax.swing.JRadioButton;
048:
049: /**
050: * Description of the Class
051: *
052: *@author magicthize
053: *@created 26 May 2002
054: */
055: public class PasswordAuthenticationOptionsPane extends
056: AbstractAdditionalOptionsPane implements ActionListener {
057: public final static String PASSWORD_AUTHENTICATION_TYPE = "password.authenticationType";
058: public final static String DONT_ASK_PASSWORD = "password.dontAsk.password";
059: public final static String TYPE_DONT_ASK = "dontAsk";
060: public final static String TYPE_USE_CVSPASS = "useCVSPASS";
061: public final static String TYPE_USE_PASSWORD_MANAGER = "usePasswordManager";
062:
063: // Private instance variables
064: private JPasswordField password;
065: private JRadioButton useCVSPASS;
066: private JRadioButton dontAsk;
067: private JRadioButton usePasswordManager;
068: private ConnectionProfile profile;
069: private XTextField cvspassFile;
070: private JButton browse;
071: private JCheckBox oldFormat;
072:
073: /**
074: * Constructor for the PasswordAuthenticationOptionsPane object
075: *
076: *@param host Description of the Parameter
077: */
078: public PasswordAuthenticationOptionsPane() {
079: this (false);
080: }
081:
082: /**
083: * Constructor for the PasswordAuthenticationOptionsPane object
084: *
085: *@param host Description of the Parameter
086: */
087: public PasswordAuthenticationOptionsPane(boolean offerUseOfCVSPASS) {
088: setLayout(new GridBagLayout());
089:
090: Insets i1 = new Insets(1, 1, 2, 2);
091: Insets i2 = new Insets(1, 24, 2, 2);
092:
093: GridBagConstraints gbc = new GridBagConstraints();
094: gbc.anchor = GridBagConstraints.NORTHWEST;
095: gbc.fill = GridBagConstraints.HORIZONTAL;
096: gbc.insets = i1;
097:
098: ButtonGroup bg = new ButtonGroup();
099:
100: gbc.insets = i1;
101: gbc.weightx = 2.0;
102: usePasswordManager = new JRadioButton("Use Password Manager");
103: UIUtil.jGridBagAdd(this , usePasswordManager, gbc,
104: GridBagConstraints.REMAINDER);
105: usePasswordManager.setMnemonic('m');
106: usePasswordManager.addActionListener(this );
107: bg.add(usePasswordManager);
108:
109: if (offerUseOfCVSPASS) {
110: useCVSPASS = new JRadioButton("Use ~/.cvspass");
111: UIUtil.jGridBagAdd(this , useCVSPASS, gbc,
112: GridBagConstraints.REMAINDER);
113: useCVSPASS.setMnemonic('c');
114: useCVSPASS.addActionListener(this );
115: bg.add(useCVSPASS);
116: gbc.weightx = 1.0;
117: gbc.insets = i2;
118: cvspassFile = new XTextField(12);
119: UIUtil.jGridBagAdd(this , cvspassFile, gbc,
120: GridBagConstraints.RELATIVE);
121: gbc.weightx = 0.0;
122: browse = new JButton("Browse");
123: browse.setMnemonic('b');
124: browse.addActionListener(this );
125: UIUtil.jGridBagAdd(this , browse, gbc,
126: GridBagConstraints.REMAINDER);
127: gbc.weightx = 2.0;
128: oldFormat = new JCheckBox("Use old format (pre 1.11)");
129: UIUtil.jGridBagAdd(this , oldFormat, gbc,
130: GridBagConstraints.REMAINDER);
131: oldFormat.setMnemonic('e');
132: oldFormat.addActionListener(this );
133: gbc.insets = i1;
134: }
135:
136: dontAsk = new JRadioButton("Use this password");
137: dontAsk.setMnemonic('p');
138: dontAsk.addActionListener(this );
139: UIUtil.jGridBagAdd(this , dontAsk, gbc,
140: GridBagConstraints.REMAINDER);
141: gbc.insets = i2;
142: bg.add(dontAsk);
143: gbc.weighty = 1.0;
144: UIUtil.jGridBagAdd(this , password = new JPasswordField(12),
145: gbc, GridBagConstraints.REMAINDER);
146:
147: setAvailableActions();
148: }
149:
150: /**
151: * DOCUMENT ME!
152: *
153: * @param enabled DOCUMENT ME!
154: */
155: public void setEnabled(boolean enabled) {
156: super .setEnabled(enabled);
157: usePasswordManager.setEnabled(enabled);
158: dontAsk.setEnabled(enabled);
159:
160: if (useCVSPASS != null) {
161: useCVSPASS.setEnabled(enabled);
162: oldFormat.setEnabled(enabled);
163: }
164:
165: setAvailableActions();
166: }
167:
168: /**
169: *
170: */
171: public void actionPerformed(ActionEvent evt) {
172: if (evt.getSource() == browse) {
173: File f = new File(cvspassFile.getText());
174: JFileChooser chooser = new JFileChooser(f);
175: chooser.setSelectedFile(f);
176: chooser.setDialogTitle("Choose .cvspass file");
177: chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
178:
179: if (chooser.showOpenDialog(this ) == JFileChooser.APPROVE_OPTION) {
180: cvspassFile.setText(chooser.getSelectedFile()
181: .getAbsolutePath());
182: }
183: }
184:
185: setAvailableActions();
186: }
187:
188: private void setAvailableActions() {
189: password.setEnabled(isEnabled() && dontAsk.isSelected());
190:
191: if (useCVSPASS != null) {
192: boolean enabled = isEnabled() && useCVSPASS.isSelected();
193: cvspassFile.setEnabled(enabled);
194: browse.setEnabled(enabled);
195: oldFormat.setEnabled(enabled);
196: }
197: }
198:
199: /**
200: * Set the connection profile
201: *
202: * @param profile connection profile
203: */
204: public void setProfile(ConnectionProfile profile) {
205: this .profile = profile;
206:
207: if (profile == null) {
208: password.setText("");
209: dontAsk.setSelected(true);
210: cvspassFile.setText("");
211: oldFormat.setSelected(false);
212: } else {
213: password
214: .setText(profile.getProperty(DONT_ASK_PASSWORD, ""));
215:
216: String t = profile.getProperty(
217: PASSWORD_AUTHENTICATION_TYPE,
218: (useCVSPASS == null) ? TYPE_USE_PASSWORD_MANAGER
219: : TYPE_USE_CVSPASS);
220:
221: if (t.equals(TYPE_DONT_ASK)) {
222: dontAsk.setSelected(true);
223: } else if (t.equals(TYPE_USE_PASSWORD_MANAGER)) {
224: usePasswordManager.setSelected(true);
225: } else if (t.equals(TYPE_USE_CVSPASS)
226: && (useCVSPASS != null)) {
227: useCVSPASS.setSelected(true);
228:
229: }
230: if (cvspassFile != null) {
231: cvspassFile
232: .setText(profile
233: .getProperty(
234: CVSRootPServerConnection.PROFILE_PROPERTY_CVSPASS_FILE,
235: System.getProperty("user.home")
236: + File.separator
237: + ".cvspass"));
238: oldFormat
239: .setSelected(profile
240: .getProperty(
241: CVSRootPServerConnection.PROFILE_PROPERTY_CVSPASS_OLD_FORMAT,
242: "false").equals("true"));
243: }
244: }
245:
246: setAvailableActions();
247: }
248:
249: /**
250: * Description of the Method
251: *
252: *@return Description of the Return Value
253: */
254: public boolean validateOptions() {
255: if (profile == null) {
256: return false;
257: }
258:
259: if ((useCVSPASS != null) && useCVSPASS.isSelected()) {
260: InputStream in = null;
261:
262: try {
263: File f = new File(cvspassFile.getText());
264:
265: if (!f.exists() || !f.canRead() || !f.isFile()) {
266: throw new Exception(
267: "Specified .cvspass file is invalid.");
268: }
269:
270: in = new FileInputStream(f);
271:
272: BufferedReader r = new BufferedReader(
273: new InputStreamReader(in));
274: String l = r.readLine();
275:
276: if (l != null) {
277: if (l.startsWith("/") && oldFormat.isSelected()) {
278: throw new IOException(
279: "Specified .cvspass file is not pre 1.11 format");
280: }
281:
282: if (!l.startsWith("/") && !oldFormat.isSelected()) {
283: throw new IOException(
284: "Specified .cvspass file is in pre 1.11 format");
285: }
286: }
287: } catch (Exception e) {
288: GruntspudUtil.showErrorMessage(this , "Error", e);
289: return false;
290: } finally {
291: if (in != null) {
292: try {
293: in.close();
294: } catch (IOException ioe) {
295: }
296: }
297: }
298: }
299:
300: return true;
301: }
302:
303: /**
304: * Description of the Method
305: */
306: public void applyOptions() {
307: String pw = new String(password.getPassword());
308: profile.setProperty(DONT_ASK_PASSWORD, pw);
309:
310: String t = (useCVSPASS == null) ? TYPE_USE_PASSWORD_MANAGER
311: : TYPE_USE_CVSPASS;
312:
313: if (dontAsk.isSelected()) {
314: t = TYPE_DONT_ASK;
315: } else if (usePasswordManager.isSelected()) {
316: t = TYPE_USE_PASSWORD_MANAGER;
317: } else if ((useCVSPASS != null) && useCVSPASS.isSelected()) {
318: t = TYPE_USE_CVSPASS;
319:
320: }
321: profile.setProperty(PASSWORD_AUTHENTICATION_TYPE, t);
322:
323: if (cvspassFile != null) {
324: profile
325: .setProperty(
326: CVSRootPServerConnection.PROFILE_PROPERTY_CVSPASS_FILE,
327: cvspassFile.getText());
328: profile
329: .setProperty(
330: CVSRootPServerConnection.PROFILE_PROPERTY_CVSPASS_OLD_FORMAT,
331: String.valueOf(oldFormat.isSelected()));
332: }
333: }
334: }
|