/*
 * Copyright 2000-2009 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.
 */

/*
 * @author max
 */
package com.intellij.ui.speedSearch;

import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.text.StringUtilRt;
import com.intellij.util.Function;

import javax.swing.*;

public class NameFilteringListModel<T> extends FilteringListModel<T> {
  private final Function<T, String> myNamer;

  private int myFullMatchIndex = -1;
  private int myStartsWithIndex = -1;
  private final SpeedSearch mySpeedSearch;

  public NameFilteringListModel(JList list, final Function<T, String> namer, final Condition<String> filter,
                                SpeedSearch speedSearch) {
    super(list);
    mySpeedSearch = speedSearch;
    myNamer = namer;
    setFilter(namer != null ? new Condition<T>() {
      public boolean value(T t) {
        return filter.value(namer.fun(t));
      }
    } : null);
  }

  @Override
  protected void addToFiltered(T elt) {
    super.addToFiltered(elt);

    if (myNamer != null) {
      String filterString = StringUtilRt.toUpperCase(mySpeedSearch.getFilter());
      String candidateString = StringUtilRt.toUpperCase(myNamer.fun(elt));
      int index = getSize() - 1;

      if (myFullMatchIndex == -1 && filterString.equals(candidateString)) {
        myFullMatchIndex = index;
      }

      if (myStartsWithIndex == -1 && candidateString.startsWith(filterString)) {
        myStartsWithIndex = index;
      }
    }
  }

  @Override
  public void refilter() {
    myFullMatchIndex = -1;
    myStartsWithIndex = -1;
    super.refilter();
  }

  public int getClosestMatchIndex() {
    return myFullMatchIndex != -1 ? myFullMatchIndex : myStartsWithIndex;
  }
}

@@ -20,10 +20,10 @@
 0-20
-09
+12
  Jet
@@ -655,16 +655,61 @@
 earch;%0A%0A
+import com.intellij.openapi.util.Computable;%0A
 import c
@@ -855,16 +855,114 @@
 ing.*;%0A%0A
+/**%0A * @param %3CT%3E list elements generic type%0A *%0A * @author max%0A * @author Konstantin Bulenkov%0A */%0A
 public c
@@ -1069,17 +1069,16 @@
 yNamer;%0A
-%0A
   privat
@@ -1164,33 +1164,36 @@
 nal 
-SpeedSearch mySpeedSearch
+Computable%3CString%3E myPattern
 ;%0A%0A 
@@ -1234,16 +1234,48 @@
 st list,
+%0A                               
  final F
@@ -1299,16 +1299,48 @@
 %3E namer,
+%0A                               
  final C
@@ -1396,16 +1396,22 @@
         
+final 
 SpeedSea
@@ -1437,52 +1437,754 @@
     
-super(list);%0A    mySpeedSearch = speedSearch
+this(list, namer, filter, new Computable%3CString%3E() %7B%0A      @Override%0A      public String compute() %7B%0A        return speedSearch.getFilter();%0A      %7D%0A    %7D);%0A  %7D%0A%0A  public NameFilteringListModel(JList list, final Function%3CT, String%3E namer, final Condition%3CString%3E filter, final SpeedSearchSupply speedSearch) %7B%0A    this(list, namer, filter, new Computable%3CString%3E() %7B%0A          @Override%0A          public String compute() %7B%0A            final String prefix = speedSearch.getEnteredPrefix();%0A            return prefix == null ? %22%22 : prefix;%0A          %7D%0A        %7D);%0A  %7D%0A%0A  public NameFilteringListModel(JList list, final Function%3CT, String%3E namer, final Condition%3CString%3E filter, Computable%3CString%3E pattern) %7B%0A    super(list);%0A    myPattern = pattern
 ;%0A  
@@ -2527,29 +2527,23 @@
 e(my
-SpeedSearch.getFilter
+Pattern.compute
 ());

make it work with any custom filter component