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

sdedic 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 9dc2a2e  Adding command for New Database Connection (#3358)
9dc2a2e is described below

commit 9dc2a2ec33ffceb5b5c9efab026d07a272bb1cc9
Author: jhorvath <[email protected]>
AuthorDate: Tue Dec 21 10:05:21 2021 +0100

    Adding command for New Database Connection (#3358)
    
    Adding command for New Database Connection
---
 .../nbcode/integration/nbproject/project.xml       |   2 +-
 .../nbcode/nbproject/platform.properties           |   2 +-
 java/java.lsp.server/nbproject/project.properties  |   2 +-
 .../java/lsp/server/db/DBAddConnection.java        | 183 +++++++++++++++++++++
 .../lsp/server/protocol/ShowInputBoxParams.java    |  30 ++++
 .../lsp/server/protocol/WorkspaceServiceImpl.java  |   3 +-
 java/java.lsp.server/vscode/package.json           |   5 +
 java/java.lsp.server/vscode/src/extension.ts       |   2 +-
 java/java.lsp.server/vscode/src/nbcode.ts          |   4 +-
 java/java.lsp.server/vscode/src/protocol.ts        |   4 +
 10 files changed, 231 insertions(+), 6 deletions(-)

diff --git a/java/java.lsp.server/nbcode/integration/nbproject/project.xml 
b/java/java.lsp.server/nbcode/integration/nbproject/project.xml
index 929957e..b94c2ed 100644
--- a/java/java.lsp.server/nbcode/integration/nbproject/project.xml
+++ b/java/java.lsp.server/nbcode/integration/nbproject/project.xml
@@ -81,7 +81,7 @@
                     <build-prerequisite/>
                     <compile-dependency/>
                     <run-dependency>
-                        <specification-version>1.13</specification-version>
+                        <specification-version>1.16</specification-version>
                     </run-dependency>
                 </dependency>
                 <dependency>
diff --git a/java/java.lsp.server/nbcode/nbproject/platform.properties 
b/java/java.lsp.server/nbcode/nbproject/platform.properties
index b079f8f..8107325 100644
--- a/java/java.lsp.server/nbcode/nbproject/platform.properties
+++ b/java/java.lsp.server/nbcode/nbproject/platform.properties
@@ -116,9 +116,9 @@ disabled.modules=\
     org.netbeans.modules.css.model,\
     org.netbeans.modules.css.prep,\
     org.netbeans.modules.css.visual,\
+    org.netbeans.modules.db.kit,\
     org.netbeans.modules.db.core,\
     org.netbeans.modules.db.dataview,\
-    org.netbeans.modules.db.kit,\
     org.netbeans.modules.db.mysql,\
     org.netbeans.modules.db.sql.editor,\
     org.netbeans.modules.db.sql.visualeditor,\
diff --git a/java/java.lsp.server/nbproject/project.properties 
b/java/java.lsp.server/nbproject/project.properties
index fe4a797..f633cd8 100644
--- a/java/java.lsp.server/nbproject/project.properties
+++ b/java/java.lsp.server/nbproject/project.properties
@@ -17,7 +17,7 @@
 
 javac.source=1.8
 javac.compilerargs=-Xlint -Xlint:-serial
-spec.version.base=1.15.0
+spec.version.base=1.16.0
 javadoc.arch=${basedir}/arch.xml
 requires.nb.javac=true
 lsp.build.dir=vscode/nbcode
diff --git 
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/db/DBAddConnection.java
 
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/db/DBAddConnection.java
new file mode 100644
index 0000000..a2dc1ff
--- /dev/null
+++ 
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/db/DBAddConnection.java
@@ -0,0 +1,183 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.java.lsp.server.db;
+
+import java.net.URL;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.stream.Collectors;
+import org.eclipse.lsp4j.CodeAction;
+import org.eclipse.lsp4j.CodeActionParams;
+import org.eclipse.lsp4j.MessageParams;
+import org.eclipse.lsp4j.MessageType;
+import org.netbeans.api.db.explorer.ConnectionManager;
+import org.netbeans.api.db.explorer.DatabaseConnection;
+import org.netbeans.api.db.explorer.DatabaseException;
+import org.netbeans.api.db.explorer.JDBCDriver;
+import org.netbeans.api.db.explorer.JDBCDriverManager;
+import org.netbeans.modules.java.lsp.server.protocol.CodeActionsProvider;
+import org.netbeans.modules.java.lsp.server.protocol.NbCodeLanguageClient;
+import org.netbeans.modules.java.lsp.server.protocol.QuickPickItem;
+import org.netbeans.modules.java.lsp.server.protocol.ShowInputBoxParams;
+import org.netbeans.modules.java.lsp.server.protocol.ShowQuickPickParams;
+import org.netbeans.modules.parsing.api.ResultIterator;
+import org.openide.awt.StatusDisplayer;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.URLMapper;
+import org.openide.util.NbBundle;
+import org.openide.util.lookup.ServiceProvider;
+
+/**
+ *
+ * @author Jan Horvath
+ */
[email protected]({
+    "MSG_EnterDbUrl=Enter DB URL",
+    "MSG_EnterUsername=Enter Username",
+    "MSG_EnterPassword=Enter Password",
+    "MSG_SelectDriver=Select db driver",
+    "MSG_DriverNotFound=Driver not found",
+    "MSG_ConnectionAdded=Connection added",
+    "MSG_ConnectionFailed=Connection failed",
+    "MSG_SelectSchema=Select Database Schema"
+})
+@ServiceProvider(service = CodeActionsProvider.class)
+public class DBAddConnection extends CodeActionsProvider {
+    public static final String DB_ADD_CONNECTION =  "db.add.connection"; // 
NOI18N
+
+    @Override
+    public CompletableFuture<Object> processCommand(NbCodeLanguageClient 
client, String command, List<Object> arguments) {
+        if (!DB_ADD_CONNECTION.equals(command)) {
+            return null;
+        }
+        JDBCDriver[] drivers = JDBCDriverManager.getDefault().getDrivers();
+        List<QuickPickItem> items = new ArrayList<>();
+        for (int i = 0; i < drivers.length; i++) {
+            URL[] jars = drivers[i].getURLs();
+            if (jars != null && jars.length > 0) {
+                FileObject jarFO = URLMapper.findFileObject(jars[0]);
+                if (jarFO != null && jarFO.isValid()) {
+                    items.add(
+                            new QuickPickItem(drivers[i].getName(), null, 
drivers[i].getDisplayName() + " (" + drivers[i].getClassName() + ")", false, i) 
// NOI18N
+                    );
+                }
+            }
+        }
+
+        return client.showQuickPick(new 
ShowQuickPickParams(Bundle.MSG_SelectDriver(), false, items))
+                .thenApply(selectedItems -> {
+                    if (selectedItems == null) {
+                        return null;
+                    }
+                    if (!selectedItems.isEmpty()) {
+                        int i = ((Double) 
selectedItems.get(0).getUserData()).intValue();
+                        JDBCDriver driver = drivers[i];
+                        client.showInputBox(new ShowInputBoxParams(
+                                Bundle.MSG_EnterDbUrl(), "")).thenAccept((u) 
-> {
+                            if (u == null) {
+                                return;
+                            }
+                            client.showInputBox(new ShowInputBoxParams(
+                                    Bundle.MSG_EnterUsername(), 
"")).thenAccept((username) -> { //NOI18N
+                                if (username == null) {
+                                    return;
+                                }
+                                client.showInputBox(new ShowInputBoxParams(
+                                        Bundle.MSG_EnterPassword(), "", 
true)).thenAccept((password) -> { //NOI18N
+                                    if (password == null) {
+                                        return;
+                                    }
+                                    DatabaseConnection dbconn = 
DatabaseConnection.create(driver, u, username, null, password, true);
+                                    try {
+                                        
ConnectionManager.getDefault().addConnection(dbconn);
+                                        List<String> schemas = 
getSchemas(dbconn);
+                                        if (schemas.isEmpty()) {
+                                            client.showMessage(new 
MessageParams(MessageType.Info, Bundle.MSG_ConnectionAdded()));
+                                        } else {
+                                            List<QuickPickItem> schemaItems = 
schemas.stream().map(schema -> new 
QuickPickItem(schema)).collect(Collectors.toList());
+                                            client.showQuickPick(new 
ShowQuickPickParams(Bundle.MSG_SelectSchema(), schemaItems)).thenAccept((s) -> {
+                                                if (s == null) {
+                                                    try {
+                                                        // Command was 
interrupted, we don't want the connection to be added
+                                                        
ConnectionManager.getDefault().removeConnection(dbconn);
+                                                    } catch (DatabaseException 
ex) {
+                                                        
StatusDisplayer.getDefault().setStatusText(ex.getMessage());
+                                                    }
+                                                    return;
+                                                }
+                                                String schema = 
s.get(0).getLabel();
+                                                DatabaseConnection dbconn1 = 
DatabaseConnection.create(driver, u, username, schema, password, true);
+                                                try {
+                                                    
ConnectionManager.getDefault().removeConnection(dbconn);
+                                                    
ConnectionManager.getDefault().addConnection(dbconn1);
+                                                } catch (DatabaseException ex) 
{
+                                                    client.showMessage(new 
MessageParams(MessageType.Error, ex.getMessage()));
+                                                    return;
+                                                }
+                                                client.showMessage(new 
MessageParams(MessageType.Info, Bundle.MSG_ConnectionAdded()));
+                                            });
+                                        }
+                                    } catch (SQLException | DatabaseException 
ex) {
+                                        client.showMessage(new 
MessageParams(MessageType.Error, ex.getMessage()));
+                                    } 
+                                });
+                            });
+
+                        });
+
+                    } else {
+                        client.showMessage(new 
MessageParams(MessageType.Error, Bundle.MSG_DriverNotFound()));
+                    }
+                    return null;
+                });
+    }
+
+    private static List<String> getSchemas(DatabaseConnection dbconn) throws 
SQLException, DatabaseException {
+        List<String> schemas = new ArrayList<>();
+        if (ConnectionManager.getDefault().connect(dbconn)) {
+            DatabaseMetaData dbMetaData = 
dbconn.getJDBCConnection().getMetaData();
+            if (dbMetaData.supportsSchemasInTableDefinitions()) {
+                ResultSet rs = dbMetaData.getSchemas();
+                if (rs != null) {
+                    while (rs.next()) {
+                        schemas.add(rs.getString(1).trim());
+                    }
+                }
+            }
+        }
+        return schemas;
+    }
+
+    @Override
+    public Set<String> getCommands() {
+        return Collections.singleton(DB_ADD_CONNECTION);
+    }
+
+    @Override
+    public List<CodeAction> getCodeActions(ResultIterator resultIterator, 
CodeActionParams params) throws Exception {
+        return Collections.emptyList();
+    }
+    
+}
diff --git 
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/ShowInputBoxParams.java
 
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/ShowInputBoxParams.java
index 68ad7a0..0253a8b 100644
--- 
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/ShowInputBoxParams.java
+++ 
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/ShowInputBoxParams.java
@@ -42,14 +42,24 @@ public class ShowInputBoxParams {
      */
     @NonNull
     private String value;
+    
+    /**
+     * Controls if a password input is shown. Password input hides the typed 
text.
+     */
+    private boolean password = false;
 
     public ShowInputBoxParams() {
         this("", "");
     }
 
     public ShowInputBoxParams(@NonNull final String prompt, @NonNull final 
String value) {
+        this(prompt, value, false);
+    }
+    
+    public ShowInputBoxParams(@NonNull final String prompt, @NonNull final 
String value, final boolean password) {
         this.prompt = Preconditions.checkNotNull(prompt, "prompt");
         this.value = Preconditions.checkNotNull(value, "value");
+        this.password = password;
     }
 
     /**
@@ -83,6 +93,26 @@ public class ShowInputBoxParams {
     public void setValue(@NonNull final String value) {
         this.value = Preconditions.checkNotNull(value, "value");
     }
+    
+    /**
+     * Controls if a password input is shown. Password input hides the typed 
text.
+     * 
+     * @since 1.16
+     */
+    @Pure
+    @NonNull
+    public boolean isPassword() {
+        return password;
+    }
+
+    /**
+     * Controls if a password input is shown. Password input hides the typed 
text.
+     * 
+     * @since 1.16
+     */
+    public void setPassword(boolean password) {
+        this.password = password;
+    }
 
     @Override
     @Pure
diff --git 
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/WorkspaceServiceImpl.java
 
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/WorkspaceServiceImpl.java
index d1a4b1b..f30099a 100644
--- 
a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/WorkspaceServiceImpl.java
+++ 
b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/WorkspaceServiceImpl.java
@@ -98,6 +98,7 @@ import 
org.netbeans.modules.gsf.testrunner.ui.api.TestMethodController;
 import org.netbeans.modules.gsf.testrunner.ui.api.TestMethodFinder;
 import org.netbeans.modules.java.lsp.server.LspServerState;
 import org.netbeans.modules.java.lsp.server.Utils;
+import org.netbeans.modules.java.lsp.server.db.DBAddConnection;
 import 
org.netbeans.modules.java.lsp.server.debugging.attach.AttachConfigurations;
 import 
org.netbeans.modules.java.lsp.server.debugging.attach.AttachNativeConfigurations;
 import org.netbeans.modules.java.source.ElementHandleAccessor;
@@ -516,7 +517,7 @@ public final class WorkspaceServiceImpl implements 
WorkspaceService, LanguageCli
         }
         throw new UnsupportedOperationException("Command not supported: " + 
params.getCommand());
     }
-
+    
     private final AtomicReference<BiConsumer<FileObject, 
Collection<TestMethodController.TestMethod>>> testMethodsListener = new 
AtomicReference<>();
     private final AtomicReference<PropertyChangeListener> openProjectsListener 
= new AtomicReference<>();
 
diff --git a/java/java.lsp.server/vscode/package.json 
b/java/java.lsp.server/vscode/package.json
index 0e3e3b8..5561d99 100644
--- a/java/java.lsp.server/vscode/package.json
+++ b/java/java.lsp.server/vscode/package.json
@@ -405,6 +405,11 @@
                                "command": "foundProjects.deleteEntry",
                                "title": "Delete"
                        },
+                        {
+                                "command": "db.add.connection",
+                                "title": "Add Database Connection",
+                               "category": "Database"
+                        },
                        {
                                "command": 
"nbls:Database:netbeans.db.explorer.action.Connect",
                                "title": "Connect to Database"
diff --git a/java/java.lsp.server/vscode/src/extension.ts 
b/java/java.lsp.server/vscode/src/extension.ts
index c119e41..8cd061e 100644
--- a/java/java.lsp.server/vscode/src/extension.ts
+++ b/java/java.lsp.server/vscode/src/extension.ts
@@ -757,7 +757,7 @@ function doActivateWithJDK(specifiedJDK: string | null, 
context: ExtensionContex
                 return selected ? Array.isArray(selected) ? selected : 
[selected] : undefined;
             });
             c.onRequest(InputBoxRequest.type, async param => {
-                return await window.showInputBox({ prompt: param.prompt, 
value: param.value });
+                return await window.showInputBox({ prompt: param.prompt, 
value: param.value, password: param.password });
             });
             c.onNotification(TestProgressNotification.type, param => {
                 if (testAdapter) {
diff --git a/java/java.lsp.server/vscode/src/nbcode.ts 
b/java/java.lsp.server/vscode/src/nbcode.ts
index 3db5022..8c27e65 100644
--- a/java/java.lsp.server/vscode/src/nbcode.ts
+++ b/java/java.lsp.server/vscode/src/nbcode.ts
@@ -97,7 +97,9 @@ if (typeof process === 'object' && process.argv0 === 'node') {
     let clusters = fs.readdirSync(nbcode).filter(c => c !== 'bin' && c !== 
'etc').map(c => path.join(nbcode, c));
     let args = process.argv.slice(2);
     let json = JSON.parse("" + fs.readFileSync(path.join(extension, 
'package.json')));
-    let globalStorage = path.join(os.homedir(), '.config', 'Code', 'User', 
'globalStorage', json.publisher + '.' + json.name);
+    let globalStorage = os.platform() === 'darwin' ? 
+        path.join(os.homedir(), 'Library', 'Application Support', 'Code', 
'User', 'globalStorage', json.publisher + '.' + json.name) :
+        path.join(os.homedir(), '.config', 'Code', 'User', 'globalStorage', 
json.publisher + '.' + json.name);
     let info = {
         clusters : clusters,
         extensionPath: extension,
diff --git a/java/java.lsp.server/vscode/src/protocol.ts 
b/java/java.lsp.server/vscode/src/protocol.ts
index e7865d9..a2ab4ed 100644
--- a/java/java.lsp.server/vscode/src/protocol.ts
+++ b/java/java.lsp.server/vscode/src/protocol.ts
@@ -79,6 +79,10 @@ export interface ShowInputBoxParams {
      * The value to prefill in the input box.
      */
     value: string;
+    /**
+     * Controls if a password input is shown. Password input hides the typed 
text.
+     */
+    password?: boolean;
 }
 
 export namespace InputBoxRequest {

---------------------------------------------------------------------
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