diff options
Diffstat (limited to 'java/remote-servers/impl')
9 files changed, 695 insertions, 0 deletions
diff --git a/java/remote-servers/impl/remote-servers-java-impl.iml b/java/remote-servers/impl/remote-servers-java-impl.iml index b0228d7f4075..6999e2deac71 100644 --- a/java/remote-servers/impl/remote-servers-java-impl.iml +++ b/java/remote-servers/impl/remote-servers-java-impl.iml @@ -13,6 +13,8 @@ <orderEntry type="module" module-name="execution-openapi" /> <orderEntry type="module" module-name="lang-impl" /> <orderEntry type="module" module-name="remote-servers-impl" /> + <orderEntry type="module" module-name="java-impl" /> + <orderEntry type="module" module-name="openapi" /> </component> </module> diff --git a/java/remote-servers/impl/src/META-INF/RemoteServersJava.xml b/java/remote-servers/impl/src/META-INF/RemoteServersJava.xml index cec87d9643f2..24a0776ccd29 100644 --- a/java/remote-servers/impl/src/META-INF/RemoteServersJava.xml +++ b/java/remote-servers/impl/src/META-INF/RemoteServersJava.xml @@ -1,7 +1,14 @@ <idea-plugin> + + <extensionPoints> + <extensionPoint qualifiedName="com.intellij.remoteServer.moduleBuilderContribution" + interface="com.intellij.remoteServer.impl.module.CloudModuleBuilderContribution"/> + </extensionPoints> + <extensions defaultExtensionNs="com.intellij"> <applicationService serviceInterface="com.intellij.remoteServer.configuration.deployment.JavaDeploymentSourceUtil" serviceImplementation="com.intellij.remoteServer.impl.configuration.deployment.JavaDeploymentSourceUtilImpl"/> <remoteServer.deploymentSource.type implementation="com.intellij.remoteServer.impl.configuration.deployment.ArtifactDeploymentSourceType"/> + <moduleBuilder builderClass="com.intellij.remoteServer.impl.module.CloudModuleBuilder"/> </extensions> </idea-plugin>
\ No newline at end of file diff --git a/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudApplicationConfigurable.java b/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudApplicationConfigurable.java new file mode 100644 index 000000000000..14bd043a7f23 --- /dev/null +++ b/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudApplicationConfigurable.java @@ -0,0 +1,166 @@ +/* + * 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.options.ConfigurationException; +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 CloudApplicationConfigurable< + SC extends CloudConfigurationBase, + DC extends CloudDeploymentNameConfiguration, + SR extends CloudMultiSourceServerRuntimeInstance<DC, ?, ?, ?>, + AC extends CloudApplicationConfiguration> { + + private final Project myProject; + private final Disposable myParentDisposable; + + private DelayedRunner myRunner; + + private RemoteServer<?> myAccount; + + public CloudApplicationConfigurable(@Nullable Project project, Disposable parentDisposable) { + myProject = project; + myParentDisposable = parentDisposable; + } + + public void setAccount(RemoteServer<?> account) { + myAccount = account; + clearCloudData(); + } + + protected RemoteServer<SC> getAccount() { + return (RemoteServer<SC>)myAccount; + } + + 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(); + + public abstract AC createConfiguration(); + + public abstract void validate() throws ConfigurationException; + + protected abstract class ConnectionTask<T> extends CloudConnectionTask<T, SC, DC, SR> { + + public ConnectionTask(String title) { + super(myProject, title, CloudApplicationConfigurable.this.getAccount()); + } + } +} + diff --git a/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudApplicationConfiguration.java b/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudApplicationConfiguration.java new file mode 100644 index 000000000000..9af0f1ad472b --- /dev/null +++ b/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudApplicationConfiguration.java @@ -0,0 +1,36 @@ +/* + * 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; + + +public abstract class CloudApplicationConfiguration { + + private boolean myExisting; + private final String myExistingAppName; + + protected CloudApplicationConfiguration(boolean existing, String existingAppName) { + myExisting = existing; + myExistingAppName = existingAppName; + } + + public boolean isExisting() { + return myExisting; + } + + public String getExistingAppName() { + return myExistingAppName; + } +} diff --git a/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudModuleBuilder.java b/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudModuleBuilder.java new file mode 100644 index 000000000000..4aa8d60bcea4 --- /dev/null +++ b/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudModuleBuilder.java @@ -0,0 +1,114 @@ +/* + * 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.icons.AllIcons; +import com.intellij.ide.util.projectWizard.JavaModuleBuilder; +import com.intellij.ide.util.projectWizard.ModuleBuilderListener; +import com.intellij.ide.util.projectWizard.ModuleWizardStep; +import com.intellij.ide.util.projectWizard.WizardContext; +import com.intellij.openapi.Disposable; +import com.intellij.openapi.module.JavaModuleType; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.roots.ui.configuration.ModulesProvider; +import com.intellij.remoteServer.configuration.RemoteServer; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; + + +public class CloudModuleBuilder extends JavaModuleBuilder { + + private RemoteServer<?> myAccount; + private CloudApplicationConfiguration myApplicationConfiguration; + + public CloudModuleBuilder() { + addListener(new ModuleBuilderListener() { + + @Override + public void moduleCreated(@NotNull Module module) { + configureModule(module); + } + }); + } + + public String getBuilderId() { + return getClass().getName(); + } + + @Override + public Icon getBigIcon() { + return AllIcons.General.Balloon; + } + + @Override + public Icon getNodeIcon() { + return AllIcons.General.Balloon; + } + + @Override + public String getDescription() { + return "Java module of PAAS cloud application"; + } + + @Override + public String getPresentableName() { + return "Clouds"; + } + + @Override + public String getGroupName() { + return "Clouds"; + } + + @Override + public String getParentGroup() { + return JavaModuleType.JAVA_GROUP; + } + + @Override + public int getWeight() { + return 30; + } + + @Override + public ModuleWizardStep[] createWizardSteps(@NotNull WizardContext wizardContext, @NotNull ModulesProvider modulesProvider) { + return ModuleWizardStep.EMPTY_ARRAY; + } + + @Nullable + @Override + public ModuleWizardStep getCustomOptionsStep(WizardContext context, Disposable parentDisposable) { + return new CloudModuleWizardStep(this, context.getProject(), parentDisposable); + } + + public void setAccount(RemoteServer<?> account) { + myAccount = account; + } + + public RemoteServer<?> getAccount() { + return myAccount; + } + + public void setApplicationConfiguration(CloudApplicationConfiguration applicationConfiguration) { + myApplicationConfiguration = applicationConfiguration; + } + + private void configureModule(final Module module) { + CloudModuleBuilderContribution.getInstanceByType(myAccount.getType()).configureModule(module, myAccount, myApplicationConfiguration); + } +} diff --git a/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudModuleBuilderContribution.java b/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudModuleBuilderContribution.java new file mode 100644 index 000000000000..6643c89cc1c4 --- /dev/null +++ b/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudModuleBuilderContribution.java @@ -0,0 +1,48 @@ +/* + * 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.extensions.ExtensionPointName; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.project.Project; +import com.intellij.remoteServer.ServerType; +import com.intellij.remoteServer.configuration.RemoteServer; +import org.jetbrains.annotations.Nullable; + + +public abstract class CloudModuleBuilderContribution { + + public static final ExtensionPointName<CloudModuleBuilderContribution> EP_NAME + = ExtensionPointName.create("com.intellij.remoteServer.moduleBuilderContribution"); + + public abstract ServerType<?> getCloudType(); + + public abstract CloudApplicationConfigurable createApplicationConfigurable(@Nullable Project project, Disposable parentDisposable); + + public abstract void configureModule(Module module, + RemoteServer<?> account, + CloudApplicationConfiguration configuration); + + public static CloudModuleBuilderContribution getInstanceByType(ServerType<?> cloudType) { + for (CloudModuleBuilderContribution contribution : EP_NAME.getExtensions()) { + if (contribution.getCloudType() == cloudType) { + return contribution; + } + } + return null; + } +} diff --git a/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudModuleBuilderContributionBase.java b/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudModuleBuilderContributionBase.java new file mode 100644 index 000000000000..6a927d97bb93 --- /dev/null +++ b/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudModuleBuilderContributionBase.java @@ -0,0 +1,142 @@ +/* + * 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.execution.RunManagerEx; +import com.intellij.execution.RunnerAndConfigurationSettings; +import com.intellij.execution.configurations.ConfigurationType; +import com.intellij.openapi.Disposable; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.module.ModulePointer; +import com.intellij.openapi.module.ModulePointerManager; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.MessageType; +import com.intellij.remoteServer.ServerType; +import com.intellij.remoteServer.configuration.RemoteServer; +import com.intellij.remoteServer.impl.configuration.deployment.DeployToServerConfigurationType; +import com.intellij.remoteServer.impl.configuration.deployment.DeployToServerRunConfiguration; +import com.intellij.remoteServer.impl.configuration.deployment.ModuleDeploymentSourceImpl; +import com.intellij.remoteServer.util.*; +import com.intellij.remoteServer.util.ssh.SshKeyChecker; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + + +public abstract class CloudModuleBuilderContributionBase< + SC extends CloudConfigurationBase, + DC extends CloudDeploymentNameConfiguration, + AC extends CloudApplicationConfiguration, + SR extends CloudMultiSourceServerRuntimeInstance<DC, ?, ?, ?>> + extends CloudModuleBuilderContribution { + + @Override + public void configureModule(Module module, + RemoteServer<?> account, + CloudApplicationConfiguration applicationConfiguration) { + RemoteServer<SC> castedAccount = (RemoteServer<SC>)account; + final AC castedApplicationConfiguration = (AC)applicationConfiguration; + + DC deploymentConfiguration = createDeploymentConfiguration(); + + if (applicationConfiguration.isExisting()) { + deploymentConfiguration.setDefaultDeploymentName(false); + deploymentConfiguration.setDeploymentName(applicationConfiguration.getExistingAppName()); + } + + final DeployToServerRunConfiguration<SC, DC> runConfiguration = createRunConfiguration(module, castedAccount, deploymentConfiguration); + + final String cloudName = account.getType().getPresentableName(); + final Project project = module.getProject(); + new CloudConnectionTask<Object, SC, DC, SR>(project, CloudBundle.getText("cloud.support", cloudName), castedAccount) { + + CloudNotifier myNotifier = new CloudNotifier(cloudName); + + boolean myFirstAttempt = true; + + @Override + protected Object run(SR serverRuntime) throws ServerRuntimeException { + doConfigureModule(castedApplicationConfiguration, runConfiguration, myFirstAttempt, serverRuntime); + myNotifier.showMessage(CloudBundle.getText("cloud.support.added", cloudName), MessageType.INFO); + return null; + } + + @Override + protected void runtimeErrorOccurred(@NotNull String errorMessage) { + myFirstAttempt = false; + new SshKeyChecker().checkServerError(errorMessage, myNotifier, project, this); + } + }.performAsync(); + } + + private DeployToServerRunConfiguration<SC, DC> createRunConfiguration(Module module, + RemoteServer<SC> server, + DC deploymentConfiguration) { + Project project = module.getProject(); + + String serverName = server.getName(); + + String name = generateRunConfigurationName(serverName, module.getName()); + + final RunManagerEx runManager = RunManagerEx.getInstanceEx(project); + final RunnerAndConfigurationSettings runSettings + = runManager.createRunConfiguration(name, getRunConfigurationType().getConfigurationFactories()[0]); + + final DeployToServerRunConfiguration<SC, DC> result = (DeployToServerRunConfiguration<SC, DC>)runSettings.getConfiguration(); + + result.setServerName(serverName); + + final ModulePointer modulePointer = ModulePointerManager.getInstance(project).create(module); + result.setDeploymentSource(new ModuleDeploymentSourceImpl(modulePointer)); + + result.setDeploymentConfiguration(deploymentConfiguration); + + runManager.addConfiguration(runSettings, false); + runManager.setSelectedConfiguration(runSettings); + + return result; + } + + private static String generateRunConfigurationName(String serverName, String moduleName) { + return CloudBundle.getText("run.configuration.name", serverName, moduleName); + } + + private DeployToServerConfigurationType getRunConfigurationType() { + String id = DeployToServerConfigurationType.getId(getCloudType()); + for (ConfigurationType configurationType : ConfigurationType.CONFIGURATION_TYPE_EP.getExtensions()) { + if (configurationType instanceof DeployToServerConfigurationType) { + DeployToServerConfigurationType deployConfigurationType = (DeployToServerConfigurationType)configurationType; + if (deployConfigurationType.getId().equals(id)) { + return deployConfigurationType; + } + } + } + return null; + } + + @Override + public abstract ServerType<SC> getCloudType(); + + @Override + public abstract CloudApplicationConfigurable<SC, DC, SR, AC> createApplicationConfigurable(@Nullable Project project, + Disposable parentDisposable); + + protected abstract DC createDeploymentConfiguration(); + + protected abstract void doConfigureModule(AC applicationConfiguration, + DeployToServerRunConfiguration<SC, DC> runConfiguration, + boolean firstAttempt, + SR serverRuntime) throws ServerRuntimeException; +} diff --git a/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudModuleWizardStep.form b/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudModuleWizardStep.form new file mode 100644 index 000000000000..19cbaa7f2a5f --- /dev/null +++ b/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudModuleWizardStep.form @@ -0,0 +1,50 @@ +<?xml version="1.0" encoding="UTF-8"?> +<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.remoteServer.impl.module.CloudModuleWizardStep"> + <grid id="27dc6" binding="myMainPanel" layout-manager="GridLayoutManager" row-count="5" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> + <margin top="0" left="0" bottom="0" right="0"/> + <constraints> + <xy x="20" y="20" width="415" height="400"/> + </constraints> + <properties/> + <border type="none"/> + <children> + <grid id="b29a2" binding="myAccountPanelPlaceHolder" layout-manager="BorderLayout" hgap="0" vgap="0"> + <constraints> + <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> + </constraints> + <properties/> + <border type="none"/> + <children/> + </grid> + <vspacer id="276c3"> + <constraints> + <grid row="4" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/> + </constraints> + </vspacer> + <component id="dd5e7" class="com.intellij.ui.TitledSeparator"> + <constraints> + <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> + </constraints> + <properties> + <text value="Account"/> + </properties> + </component> + <component id="61d9c" class="com.intellij.ui.TitledSeparator"> + <constraints> + <grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> + </constraints> + <properties> + <text value="Application"/> + </properties> + </component> + <grid id="582a2" binding="myApplicationPanelPlaceHolder" layout-manager="CardLayout" hgap="0" vgap="0"> + <constraints> + <grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> + </constraints> + <properties/> + <border type="none"/> + <children/> + </grid> + </children> + </grid> +</form> diff --git a/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudModuleWizardStep.java b/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudModuleWizardStep.java new file mode 100644 index 000000000000..cf0e283616b2 --- /dev/null +++ b/java/remote-servers/impl/src/com/intellij/remoteServer/impl/module/CloudModuleWizardStep.java @@ -0,0 +1,130 @@ +/* + * 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.ide.util.projectWizard.ModuleWizardStep; +import com.intellij.openapi.Disposable; +import com.intellij.openapi.options.ConfigurationException; +import com.intellij.openapi.project.Project; +import com.intellij.remoteServer.ServerType; +import com.intellij.remoteServer.configuration.RemoteServer; +import com.intellij.remoteServer.util.CloudAccountSelectionEditor; +import com.intellij.util.containers.hash.HashMap; + +import javax.swing.*; +import java.awt.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + + +public class CloudModuleWizardStep extends ModuleWizardStep { + + private JPanel myMainPanel; + private JPanel myAccountPanelPlaceHolder; + private JPanel myApplicationPanelPlaceHolder; + + private final CloudModuleBuilder myModuleBuilder; + private final Project myProject; + private final Disposable myParentDisposable; + + private CloudAccountSelectionEditor myAccountSelectionPanel; + + private Map<ServerType<?>, CloudApplicationConfigurable> myCloudType2ApplicationConfigurable; + + public CloudModuleWizardStep(CloudModuleBuilder moduleBuilder, Project project, Disposable parentDisposable) { + myModuleBuilder = moduleBuilder; + myProject = project; + myParentDisposable = parentDisposable; + + myCloudType2ApplicationConfigurable = new HashMap<ServerType<?>, CloudApplicationConfigurable>(); + + List<ServerType<?>> cloudTypes = new ArrayList<ServerType<?>>(); + for (CloudModuleBuilderContribution contribution : CloudModuleBuilderContribution.EP_NAME.getExtensions()) { + cloudTypes.add(contribution.getCloudType()); + } + + myAccountSelectionPanel = new CloudAccountSelectionEditor(cloudTypes); + myAccountPanelPlaceHolder.add(myAccountSelectionPanel.getMainPanel()); + + myAccountSelectionPanel.setAccountSelectionListener(new Runnable() { + + @Override + public void run() { + onAccountSelectionChanged(); + } + }); + onAccountSelectionChanged(); + } + + private RemoteServer<?> getSelectedAccount() { + return myAccountSelectionPanel.getSelectedAccount(); + } + + private void onAccountSelectionChanged() { + CardLayout applicationPlaceHolderLayout = (CardLayout)myApplicationPanelPlaceHolder.getLayout(); + + RemoteServer<?> account = getSelectedAccount(); + boolean haveAccount = account != null; + myApplicationPanelPlaceHolder.setVisible(haveAccount); + if (!haveAccount) { + return; + } + + ServerType<?> cloudType = account.getType(); + String cardName = cloudType.getId(); + CloudApplicationConfigurable<?, ?, ?, ?> applicationConfigurable = getApplicationConfigurable(); + if (applicationConfigurable == null) { + applicationConfigurable + = CloudModuleBuilderContribution.getInstanceByType(cloudType).createApplicationConfigurable(myProject, myParentDisposable); + myCloudType2ApplicationConfigurable.put(cloudType, applicationConfigurable); + myApplicationPanelPlaceHolder.add(applicationConfigurable.getComponent(), cardName); + } + applicationPlaceHolderLayout.show(myApplicationPanelPlaceHolder, cardName); + + applicationConfigurable.setAccount(account); + } + + @Override + public JComponent getComponent() { + return myMainPanel; + } + + private CloudApplicationConfigurable<?, ?, ?, ?> getApplicationConfigurable() { + RemoteServer<?> account = getSelectedAccount(); + if (account == null) { + return null; + } + return myCloudType2ApplicationConfigurable.get(account.getType()); + } + + @Override + public void updateDataModel() { + myModuleBuilder.setAccount(myAccountSelectionPanel.getSelectedAccount()); + CloudApplicationConfigurable<?, ?, ?, ?> configurable = getApplicationConfigurable(); + myModuleBuilder.setApplicationConfiguration(configurable == null ? null : configurable.createConfiguration()); + } + + @Override + public boolean validate() throws ConfigurationException { + myAccountSelectionPanel.validate(); + CloudApplicationConfigurable<?, ?, ?, ?> configurable = getApplicationConfigurable(); + if (configurable != null) { + configurable.validate(); + } + return super.validate(); + } +} |