summaryrefslogtreecommitdiff
path: root/platform/remote-servers/impl/src/com/intellij/remoteServer/util/CloudAccountSelectionEditor.java
blob: 5a3d150daf5788031548216218e17c203c32ddc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed 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 com.intellij.remoteServer.util;

import com.intellij.ide.DataManager;
import com.intellij.ide.actions.ShowSettingsUtilImpl;
import com.intellij.ide.util.projectWizard.WizardContext;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.ex.SingleConfigurableEditor;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Ref;
import com.intellij.remoteServer.ServerType;
import com.intellij.remoteServer.configuration.RemoteServer;
import com.intellij.remoteServer.configuration.RemoteServersManager;
import com.intellij.remoteServer.impl.configuration.RemoteServerConfigurable;
import com.intellij.util.Consumer;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.hash.HashMap;
import com.intellij.util.text.UniqueNameGenerator;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Map;


public class CloudAccountSelectionEditor {

  private static final Map<ServerType<?>, Key<RemoteServer<?>>> ourCloudType2AccountKey
    = new HashMap<ServerType<?>, Key<RemoteServer<?>>>();


  private JButton myNewButton;
  private ComboBox myAccountComboBox;
  private JPanel myMainPanel;

  private final List<ServerType<?>> myCloudTypes;

  private Runnable myServerSelectionListener;

  public CloudAccountSelectionEditor(List<ServerType<?>> cloudTypes) {
    myCloudTypes = cloudTypes;

    for (ServerType<?> cloudType : cloudTypes) {
      for (RemoteServer<?> account : RemoteServersManager.getInstance().getServers(cloudType)) {
        myAccountComboBox.addItem(new AccountItem(account));
      }
    }

    myNewButton.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        onNewButton();
      }
    });

    myAccountComboBox.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        if (myServerSelectionListener != null) {
          myServerSelectionListener.run();
        }
      }
    });
  }

  public void setAccountSelectionListener(Runnable listener) {
    myServerSelectionListener = listener;
  }

  private void onNewButton() {
    if (myCloudTypes.size() == 1) {
      createAccount(ContainerUtil.getFirstItem(myCloudTypes));
      return;
    }

    DefaultActionGroup group = new DefaultActionGroup();
    for (final ServerType<?> cloudType : myCloudTypes) {
      group.add(new AnAction(cloudType.getPresentableName(), cloudType.getPresentableName(), cloudType.getIcon()) {

        @Override
        public void actionPerformed(AnActionEvent e) {
          createAccount(cloudType);
        }
      });
    }
    JBPopupFactory.getInstance().createActionGroupPopup("New Account", group, DataManager.getInstance().getDataContext(myMainPanel),
                                                        JBPopupFactory.ActionSelectionAid.SPEEDSEARCH, false)
      .showUnderneathOf(myNewButton);
  }

  private void createAccount(ServerType<?> cloudType) {
    RemoteServer<?> newAccount = RemoteServersManager.getInstance().createServer(cloudType, generateServerName(cloudType));

    final Ref<Consumer<String>> errorConsumerRef = new Ref<Consumer<String>>();

    RemoteServerConfigurable configurable = new RemoteServerConfigurable(newAccount, null, true) {

      @Override
      protected void setConnectionStatusText(boolean error, String text) {
        super.setConnectionStatusText(error, error ? "" : text);
        errorConsumerRef.get().consume(error ? text : null);
      }
    };

    final SingleConfigurableEditor configurableEditor
      = new SingleConfigurableEditor(myMainPanel, configurable, ShowSettingsUtilImpl.createDimensionKey(configurable), false) {

      {
        errorConsumerRef.set(new Consumer<String>() {

          @Override
          public void consume(String s) {
            setErrorText(s);
          }
        });
      }
    };

    if (!configurableEditor.showAndGet()) {
      return;
    }

    newAccount.setName(configurable.getDisplayName());

    RemoteServersManager.getInstance().addServer(newAccount);
    AccountItem newAccountItem = new AccountItem(newAccount);
    myAccountComboBox.addItem(newAccountItem);
    myAccountComboBox.setSelectedItem(newAccountItem);
  }

  public JComponent getMainPanel() {
    return myMainPanel;
  }

  @Nullable
  public RemoteServer<?> getSelectedAccount() {
    AccountItem selectedItem = (AccountItem)myAccountComboBox.getSelectedItem();
    return selectedItem == null ? null : selectedItem.getAccount();
  }

  private static String generateServerName(ServerType<?> cloudType) {
    return UniqueNameGenerator.generateUniqueName(cloudType.getPresentableName(), new Condition<String>() {

      @Override
      public boolean value(String s) {
        for (RemoteServer<?> server : RemoteServersManager.getInstance().getServers()) {
          if (server.getName().equals(s)) {
            return false;
          }
        }
        return true;
      }
    });
  }

  public void validate() throws ConfigurationException {
    if (getSelectedAccount() == null) {
      throw new ConfigurationException("Account required");
    }
  }

  public void setAccountOnContext(WizardContext context) {
    RemoteServer<?> account = getSelectedAccount();
    if (account == null) {
      return;
    }
    context.putUserData(getKey(account.getType()), account);
  }

  public static void unsetAccountOnContext(WizardContext context, ServerType<?> cloudType) {
    context.putUserData(getKey(cloudType), null);
  }

  private static Key<RemoteServer<?>> getKey(ServerType<?> cloudType) {
    Key<RemoteServer<?>> result = ourCloudType2AccountKey.get(cloudType);
    if (result == null) {
      result = new Key<RemoteServer<?>>("cloud-account-" + cloudType.getId());
      ourCloudType2AccountKey.put(cloudType, result);
    }
    return result;
  }

  public static void createRunConfiguration(WizardContext context,
                                            ServerType<?> cloudType,
                                            Module module,
                                            CloudDeploymentNameConfiguration configuration) {
    RemoteServer<?> account = context.getUserData(getKey(cloudType));
    if (account == null) {
      return;
    }
    CloudRunConfigurationUtil.createRunConfiguration(account, module, configuration);
  }

  private static class AccountItem {

    private final RemoteServer<?> myAccount;

    public AccountItem(RemoteServer<?> account) {
      myAccount = account;
    }

    public RemoteServer<?> getAccount() {
      return myAccount;
    }

    @Override
    public String toString() {
      return myAccount.getName();
    }
  }
}