From 7668fa3b8c7343e63cf2758e71efe357a5f7c1c8 Mon Sep 17 00:00:00 2001 From: Alexander Koshevoy Date: Sat, 20 Feb 2016 21:03:45 +0300 Subject: [PATCH] PY-17716 Additional editors added for Python run configurations to be able to edit Docker-specific settings for Docker based interpreter configurations Pythonid.runConfigurationEditorExtension added to provide Python run configuration parameters-specific (in Docker case dependent on selected interpreter) editors. --- python/src/META-INF/python-core-common.xml | 1 + .../run/AbstractPythonRunConfiguration.java | 2 +- .../PyRunConfigurationEditorExtension.java | 47 ++++++++ .../PythonExtendedConfigurationEditor.java | 114 ++++++++++++++++++ 4 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 python/src/com/jetbrains/python/run/PyRunConfigurationEditorExtension.java create mode 100644 python/src/com/jetbrains/python/run/PythonExtendedConfigurationEditor.java diff --git a/python/src/META-INF/python-core-common.xml b/python/src/META-INF/python-core-common.xml index 96e89eec959c..753f27d8b397 100644 --- a/python/src/META-INF/python-core-common.xml +++ b/python/src/META-INF/python-core-common.xml @@ -612,6 +612,7 @@ + diff --git a/python/src/com/jetbrains/python/run/AbstractPythonRunConfiguration.java b/python/src/com/jetbrains/python/run/AbstractPythonRunConfiguration.java index fde9a5e5da59..866ad1f49471 100644 --- a/python/src/com/jetbrains/python/run/AbstractPythonRunConfiguration.java +++ b/python/src/com/jetbrains/python/run/AbstractPythonRunConfiguration.java @@ -129,7 +129,7 @@ public abstract class AbstractPythonRunConfiguration getConfigurationEditor() { - final SettingsEditor runConfigurationEditor = createConfigurationEditor(); + final SettingsEditor runConfigurationEditor = PythonExtendedConfigurationEditor.create(createConfigurationEditor()); final SettingsEditorGroup group = new SettingsEditorGroup(); diff --git a/python/src/com/jetbrains/python/run/PyRunConfigurationEditorExtension.java b/python/src/com/jetbrains/python/run/PyRunConfigurationEditorExtension.java new file mode 100644 index 000000000000..0885a0d96eac --- /dev/null +++ b/python/src/com/jetbrains/python/run/PyRunConfigurationEditorExtension.java @@ -0,0 +1,47 @@ +/* + * Copyright 2000-2016 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.jetbrains.python.run; + +import com.intellij.openapi.extensions.ExtensionPointName; +import com.intellij.openapi.options.SettingsEditor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * @author Alexander Koshevoy + */ +public interface PyRunConfigurationEditorExtension { + ExtensionPointName EP_NAME = + ExtensionPointName.create("Pythonid.runConfigurationEditorExtension"); + + boolean accepts(@NotNull AbstractPythonRunConfiguration configuration); + + @NotNull + SettingsEditor createEditor(@NotNull AbstractPythonRunConfiguration configuration); + + class Factory { + @Nullable + public static PyRunConfigurationEditorExtension getExtension(@NotNull AbstractPythonRunConfiguration configuration) { + PyRunConfigurationEditorExtension[] extensions = EP_NAME.getExtensions(); + for (PyRunConfigurationEditorExtension extension : extensions) { + if (extension.accepts(configuration)) { + return extension; + } + } + return null; + } + } +} diff --git a/python/src/com/jetbrains/python/run/PythonExtendedConfigurationEditor.java b/python/src/com/jetbrains/python/run/PythonExtendedConfigurationEditor.java new file mode 100644 index 000000000000..fae0d77015ce --- /dev/null +++ b/python/src/com/jetbrains/python/run/PythonExtendedConfigurationEditor.java @@ -0,0 +1,114 @@ +/* + * Copyright 2000-2016 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.jetbrains.python.run; + +import com.intellij.openapi.options.ConfigurationException; +import com.intellij.openapi.options.SettingsEditor; +import com.intellij.openapi.util.Disposer; +import org.jetbrains.annotations.NotNull; + +import javax.swing.*; +import java.awt.*; + +/** + * @author Alexander Koshevoy + */ +public class PythonExtendedConfigurationEditor> extends SettingsEditor { + @NotNull private final SettingsEditor myMainSettingsEditor; + + private PyRunConfigurationEditorExtension myCurrentEditor; + private SettingsEditor myCurrentSettingsEditor; + private JComponent myCurrentSettingsEditorComponent; + + private JPanel mySettingsPlaceholder; + + public PythonExtendedConfigurationEditor(@NotNull SettingsEditor editor) { + myMainSettingsEditor = editor; + + Disposer.register(this, myMainSettingsEditor); + } + + @Override + protected void resetEditorFrom(T s) { + myMainSettingsEditor.resetFrom(s); + updateCurrentEditor(s); + if (myCurrentSettingsEditor != null) { + myCurrentSettingsEditor.resetFrom(s); + } + } + + @Override + protected void applyEditorTo(T s) throws ConfigurationException { + myMainSettingsEditor.applyTo(s); + boolean updated = updateCurrentEditor(s); + if (myCurrentSettingsEditor != null) { + if (updated) { + myCurrentSettingsEditor.resetFrom(s); + } + else { + myCurrentSettingsEditor.applyTo(s); + } + } + } + + private boolean updateCurrentEditor(T s) { + PyRunConfigurationEditorExtension newEditor = PyRunConfigurationEditorExtension.Factory.getExtension(s); + if (myCurrentEditor != newEditor) { + // discard previous + if (myCurrentSettingsEditorComponent != null) { + mySettingsPlaceholder.remove(myCurrentSettingsEditorComponent); + } + if (myCurrentSettingsEditor != null) { + Disposer.dispose(myCurrentSettingsEditor); + } + // set current editor + myCurrentEditor = newEditor; + myCurrentSettingsEditor = null; + // add new + if (newEditor != null) { + myCurrentSettingsEditor = newEditor.createEditor(s); + myCurrentSettingsEditorComponent = myCurrentSettingsEditor.getComponent(); + GridBagConstraints constraints = new GridBagConstraints(); + constraints.gridy = 1; + constraints.fill = GridBagConstraints.BOTH; + constraints.weightx = 1.0; + constraints.insets.top = 10; + mySettingsPlaceholder.add(myCurrentSettingsEditorComponent, constraints); + + Disposer.register(this, myCurrentSettingsEditor); + } + return true; + } + return false; + } + + @NotNull + @Override + protected JComponent createEditor() { + JComponent mainEditorComponent = myMainSettingsEditor.getComponent(); + mySettingsPlaceholder = new JPanel(new GridBagLayout()); + GridBagConstraints constraints = new GridBagConstraints(); + constraints.fill = GridBagConstraints.BOTH; + constraints.weightx = 1.0; + mySettingsPlaceholder.add(mainEditorComponent, constraints); + return mySettingsPlaceholder; + } + + @NotNull + public static > PythonExtendedConfigurationEditor create(@NotNull SettingsEditor editor) { + return new PythonExtendedConfigurationEditor<>(editor); + } +}