changedUpdate exception
[fedora-idea.git] / platform / platform-impl / src / com / intellij / ui / ListSpeedSearch.java
blob1ba521c4ba22a96713900a3611399570c64d9cac
1 /*
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.
16 package com.intellij.ui;
18 import com.intellij.util.Function;
20 import javax.swing.*;
22 public class ListSpeedSearch extends SpeedSearchBase<JList> {
23 private Function<Object, String> myElementTextDelegate;
25 public ListSpeedSearch(JList list) {
26 super(list);
29 public ListSpeedSearch(final JList component, final Function<Object, String> elementTextDelegate) {
30 super(component);
31 myElementTextDelegate = elementTextDelegate;
34 protected void selectElement(Object element, String selectedText) {
35 ListScrollingUtil.selectItem(myComponent, element);
38 protected int getSelectedIndex() {
39 return myComponent.getSelectedIndex();
42 protected Object[] getAllElements() {
43 ListModel model = myComponent.getModel();
44 if (model instanceof DefaultListModel){ // optimization
45 return ((DefaultListModel)model).toArray();
47 else{
48 Object[] elements = new Object[model.getSize()];
49 for(int i = 0; i < elements.length; i++){
50 elements[i] = model.getElementAt(i);
52 return elements;
56 protected String getElementText(Object element) {
57 if (myElementTextDelegate != null) {
58 return myElementTextDelegate.fun(element);
60 return element.toString();