1 /*
2  * NewConnectionSnippetDialog.java
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 
16 package org.rstudio.studio.client.workbench.views.connections.ui;
17 
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 
21 import com.google.gwt.aria.client.Roles;
22 import org.rstudio.core.client.widget.FormLabel;
23 import org.rstudio.core.client.widget.LayoutGrid;
24 import org.rstudio.core.client.widget.ModalDialog;
25 import org.rstudio.core.client.widget.OperationWithInput;
26 import org.rstudio.studio.client.RStudioGinjector;
27 import org.rstudio.studio.client.common.HelpLink;
28 import org.rstudio.studio.client.workbench.views.connections.model.NewConnectionInfo;
29 
30 import com.google.gwt.core.client.GWT;
31 import com.google.gwt.event.dom.client.ChangeEvent;
32 import com.google.gwt.event.dom.client.ChangeHandler;
33 import com.google.gwt.resources.client.ClientBundle;
34 import com.google.gwt.resources.client.CssResource;
35 import com.google.gwt.user.client.ui.HasVerticalAlignment;
36 import com.google.gwt.user.client.ui.SimplePanel;
37 import com.google.gwt.user.client.ui.TextBox;
38 import com.google.gwt.user.client.ui.Widget;
39 import com.google.inject.Inject;
40 
41 public class NewConnectionSnippetDialog extends ModalDialog<HashMap<String, String>>
42 {
43    public interface NewConnectionSnippetDialogStyle extends CssResource
44    {
45    }
46 
47    @Inject
initialize()48    private void initialize()
49    {
50    }
51 
NewConnectionSnippetDialog( OperationWithInput<HashMap<String, String>> operation, ArrayList<NewConnectionSnippetParts> config, NewConnectionInfo newConnectionInfo)52    public NewConnectionSnippetDialog(
53       OperationWithInput<HashMap<String, String>> operation,
54       ArrayList<NewConnectionSnippetParts> config,
55       NewConnectionInfo newConnectionInfo)
56    {
57       super("Advanced Options", Roles.getDialogRole(), operation);
58       initialConfig_ = config;
59       newConnectionInfo_ = newConnectionInfo;
60 
61       RStudioGinjector.INSTANCE.injectMembers(this);
62    }
63 
64    @Override
onDialogShown()65    protected void onDialogShown()
66    {
67       super.onDialogShown();
68 
69       setOkButtonCaption("Configure");
70 
71       if (newConnectionInfo_.getHelp() != null) {
72          HelpLink helpLink = new HelpLink(
73             "Using " + newConnectionInfo_.getName(),
74             newConnectionInfo_.getHelp(),
75             false,
76             false);
77 
78          addLeftWidget(helpLink);
79       }
80    }
81 
82    @Override
createMainWidget()83    protected Widget createMainWidget()
84    {
85       SimplePanel wrapper = new SimplePanel();
86       wrapper.setStyleName(RES.styles().wrapper());
87 
88       LayoutGrid connGrid = new LayoutGrid(initialConfig_.size(), 2);
89       connGrid.addStyleName(RES.styles().grid());
90 
91       for (int idxParams = 0; idxParams < initialConfig_.size(); idxParams++) {
92          final String key = initialConfig_.get(idxParams).getKey();
93          FormLabel label = new FormLabel(key + ":");
94          label.addStyleName(RES.styles().label());
95          connGrid.setWidget(idxParams, 0, label);
96          connGrid.getRowFormatter().setVerticalAlign(idxParams, HasVerticalAlignment.ALIGN_TOP);
97 
98          final TextBox textbox = new TextBox();
99          label.setFor(textbox);
100          textbox.setText(initialConfig_.get(idxParams).getValue());
101          textbox.addStyleName(RES.styles().textbox());
102          connGrid.setWidget(idxParams, 1, textbox);
103 
104          textbox.addChangeHandler(new ChangeHandler()
105          {
106             @Override
107             public void onChange(ChangeEvent arg0)
108             {
109                partsKeyValues_.put(key, textbox.getValue());
110             }
111          });
112       }
113 
114       wrapper.add(connGrid);
115 
116       return wrapper;
117    }
118 
119    @Override
collectInput()120    protected HashMap<String, String> collectInput()
121    {
122       return partsKeyValues_;
123    }
124 
125    public interface Styles extends CssResource
126    {
grid()127       String grid();
label()128       String label();
textbox()129       String textbox();
wrapper()130       String wrapper();
131    }
132 
133    public interface Resources extends ClientBundle
134    {
135       @Source("NewConnectionSnippetDialog.css")
styles()136       Styles styles();
137    }
138 
139    public static Resources RES = GWT.create(Resources.class);
ensureStylesInjected()140    public static void ensureStylesInjected()
141    {
142       RES.styles().ensureInjected();
143    }
144 
145    private NewConnectionInfo newConnectionInfo_;
146    private ArrayList<NewConnectionSnippetParts> initialConfig_;
147    HashMap<String, String> partsKeyValues_ = new HashMap<>();
148 }
149