You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by jh...@apache.org on 2023/01/26 14:55:41 UTC

[netbeans] branch master updated: Use NotifyDescriptor.ComposedInput for Add ADB command (#5353)

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

jhorvath 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 d018622771a Use NotifyDescriptor.ComposedInput for Add ADB command (#5353)
d018622771a is described below

commit d018622771aa7910274cb7260a87f6793061f431
Author: jhorvath <ja...@horvath.cz>
AuthorDate: Thu Jan 26 15:55:33 2023 +0100

    Use NotifyDescriptor.ComposedInput for Add ADB command (#5353)
    
    * Use NotifyDescriptor.ComposedInput for Add ADB command
    
    * Fixing steps count, repeat for empty username
---
 .../modules/cloud/oracle/actions/AddADBAction.java | 144 +++++++++++++--------
 .../cloud/oracle/actions/DownloadWalletAction.java |  81 ++++++------
 .../cloud/oracle/actions/DownloadWalletDialog.java |   2 +-
 3 files changed, 132 insertions(+), 95 deletions(-)

diff --git a/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/AddADBAction.java b/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/AddADBAction.java
index beb96cc5b72..6fd88b62c8f 100644
--- a/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/AddADBAction.java
+++ b/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/AddADBAction.java
@@ -21,14 +21,18 @@ package org.netbeans.modules.cloud.oracle.actions;
 import com.oracle.bmc.model.BmcException;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.io.IOException;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Optional;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.stream.Collectors;
 import org.netbeans.modules.cloud.oracle.OCIManager;
 import org.netbeans.modules.cloud.oracle.OCIProfile;
+import org.netbeans.modules.cloud.oracle.actions.DownloadWalletDialog.WalletInfo;
 import org.netbeans.modules.cloud.oracle.compartment.CompartmentItem;
 import org.netbeans.modules.cloud.oracle.compartment.CompartmentNode;
 import org.netbeans.modules.cloud.oracle.database.DatabaseItem;
@@ -38,11 +42,14 @@ import org.netbeans.modules.cloud.oracle.items.TenancyItem;
 import org.openide.DialogDescriptor;
 import org.openide.DialogDisplayer;
 import org.openide.NotifyDescriptor;
+import org.openide.NotifyDescriptor.ComposedInput.Callback;
+import org.openide.NotifyDescriptor.QuickPick;
 import org.openide.NotifyDescriptor.QuickPick.Item;
 import org.openide.awt.ActionID;
 import org.openide.awt.ActionReference;
 import org.openide.awt.ActionReferences;
 import org.openide.awt.ActionRegistration;
+import org.openide.util.Exceptions;
 import org.openide.util.NbBundle;
 
 /**
@@ -65,80 +72,109 @@ import org.openide.util.NbBundle;
     "AddADB=Add Oracle Autonomous DB",
     "SelectTenancy=Select Tenancy",
     "SelectCompartment=Select Compartment",
-    "SelectDatabase=Select Database"
+    "SelectDatabase=Select Compartment or Database",
+    "EnterUsername=Enter Username",
+    "EnterPassword=Enter Password"
 })
 public class AddADBAction implements ActionListener {
     private static final Logger LOGGER = Logger.getLogger(AddADBAction.class.getName());
     
+    private static final String DB = "db"; //NOI18N
+    private static final String USERNAME = "username"; //NOI18N
+    private static final String PASSWORD = "password"; //NOI18N
 
     @Override
     public void actionPerformed(ActionEvent e) {
-        List<TenancyItem> tenancies = new ArrayList<>();
-        for (OCIProfile p : OCIManager.getDefault().getConnectedProfiles()) {
-           p.getTenancy().ifPresent(tenancies::add);
-        }
-        Optional<TenancyItem> selectedTenancy = chooseOneItem(tenancies, Bundle.SelectTenancy());
-        
-        Optional<CompartmentItem> selectedCompartment = Optional.empty();
-                
-        if (!selectedTenancy.isPresent()) {
-            return;
-        }
-        
-        List<CompartmentItem> compartments = CompartmentNode.getCompartments().apply(selectedTenancy.get());
-        selectedCompartment = chooseOneItem(compartments, Bundle.SelectCompartment());
-        DatabaseItem selectedDatabase = null;
+        Map<String, Object> result = new HashMap<> ();
         
-        if (selectedCompartment.isPresent()) {
-            while(selectedDatabase == null) {
-                OCIItem item = chooseCopartmentOrDb(selectedCompartment.get());
-                if (item == null) {
-                    return;
-                }
-                if (item instanceof DatabaseItem) {
-                    selectedDatabase = (DatabaseItem) item;
-                }
-                if (item instanceof CompartmentItem) {
-                    selectedCompartment = Optional.of((CompartmentItem) item);
+        NotifyDescriptor.ComposedInput ci = new NotifyDescriptor.ComposedInput(Bundle.AddADB(), 3, new Callback() {
+            Map<Integer, List> values = new HashMap<> ();
+            
+            @Override
+            public NotifyDescriptor createInput(NotifyDescriptor.ComposedInput input, int number) {
+                if (number == 1) {
+                    List<TenancyItem> tenancies = new ArrayList<>();
+                    for (OCIProfile p : OCIManager.getDefault().getConnectedProfiles()) {
+                        p.getTenancy().ifPresent(tenancies::add);
+                    }
+                    String title;
+                    if (tenancies.size() == 1) {
+                        values.put(1, getCompartmentsAndDbs(tenancies.get(0)));
+                        title = Bundle.SelectCompartment();
+                    } else {
+                        values.put(1, tenancies);
+                        title = Bundle.SelectTenancy();
+                    }
+                    return createQuickPick(values.get(1), title);
+                } else {
+                    NotifyDescriptor prev = input.getInputs()[number - 2];
+                    OCIItem prevItem = null;
+                    if (prev instanceof NotifyDescriptor.QuickPick) {
+                        Optional<String> selected = ((QuickPick) prev).getItems().stream().filter(item -> item.isSelected()).map(item -> item.getLabel()).findFirst();
+                        if (selected.isPresent()) {
+                            Optional<? extends OCIItem> ti = values.get(number - 1).stream().filter(t -> ((OCIItem) t).getName().equals(selected.get())).findFirst();
+                            if (ti.isPresent()) {
+                                prevItem = ti.get();
+                            }
+                        }
+                        if (prevItem instanceof DatabaseItem) {
+                            result.put(DB, prevItem);
+                            return new NotifyDescriptor.InputLine(Bundle.EnterUsername(), Bundle.EnterUsername());
+                        }
+                        values.put(number, getCompartmentsAndDbs(prevItem));
+                        input.setEstimatedNumberOfInputs(input.getEstimatedNumberOfInputs() + 1);
+                        return createQuickPick(values.get(number), Bundle.SelectDatabase());
+                    } else if (prev instanceof NotifyDescriptor.PasswordLine) {
+                        result.put(PASSWORD, ((NotifyDescriptor.PasswordLine) prev).getInputText());
+                        return null;
+                    } else if (prev instanceof NotifyDescriptor.InputLine) {
+                        String username = ((NotifyDescriptor.InputLine) prev).getInputText();
+                        if (username == null || username.trim().isEmpty()) {
+                            return prev;
+                        }
+                        result.put(USERNAME, username);
+                        return new NotifyDescriptor.PasswordLine(Bundle.EnterPassword(), Bundle.EnterPassword());
+                    }
+                    return null;
                 }
             }
-        }
-        if (selectedDatabase != null) {
-            DownloadWalletAction action = new DownloadWalletAction(selectedDatabase);
-            action.actionPerformed(null);
+            
+        });
+        if (DialogDescriptor.OK_OPTION ==  DialogDisplayer.getDefault().notify(ci)) {
+            try {
+                DatabaseItem selectedDatabase = (DatabaseItem) result.get(DB);
+                DownloadWalletAction action = new DownloadWalletAction(selectedDatabase);
+                WalletInfo info = new WalletInfo(
+                        DownloadWalletDialog.getWalletsDir().getAbsolutePath(),
+                        AbstractPasswordPanel.generatePassword(),
+                        (String) result.get(USERNAME),
+                        ((String) result.get(PASSWORD)).toCharArray());
+                action.addConnection(info);
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
         }
     }
     
-    private <T extends OCIItem> Optional<T> chooseOneItem(List<T> ociItems, String title) {
-        Optional<T> result = Optional.empty();
-        if (ociItems.size() == 1) {
-            result = Optional.of(ociItems.get(0));
-        } else if (ociItems.size() > 0) {
-            List<Item> items = ociItems.stream()
-                    .map(tenancy -> new Item(tenancy.getName(), tenancy.getDescription()))
-                    .collect(Collectors.toList());
-            NotifyDescriptor.QuickPick qp = new NotifyDescriptor.QuickPick(title, title, items, false);
-            if (DialogDescriptor.OK_OPTION == DialogDisplayer.getDefault().notify(qp)) {
-                Optional<String> selected = qp.getItems().stream().filter(item -> item.isSelected()).map(item -> item.getLabel()).findFirst();
-                if (selected.isPresent()) {
-                    result = ociItems.stream().filter(t -> t.getName().equals(selected.get())).findFirst();
-                }
-                
-            }
-        } 
-        return result;
+    private <T extends OCIItem> NotifyDescriptor.QuickPick createQuickPick(List<T> ociItems, String title) {
+        
+        List<Item> items = ociItems.stream()
+                .map(tenancy -> new Item(tenancy.getName(), tenancy.getDescription()))
+                .collect(Collectors.toList());
+        return new NotifyDescriptor.QuickPick(title, title, items, false);
     }
     
-    
-    private OCIItem chooseCopartmentOrDb(CompartmentItem compartment) {
+    private List<OCIItem> getCompartmentsAndDbs(OCIItem parent) {
         List<OCIItem> items = new ArrayList<> ();
         try {
-            items.addAll(DatabaseNode.getDatabases().apply(compartment));
+            if (parent instanceof CompartmentItem) {
+                items.addAll(DatabaseNode.getDatabases().apply((CompartmentItem) parent));
+            }
         } catch (BmcException e) {
             LOGGER.log(Level.SEVERE, "Unable to load compartment list", e); // NOI18N
         }
-        items.addAll(CompartmentNode.getCompartments().apply(compartment));
-        return chooseOneItem(items, Bundle.SelectDatabase()).orElseGet(() -> null);
+        items.addAll(CompartmentNode.getCompartments().apply(parent));
+        return items;
     }
     
 }
diff --git a/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/DownloadWalletAction.java b/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/DownloadWalletAction.java
index 2e625f5f8c7..81a909b86ea 100644
--- a/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/DownloadWalletAction.java
+++ b/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/DownloadWalletAction.java
@@ -19,7 +19,6 @@
 package org.netbeans.modules.cloud.oracle.actions;
 
 import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -58,8 +57,8 @@ import org.openide.util.NbBundle;
         category = "Tools",
         id = "org.netbeans.modules.cloud.oracle.actions.DownloadWalletAction"
 )
-@ActionRegistration( 
-        displayName = "#CTL_DownloadWalletAction", 
+@ActionRegistration(
+        displayName = "#CTL_DownloadWalletAction",
         asynchronous = true,
         lazy = true
 )
@@ -75,7 +74,7 @@ import org.openide.util.NbBundle;
     "MSG_WalletNoConnection=Wallet doesn't contain any connection"
 })
 public class DownloadWalletAction extends AbstractAction implements ContextAwareAction {
-    
+
     private static final String URL_TEMPLATE = "jdbc:oracle:thin:@{0}?TNS_ADMIN=\"{1}\""; //NOI18N
     private final DatabaseItem context;
     private OCIProfile session;
@@ -99,47 +98,49 @@ public class DownloadWalletAction extends AbstractAction implements ContextAware
     @Override
     public void actionPerformed(ActionEvent ev) {
         Optional<DownloadWalletDialog.WalletInfo> result = DownloadWalletDialog.showDialog(context);
-        result.ifPresent((p) -> {
-            try {
-                Path walletPath = session.downloadWallet(context, new String(p.getWalletPassword()), p.getPath());
-                if (p.getDbUser() != null && p.getDbPassword() != null) {
-                    
-                    JDBCDriver[] drivers = JDBCDriverManager.getDefault().getDrivers("oracle.jdbc.OracleDriver"); //NOI18N
-                    if (drivers.length > 0) {
-                        String connectionName = context.getConnectionName();
-                        if (connectionName == null) {
-                            Optional<String> n = parseConnectionNames(walletPath).stream().findFirst();
-                            if (n.isPresent()) {
-                                connectionName = n.get();
-                            } else {
-                                StatusDisplayer.getDefault().setStatusText(Bundle.MSG_WalletNoConnection());
-                                return;
-                            }
+        result.ifPresent(p ->  addConnection(p));
+    }
+
+    void addConnection(DownloadWalletDialog.WalletInfo p) {
+        try {
+            Path walletPath = session.downloadWallet(context, new String(p.getWalletPassword()), p.getPath());
+            if (p.getDbUser() != null && p.getDbPassword() != null) {
+
+                JDBCDriver[] drivers = JDBCDriverManager.getDefault().getDrivers("oracle.jdbc.OracleDriver"); //NOI18N
+                if (drivers.length > 0) {
+                    String connectionName = context.getConnectionName();
+                    if (connectionName == null) {
+                        Optional<String> n = parseConnectionNames(walletPath).stream().findFirst();
+                        if (n.isPresent()) {
+                            connectionName = n.get();
+                        } else {
+                            StatusDisplayer.getDefault().setStatusText(Bundle.MSG_WalletNoConnection());
+                            return;
                         }
-                        String dbUrl = MessageFormat.format(URL_TEMPLATE, connectionName, walletPath);
-                        DatabaseConnection dbConn = DatabaseConnection.create(
-                                drivers[0], 
-                                dbUrl, 
-                                p.getDbUser(), 
-                                p.getDbUser(), 
-                                new String(p.getDbPassword()), 
-                                true, 
-                                context.getName());
-                        ConnectionManager.getDefault().addConnection(dbConn);
                     }
-                    DialogDisplayer.getDefault().notifyLater(
-                            new NotifyDescriptor.Message(
-                                    Bundle.MSG_WalletDownloadedPassword(
-                                            new String(p.getWalletPassword()))));
-                } else {
-                    StatusDisplayer.getDefault().setStatusText(Bundle.MSG_WalletDownloaded(walletPath.toString()));
+                    String dbUrl = MessageFormat.format(URL_TEMPLATE, connectionName, walletPath);
+                    DatabaseConnection dbConn = DatabaseConnection.create(
+                            drivers[0],
+                            dbUrl,
+                            p.getDbUser(),
+                            p.getDbUser(),
+                            new String(p.getDbPassword()),
+                            true,
+                            context.getName());
+                    ConnectionManager.getDefault().addConnection(dbConn);
                 }
-            } catch (DatabaseException | IOException ex) {
-                Exceptions.printStackTrace(ex);
+                DialogDisplayer.getDefault().notifyLater(
+                        new NotifyDescriptor.Message(
+                                Bundle.MSG_WalletDownloadedPassword(
+                                        new String(p.getWalletPassword()))));
+            } else {
+                StatusDisplayer.getDefault().setStatusText(Bundle.MSG_WalletDownloaded(walletPath.toString()));
             }
-        });
+        } catch (DatabaseException | IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
     }
-    
+
     protected List<String> parseConnectionNames(Path wallet) {
         Path tns = wallet.resolve("tnsnames.ora"); //NOI18N
         try {
diff --git a/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/DownloadWalletDialog.java b/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/DownloadWalletDialog.java
index 3764e305c6a..6683788ec77 100644
--- a/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/DownloadWalletDialog.java
+++ b/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/DownloadWalletDialog.java
@@ -243,7 +243,7 @@ final class DownloadWalletDialog extends AbstractPasswordPanel {
         checkPasswordLogic(passwd1, passwd2, (m) -> errorMessage(m));
     }
     
-    private static File getWalletsDir() throws IOException {
+    static File getWalletsDir() throws IOException {
         FileObject fo = FileUtil.createFolder(FileUtil.getConfigRoot(), WALLETS_PATH);
         return FileUtil.toFile(fo);
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org

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