2 * Copyright 2000-2013 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.intellij.remoteServer.impl.configuration.deployment;
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;
42 import java.awt.event.ActionEvent;
43 import java.awt.event.ActionListener;
44 import java.util.Comparator;
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;
62 public DeployToServerSettingsEditor(final ServerType<S> type, DeploymentConfigurator<D, S> deploymentConfigurator, Project project) {
64 myDeploymentConfigurator = deploymentConfigurator;
67 myServerListModel = new SortedComboBoxModel<>(String.CASE_INSENSITIVE_ORDER);
68 myServerComboBox = new ComboboxWithBrowseButton(new ComboBox(myServerListModel));
69 fillApplicationServersList(null);
70 myServerComboBox.addActionListener(new ActionListener() {
72 public void actionPerformed(ActionEvent e) {
73 RemoteServerListConfigurable configurable = RemoteServerListConfigurable.createConfigurable(type);
74 if (ShowSettingsUtil.getInstance().editConfigurable(myServerComboBox, configurable)) {
75 fillApplicationServersList(configurable.getLastSelectedServer());
79 myServerComboBox.getComboBox().addActionListener(new ActionListener() {
81 public void actionPerformed(ActionEvent e) {
82 updateDeploymentSettingsEditor();
85 myServerComboBox.getComboBox().setRenderer(new ColoredListCellRendererWrapper<String>() {
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);
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>() {
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());
109 myDeploymentSettingsComponent = new JPanel(new BorderLayout());
110 mySourceComboBox.addActionListener(new ActionListener() {
112 public void actionPerformed(ActionEvent e) {
113 updateDeploymentSettingsEditor();
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());
124 myServerComboBox.getComboBox().setSelectedItem(newSelection != null ? newSelection.getName() : oldSelection);
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)) {
135 if (!Comparing.equal(selectedSource, myLastSelectedSource)) {
136 updateBeforeRunOptions(myLastSelectedSource, false);
137 updateBeforeRunOptions(selectedSource, true);
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());
147 myLastSelectedSource = selectedSource;
148 myLastSelectedServer = selectedServer;
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);
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);
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);
174 protected void applyEditorTo(DeployToServerRunConfiguration<S,D> configuration) throws ConfigurationException {
175 updateDeploymentSettingsEditor();
177 configuration.setServerName(myServerListModel.getSelectedItem());
178 DeploymentSource deploymentSource = mySourceListModel.getSelectedItem();
179 configuration.setDeploymentSource(deploymentSource);
181 if (deploymentSource != null) {
182 D deployment = configuration.getDeploymentConfiguration();
183 if (deployment == null) {
184 deployment = myDeploymentConfigurator.createDefaultConfiguration(deploymentSource);
185 configuration.setDeploymentConfiguration(deployment);
187 if (myDeploymentSettingsEditor != null) {
188 myDeploymentSettingsEditor.applyTo(deployment);
192 configuration.setDeploymentConfiguration(null);
198 protected JComponent createEditor() {
199 return FormBuilder.createFormBuilder()
200 .addLabeledComponent("Server:", myServerComboBox)
201 .addLabeledComponent("Deployment:", mySourceComboBox)
202 .addComponentFillVertically(myDeploymentSettingsComponent, UIUtil.DEFAULT_VGAP)