summaryrefslogtreecommitdiff
path: root/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudSourceApplicationConfigurable.java
blob: b6e0dfc857b0b3b74411cdf9cf5fc8e2a7576f54 (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
/*
 * 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.impl.module;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
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.remoteServer.util.*;
import com.intellij.util.concurrency.Semaphore;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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


public abstract class CloudSourceApplicationConfigurable<
  SC extends CloudConfigurationBase,
  DC extends CloudDeploymentNameConfiguration,
  SR extends CloudMultiSourceServerRuntimeInstance<DC, ?, ?, ?>,
  AC extends CloudApplicationConfiguration> extends CloudApplicationConfigurable {

  private final Project myProject;
  private final Disposable myParentDisposable;

  private DelayedRunner myRunner;

  private RemoteServer<?> myAccount;

  public CloudSourceApplicationConfigurable(@Nullable Project project, Disposable parentDisposable) {
    myProject = project;
    myParentDisposable = parentDisposable;
  }

  @Override
  public void setAccount(RemoteServer<?> account) {
    myAccount = account;
    clearCloudData();
  }

  protected RemoteServer<SC> getAccount() {
    return (RemoteServer<SC>)myAccount;
  }

  @Override
  public JComponent getComponent() {
    JComponent result = getMainPanel();
    if (myRunner == null) {
      myRunner = new DelayedRunner(result) {

        private RemoteServer<?> myPreviousAccount;

        @Override
        protected boolean wasChanged() {
          boolean result = myPreviousAccount != myAccount;
          if (result) {
            myPreviousAccount = myAccount;
          }
          return result;
        }

        @Override
        protected void run() {
          loadCloudData();
        }
      };
      Disposer.register(myParentDisposable, myRunner);
    }
    return result;
  }

  protected void clearCloudData() {
    getExistingComboBox().removeAllItems();
  }

  protected 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(myParentDisposable)) {
                      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());
    }
  }

  protected Project getProject() {
    return myProject;
  }

  protected abstract JComboBox getExistingComboBox();

  protected abstract JComponent getMainPanel();

  @Override
  public abstract AC createConfiguration();

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

    public ConnectionTask(String title) {
      super(myProject, title, CloudSourceApplicationConfigurable.this.getAccount());
    }
  }
}