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;
48 public class DeployToServerSettingsEditor<S extends ServerConfiguration, D extends DeploymentConfiguration> extends SettingsEditor<DeployToServerRunConfiguration<S, D>> {
49 private final ServerType<S> myServerType;
50 private final DeploymentConfigurator<D, S> myDeploymentConfigurator;
51 private final Project myProject;
52 private final ComboboxWithBrowseButton myServerComboBox;
53 private final ComboBox<DeploymentSource> mySourceComboBox;
54 private final SortedComboBoxModel<String> myServerListModel;
55 private final SortedComboBoxModel<DeploymentSource> mySourceListModel;
56 private final JPanel myDeploymentSettingsComponent;
57 private SettingsEditor<D> myDeploymentSettingsEditor;
58 private DeploymentSource myLastSelectedSource;
59 private RemoteServer<S> myLastSelectedServer;
61 public DeployToServerSettingsEditor(final ServerType<S> type, DeploymentConfigurator<D, S> deploymentConfigurator, Project project) {
63 myDeploymentConfigurator = deploymentConfigurator;
66 myServerListModel = new SortedComboBoxModel<>(String.CASE_INSENSITIVE_ORDER);
67 myServerComboBox = new ComboboxWithBrowseButton(new ComboBox<>(myServerListModel));
68 fillApplicationServersList(null);
69 myServerComboBox.addActionListener(new ActionListener() {
71 public void actionPerformed(ActionEvent e) {
72 RemoteServerListConfigurable configurable = RemoteServerListConfigurable.createConfigurable(type);
73 if (ShowSettingsUtil.getInstance().editConfigurable(myServerComboBox, configurable)) {
74 fillApplicationServersList(configurable.getLastSelectedServer());
78 myServerComboBox.getComboBox().addActionListener(new ActionListener() {
80 public void actionPerformed(ActionEvent e) {
81 updateDeploymentSettingsEditor();
84 myServerComboBox.getComboBox().setRenderer(new ColoredListCellRenderer<String>() {
86 protected void customizeCellRenderer(@NotNull JList<? extends String> list, String value, int index, boolean selected, boolean hasFocus) {
87 if (value == null) return;
88 RemoteServer<S> server = RemoteServersManager.getInstance().findByName(value, type);
89 SimpleTextAttributes attributes = server == null ? SimpleTextAttributes.ERROR_ATTRIBUTES : SimpleTextAttributes.REGULAR_ATTRIBUTES;
90 setIcon(server != null ? server.getType().getIcon() : null);
91 append(value, attributes);
95 mySourceListModel = new SortedComboBoxModel<>(
96 (o1, o2) -> o1.getPresentableName().compareToIgnoreCase(o2.getPresentableName()));
97 mySourceListModel.addAll(deploymentConfigurator.getAvailableDeploymentSources());
98 mySourceComboBox = new ComboBox<>(mySourceListModel);
99 mySourceComboBox.setRenderer(new ListCellRendererWrapper<DeploymentSource>() {
101 public void customize(JList list, DeploymentSource value, int index, boolean selected, boolean hasFocus) {
102 if (value == null) return;
103 setIcon(value.getIcon());
104 setText(value.getPresentableName());
108 myDeploymentSettingsComponent = new JPanel(new BorderLayout());
109 mySourceComboBox.addActionListener(new ActionListener() {
111 public void actionPerformed(ActionEvent e) {
112 updateDeploymentSettingsEditor();
117 private void fillApplicationServersList(@Nullable RemoteServer<?> newSelection) {
118 String oldSelection = myServerListModel.getSelectedItem();
119 myServerListModel.clear();
120 for (RemoteServer<?> server : RemoteServersManager.getInstance().getServers(myServerType)) {
121 myServerListModel.add(server.getName());
123 myServerComboBox.getComboBox().setSelectedItem(newSelection != null ? newSelection.getName() : oldSelection);
126 private void updateDeploymentSettingsEditor() {
127 String serverName = myServerListModel.getSelectedItem();
128 RemoteServer<S> selectedServer = serverName != null ? RemoteServersManager.getInstance().findByName(serverName, myServerType) : null;
129 DeploymentSource selectedSource = mySourceListModel.getSelectedItem();
130 if (Comparing.equal(selectedSource, myLastSelectedSource) && Comparing.equal(selectedServer, myLastSelectedServer)) {
134 if (!Comparing.equal(selectedSource, myLastSelectedSource)) {
135 updateBeforeRunOptions(myLastSelectedSource, false);
136 updateBeforeRunOptions(selectedSource, true);
138 if (selectedSource != null && selectedServer != null) {
139 myDeploymentSettingsComponent.removeAll();
140 myDeploymentSettingsEditor = myDeploymentConfigurator.createEditor(selectedSource, selectedServer);
141 if (myDeploymentSettingsEditor != null) {
142 Disposer.register(this, myDeploymentSettingsEditor);
143 myDeploymentSettingsComponent.add(BorderLayout.CENTER, myDeploymentSettingsEditor.getComponent());
146 myLastSelectedSource = selectedSource;
147 myLastSelectedServer = selectedServer;
150 private void updateBeforeRunOptions(@Nullable DeploymentSource source, boolean selected) {
151 if (source != null) {
152 DeploymentSourceType type = source.getType();
153 type.updateBuildBeforeRunOption(myServerComboBox, myProject, source, selected);
158 protected void resetEditorFrom(@NotNull DeployToServerRunConfiguration<S,D> configuration) {
159 String serverName = configuration.getServerName();
160 if (serverName != null && !myServerListModel.getItems().contains(serverName)) {
161 myServerListModel.add(serverName);
163 myServerComboBox.getComboBox().setSelectedItem(serverName);
164 mySourceComboBox.setSelectedItem(configuration.getDeploymentSource());
165 D deploymentConfiguration = configuration.getDeploymentConfiguration();
166 updateDeploymentSettingsEditor();
167 if (deploymentConfiguration != null && myDeploymentSettingsEditor != null) {
168 myDeploymentSettingsEditor.resetFrom(deploymentConfiguration);
173 protected void applyEditorTo(@NotNull DeployToServerRunConfiguration<S,D> configuration) throws ConfigurationException {
174 updateDeploymentSettingsEditor();
176 configuration.setServerName(myServerListModel.getSelectedItem());
177 DeploymentSource deploymentSource = mySourceListModel.getSelectedItem();
178 configuration.setDeploymentSource(deploymentSource);
180 if (deploymentSource != null) {
181 D deployment = configuration.getDeploymentConfiguration();
182 if (deployment == null) {
183 deployment = myDeploymentConfigurator.createDefaultConfiguration(deploymentSource);
184 configuration.setDeploymentConfiguration(deployment);
186 if (myDeploymentSettingsEditor != null) {
187 myDeploymentSettingsEditor.applyTo(deployment);
191 configuration.setDeploymentConfiguration(null);
197 protected JComponent createEditor() {
198 return FormBuilder.createFormBuilder()
199 .addLabeledComponent("Server:", myServerComboBox)
200 .addLabeledComponent("Deployment:", mySourceComboBox)
201 .addComponentFillVertically(myDeploymentSettingsComponent, UIUtil.DEFAULT_VGAP)