2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 package com
.intellij
.ui
.speedSearch
;
22 import com
.intellij
.openapi
.util
.Condition
;
23 import com
.intellij
.util
.Function
;
27 public class NameFilteringListModel
<T
> extends FilteringListModel
<T
> {
28 private final Function
<T
, String
> myNamer
;
30 private int myFullMatchIndex
= -1;
31 private int myStartsWithIndex
= -1;
32 private final SpeedSearch mySpeedSearch
;
34 public NameFilteringListModel(JList list
, final Function
<T
, String
> namer
, final Condition
<String
> filter
,
35 SpeedSearch speedSearch
) {
37 mySpeedSearch
= speedSearch
;
39 setFilter(namer
!= null ?
new Condition
<T
>() {
40 public boolean value(T t
) {
41 return filter
.value(namer
.fun(t
));
47 protected void addToFiltered(T elt
) {
48 super.addToFiltered(elt
);
50 if (myNamer
!= null) {
51 String filterString
= mySpeedSearch
.getFilter().toUpperCase();
52 String candidateString
= myNamer
.fun(elt
).toUpperCase();
53 int index
= getSize() - 1;
55 if (myFullMatchIndex
== -1 && filterString
.equals(candidateString
)) {
56 myFullMatchIndex
= index
;
59 if (myStartsWithIndex
== -1 && candidateString
.startsWith(filterString
)) {
60 myStartsWithIndex
= index
;
66 public void refilter() {
67 myFullMatchIndex
= -1;
68 myStartsWithIndex
= -1;
72 public int getClosestMatchIndex() {
73 return myFullMatchIndex
!= -1 ? myFullMatchIndex
: myStartsWithIndex
;