summaryrefslogtreecommitdiff
path: root/platform/remote-servers/impl/src/com/intellij/remoteServer/util/CloudSupportConfigurableBase.java
blob: 421e75da7eae40d3bb0fee6905c10479974c2c21 (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
package com.intellij.remoteServer.util;

import com.intellij.ProjectTopics;
import com.intellij.ide.util.frameworkSupport.FrameworkSupportConfigurable;
import com.intellij.ide.util.frameworkSupport.FrameworkSupportModel;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.ModuleAdapter;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupManager;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.util.Disposer;
import com.intellij.remoteServer.ServerType;
import com.intellij.remoteServer.configuration.RemoteServer;
import com.intellij.remoteServer.runtime.Deployment;
import com.intellij.remoteServer.runtime.ServerConnection;
import com.intellij.remoteServer.runtime.ServerConnector;
import com.intellij.remoteServer.runtime.deployment.ServerRuntimeInstance;
import com.intellij.util.concurrency.Semaphore;
import com.intellij.util.messages.MessageBusConnection;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author michael.golubev
 */
public abstract class CloudSupportConfigurableBase<
  SC extends CloudConfigurationBase,
  DC extends CloudDeploymentNameConfiguration,
  SR extends CloudMultiSourceServerRuntimeInstance<DC, ?, ?, ?>,
  ST extends ServerType<SC>>
  extends FrameworkSupportConfigurable implements CloudDataLoader {

  private final ST myCloudType;
  private final Project myModelProject;

  private final CloudNotifier myNotifier;

  private boolean myInitialized = false;

  private CloudAccountSelectionEditor<SC, DC, ST> myAccountSelectionEditor;

  public CloudSupportConfigurableBase(ST cloudType, FrameworkSupportModel frameworkSupportModel) {
    myCloudType = cloudType;
    myModelProject = frameworkSupportModel.getProject();
    myNotifier = new CloudNotifier(cloudType.getPresentableName());
  }

  @Override
  public void dispose() {
    Disposer.dispose(getAccountSelectionEditor());
  }

  protected void initUI() {
    getAccountSelectionEditor().initUI();
  }

  @Override
  public void clearCloudData() {
    getExistingComboBox().removeAllItems();
  }

  @Override
  public void loadCloudData() {
    new ConnectionTask<Collection<Deployment>>("Loading existing applications list") {

      @Override
      protected void run(final ServerConnection<DC> connection,
                         final Semaphore semaphore,
                         final AtomicReference<Collection<Deployment>> result) {
        connection.connectIfNeeded(new ServerConnector.ConnectionCallback<DC>() {

          @Override
          public void connected(@NotNull ServerRuntimeInstance<DC> serverRuntimeInstance) {
            connection.computeDeployments(new Runnable() {

              @Override
              public void run() {
                result.set(connection.getDeployments());
                semaphore.up();
                UIUtil.invokeLaterIfNeeded(new Runnable() {
                  @Override
                  public void run() {
                    if (!Disposer.isDisposed(CloudSupportConfigurableBase.this)) {
                      setupExistingApplications(result.get());
                    }
                  }
                });
              }
            });
          }

          @Override
          public void errorOccurred(@NotNull String errorMessage) {
            runtimeErrorOccurred(errorMessage);
            semaphore.up();
          }
        });
      }

      @Override
      protected Collection<Deployment> run(SR serverRuntimeInstance) throws ServerRuntimeException {
        return null;
      }
    }.performAsync();
  }

  private void setupExistingApplications(Collection<Deployment> deployments) {
    JComboBox existingComboBox = getExistingComboBox();
    existingComboBox.removeAllItems();
    for (Deployment deployment : deployments) {
      existingComboBox.addItem(deployment.getName());
    }
  }

  @Override
  public void onFrameworkSelectionChanged(boolean selected) {
    if (selected && !myInitialized) {
      myInitialized = true;
      initUI();
      updateApplicationUI();
    }
  }

  protected void showMessage(String message, MessageType messageType) {
    getNotifier().showMessage(message, messageType);
  }

  protected Project getProject() {
    return myModelProject;
  }

  protected RemoteServer<SC> getServer() {
    return getAccountSelectionEditor().getServer();
  }

  protected void runOnModuleAdded(final Module module, final Runnable runnable) {
    if (myModelProject == null) {
      StartupManager.getInstance(module.getProject()).runWhenProjectIsInitialized(runnable);
    }
    else {
      MessageBusConnection connection = myModelProject.getMessageBus().connect(myModelProject);
      connection.subscribe(ProjectTopics.MODULES, new ModuleAdapter() {

        public void moduleAdded(Project project, Module addedModule) {
          if (addedModule == module) {
            runnable.run();
          }
        }
      });
    }
  }

  protected CloudAccountSelectionEditor<SC, DC, ST> getAccountSelectionEditor() {
    if (myAccountSelectionEditor == null) {
      myAccountSelectionEditor = new CloudAccountSelectionEditor<SC, DC, ST>(myCloudType) {

        @Override
        protected void handleError(ConfigurationException e) {
          getNotifier().showMessage(e.getMessage(), MessageType.ERROR);
        }
      };
      myAccountSelectionEditor.setDataLoader(this);
    }
    return myAccountSelectionEditor;
  }

  protected CloudNotifier getNotifier() {
    return myNotifier;
  }

  protected abstract JComboBox getExistingComboBox();

  protected abstract void updateApplicationUI();

  protected abstract class ConnectionTask<T> extends CloudConnectionTask<T, SC, DC, SR> {

    public ConnectionTask(String title) {
      super(myModelProject, title, CloudSupportConfigurableBase.this.getServer());
    }

    public ConnectionTask(Module module, String title) {
      super(module.getProject(), title, CloudSupportConfigurableBase.this.getServer());
    }
  }
}