/*
 * Copyright 2000-2011 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.ide.dnd.*;
import com.intellij.util.Function;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import java.awt.*;

/**
 * @author Konstantin Bulenkov
 */
public class TableRowsDnDSupport {
  private TableRowsDnDSupport() {
  }

  public static void install(@NotNull final JTable table, @NotNull final RowEditableTableModel model) {
    table.setDragEnabled(true);
    //table.setDropMode(DropMode.ON);
    DnDSupport.createBuilder(table)
      .setBeanProvider(new Function<DnDActionInfo, DnDDragStartBean>() {
        @Override
        public DnDDragStartBean fun(DnDActionInfo info) {
          final Point p = info.getPoint();
          final TableCellEditor cellEditor = table.getCellEditor();
          if (cellEditor != null) {
            cellEditor.stopCellEditing();
          }
          return new DnDDragStartBean(Integer.valueOf(table.rowAtPoint(p)));
        }
      })
      .setDropHandler(new DnDDropHandler() {
        @Override
        public void drop(DnDEvent event) {
          final Object o = event.getAttachedObject();
          final Point p = event.getPoint();
          if (o instanceof Integer) {
            final int oldIndex = ((Integer)o).intValue();
            final int newIndex = table.rowAtPoint(p);
            model.exchangeRows(oldIndex, newIndex);
          }
        }
      }).install();
  }
}

@@ -1832,30 +1832,24 @@
             
-final 
 int oldIndex
@@ -1888,22 +1888,16 @@
         
-final 
 int newI
@@ -1940,47 +1940,517 @@
     
-model.exchan
+if (newIndex == -1) %7B%0A              newIndex = table.
 ge
+t
 Row
-s(oldIndex, newIndex);
+Count() - 1;%0A            %7D%0A            int min = Math.min(oldIndex, newIndex);%0A            int max = Math.max(oldIndex, newIndex);%0A            if (newIndex %3E oldIndex) %7B%0A              while (min %3C max) %7B%0A                model.exchangeRows(min, min + 1);%0A                min++;%0A              %7D%0A            %7D else %7B%0A              while (max %3E min) %7B%0A                model.exchangeRows(max, max - 1);%0A                max--;%0A              %7D%0A            %7D
 %0A   

fix AIOOBE and proper DnD moves