This is an automated email from the ASF dual-hosted git repository.

thurka pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new b650da6f3a headless mode - allow to re-enter username and password if 
connecting to database fails
     new 850af2af6c Merge pull request #6057 from thurka/GCN-2644
b650da6f3a is described below

commit b650da6f3a3523623270acfba5f482b7f3d7c627
Author: Tomas Hurka <[email protected]>
AuthorDate: Fri Jun 9 07:25:20 2023 +0200

    headless mode - allow to re-enter username and password if connecting to 
database fails
---
 .../modules/db/explorer/action/ConnectAction.java  | 260 +++++++++++++++------
 1 file changed, 191 insertions(+), 69 deletions(-)

diff --git 
a/ide/db/src/org/netbeans/modules/db/explorer/action/ConnectAction.java 
b/ide/db/src/org/netbeans/modules/db/explorer/action/ConnectAction.java
index 7418c3756e..bbe0f41327 100644
--- a/ide/db/src/org/netbeans/modules/db/explorer/action/ConnectAction.java
+++ b/ide/db/src/org/netbeans/modules/db/explorer/action/ConnectAction.java
@@ -35,7 +35,6 @@ import java.sql.SQLException;
 import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.concurrent.CountDownLatch;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import javax.swing.AbstractAction;
@@ -68,6 +67,7 @@ import org.openide.util.HelpCtx;
 import org.openide.util.Lookup;
 import org.openide.util.Mutex;
 import org.openide.util.NbBundle;
+import org.openide.util.RequestProcessor;
 import org.openide.util.WeakListeners;
 
 @ActionRegistration(
@@ -163,7 +163,6 @@ public class ConnectAction extends AbstractAction 
implements ContextAwareAction,
         @NbBundle.Messages({
             "# {0} - connection name",
             "Progress_ConnectingDB=Connecting to {0}",
-            "CannotConnectHeadless=Required connection properties missing or 
incorrect, cannot connect."
         })
         public void showDialog(final DatabaseConnection dbcon, boolean 
showDialog) {
             String user = dbcon.getUser();
@@ -182,12 +181,13 @@ public class ConnectAction extends AbstractAction 
implements ContextAwareAction,
             if (((!supportsConnectWithoutUsername(dbcon))
                     && (user == null || !remember)) || showDialog) {
                 
+                final Credentials input;
                 if (headless) {
-                    DialogDisplayer.getDefault().notifyLater(
-                            new 
NotifyDescriptor.Message(Bundle.CannotConnectHeadless(), 
NotifyDescriptor.ERROR_MESSAGE));
-                    return;
+                    input = new CredetialsLine(dbcon);
+                } else {
+                    ConnectPanel basePanel = new ConnectPanel(this, dbcon);
+                    input = new CredetialsUI(basePanel);
                 }
-                final ConnectPanel basePanel = new ConnectPanel(this, dbcon);
 
                 final PropertyChangeListener connectionListener = new 
PropertyChangeListener() {
                     @Override
@@ -207,13 +207,15 @@ public class ConnectAction extends AbstractAction 
implements ContextAwareAction,
                                 
realDbcon.setRememberPassword(dbcon.rememberPassword());
                             }
                             
-                            
dbcon.setRememberPassword(basePanel.rememberPassword());
+                            
dbcon.setRememberPassword(input.rememberPassword());
 
-                            SwingUtilities.invokeLater(() -> {
-                                if (dlg != null) {
-                                    dlg.close();
-                                }
-                            });
+                            if (!headless) {
+                                SwingUtilities.invokeLater(() -> {
+                                    if (dlg != null) {
+                                        dlg.close();
+                                    }
+                                });
+                            }
                         }
                     }
                     }
@@ -221,43 +223,29 @@ public class ConnectAction extends AbstractAction 
implements ContextAwareAction,
 
                 dbcon.addPropertyChangeListener(connectionListener);
 
-                ActionListener actionListener = new ActionListener() {
-                    @Override
-                    public void actionPerformed(ActionEvent event) {
-                        if (event.getSource() == DialogDescriptor.OK_OPTION) {
-                            dbcon.setUser(basePanel.getUser());
-                            dbcon.setPassword(basePanel.getPassword());
-                            dbcon.setUser(basePanel.getUser());
-                            dbcon.setPassword(basePanel.getPassword());
-                            
dbcon.setRememberPassword(basePanel.rememberPassword());
-
-                            if (! dbcon.isVitalConnection()) {
-                                dbcon.connectAsync();
-                            } else {
-                                DatabaseConnection realDbcon = 
ConnectionList.getDefault().getConnection(dbcon);
-                                if (realDbcon != null) {
-                                    realDbcon.setPassword(dbcon.getPassword());
-                                    realDbcon.setRememberPassword(
-                                            basePanel.rememberPassword());
-                                }
-                                
dbcon.setRememberPassword(basePanel.rememberPassword());
-
-                                if (dlg != null) {
-                                    dlg.close();
+                try {
+                    if (headless) {
+                        connectWithNewInfo(dbcon, input);
+                    } else {
+                        ActionListener actionListener = new ActionListener() {
+                            @Override
+                            public void actionPerformed(ActionEvent event) {
+                                if (event.getSource() == 
DialogDescriptor.OK_OPTION) {
+                                    connectWithNewInfo(dbcon, input);
                                 }
                             }
-                        }
-                    }
-                };
+                        };
                 
-                try {
-                    SwingUtilities.invokeAndWait(() -> {
-                        dlg = new ConnectionDialog(this, basePanel, 
basePanel.getTitle(), CONNECT_ACTION_HELPCTX, actionListener);
-                        dlg.setVisible(true);
-                    });
+                        SwingUtilities.invokeAndWait(() -> {
+                            ConnectPanel basePanel = input.getConnectPanel();
+                            dlg = new ConnectionDialog(this, basePanel, 
basePanel.getTitle(), CONNECT_ACTION_HELPCTX, actionListener);
+                            dlg.setVisible(true);
+                        });
+                    }
                 } catch (InterruptedException | InvocationTargetException ex) {
                     Exceptions.printStackTrace(ex);
                 }
+                dbcon.removeExceptionListener(excListener);
             } else { // without dialog with connection data (username, 
password), just with progress dlg
                 try {
                     DialogDescriptor descriptor = null;
@@ -265,7 +253,6 @@ public class ConnectAction extends AbstractAction 
implements ContextAwareAction,
                     
                     progress = 
ProgressHandle.createHandle(Bundle.Progress_ConnectingDB(dbcon.getDisplayName()));
                     
-                    final CountDownLatch connected = new CountDownLatch(1);
                     final Dialog dialog;
                     
                     if (headless) {
@@ -298,7 +285,6 @@ public class ConnectAction extends AbstractAction 
implements ContextAwareAction,
                         private void handlePropertyChange(PropertyChangeEvent 
event) {
                             if ("state".equals(event.getPropertyName())) {
                                 if (event.getNewValue() == State.connected) { 
//NOI18N
-                                    connected.countDown();
                                     if (dialog != null) {
                                         dialog.setVisible(false);
                                     }
@@ -312,7 +298,6 @@ public class ConnectAction extends AbstractAction 
implements ContextAwareAction,
                                     // to fully complete, and *then* raise the 
Connect
                                     // dialog.
                                     failed = true;
-                                    connected.countDown();
                                     if (dialog != null) {
                                         dialog.setVisible(false);
                                     }
@@ -324,34 +309,33 @@ public class ConnectAction extends AbstractAction 
implements ContextAwareAction,
                     failed = false;
 
                     dbcon.addPropertyChangeListener(connectionListener);
-                    dbcon.connectAsync();
+                    try {
+                        RequestProcessor.Task connectTask = 
dbcon.connectAsync();
 
-                    progress.start();
-                    progress.switchToIndeterminate();
-                    
-                    if (dialog == null) {
-                        connected.await();
-                    } else {
-                        dialog.setVisible(true);
-                    }
-                    progress.finish();                    
-                    
-                    if (dialog != null) {
-                        dialog.dispose();
-                    }
-                    
+                        progress.start();
+                        progress.switchToIndeterminate();
+
+                        if (dialog == null) {
+                            connectTask.waitFinished();
+                        } else {
+                            dialog.setVisible(true);
+                        }
+                        progress.finish();                    
 
-                    if ( failed ) {
-                        // If the connection fails with a progress bar only, 
then 
-                        // display the full Connect dialog so the user can 
give it
-                        // another shot after changing some values, like the 
username
-                        // or password.
-                        showDialog(dbcon, true);
+                        if (dialog != null) {
+                            dialog.dispose();
+                        }
+                    } finally {                        
+                        dbcon.removePropertyChangeListener(connectionListener);
                     }
                 } catch (Exception exc) {
                     String message = MessageFormat.format(NbBundle.getMessage 
(ConnectAction.class, "ERR_UnableToConnect"), exc.getMessage()); // NOI18N
                     DialogDisplayer.getDefault().notify(new 
NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE));
                     
+                    failed = true;
+                }
+                dbcon.removeExceptionListener(excListener);
+                if ( failed ) {
                     // If the connection fails with a progress bar only, then 
                     // display the full Connect dialog so the user can give it
                     // another shot after changing some values, like the 
username
@@ -359,8 +343,6 @@ public class ConnectAction extends AbstractAction 
implements ContextAwareAction,
                     showDialog(dbcon, true);
                 }
             }
-
-            dbcon.removeExceptionListener(excListener);
         }
 
         @Override
@@ -394,6 +376,146 @@ public class ConnectAction extends AbstractAction 
implements ContextAwareAction,
                 return false;
             }
         }
+
+        private void connectWithNewInfo(DatabaseConnection dbcon, Credentials 
input) {
+            dbcon.setUser(input.getUser());
+            dbcon.setPassword(input.getPassword());
+            dbcon.setUser(input.getUser());
+            dbcon.setPassword(input.getPassword());
+            dbcon.setRememberPassword(input.rememberPassword());
+
+            if (!dbcon.isVitalConnection()) {
+                dbcon.connectAsync();
+            } else {
+                DatabaseConnection realDbcon = 
ConnectionList.getDefault().getConnection(dbcon);
+                if (realDbcon != null) {
+                    realDbcon.setPassword(dbcon.getPassword());
+                    realDbcon.setRememberPassword(
+                            input.rememberPassword());
+                }
+                dbcon.setRememberPassword(input.rememberPassword());
+
+                if (dlg != null) {
+                    dlg.close();
+                }
+            }
+        }
+    }
+
+    private static abstract class Credentials {
+
+        abstract String getUser();
+
+        abstract String getPassword();
+
+        abstract boolean rememberPassword();
+
+        abstract ConnectPanel getConnectPanel();
+    }
+
+    private static class CredetialsLine extends Credentials {
+
+        final private DatabaseConnection dbconnection;
+        private String user;
+        private String password;
+        private boolean initialized;
+
+        private CredetialsLine(DatabaseConnection dbconn) {
+            dbconnection = dbconn;
+        }
+
+        private void init() {
+            if (!initialized) {
+                initialized = true;
+                setUsernameAndPassword();
+            }
+        }
+
+        @Override
+        String getUser() {
+            init();
+            return user;
+        }
+
+        @Override
+        String getPassword() {
+            init();
+            return password;
+        }
+
+        @Override
+        boolean rememberPassword() {
+            return dbconnection.rememberPassword();
+        }
+
+        @Override
+        ConnectPanel getConnectPanel() {
+            throw new IllegalStateException();
+        }
+
+        private boolean setUsernameAndPassword() {
+            CredentialsCallback callback = new 
CredentialsCallback(dbconnection.getUser());
+            NotifyDescriptor.ComposedInput userPassword = new 
NotifyDescriptor.ComposedInput(dbconnection.getDisplayName(), 2, callback);
+            if (DialogDescriptor.OK_OPTION == 
DialogDisplayer.getDefault().notify(userPassword)) {
+                NotifyDescriptor[] inputs = userPassword.getInputs();
+                user = ((NotifyDescriptor.InputLine)inputs[0]).getInputText();
+                password = 
((NotifyDescriptor.InputLine)inputs[1]).getInputText();
+                return true;
+            }
+            return false;
+        }
+    }
+
+    private static class CredetialsUI extends Credentials {
+
+        private final ConnectPanel connectPanel;
+
+        private CredetialsUI(ConnectPanel cp) {
+            connectPanel = cp;
+        }
+
+        @Override
+        String getPassword() {
+            return connectPanel.getPassword();
+        }
+
+        @Override
+        String getUser() {
+            return connectPanel.getUser();
+        }
+
+        @Override
+        boolean rememberPassword() {
+            return connectPanel.rememberPassword();
+        }
+
+        @Override
+        ConnectPanel getConnectPanel() {
+            return connectPanel;
+        }
     }
 
+    @NbBundle.Messages({
+        "MSG_EnterUsername=Enter Username",
+        "MSG_EnterPassword=Enter Password",})
+    private static class CredentialsCallback implements 
NotifyDescriptor.ComposedInput.Callback {
+
+        private final String initialUser;
+
+        public CredentialsCallback(String user) {
+            initialUser = user;
+        }
+
+        @Override
+        public NotifyDescriptor createInput(NotifyDescriptor.ComposedInput 
input, int number) {
+            if (number == 1) {
+                NotifyDescriptor.InputLine inputLine = new 
NotifyDescriptor.InputLine(Bundle.MSG_EnterUsername(), 
Bundle.MSG_EnterUsername());
+                inputLine.setInputText(initialUser);
+                return inputLine;
+            } else if (number == 2) {
+                return  new 
NotifyDescriptor.PasswordLine(Bundle.MSG_EnterPassword(),Bundle.MSG_EnterPassword());
+            }
+            return null;
+        }
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to