001: package com.vividsolutions.jump.workbench.ui.plugin.datastore;
002:
003: import java.awt.Component;
004: import java.awt.Dialog;
005: import java.awt.Frame;
006: import java.awt.GridBagConstraints;
007: import java.awt.GridBagLayout;
008: import java.awt.Insets;
009: import java.awt.Window;
010: import java.awt.event.ActionEvent;
011: import java.awt.event.ActionListener;
012: import java.lang.reflect.InvocationTargetException;
013: import java.util.Arrays;
014: import java.util.Iterator;
015:
016: import javax.swing.JButton;
017: import javax.swing.JDialog;
018: import javax.swing.JFrame;
019: import javax.swing.JLabel;
020: import javax.swing.JPasswordField;
021: import javax.swing.SwingUtilities;
022:
023: import com.vividsolutions.jts.util.Assert;
024: import com.vividsolutions.jump.I18N;
025: import com.vividsolutions.jump.datastore.DataStoreConnection;
026: import com.vividsolutions.jump.datastore.DataStoreException;
027: import com.vividsolutions.jump.workbench.datastore.ConnectionDescriptor;
028: import com.vividsolutions.jump.workbench.datastore.ConnectionManager;
029: import com.vividsolutions.jump.workbench.ui.GUIUtil;
030:
031: public class PasswordPrompter {
032: public DataStoreConnection getOpenConnection(
033: ConnectionManager connectionManager,
034: ConnectionDescriptor connectionDescriptor,
035: Component parentComponent) throws Exception {
036: return connectionManager
037: .getOpenConnection(promptForPasswordIfNecessary(
038: connectionDescriptor, connectionManager,
039: parentComponent));
040: }
041:
042: private ConnectionDescriptor promptForPasswordIfNecessary(
043: ConnectionDescriptor connectionDescriptor,
044: ConnectionManager connectionManager,
045: Component parentComponent) throws DataStoreException {
046: String passwordParameterName = ConnectionDescriptor
047: .passwordParameterName(connectionDescriptor
048: .getParameterList().getSchema());
049: if (passwordParameterName == null) {
050: return connectionDescriptor;
051: }
052: // To know if a password has been entered correctly, check if the
053: // connection is open rather then if the password has been set,
054: // as the user may have set a password that is incorrect.
055: // [Jon Aquino 2005-03-11]
056: /*
057: * if
058: * (!connectionManager.getConnection(connectionDescriptor).isClosed()) {
059: * return connectionDescriptor; }
060: */
061: // The above isn't going to work anymore, because we are now saving
062: // passwords. Just check whether the password is null for now.
063: // Of course this doesn't handle the case in which a password
064: // is incorrect or changed. Need to design a generic way to identify an
065: // error as being a password error, without being specific to Oracle,
066: // SDE, etc. [Jon Aquino 2005-03-17]
067: if (connectionDescriptor.getParameterList().getParameterString(
068: passwordParameterName) != null) {
069: return connectionDescriptor;
070: }
071: connectionDescriptor.getParameterList().setParameter(
072: passwordParameterName,
073: promptForPassword(identifier(connectionDescriptor),
074: parentComponent));
075: return connectionDescriptor;
076: }
077:
078: /**
079: * @return a username or other brief description of this
080: * ConnectionDescriptor
081: */
082: private String identifier(ConnectionDescriptor connectionDescriptor) {
083: for (Iterator i = Arrays.asList(
084: connectionDescriptor.getParameterList().getSchema()
085: .getNames()).iterator(); i.hasNext();) {
086: String name = (String) i.next();
087: if (name.toLowerCase().matches("user.*")) {
088: return connectionDescriptor.getParameterList()
089: .getParameter(name).toString();
090: }
091: }
092: return connectionDescriptor.getParameterList().getParameter(
093: connectionDescriptor.getParameterList().getSchema()
094: .getNames()[0]).toString();
095: }
096:
097: /**
098: * @param identifier
099: * username or other description of what the password is for
100: */
101: private String promptForPassword(String identifier, Component parent) {
102: Assert.isTrue(SwingUtilities.isEventDispatchThread());
103: final JPasswordField passwordField = new JPasswordField(15);
104: final JDialog dialog = createDialog(
105: I18N
106: .get("jump.workbench.ui.plugin.datastore.PasswordPrompter.Password"),
107: parent);
108: dialog.getContentPane().setLayout(new GridBagLayout());
109: dialog
110: .getContentPane()
111: .add(
112: new JLabel(
113: I18N
114: .get("jump.workbench.ui.plugin.datastore.PasswordPrompter.Enter-password-for")
115: + " " + identifier + ": "),
116: new GridBagConstraints(0, 0, 1, 1, 0, 0,
117: GridBagConstraints.CENTER,
118: GridBagConstraints.NONE, new Insets(2,
119: 2, 2, 2), 0, 0));
120: dialog.getContentPane().add(
121: passwordField,
122: new GridBagConstraints(0, 1, 1, 1, 0, 0,
123: GridBagConstraints.CENTER,
124: GridBagConstraints.NONE,
125: new Insets(2, 2, 2, 2), 0, 0));
126: dialog
127: .getContentPane()
128: .add(
129: new JButton(
130: I18N
131: .get("jump.workbench.ui.plugin.datastore.PasswordPrompter.OK")) {
132: {
133: addActionListener(new ActionListener() {
134: public void actionPerformed(
135: ActionEvent e) {
136: dialog.setVisible(false);
137: }
138: });
139: dialog.getRootPane().setDefaultButton(
140: this );
141: }
142: },
143: new GridBagConstraints(0, 2, 1, 1, 0, 0,
144: GridBagConstraints.CENTER,
145: GridBagConstraints.NONE, new Insets(2,
146: 2, 2, 2), 0, 0));
147: dialog.pack();
148: GUIUtil.centreOnWindow(dialog);
149: dialog.setVisible(true);
150: return new String(passwordField.getPassword());
151: }
152:
153: private JDialog createDialog(String title, Component parent) {
154: return window(parent) instanceof Frame ? new JDialog(
155: (Frame) window(parent), title, true) : new JDialog(
156: (Dialog) window(parent), title, true);
157: }
158:
159: private Window window(Component component) {
160: return component instanceof Window ? (Window) component
161: : SwingUtilities.windowForComponent(component);
162: }
163:
164: public static void main(String[] args) throws InterruptedException,
165: InvocationTargetException {
166: SwingUtilities.invokeAndWait(new Runnable() {
167: public void run() {
168: JFrame frame = new JFrame();
169: GUIUtil.centreOnScreen(frame);
170: new PasswordPrompter()
171: .promptForPassword("scott", frame);
172: }
173: });
174: }
175: }
|