Search in sources :

Example 1 with Reorderable

use of com.revolsys.util.Reorderable in project com.revolsys.open by revolsys.

the class ListReorderableTransferHandler method importData.

@Override
public boolean importData(final TransferHandler.TransferSupport info) {
    final JList target = (JList) info.getComponent();
    final JList.DropLocation dropLocation = (JList.DropLocation) info.getDropLocation();
    int dropIndex = dropLocation.getIndex();
    final ListModel model = this.list.getModel();
    final int max = model.getSize();
    if (dropIndex < 0 || dropIndex > max) {
        dropIndex = max;
    }
    target.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    try {
        final Transferable transferable = info.getTransferable();
        final int[] indices = (int[]) transferable.getTransferData(this.localObjectFlavor);
        if (indices.length > 0) {
            final Reorderable reorderable = (Reorderable) model;
            int currentIndex = dropIndex;
            int count = 0;
            for (int index : indices) {
                if (count > 0) {
                    if (currentIndex > index) {
                        index -= count;
                    }
                }
                count++;
                if (index == currentIndex) {
                    currentIndex++;
                } else {
                    reorderable.reorder(index, currentIndex);
                    if (index > currentIndex) {
                        currentIndex++;
                    }
                }
            }
            final ListSelectionModel selectionModel = target.getSelectionModel();
            selectionModel.clearSelection();
            selectionModel.addSelectionInterval(currentIndex - indices.length, currentIndex - 1);
            return true;
        }
    } catch (final Throwable e) {
        Logs.error(this, "Unexpected error", e);
    }
    return false;
}
Also used : ListModel(javax.swing.ListModel) Transferable(java.awt.datatransfer.Transferable) ListSelectionModel(javax.swing.ListSelectionModel) Reorderable(com.revolsys.util.Reorderable) JList(javax.swing.JList)

Example 2 with Reorderable

use of com.revolsys.util.Reorderable in project com.revolsys.open by revolsys.

the class TableRowTransferHandler method importData.

@Override
public boolean importData(final TransferHandler.TransferSupport info) {
    final JTable target = (JTable) info.getComponent();
    final JTable.DropLocation dropLocation = (JTable.DropLocation) info.getDropLocation();
    int index = dropLocation.getRow();
    final TableModel model = this.table.getModel();
    final int max = model.getRowCount();
    if (index < 0 || index > max) {
        index = max;
    }
    target.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    try {
        final Transferable transferable = info.getTransferable();
        final Integer rowFrom = (Integer) transferable.getTransferData(this.localObjectFlavor);
        if (rowFrom != -1 && rowFrom != index) {
            ((Reorderable) model).reorder(rowFrom, index);
            if (index > rowFrom) {
                index--;
            }
            target.getSelectionModel().addSelectionInterval(index, index);
            return true;
        }
    } catch (final Exception e) {
        Logs.error(this, "Unexpected error", e);
    }
    return false;
}
Also used : JTable(javax.swing.JTable) Transferable(java.awt.datatransfer.Transferable) Reorderable(com.revolsys.util.Reorderable) TableModel(javax.swing.table.TableModel)

Aggregations

Reorderable (com.revolsys.util.Reorderable)2 Transferable (java.awt.datatransfer.Transferable)2 JList (javax.swing.JList)1 JTable (javax.swing.JTable)1 ListModel (javax.swing.ListModel)1 ListSelectionModel (javax.swing.ListSelectionModel)1 TableModel (javax.swing.table.TableModel)1