/*
 * Copyright 2000-2010 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.intellij.ui;

import com.intellij.util.PairFunction;
import com.intellij.util.containers.Convertor;

import javax.swing.*;
import javax.swing.table.TableModel;
import java.util.ListIterator;

public class TableSpeedSearch extends SpeedSearchBase<JTable> {
  private static final PairFunction<Object, Cell, String> TO_STRING = new PairFunction<Object, Cell, String>() {
    @Override
    public String fun(Object o, Cell cell) {
      return o == null || o instanceof Boolean ? "" : o.toString();
    }
  };
  private final PairFunction<Object, Cell, String> myToStringConvertor;

  public TableSpeedSearch(JTable table) {
    this(table, TO_STRING);
  }

  public TableSpeedSearch(JTable table, final Convertor<Object, String> toStringConvertor) {
    this(table, new PairFunction<Object, Cell, String>() {
      @Override
      public String fun(Object o, Cell c) {
        return toStringConvertor.convert(o);
      }
    });
  }

  public TableSpeedSearch(JTable table, final PairFunction<Object, Cell, String> toStringConvertor) {
    super(table);

    myToStringConvertor = toStringConvertor;
    // edit on F2 & double click, do not interfere with quick search
    table.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);
  }

  @Override
  protected boolean isSpeedSearchEnabled() {
    JTable table = getComponent();
    boolean tableIsNotEmpty = table.getRowCount() != 0 && table.getColumnCount() != 0;
    return tableIsNotEmpty && !table.isEditing() && super.isSpeedSearchEnabled();
  }

  @Override
  protected ListIterator<Object> getElementIterator(int startingIndex) {
    return new MyListIterator(startingIndex);
  }

  @Override
  protected int getElementCount() {
    final TableModel tableModel = myComponent.getModel();
    return tableModel.getRowCount() * tableModel.getColumnCount();
  }

  @Override
  protected void selectElement(Object element, String selectedText) {
    final int index = ((Integer)element).intValue();
    final TableModel model = myComponent.getModel();
    final int row = index / model.getColumnCount();
    final int col = index % model.getColumnCount();
    myComponent.getSelectionModel().setSelectionInterval(row, row);
    myComponent.getColumnModel().getSelectionModel().setSelectionInterval(col, col);
    TableUtil.scrollSelectionToVisible(myComponent);
  }

  @Override
  protected int getSelectedIndex() {
    final int row = myComponent.getSelectedRow();
    final int col = myComponent.getSelectedColumn();
    // selected row is not enough as we want to select specific cell in a large multi-column table
    return row > -1 && col > -1 ? row * myComponent.getModel().getColumnCount() + col : -1;
  }

  @Override
  protected Object[] getAllElements() {
    throw new UnsupportedOperationException("Not implemented");
  }

  @Override
  protected String getElementText(Object element) {
    final int index = ((Integer)element).intValue();
    final TableModel model = myComponent.getModel();
    int row = myComponent.convertRowIndexToModel(index / model.getColumnCount());
    int col = myComponent.convertColumnIndexToModel(index % model.getColumnCount());
    Object value = model.getValueAt(row, col);
    return myToStringConvertor.fun(value, new Cell(row, col));
  }

  private class MyListIterator implements ListIterator<Object> {

    private int myCursor;

    public MyListIterator(int startingIndex) {
      final int total = getElementCount();
      myCursor = startingIndex < 0 ? total : startingIndex;
    }

    @Override
    public boolean hasNext() {
      return myCursor < getElementCount();
    }

    @Override
    public Object next() {
      return myCursor++;
    }

    @Override
    public boolean hasPrevious() {
      return myCursor > 0;
    }

    @Override
    public Object previous() {
      return (myCursor--) - 1;
    }

    @Override
    public int nextIndex() {
      return myCursor;
    }

    @Override
    public int previousIndex() {
      return myCursor - 1;
    }

    @Override
    public void remove() {
      throw new AssertionError("Not Implemented");
    }

    @Override
    public void set(Object o) {
      throw new AssertionError("Not Implemented");
    }

    @Override
    public void add(Object o) {
      throw new AssertionError("Not Implemented");
    }
  }
}

@@ -734,45 +734,8 @@
 .*;%0A
-import javax.swing.table.TableModel;%0A
 impo
@@ -761,16 +761,16 @@
 erator;%0A
+
 %0Apublic 
@@ -1873,43 +1873,8 @@
 ) %7B%0A
-    JTable table = getComponent();%0A
     
@@ -1899,21 +1899,27 @@
 Empty = 
-table
+myComponent
 .getRowC
@@ -1933,21 +1933,27 @@
 != 0 && 
-table
+myComponent
 .getColu
@@ -1999,21 +1999,27 @@
 pty && !
-table
+myComponent
 .isEditi
@@ -2253,83 +2253,26 @@
     
-final TableModel tableModel = myComponent.getModel();%0A    return tableModel
+return myComponent
 .get
@@ -2288,18 +2288,19 @@
 ) * 
-tableModel
+myComponent
 .get
@@ -2458,61 +2458,8 @@
 ();%0A
-    final TableModel model = myComponent.getModel();%0A
     
@@ -2475,36 +2475,42 @@
  row = index / m
-odel
+yComponent
 .getColumnCount(
@@ -2533,36 +2533,42 @@
  col = index %25 m
-odel
+yComponent
 .getColumnCount(
@@ -3088,19 +3088,8 @@
 ent.
-getModel().
 getC
@@ -3364,119 +3364,37 @@
     
-f
 in
-al TableModel model = myComponent.getModel();%0A    int row = myComponent.convertRowIndexToModel(index / model
+t row = index / myComponent
 .get
@@ -3402,25 +3402,24 @@
 olumnCount()
-)
 ;%0A    int co
@@ -3413,32 +3413,40 @@
 );%0A    int col =
+ index %25
  myComponent.con
@@ -3446,48 +3446,8 @@
 ent.
-convertColumnIndexToModel(index %25 model.
 getC
@@ -3458,17 +3458,16 @@
 nCount()
-)
 ;%0A    Ob
@@ -3480,20 +3480,26 @@
 alue = m
-odel
+yComponent
 .getValu

use view indices in TableSpeedSearch