77de5769820dd4b47e57dafca71fca333d88b4e1
[idea/community.git] / platform / remote-servers / impl / src / com / intellij / remoteServer / impl / configuration / deployment / DeployToServerSettingsEditor.java
1 /*
2  * Copyright 2000-2013 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.remoteServer.impl.configuration.deployment;
17
18 import com.intellij.openapi.options.ConfigurationException;
19 import com.intellij.openapi.options.SettingsEditor;
20 import com.intellij.openapi.options.ShowSettingsUtil;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.ui.ComboBox;
23 import com.intellij.openapi.util.Comparing;
24 import com.intellij.openapi.util.Disposer;
25 import com.intellij.remoteServer.ServerType;
26 import com.intellij.remoteServer.configuration.RemoteServer;
27 import com.intellij.remoteServer.configuration.RemoteServersManager;
28 import com.intellij.remoteServer.configuration.ServerConfiguration;
29 import com.intellij.remoteServer.configuration.deployment.DeploymentConfiguration;
30 import com.intellij.remoteServer.configuration.deployment.DeploymentConfigurator;
31 import com.intellij.remoteServer.configuration.deployment.DeploymentSource;
32 import com.intellij.remoteServer.configuration.deployment.DeploymentSourceType;
33 import com.intellij.remoteServer.impl.configuration.RemoteServerListConfigurable;
34 import com.intellij.ui.*;
35 import com.intellij.util.ui.FormBuilder;
36 import com.intellij.util.ui.UIUtil;
37 import org.jetbrains.annotations.NotNull;
38 import org.jetbrains.annotations.Nullable;
39
40 import javax.swing.*;
41 import java.awt.*;
42 import java.awt.event.ActionEvent;
43 import java.awt.event.ActionListener;
44 import java.util.Comparator;
45
46 /**
47  * @author nik
48  */
49 public class DeployToServerSettingsEditor<S extends ServerConfiguration, D extends DeploymentConfiguration> extends SettingsEditor<DeployToServerRunConfiguration<S, D>> {
50   private final ServerType<S> myServerType;
51   private final DeploymentConfigurator<D, S> myDeploymentConfigurator;
52   private final Project myProject;
53   private final ComboboxWithBrowseButton myServerComboBox;
54   private final ComboBox mySourceComboBox;
55   private final SortedComboBoxModel<String> myServerListModel;
56   private final SortedComboBoxModel<DeploymentSource> mySourceListModel;
57   private final JPanel myDeploymentSettingsComponent;
58   private SettingsEditor<D> myDeploymentSettingsEditor;
59   private DeploymentSource myLastSelectedSource;
60   private RemoteServer<S> myLastSelectedServer;
61
62   public DeployToServerSettingsEditor(final ServerType<S> type, DeploymentConfigurator<D, S> deploymentConfigurator, Project project) {
63     myServerType = type;
64     myDeploymentConfigurator = deploymentConfigurator;
65     myProject = project;
66
67     myServerListModel = new SortedComboBoxModel<>(String.CASE_INSENSITIVE_ORDER);
68     myServerComboBox = new ComboboxWithBrowseButton(new ComboBox(myServerListModel));
69     fillApplicationServersList(null);
70     myServerComboBox.addActionListener(new ActionListener() {
71       @Override
72       public void actionPerformed(ActionEvent e) {
73         RemoteServerListConfigurable configurable = RemoteServerListConfigurable.createConfigurable(type);
74         if (ShowSettingsUtil.getInstance().editConfigurable(myServerComboBox, configurable)) {
75           fillApplicationServersList(configurable.getLastSelectedServer());
76         }
77       }
78     });
79     myServerComboBox.getComboBox().addActionListener(new ActionListener() {
80       @Override
81       public void actionPerformed(ActionEvent e) {
82         updateDeploymentSettingsEditor();
83       }
84     });
85     myServerComboBox.getComboBox().setRenderer(new ColoredListCellRendererWrapper<String>() {
86       @Override
87       protected void doCustomize(JList list, String value, int index, boolean selected, boolean hasFocus) {
88         if (value == null) return;
89         RemoteServer<S> server = RemoteServersManager.getInstance().findByName(value, type);
90         SimpleTextAttributes attributes = server == null ? SimpleTextAttributes.ERROR_ATTRIBUTES : SimpleTextAttributes.REGULAR_ATTRIBUTES;
91         setIcon(server != null ? server.getType().getIcon() : null);
92         append(value, attributes);
93       }
94     });
95
96     mySourceListModel = new SortedComboBoxModel<>(
97       (o1, o2) -> o1.getPresentableName().compareToIgnoreCase(o2.getPresentableName()));
98     mySourceListModel.addAll(deploymentConfigurator.getAvailableDeploymentSources());
99     mySourceComboBox = new ComboBox(mySourceListModel);
100     mySourceComboBox.setRenderer(new ListCellRendererWrapper<DeploymentSource>() {
101       @Override
102       public void customize(JList list, DeploymentSource value, int index, boolean selected, boolean hasFocus) {
103         if (value == null) return;
104         setIcon(value.getIcon());
105         setText(value.getPresentableName());
106       }
107     });
108
109     myDeploymentSettingsComponent = new JPanel(new BorderLayout());
110     mySourceComboBox.addActionListener(new ActionListener() {
111       @Override
112       public void actionPerformed(ActionEvent e) {
113         updateDeploymentSettingsEditor();
114       }
115     });
116   }
117
118   private void fillApplicationServersList(@Nullable RemoteServer<?> newSelection) {
119     String oldSelection = myServerListModel.getSelectedItem();
120     myServerListModel.clear();
121     for (RemoteServer<?> server : RemoteServersManager.getInstance().getServers(myServerType)) {
122       myServerListModel.add(server.getName());
123     }
124     myServerComboBox.getComboBox().setSelectedItem(newSelection != null ? newSelection.getName() : oldSelection);
125   }
126
127   private void updateDeploymentSettingsEditor() {
128     String serverName = myServerListModel.getSelectedItem();
129     RemoteServer<S> selectedServer = serverName != null ? RemoteServersManager.getInstance().findByName(serverName, myServerType) : null;
130     DeploymentSource selectedSource = mySourceListModel.getSelectedItem();
131     if (Comparing.equal(selectedSource, myLastSelectedSource) && Comparing.equal(selectedServer, myLastSelectedServer)) {
132       return;
133     }
134
135     if (!Comparing.equal(selectedSource, myLastSelectedSource)) {
136       updateBeforeRunOptions(myLastSelectedSource, false);
137       updateBeforeRunOptions(selectedSource, true);
138     }
139     if (selectedSource != null && selectedServer != null) {
140       myDeploymentSettingsComponent.removeAll();
141       myDeploymentSettingsEditor = myDeploymentConfigurator.createEditor(selectedSource, selectedServer);
142       if (myDeploymentSettingsEditor != null) {
143         Disposer.register(this, myDeploymentSettingsEditor);
144         myDeploymentSettingsComponent.add(BorderLayout.CENTER, myDeploymentSettingsEditor.getComponent());
145       }
146     }
147     myLastSelectedSource = selectedSource;
148     myLastSelectedServer = selectedServer;
149   }
150
151   private void updateBeforeRunOptions(@Nullable DeploymentSource source, boolean selected) {
152     if (source != null) {
153       DeploymentSourceType type = source.getType();
154       type.updateBuildBeforeRunOption(myServerComboBox, myProject, source, selected);
155     }
156   }
157
158   @Override
159   protected void resetEditorFrom(DeployToServerRunConfiguration<S,D> configuration) {
160     String serverName = configuration.getServerName();
161     if (serverName != null && !myServerListModel.getItems().contains(serverName)) {
162       myServerListModel.add(serverName);
163     }
164     myServerComboBox.getComboBox().setSelectedItem(serverName);
165     mySourceComboBox.setSelectedItem(configuration.getDeploymentSource());
166     D deploymentConfiguration = configuration.getDeploymentConfiguration();
167     updateDeploymentSettingsEditor();
168     if (deploymentConfiguration != null && myDeploymentSettingsEditor != null) {
169       myDeploymentSettingsEditor.resetFrom(deploymentConfiguration);
170     }
171   }
172
173   @Override
174   protected void applyEditorTo(DeployToServerRunConfiguration<S,D> configuration) throws ConfigurationException {
175     updateDeploymentSettingsEditor();
176
177     configuration.setServerName(myServerListModel.getSelectedItem());
178     DeploymentSource deploymentSource = mySourceListModel.getSelectedItem();
179     configuration.setDeploymentSource(deploymentSource);
180
181     if (deploymentSource != null) {
182       D deployment = configuration.getDeploymentConfiguration();
183       if (deployment == null) {
184         deployment = myDeploymentConfigurator.createDefaultConfiguration(deploymentSource);
185         configuration.setDeploymentConfiguration(deployment);
186       }
187       if (myDeploymentSettingsEditor != null) {
188         myDeploymentSettingsEditor.applyTo(deployment);
189       }
190     }
191     else {
192       configuration.setDeploymentConfiguration(null);
193     }
194   }
195
196   @NotNull
197   @Override
198   protected JComponent createEditor() {
199     return FormBuilder.createFormBuilder()
200       .addLabeledComponent("Server:", myServerComboBox)
201       .addLabeledComponent("Deployment:", mySourceComboBox)
202       .addComponentFillVertically(myDeploymentSettingsComponent, UIUtil.DEFAULT_VGAP)
203       .getPanel();
204   }
205 }