summaryrefslogtreecommitdiff
path: root/python/src/com/jetbrains/python/sdk/PySdkListCellRenderer.java
blob: 1a42621dde3d8461a7a790e503bb87ae7cfca18c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright 2000-2013 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.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.io.File;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PySdkListCellRenderer extends ListCellRendererWrapper<Object> {
  private final String myNullText;
  private final Map<Sdk, SdkModificator> mySdkModifiers;
  public static final String SEPARATOR = "separator";

  final Pattern PYTHON_PATTERN = Pattern.compile("(\\d\\.?\\d\\.?\\d?)[ ]*\\(([^\\(\\)]*)\\)|(\\d\\.?\\d\\.?\\d?)[ ]*([^\\(\\)]*)");
  private boolean isShortVersion;

  public PySdkListCellRenderer(boolean shortVersion) {
    isShortVersion = shortVersion;
    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 (name.startsWith("Remote")) name = name.substring(7);
      final String flavorName = flavor == null ? "Python" : flavor.getName();
      if (name.startsWith(flavorName)) name = name.substring(flavorName.length() + 1);

      if (isShortVersion){
        name = shortenName(name);
      }

      if (PythonSdkType.isInvalid(sdk)) {
        setText("[invalid] " + name);
        setIcon(wrapIconWithWarningDecorator(icon));
      }
      else if (PythonSdkType.isIncompleteRemote(sdk)) {
        setText("[incomplete] " + name);
        setIcon(wrapIconWithWarningDecorator(icon));
      }
      else if (sdk instanceof PyDetectedSdk){
        setText(name);
        setIcon(IconLoader.getTransparentIcon(icon));
      }
      else {
        setText(name);
        setIcon(icon);
      }
    }
    else if (SEPARATOR.equals(item))
      setSeparator();
    else if (item == null)
      setText(myNullText);
  }

  private String shortenName(@NotNull String name) {
    final Matcher matcher = PYTHON_PATTERN.matcher(name);
    if (matcher.matches()) {
      String path = matcher.group(2);
      if (path != null) {
        name = matcher.group(1) + " at " + path;
      }
      else {
        path = matcher.group(4);
        final int index = path.lastIndexOf(File.separator);
        if (index > 0) {
          path = path.substring(index);
        }
        name = matcher.group(3) + " at ..." + path;
      }
    }
    else if (new File(name).exists()) {
      name = "..." + File.separator + new File(name).getParentFile().getParentFile().getName();
    }
    return name;
  }

  private static LayeredIcon wrapIconWithWarningDecorator(Icon icon) {
    final LayeredIcon layered = new LayeredIcon(2);
    layered.setIcon(icon, 0);
    // TODO: Create a separate invalid SDK overlay icon (DSGN-497)
    final Icon overlay = AllIcons.Actions.Cancel;
    layered.setIcon(overlay, 1);
    return layered;
  }
}