PY-22324 Converted PySdkListCellRenderer to Kotlin

This commit is contained in:
Andrey Vlasovskikh
2017-10-04 18:46:54 +03:00
parent 68adc51481
commit e29a464d4a
9 changed files with 83 additions and 103 deletions

View File

@@ -219,7 +219,7 @@ public class PyStudyDirectoryProjectGenerator extends PythonProjectGenerator<PyN
}
private static void patchRenderer(@NotNull ProjectJdkImpl fakeSdk, @NotNull PythonSdkChooserCombo combo) {
combo.getComboBox().setRenderer(new PySdkListCellRenderer() {
combo.getComboBox().setRenderer(new PySdkListCellRenderer(null) {
@Override
public void customize(JList list, Object item, int index, boolean selected, boolean hasFocus) {
super.customize(list, item, index, selected, hasFocus);

View File

@@ -48,7 +48,7 @@ public class PyManagePackagesDialog extends DialogWrapper {
List<Sdk> sdks = PythonSdkType.getAllSdks();
Collections.sort(sdks, new PreferredSdkComparator());
final JComboBox sdkComboBox = new JComboBox(new CollectionComboBoxModel(sdks, sdk));
sdkComboBox.setRenderer(new PySdkListCellRenderer());
sdkComboBox.setRenderer(new PySdkListCellRenderer(null));
PackagesNotificationPanel notificationPanel = new PackagesNotificationPanel();
final PyInstalledPackagesPanel packagesPanel = new PyInstalledPackagesPanel(project, notificationPanel);

View File

@@ -36,7 +36,7 @@ public class PythonSdkComboBox extends ComboboxWithBrowseButton {
private Project myProject;
public PythonSdkComboBox() {
getComboBox().setRenderer(new PySdkListCellRenderer("<No Interpreter>", null));
getComboBox().setRenderer(new PySdkListCellRenderer(null));
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Sdk selectedSdk = getSelectedSdk();

View File

@@ -329,7 +329,7 @@ public class PyActiveSdkConfigurable implements UnnamedConfigurable {
items.add(PySdkListCellRenderer.SEPARATOR);
items.add(SHOW_ALL);
mySdkCombo.setRenderer(new PySdkListCellRenderer());
mySdkCombo.setRenderer(new PySdkListCellRenderer(null));
mySdkCombo.setModel(new CollectionComboBoxModel<>(items, selection));
}

View File

@@ -121,7 +121,7 @@ public class PythonSdkDetailsDialog extends DialogWrapper {
protected JComponent createCenterPanel() {
mySdkList = new JBList<>();
//noinspection unchecked
mySdkList.setCellRenderer(new PySdkListCellRenderer("", myModificators));
mySdkList.setCellRenderer(new PySdkListCellRenderer(myModificators));
mySdkList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
new ListSpeedSearch<>(mySdkList);

View File

@@ -56,7 +56,7 @@ public class PythonSdkChooserCombo extends ComboboxWithBrowseButton {
final Sdk initialSelection = ContainerUtil.find(sdks, acceptableSdkCondition);
final JComboBox comboBox = getComboBox();
comboBox.setModel(new CollectionComboBoxModel(sdks, initialSelection));
comboBox.setRenderer(new PySdkListCellRenderer());
comboBox.setRenderer(new PySdkListCellRenderer(null));
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showOptions(project);

View File

@@ -1,96 +0,0 @@
/*
* 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.jetbrains.python.sdk;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.SdkModificator;
import com.intellij.openapi.projectRoots.SdkType;
import com.intellij.openapi.util.IconLoader;
import com.intellij.ui.LayeredIcon;
import com.intellij.ui.ListCellRendererWrapper;
import com.jetbrains.python.sdk.flavors.PythonSdkFlavor;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.Map;
public class PySdkListCellRenderer extends ListCellRendererWrapper<Object> {
private final String myNullText;
private final Map<Sdk, SdkModificator> mySdkModifiers;
public static final String SEPARATOR = "separator";
public PySdkListCellRenderer() {
myNullText = "";
mySdkModifiers = null;
}
public PySdkListCellRenderer(String nullText, @Nullable Map<Sdk, SdkModificator> sdkModifiers) {
myNullText = nullText;
mySdkModifiers = sdkModifiers;
}
@Override
public void customize(JList list, Object item, int index, boolean selected, boolean hasFocus) {
if (item instanceof Sdk) {
Sdk sdk = (Sdk)item;
final PythonSdkFlavor flavor = PythonSdkFlavor.getPlatformIndependentFlavor(sdk.getHomePath());
final Icon icon = flavor != null ? flavor.getIcon() : ((SdkType)sdk.getSdkType()).getIcon();
String name;
if (mySdkModifiers != null && mySdkModifiers.containsKey(sdk)) {
name = mySdkModifiers.get(sdk).getName();
}
else {
name = sdk.getName();
}
if (PythonSdkType.isInvalid(sdk)) {
setText("[invalid] " + name);
setIcon(wrapIconWithWarningDecorator(icon));
}
else if (PythonSdkType.isIncompleteRemote(sdk)) {
setText("[incomplete] " + name);
setIcon(wrapIconWithWarningDecorator(icon));
}
else if (PythonSdkType.hasInvalidRemoteCredentials(sdk)) {
setText("[invalid] " + name);
setIcon(wrapIconWithWarningDecorator(icon));
}
else if (sdk instanceof PyDetectedSdk) {
setText(name);
setIcon(IconLoader.getTransparentIcon(icon));
}
else {
setText(name);
setIcon(icon);
}
setToolTipText(sdk.getHomePath());
}
else if (SEPARATOR.equals(item))
setSeparator();
else if (item == null)
setText(myNullText);
}
private static LayeredIcon wrapIconWithWarningDecorator(Icon icon) {
final LayeredIcon layered = new LayeredIcon(2);
layered.setIcon(icon, 0);
final Icon overlay = AllIcons.Actions.Cancel;
layered.setIcon(overlay, 1);
return layered;
}
}

View File

@@ -0,0 +1,76 @@
/*
* 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.jetbrains.python.sdk
import com.intellij.icons.AllIcons
import com.intellij.openapi.projectRoots.Sdk
import com.intellij.openapi.projectRoots.SdkModificator
import com.intellij.openapi.projectRoots.SdkType
import com.intellij.openapi.util.IconLoader
import com.intellij.ui.LayeredIcon
import com.intellij.ui.ListCellRendererWrapper
import com.jetbrains.python.sdk.flavors.PythonSdkFlavor
import javax.swing.Icon
import javax.swing.JList
/**
* @author vlan
*/
open class PySdkListCellRenderer(private val sdkModifiers: Map<Sdk, SdkModificator>?) : ListCellRendererWrapper<Any>() {
override fun customize(list: JList<*>, item: Any?, index: Int, selected: Boolean, hasFocus: Boolean) {
when (item) {
is Sdk -> {
val flavor = PythonSdkFlavor.getPlatformIndependentFlavor(item.homePath)
val icon = if (flavor != null) flavor.icon else (item.sdkType as SdkType).icon
icon?.let {
setIcon(customizeIcon(item, it))
}
val name = sdkModifiers?.get(item)?.name ?: item.name
setText(customizeName(item, name))
setToolTipText(item.homePath)
}
SEPARATOR -> setSeparator()
null -> setText("<No interpreter>")
}
}
companion object {
const val SEPARATOR = "separator"
private fun customizeName(sdk: Sdk, name: String): String =
when {
PythonSdkType.isInvalid(sdk) || PythonSdkType.hasInvalidRemoteCredentials(sdk) -> "[invalid] $name"
PythonSdkType.isIncompleteRemote(sdk) -> "[incomplete] $name"
else -> name
}
private fun customizeIcon(sdk: Sdk, icon: Icon): Icon =
when {
PythonSdkType.isInvalid(sdk) || PythonSdkType.isIncompleteRemote(sdk) || PythonSdkType.hasInvalidRemoteCredentials(sdk) ->
wrapIconWithWarningDecorator(icon)
sdk is PyDetectedSdk ->
IconLoader.getTransparentIcon(icon)
else ->
icon
}
private fun wrapIconWithWarningDecorator(icon: Icon): LayeredIcon =
LayeredIcon(2).apply {
setIcon(icon, 0)
setIcon(AllIcons.Actions.Cancel, 1)
}
}
}

View File

@@ -33,7 +33,7 @@ class PySdkPathChoosingComboBox(sdks: List<Sdk>, suggestedFile: VirtualFile?) :
init {
childComponent.apply {
renderer = PySdkListCellRenderer("<No Interpreter>", null)
renderer = PySdkListCellRenderer(null)
ComboboxSpeedSearch(this)
}
addActionListener {