call `SearchEverywhereContributor#getElementsRenderer` once and cache the result
[idea/community.git] / platform / lang-impl / src / com / intellij / ide / actions / searcheverywhere / SearchEverywhereFoundElementInfo.java
1 // Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.ide.actions.searcheverywhere;
3
4 import com.intellij.util.concurrency.annotations.RequiresEdt;
5 import org.jetbrains.annotations.NotNull;
6
7 import javax.swing.*;
8 import java.util.Comparator;
9
10 /**
11  * Class containing info about found elements
12  */
13 public class SearchEverywhereFoundElementInfo {
14   public final int priority;
15   public final Object element;
16   public final SearchEverywhereContributor<?> contributor;
17   private ListCellRenderer<?> renderer;
18
19   public SearchEverywhereFoundElementInfo(Object element, int priority, SearchEverywhereContributor<?> contributor) {
20     this.priority = priority;
21     this.element = element;
22     this.contributor = contributor;
23   }
24
25   public int getPriority() {
26     return priority;
27   }
28
29   public Object getElement() {
30     return element;
31   }
32
33   public SearchEverywhereContributor<?> getContributor() {
34     return contributor;
35   }
36
37   @RequiresEdt
38   public @NotNull ListCellRenderer<?> getRenderer() {
39     var result = renderer;
40     if (result == null) {
41       result = renderer = contributor.getElementsRenderer();
42     }
43     return result;
44   }
45
46   public static final Comparator<SearchEverywhereFoundElementInfo> COMPARATOR = (o1, o2) -> {
47     int res = Integer.compare(o1.priority, o2.priority);
48     if (res != 0) return res;
49
50     return -Integer.compare(o1.contributor.getSortWeight(), o2.contributor.getSortWeight());
51   };
52 }