Search in sources :

Example 1 with TableRowPainted

use of com.biglybt.ui.swt.views.table.painted.TableRowPainted in project BiglyBT by BiglySoftware.

the class MyTorrentsView method createDragDrop.

private void createDragDrop() {
    try {
        Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
        if (dragSource != null && !dragSource.isDisposed()) {
            dragSource.dispose();
        }
        if (dropTarget != null && !dropTarget.isDisposed()) {
            dropTarget.dispose();
        }
        dragSource = tv.createDragSource(DND.DROP_MOVE | DND.DROP_COPY);
        if (dragSource != null) {
            dragSource.setTransfer(types);
            dragSource.addDragListener(new DragSourceAdapter() {

                private String eventData;

                @Override
                public void dragStart(DragSourceEvent event) {
                    TableRowCore[] rows = tv.getSelectedRows();
                    if (rows.length != 0) {
                        event.doit = true;
                        // System.out.println("DragStart");
                        drag_drop_line_start = rows[0].getIndex();
                        drag_drop_rows = rows;
                    } else {
                        event.doit = false;
                        drag_drop_line_start = -1;
                        drag_drop_rows = null;
                    }
                    // Build eventData here because on OSX, selection gets cleared
                    // by the time dragSetData occurs
                    boolean onlyDMs = true;
                    StringBuilder sb = new StringBuilder();
                    Object[] selectedDataSources = tv.getSelectedDataSources(true);
                    for (Object ds : selectedDataSources) {
                        if (ds instanceof DownloadManager) {
                            DownloadManager dm = (DownloadManager) ds;
                            TOTorrent torrent = dm.getTorrent();
                            if (torrent != null) {
                                try {
                                    sb.append(torrent.getHashWrapper().toBase32String());
                                    sb.append('\n');
                                } catch (TOTorrentException e) {
                                }
                            }
                        } else if (ds instanceof DiskManagerFileInfo) {
                            DiskManagerFileInfo fileInfo = (DiskManagerFileInfo) ds;
                            DownloadManager dm = fileInfo.getDownloadManager();
                            TOTorrent torrent = dm.getTorrent();
                            if (torrent != null) {
                                try {
                                    sb.append(torrent.getHashWrapper().toBase32String());
                                    sb.append(';');
                                    sb.append(fileInfo.getIndex());
                                    sb.append('\n');
                                    onlyDMs = false;
                                } catch (TOTorrentException e) {
                                }
                            }
                        }
                    }
                    eventData = (onlyDMs ? "DownloadManager\n" : "DiskManagerFileInfo\n") + sb.toString();
                }

                @Override
                public void dragSetData(DragSourceEvent event) {
                    // System.out.println("DragSetData");
                    event.data = eventData;
                }
            });
        }
        dropTarget = tv.createDropTarget(DND.DROP_DEFAULT | DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK | DND.DROP_TARGET_MOVE);
        if (dropTarget != null) {
            dropTarget.setTransfer(new Transfer[] { FixedHTMLTransfer.getInstance(), FixedURLTransfer.getInstance(), FileTransfer.getInstance(), TextTransfer.getInstance() });
            dropTarget.addDropListener(new DropTargetAdapter() {

                Point enterPoint = null;

                @Override
                public void dropAccept(DropTargetEvent event) {
                    event.currentDataType = FixedURLTransfer.pickBestType(event.dataTypes, event.currentDataType);
                }

                @Override
                public void dragEnter(DropTargetEvent event) {
                    // if ours
                    if (drag_drop_line_start < 0) {
                        if (event.detail != DND.DROP_COPY) {
                            if ((event.operations & DND.DROP_LINK) > 0)
                                event.detail = DND.DROP_LINK;
                            else if ((event.operations & DND.DROP_COPY) > 0)
                                event.detail = DND.DROP_COPY;
                        }
                    } else if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) {
                        event.detail = tv.getTableRowWithCursor() == null ? DND.DROP_NONE : DND.DROP_MOVE;
                        event.feedback = DND.FEEDBACK_SCROLL;
                        enterPoint = new Point(event.x, event.y);
                    }
                }

                // @see org.eclipse.swt.dnd.DropTargetAdapter#dragLeave(org.eclipse.swt.dnd.DropTargetEvent)
                @Override
                public void dragLeave(DropTargetEvent event) {
                    super.dragLeave(event);
                    tv.getComposite().redraw();
                }

                @Override
                public void dragOver(DropTargetEvent event) {
                    if (drag_drop_line_start >= 0) {
                        if (drag_drop_rows.length > 0 && !(drag_drop_rows[0].getDataSource(true) instanceof DownloadManager)) {
                            event.detail = DND.DROP_NONE;
                            return;
                        }
                        TableRowCore row = tv.getTableRowWithCursor();
                        if (row instanceof TableRowPainted) {
                            boolean dragging_down = row.getIndex() > drag_drop_line_start;
                            Rectangle bounds = ((TableRowPainted) row).getBounds();
                            tv.getComposite().redraw();
                            tv.getComposite().update();
                            GC gc = new GC(tv.getComposite());
                            gc.setLineWidth(2);
                            int y_pos = bounds.y;
                            if (dragging_down) {
                                y_pos += bounds.height;
                            }
                            gc.drawLine(bounds.x, y_pos, bounds.x + bounds.width, y_pos);
                            gc.dispose();
                        }
                        event.detail = row == null ? DND.DROP_NONE : DND.DROP_MOVE;
                        event.feedback = DND.FEEDBACK_SCROLL | ((enterPoint != null && enterPoint.y > event.y) ? DND.FEEDBACK_INSERT_BEFORE : DND.FEEDBACK_INSERT_AFTER);
                    }
                }

                @Override
                public void drop(DropTargetEvent event) {
                    if (!(event.data instanceof String)) {
                        TorrentOpener.openDroppedTorrents(event, true);
                        return;
                    }
                    String data = (String) event.data;
                    if (data.startsWith("DiskManagerFileInfo\n")) {
                        return;
                    }
                    if (!data.startsWith("DownloadManager\n")) {
                        TorrentOpener.openDroppedTorrents(event, true);
                        return;
                    }
                    event.detail = DND.DROP_NONE;
                    // Torrent file from shell dropped
                    if (drag_drop_line_start >= 0) {
                        // event.data == null
                        event.detail = DND.DROP_NONE;
                        TableRowCore row = tv.getRow(event);
                        if (row == null)
                            return;
                        if (row.getParentRowCore() != null) {
                            row = row.getParentRowCore();
                        }
                        int drag_drop_line_end = row.getIndex();
                        if (drag_drop_line_end != drag_drop_line_start) {
                            DownloadManager dm = (DownloadManager) row.getDataSource(true);
                            moveRowsTo(drag_drop_rows, dm.getPosition());
                            event.detail = DND.DROP_MOVE;
                        }
                        drag_drop_line_start = -1;
                        drag_drop_rows = null;
                    }
                }
            });
        }
    } catch (Throwable t) {
        Logger.log(new LogEvent(LOGID, "failed to init drag-n-drop", t));
    }
}
Also used : DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) LogEvent(com.biglybt.core.logging.LogEvent) DownloadManager(com.biglybt.core.download.DownloadManager) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) TOTorrent(com.biglybt.core.torrent.TOTorrent) FixedURLTransfer(com.biglybt.ui.swt.FixedURLTransfer) TableRowPainted(com.biglybt.ui.swt.views.table.painted.TableRowPainted)

Aggregations

DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)1 DownloadManager (com.biglybt.core.download.DownloadManager)1 LogEvent (com.biglybt.core.logging.LogEvent)1 TOTorrent (com.biglybt.core.torrent.TOTorrent)1 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)1 FixedURLTransfer (com.biglybt.ui.swt.FixedURLTransfer)1 TableRowPainted (com.biglybt.ui.swt.views.table.painted.TableRowPainted)1